| 1 | /* |
| 2 | * Copyright (C) 2012-2015 DataStax Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMTestsSupport; |
| 19 | import com.datastax.driver.core.utils.CassandraVersion; |
| 20 | import com.datastax.driver.mapping.annotations.*; |
| 21 | import com.google.common.base.Objects; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.common.collect.Sets; |
| 24 | import org.testng.annotations.Test; |
| 25 | import org.testng.collections.Lists; |
| 26 | |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static org.testng.Assert.assertEquals; |
| 32 | |
| 33 | /** |
| 34 | * Tests mapping of collections of UDTs. |
| 35 | */ |
| 36 | @SuppressWarnings("unused") |
| 37 | @CassandraVersion("2.1.0") |
| 38 | public class MapperUDTCollectionsTest extends CCMTestsSupport { |
| 39 | |
| 40 | @Override |
| 41 | public void onTestContextInitialized() { |
| 42 | execute("CREATE TYPE \"Sub\"(i int)", |
| 43 | "CREATE TABLE collection_examples (id int PRIMARY KEY, " + |
| 44 | "l list<frozen<\"Sub\">>, " + |
| 45 | "s set<frozen<\"Sub\">>, " + |
| 46 | "m1 map<int,frozen<\"Sub\">>, " + |
| 47 | "m2 map<frozen<\"Sub\">,int>, " + |
| 48 | "m3 map<frozen<\"Sub\">,frozen<\"Sub\">>)"); |
| 49 | } |
| 50 | |
| 51 | @UDT(name = "Sub", caseSensitiveType = true) |
| 52 | public static class Sub { |
| 53 | private int i; |
| 54 | |
| 55 | public Sub() { |
| 56 | } |
| 57 | |
| 58 | public Sub(int i) { |
| 59 | this.i = i; |
| 60 | } |
| 61 | |
| 62 | public int getI() { |
| 63 | return i; |
| 64 | } |
| 65 | |
| 66 | public void setI(int i) { |
| 67 | this.i = i; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean equals(Object other) { |
| 72 | if (other instanceof Sub) { |
| 73 | Sub that = (Sub) other; |
| 74 | return this.i == that.i; |
| 75 | } |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int hashCode() { |
| 81 | return Objects.hashCode(i); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Table(name = "collection_examples") |
| 86 | public static class CollectionExamples { |
| 87 | @PartitionKey |
| 88 | private int id; |
| 89 | |
| 90 | @FrozenValue |
| 91 | private List<Sub> l; |
| 92 | |
| 93 | @FrozenValue |
| 94 | private Set<Sub> s; |
| 95 | |
| 96 | @FrozenValue |
| 97 | private Map<Integer, Sub> m1; |
| 98 | |
| 99 | @FrozenKey |
| 100 | private Map<Sub, Integer> m2; |
| 101 | |
| 102 | @FrozenKey |
| 103 | @FrozenValue |
| 104 | private Map<Sub, Sub> m3; |
| 105 | |
| 106 | public CollectionExamples() { |
| 107 | } |
| 108 | |
| 109 | public CollectionExamples(int id, int value) { |
| 110 | this.id = id; |
| 111 | // Just fill the collections with random values |
| 112 | Sub sub1 = new Sub(value); |
| 113 | Sub sub2 = new Sub(value + 1); |
| 114 | this.l = Lists.newArrayList(sub1, sub2); |
| 115 | this.s = Sets.newHashSet(sub1, sub2); |
| 116 | this.m1 = ImmutableMap.of(1, sub1, 2, sub2); |
| 117 | this.m2 = ImmutableMap.of(sub1, 1, sub2, 2); |
| 118 | this.m3 = ImmutableMap.of(sub1, sub1, sub2, sub2); |
| 119 | } |
| 120 | |
| 121 | public int getId() { |
| 122 | return id; |
| 123 | } |
| 124 | |
| 125 | public void setId(int id) { |
| 126 | this.id = id; |
| 127 | } |
| 128 | |
| 129 | public List<Sub> getL() { |
| 130 | return l; |
| 131 | } |
| 132 | |
| 133 | public void setL(List<Sub> l) { |
| 134 | this.l = l; |
| 135 | } |
| 136 | |
| 137 | public Set<Sub> getS() { |
| 138 | return s; |
| 139 | } |
| 140 | |
| 141 | public void setS(Set<Sub> s) { |
| 142 | this.s = s; |
| 143 | } |
| 144 | |
| 145 | public Map<Integer, Sub> getM1() { |
| 146 | return m1; |
| 147 | } |
| 148 | |
| 149 | public void setM1(Map<Integer, Sub> m1) { |
| 150 | this.m1 = m1; |
| 151 | } |
| 152 | |
| 153 | public Map<Sub, Integer> getM2() { |
| 154 | return m2; |
| 155 | } |
| 156 | |
| 157 | public void setM2(Map<Sub, Integer> m2) { |
| 158 | this.m2 = m2; |
| 159 | } |
| 160 | |
| 161 | public Map<Sub, Sub> getM3() { |
| 162 | return m3; |
| 163 | } |
| 164 | |
| 165 | public void setM3(Map<Sub, Sub> m3) { |
| 166 | this.m3 = m3; |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public boolean equals(Object other) { |
| 171 | if (other instanceof CollectionExamples) { |
| 172 | CollectionExamples that = (CollectionExamples) other; |
| 173 | return Objects.equal(this.id, that.id) && |
| 174 | Objects.equal(this.l, that.l) && |
| 175 | Objects.equal(this.s, that.s) && |
| 176 | Objects.equal(this.m1, that.m1) && |
| 177 | Objects.equal(this.m2, that.m2) && |
| 178 | Objects.equal(this.m3, that.m3); |
| 179 | } |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | @Test(groups = "short") |
| 185 | public void testCollections() throws Exception { |
| 186 | Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class); |
| 187 | |
| 188 | CollectionExamples c = new CollectionExamples(1, 1); |
| 189 | m.save(c); |
| 190 | |
| 191 | assertEquals(m.get(c.getId()), c); |
| 192 | } |
| 193 | |
| 194 | @Test(groups = "short") |
| 195 | public void testNullCollection() { |
| 196 | Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class); |
| 197 | |
| 198 | CollectionExamples c = new CollectionExamples(1, 1); |
| 199 | c.setL(null); |
| 200 | c.setS(null); |
| 201 | c.setM1(null); |
| 202 | c.setM2(null); |
| 203 | c.setM3(null); |
| 204 | m.save(c); |
| 205 | |
| 206 | assertEquals(m.get(c.getId()), c); |
| 207 | } |
| 208 | } |