Elffuss BraidHub

kikocis/java-driver public

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