Elffuss BraidHub

kikocis/java-driver public

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