Elffuss BraidHub

kikocis/java-driver public

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