Elffuss BraidHub

kikocis/java-driver public

b1c8c4921d4c · 5.5 KiBRawPermalink
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 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.CCMTestsSupport;
19import com.datastax.driver.mapping.annotations.*;
20import com.google.common.base.Objects;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Sets;
23import org.testng.annotations.Test;
24import org.testng.collections.Lists;
25
26import java.util.List;
27import java.util.Map;
28import java.util.Set;
29
30import static org.testng.Assert.assertEquals;
31
32/**
33 * Tests mapping of collections of UDTs.
34 */
35@SuppressWarnings("unused")
36public class MapperUDTCollectionsTest extends CCMTestsSupport {
37
38 @Override
39 public void onTestContextInitialized() {
40 execute("CREATE TYPE \"Sub\"(i int)",
41 "CREATE TABLE collection_examples (id int PRIMARY KEY, " +
42 "l list<frozen<\"Sub\">>, " +
43 "s set<frozen<\"Sub\">>, " +
44 "m1 map<int,frozen<\"Sub\">>, " +
45 "m2 map<frozen<\"Sub\">,int>, " +
46 "m3 map<frozen<\"Sub\">,frozen<\"Sub\">>)");
47 }
48
49 @UDT(name = "Sub", caseSensitiveType = true)
50 public static class Sub {
51 private int i;
52
53 public Sub() {
54 }
55
56 public Sub(int i) {
57 this.i = i;
58 }
59
60 public int getI() {
61 return i;
62 }
63
64 public void setI(int i) {
65 this.i = i;
66 }
67
68 @Override
69 public boolean equals(Object other) {
70 if (other instanceof Sub) {
71 Sub that = (Sub) other;
72 return this.i == that.i;
73 }
74 return false;
75 }
76
77 @Override
78 public int hashCode() {
79 return Objects.hashCode(i);
80 }
81 }
82
83 @Table(name = "collection_examples")
84 public static class CollectionExamples {
85 @PartitionKey
86 private int id;
87
88 @FrozenValue
89 private List<Sub> l;
90
91 @FrozenValue
92 private Set<Sub> s;
93
94 @FrozenValue
95 private Map<Integer, Sub> m1;
96
97 @FrozenKey
98 private Map<Sub, Integer> m2;
99
100 @FrozenKey
101 @FrozenValue
102 private Map<Sub, Sub> m3;
103
104 public CollectionExamples() {
105 }
106
107 public CollectionExamples(int id, int value) {
108 this.id = id;
109 // Just fill the collections with random values
110 Sub sub1 = new Sub(value);
111 Sub sub2 = new Sub(value + 1);
112 this.l = Lists.newArrayList(sub1, sub2);
113 this.s = Sets.newHashSet(sub1, sub2);
114 this.m1 = ImmutableMap.of(1, sub1, 2, sub2);
115 this.m2 = ImmutableMap.of(sub1, 1, sub2, 2);
116 this.m3 = ImmutableMap.of(sub1, sub1, sub2, sub2);
117 }
118
119 public int getId() {
120 return id;
121 }
122
123 public void setId(int id) {
124 this.id = id;
125 }
126
127 public List<Sub> getL() {
128 return l;
129 }
130
131 public void setL(List<Sub> l) {
132 this.l = l;
133 }
134
135 public Set<Sub> getS() {
136 return s;
137 }
138
139 public void setS(Set<Sub> s) {
140 this.s = s;
141 }
142
143 public Map<Integer, Sub> getM1() {
144 return m1;
145 }
146
147 public void setM1(Map<Integer, Sub> m1) {
148 this.m1 = m1;
149 }
150
151 public Map<Sub, Integer> getM2() {
152 return m2;
153 }
154
155 public void setM2(Map<Sub, Integer> m2) {
156 this.m2 = m2;
157 }
158
159 public Map<Sub, Sub> getM3() {
160 return m3;
161 }
162
163 public void setM3(Map<Sub, Sub> m3) {
164 this.m3 = m3;
165 }
166
167 @Override
168 public boolean equals(Object other) {
169 if (other instanceof CollectionExamples) {
170 CollectionExamples that = (CollectionExamples) other;
171 return Objects.equal(this.id, that.id) &&
172 Objects.equal(this.l, that.l) &&
173 Objects.equal(this.s, that.s) &&
174 Objects.equal(this.m1, that.m1) &&
175 Objects.equal(this.m2, that.m2) &&
176 Objects.equal(this.m3, that.m3);
177 }
178 return false;
179 }
180 }
181
182 @Test(groups = "short")
183 public void testCollections() throws Exception {
184 Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class);
185
186 CollectionExamples c = new CollectionExamples(1, 1);
187 m.save(c);
188
189 assertEquals(m.get(c.getId()), c);
190 }
191
192 @Test(groups = "short")
193 public void testNullCollection() {
194 Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class);
195
196 CollectionExamples c = new CollectionExamples(1, 1);
197 c.setL(null);
198 c.setS(null);
199 c.setM1(null);
200 c.setM2(null);
201 c.setM3(null);
202 m.save(c);
203
204 assertEquals(m.get(c.getId()), c);
205 }
206}