Elffuss BraidHub

kikocis/java-driver public

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