Elffuss BraidHub

kikocis/java-driver public

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