Elffuss BraidHub

kikocis/java-driver public

356657f61364 · 13.5 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.Sets;
22import org.testng.annotations.AfterMethod;
23import org.testng.annotations.BeforeClass;
24import org.testng.annotations.Test;
25
26import java.util.Set;
27import java.util.UUID;
28
29import static org.assertj.core.api.Assertions.assertThat;
30
31/**
32 * Tests for
33 * JAVA-541 Add polymorphism support to object mapper
34 * JAVA-636 Allow @Column annotations on getters/setters as well as fields
35 */
36@SuppressWarnings({"unused", "WeakerAccess"})
37public class MapperPolymorphismTest extends CCMTestsSupport {
38
39 Circle circle = new Circle(new Point2D(11, 22), 12.34);
40 Rectangle rectangle = new Rectangle(new Point2D(20, 30), new Point2D(50, 60));
41 Square square = new Square(new Point2D(20, 30), new Point2D(50, 60));
42 Sphere sphere = new Sphere(new Point3D(11, 22, 33), 34.56);
43
44 Mapper<Circle> circleMapper;
45 Mapper<Rectangle> rectangleMapper;
46 Mapper<Square> squareMapper;
47 Mapper<Sphere> sphereMapper;
48
49 @Override
50 public void onTestContextInitialized() {
51 execute(
52 "CREATE TYPE point2d (\"X\" int, \"Y\" int)",
53 "CREATE TYPE point3d (\"X\" int, \"Y\" int, \"Z\" int)",
54 "CREATE TABLE circles (circle_id uuid PRIMARY KEY, center2d frozen<point2d>, radius double, tags set<text>)",
55 "CREATE TABLE rectangles (rect_id uuid PRIMARY KEY, bottom_left frozen<point2d>, top_right frozen<point2d>, tags set<text>)",
56 "CREATE TABLE squares (square_id uuid PRIMARY KEY, bottom_left frozen<point2d>, top_right frozen<point2d>, tags set<text>)",
57 "CREATE TABLE spheres (sphere_id uuid PRIMARY KEY, center3d frozen<point3d>, radius double, tags set<text>)");
58 }
59
60 @BeforeClass(groups = "short")
61 public void createMappers() throws Exception {
62 MappingManager mappingManager = new MappingManager(session());
63 circleMapper = mappingManager.mapper(Circle.class);
64 rectangleMapper = mappingManager.mapper(Rectangle.class);
65 squareMapper = mappingManager.mapper(Square.class);
66 sphereMapper = mappingManager.mapper(Sphere.class);
67 }
68
69 @AfterMethod(groups = "short")
70 public void clean() {
71 execute("TRUNCATE circles", "TRUNCATE rectangles", "TRUNCATE squares", "TRUNCATE spheres");
72 }
73
74 @UDT(name = "point2d")
75 static class Point2D {
76
77 // test mix of field and getter - getter should win
78 @Field(name = "wrong")
79 private int x;
80
81 private int y;
82
83 // test private constructor + "immutability"
84 private Point2D() {
85 }
86
87 public Point2D(int x, int y) {
88 this.x = x;
89 this.y = y;
90 }
91
92 @Field(name = "X", caseSensitive = true)
93 public int getX() {
94 return x;
95 }
96
97 @Field(name = "Y", caseSensitive = true)
98 public int getY() {
99 return y;
100 }
101
102 @Override
103 public boolean equals(Object o) {
104 if (this == o) return true;
105 if (o == null || getClass() != o.getClass()) return false;
106 Point2D point2D = (Point2D) o;
107 return getX() == point2D.getX() &&
108 getY() == point2D.getY();
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hashCode(getX(), getY());
114 }
115 }
116
117 @UDT(name = "point3d")
118 static class Point3D extends Point2D {
119
120 private int z;
121
122 // test private constructor + "immutability"
123 private Point3D() {
124 }
125
126 public Point3D(int x, int y, int z) {
127 super(x, y);
128 this.z = z;
129 }
130
131 @Field(name = "Z", caseSensitive = true)
132 public int getZ() {
133 return z;
134 }
135
136 @Override
137 public boolean equals(Object o) {
138 if (this == o) return true;
139 if (o == null || getClass() != o.getClass()) return false;
140 if (!super.equals(o)) return false;
141 Point3D point3D = (Point3D) o;
142 return getZ() == point3D.getZ();
143 }
144
145 @Override
146 public int hashCode() {
147 return Objects.hashCode(super.hashCode(), getZ());
148 }
149 }
150
151 interface Shape2D {
152
153 Set<String> getTags();
154
155 // test annotation on interface method, should get inherited everywhere
156 @Transient
157 double getArea();
158
159 }
160
161 interface Shape3D {
162
163 double getVolume();
164
165 }
166
167 static abstract class Shape implements Shape2D {
168
169 @PartitionKey // annotated field on superclass; annotation will get inherited in all subclasses
170 protected UUID id;
171
172 protected Set<String> tags;
173
174 public Shape() {
175 this.id = UUID.randomUUID();
176 this.tags = Sets.newHashSet("cool", "awesome");
177 }
178
179 @Column(name = "wrong") // gets overridden in all subclasses
180 public abstract UUID getId();
181
182 public void setId(UUID id) {
183 this.id = id;
184 }
185
186 @Override
187 public Set<String> getTags() {
188 return tags;
189 }
190
191 @Override
192 public boolean equals(Object o) {
193 if (this == o) return true;
194 if (o == null || getClass() != o.getClass()) return false;
195 Shape shape = (Shape) o;
196 return Objects.equal(getId(), shape.getId());
197 }
198
199 @Override
200 public int hashCode() {
201 return Objects.hashCode(getId());
202 }
203 }
204
205 @Table(name = "circles")
206 static class Circle extends Shape {
207
208 @Column(name = "center2d") // overridden by a getter in Sphere
209 @Frozen
210 private Point2D center;
211
212 // tests unusual field name (i.e. does not correspond to getter/setter)
213 // will be considered as a separate "property" with no getter nor setter;
214 // thus needs to be annotated with @Transient
215 @Transient
216 private double _radius;
217
218 private long writeTime;
219
220 public Circle() {
221 }
222
223 public Circle(Point2D center, double radius) {
224 this.center = center;
225 this._radius = radius;
226 }
227
228 @Column(name = "circle_id")
229 @Override
230 public UUID getId() {
231 return id;
232 }
233
234 public Point2D getCenter() {
235 return center;
236 }
237
238 public void setCenter(Point2D center) {
239 this.center = center;
240 }
241
242 public double getRadius() {
243 return _radius;
244 }
245
246 public void setRadius(double radius) {
247 this._radius = radius;
248 }
249
250 @Override
251 // inherits @Transient
252 public double getArea() {
253 return Math.PI * (Math.pow(getRadius(), 2));
254 }
255
256 // tests computed columns - no setter
257 @Computed(value = "writetime(\"radius\")")
258 public long getWriteTime() {
259 return writeTime;
260 }
261
262 // mismatched setter - getter is declared in Shape
263 public void setTags(Set<String> tags) {
264 this.tags = tags;
265 }
266
267 @Override
268 public boolean equals(Object o) {
269 if (this == o) return true;
270 if (o == null || getClass() != o.getClass()) return false;
271 if (!super.equals(o)) return false;
272 Circle circle = (Circle) o;
273 return Double.compare(circle.getRadius(), getRadius()) == 0 &&
274 Objects.equal(getCenter(), circle.getCenter());
275 }
276
277 @Override
278 public int hashCode() {
279 return Objects.hashCode(super.hashCode(), getCenter(), getRadius());
280 }
281 }
282
283 @Table(name = "rectangles")
284 static class Rectangle extends Shape {
285
286 private Point2D bottomLeft;
287
288 private Point2D topRight;
289
290 public Rectangle() {
291 }
292
293 public Rectangle(Point2D bottomLeft, Point2D topRight) {
294 this.bottomLeft = bottomLeft;
295 this.topRight = topRight;
296 }
297
298 @Column(name = "rect_id")
299 @Override
300 public UUID getId() {
301 return id;
302 }
303
304 @Column(name = "bottom_left")
305 @Frozen
306 public Point2D getBottomLeft() {
307 return bottomLeft;
308 }
309
310 public void setBottomLeft(Point2D bottomLeft) {
311 this.bottomLeft = bottomLeft;
312 }
313
314 @Column(name = "top_right")
315 @Frozen
316 public Point2D getTopRight() {
317 return topRight;
318 }
319
320 public void setTopRight(Point2D topRight) {
321 this.topRight = topRight;
322 }
323
324 // test annotation in class method
325 @Transient
326 public double getWidth() {
327 return Math.abs(topRight.getX() - bottomLeft.getX());
328 }
329
330 @Transient
331 public double getHeight() {
332 return Math.abs(topRight.getY() - bottomLeft.getY());
333 }
334
335 @Override
336 // inherits @Transient
337 public double getArea() {
338 return getWidth() * getHeight();
339 }
340
341 @Override
342 public boolean equals(Object o) {
343 if (this == o) return true;
344 if (o == null || getClass() != o.getClass()) return false;
345 if (!super.equals(o)) return false;
346 Rectangle rectangle = (Rectangle) o;
347 return Objects.equal(getBottomLeft(), rectangle.getBottomLeft()) &&
348 Objects.equal(getTopRight(), rectangle.getTopRight());
349 }
350
351 @Override
352 public int hashCode() {
353 return Objects.hashCode(super.hashCode(), getBottomLeft(), getTopRight());
354 }
355 }
356
357 @Table(name = "squares")
358 static class Square extends Rectangle {
359
360 public Square() {
361 }
362
363 public Square(Point2D bottomLeft, Point2D topRight) {
364 super(bottomLeft, topRight);
365 assert getHeight() == getWidth();
366 }
367
368 @Column(name = "square_id")
369 @Override
370 public UUID getId() {
371 return id;
372 }
373
374 @Override
375 // inherits @Column and @Frozen
376 public Point2D getBottomLeft() {
377 return super.getBottomLeft();
378 }
379
380 @Override
381 // inherits @Transient
382 public double getHeight() {
383 return getWidth();
384 }
385
386 @Override
387 public boolean equals(Object o) {
388 if (this == o) return true;
389 if (o == null || getClass() != o.getClass()) return false;
390 if (!super.equals(o)) return false;
391 Square square = (Square) o;
392 return Objects.equal(getBottomLeft(), square.getBottomLeft()) &&
393 Objects.equal(getTopRight(), square.getTopRight());
394 }
395
396 }
397
398 @Table(name = "spheres")
399 static class Sphere extends Circle implements Shape3D {
400
401 // masks field Circle.center
402 @Frozen
403 private Point3D center;
404
405 private long writeTime;
406
407 public Sphere() {
408 }
409
410 public Sphere(Point3D center, double radius) {
411 this.center = center;
412 }
413
414 @Column(name = "sphere_id")
415 @Override
416 public UUID getId() {
417 return id;
418 }
419
420 // overrides field annotation in Circle,
421 // note that the property type is narrowed down to Point3D
422 @Column(name = "center3d")
423 @Frozen
424 @Override
425 public Point3D getCenter() {
426 return center;
427 }
428
429 public void setCenter(Point3D center) {
430 this.center = center;
431 }
432
433 @Override
434 public boolean equals(Object o) {
435 if (this == o) return true;
436 if (o == null || getClass() != o.getClass()) return false;
437 if (!super.equals(o)) return false;
438 Sphere sphere = (Sphere) o;
439 return Objects.equal(getCenter(), sphere.getCenter());
440 }
441
442 @Override
443 public int hashCode() {
444 return Objects.hashCode(super.hashCode(), getCenter());
445 }
446
447 // test annotation on implementation
448 @Override
449 @Transient
450 public double getVolume() {
451 return 4d / 3d * Math.PI * Math.pow(getRadius(), 3);
452 }
453
454 }
455
456 @Test(groups = "short")
457 public void should_save_and_retrieve_circle() throws Exception {
458 circleMapper.save(circle);
459 assertThat(circleMapper.get(circle.getId())).isEqualTo(circle);
460 }
461
462 @Test(groups = "short")
463 public void should_save_and_retrieve_rectangle() throws Exception {
464 rectangleMapper.save(rectangle);
465 assertThat(rectangleMapper.get(rectangle.getId())).isEqualTo(rectangle);
466 }
467
468 @Test(groups = "short")
469 public void should_save_and_retrieve_square() throws Exception {
470 squareMapper.save(square);
471 assertThat(squareMapper.get(square.getId())).isEqualTo(square);
472 }
473
474 @Test(groups = "short")
475 public void should_save_and_retrieve_sphere() throws Exception {
476 sphereMapper.save(sphere);
477 assertThat(sphereMapper.get(sphere.getId())).isEqualTo(sphere);
478 }
479
480}