Elffuss BraidHub

kikocis/java-driver public

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