Elffuss BraidHub

kikocis/java-driver public

c3395b80b593 · 5.3 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, l list<frozen<sub>>, s set<frozen<sub>>, m1 map<int,frozen<sub>>, m2 map<frozen<sub>,int>, m3 map<frozen<sub>,frozen<sub>>)");
42 }
43
44 @UDT(name = "sub")
45 public static class Sub {
46 private int i;
47
48 public Sub() {
49 }
50
51 public Sub(int i) {
52 this.i = i;
53 }
54
55 public int getI() {
56 return i;
57 }
58
59 public void setI(int i) {
60 this.i = i;
61 }
62
63 @Override
64 public boolean equals(Object other) {
65 if (other instanceof Sub) {
66 Sub that = (Sub) other;
67 return this.i == that.i;
68 }
69 return false;
70 }
71
72 @Override
73 public int hashCode() {
74 return Objects.hashCode(i);
75 }
76 }
77
78 @Table(name = "collection_examples")
79 public static class CollectionExamples {
80 @PartitionKey
81 private int id;
82
83 @FrozenValue
84 private List<Sub> l;
85
86 @FrozenValue
87 private Set<Sub> s;
88
89 @FrozenValue
90 private Map<Integer, Sub> m1;
91
92 @FrozenKey
93 private Map<Sub, Integer> m2;
94
95 @FrozenKey
96 @FrozenValue
97 private Map<Sub, Sub> m3;
98
99 public CollectionExamples() {
100 }
101
102 public CollectionExamples(int id, int value) {
103 this.id = id;
104 // Just fill the collections with random values
105 Sub sub1 = new Sub(value);
106 Sub sub2 = new Sub(value + 1);
107 this.l = Lists.newArrayList(sub1, sub2);
108 this.s = Sets.newHashSet(sub1, sub2);
109 this.m1 = ImmutableMap.of(1, sub1, 2, sub2);
110 this.m2 = ImmutableMap.of(sub1, 1, sub2, 2);
111 this.m3 = ImmutableMap.of(sub1, sub1, sub2, sub2);
112 }
113
114 public int getId() {
115 return id;
116 }
117
118 public void setId(int id) {
119 this.id = id;
120 }
121
122 public List<Sub> getL() {
123 return l;
124 }
125
126 public void setL(List<Sub> l) {
127 this.l = l;
128 }
129
130 public Set<Sub> getS() {
131 return s;
132 }
133
134 public void setS(Set<Sub> s) {
135 this.s = s;
136 }
137
138 public Map<Integer, Sub> getM1() {
139 return m1;
140 }
141
142 public void setM1(Map<Integer, Sub> m1) {
143 this.m1 = m1;
144 }
145
146 public Map<Sub, Integer> getM2() {
147 return m2;
148 }
149
150 public void setM2(Map<Sub, Integer> m2) {
151 this.m2 = m2;
152 }
153
154 public Map<Sub, Sub> getM3() {
155 return m3;
156 }
157
158 public void setM3(Map<Sub, Sub> m3) {
159 this.m3 = m3;
160 }
161
162 @Override
163 public boolean equals(Object other) {
164 if (other instanceof CollectionExamples) {
165 CollectionExamples that = (CollectionExamples) other;
166 return Objects.equal(this.id, that.id) &&
167 Objects.equal(this.l, that.l) &&
168 Objects.equal(this.s, that.s) &&
169 Objects.equal(this.m1, that.m1) &&
170 Objects.equal(this.m2, that.m2) &&
171 Objects.equal(this.m3, that.m3);
172 }
173 return false;
174 }
175 }
176
177 @Test(groups = "short")
178 public void testCollections() throws Exception {
179 Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class);
180
181 CollectionExamples c = new CollectionExamples(1, 1);
182 m.save(c);
183
184 assertEquals(m.get(c.getId()), c);
185 }
186
187 @Test(groups = "short")
188 public void testNullCollection() {
189 Mapper<CollectionExamples> m = new MappingManager(session()).mapper(CollectionExamples.class);
190
191 CollectionExamples c = new CollectionExamples(1, 1);
192 c.setL(null);
193 c.setS(null);
194 c.setM1(null);
195 c.setM2(null);
196 c.setM3(null);
197 m.save(c);
198
199 assertEquals(m.get(c.getId()), c);
200 }
201}