Elffuss BraidHub

kikocis/java-driver public

a7a8be09fd1a · 13.8 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 // builder-style setter; because the field isn't named in a standard way,
247 // if this setter is not detected the test would fail
248 public Circle setRadius(double radius) {
249 this._radius = radius;
250 return this;
251 }
252
253 @Override
254 // inherits @Transient
255 public double getArea() {
256 return Math.PI * (Math.pow(getRadius(), 2));
257 }
258
259 // tests computed columns - no setter
260 @Computed(value = "writetime(\"radius\")")
261 public long getWriteTime() {
262 return writeTime;
263 }
264
265 // mismatched setter - getter is declared in Shape
266 public void setTags(Set<String> tags) {
267 this.tags = tags;
268 }
269
270 @Override
271 public boolean equals(Object o) {
272 if (this == o) return true;
273 if (o == null || getClass() != o.getClass()) return false;
274 if (!super.equals(o)) return false;
275 Circle circle = (Circle) o;
276 return Double.compare(circle.getRadius(), getRadius()) == 0 &&
277 Objects.equal(getCenter(), circle.getCenter());
278 }
279
280 @Override
281 public int hashCode() {
282 return Objects.hashCode(super.hashCode(), getCenter(), getRadius());
283 }
284 }
285
286 @Table(name = "rectangles")
287 static class Rectangle extends Shape {
288
289 private Point2D bottomLeft;
290
291 private Point2D topRight;
292
293 public Rectangle() {
294 }
295
296 public Rectangle(Point2D bottomLeft, Point2D topRight) {
297 this.bottomLeft = bottomLeft;
298 this.topRight = topRight;
299 }
300
301 @Column(name = "rect_id")
302 @Override
303 public UUID getId() {
304 return id;
305 }
306
307 @Column(name = "bottom_left")
308 @Frozen
309 public Point2D getBottomLeft() {
310 return bottomLeft;
311 }
312
313 public void setBottomLeft(Point2D bottomLeft) {
314 this.bottomLeft = bottomLeft;
315 }
316
317 @Column(name = "top_right")
318 @Frozen
319 public Point2D getTopRight() {
320 return topRight;
321 }
322
323 public void setTopRight(Point2D topRight) {
324 this.topRight = topRight;
325 }
326
327 // test annotation in class method
328 @Transient
329 public double getWidth() {
330 return Math.abs(topRight.getX() - bottomLeft.getX());
331 }
332
333 @Transient
334 public double getHeight() {
335 return Math.abs(topRight.getY() - bottomLeft.getY());
336 }
337
338 @Override
339 // inherits @Transient
340 public double getArea() {
341 return getWidth() * getHeight();
342 }
343
344 @Override
345 public boolean equals(Object o) {
346 if (this == o) return true;
347 if (o == null || getClass() != o.getClass()) return false;
348 if (!super.equals(o)) return false;
349 Rectangle rectangle = (Rectangle) o;
350 return Objects.equal(getBottomLeft(), rectangle.getBottomLeft()) &&
351 Objects.equal(getTopRight(), rectangle.getTopRight());
352 }
353
354 @Override
355 public int hashCode() {
356 return Objects.hashCode(super.hashCode(), getBottomLeft(), getTopRight());
357 }
358 }
359
360 @Table(name = "squares")
361 static class Square extends Rectangle {
362
363 public Square() {
364 }
365
366 public Square(Point2D bottomLeft, Point2D topRight) {
367 super(bottomLeft, topRight);
368 assert getHeight() == getWidth();
369 }
370
371 @Column(name = "square_id")
372 @Override
373 public UUID getId() {
374 return id;
375 }
376
377 @Override
378 // inherits @Column and @Frozen
379 public Point2D getBottomLeft() {
380 return super.getBottomLeft();
381 }
382
383 @Override
384 // inherits @Transient
385 public double getHeight() {
386 return getWidth();
387 }
388
389 @Override
390 public boolean equals(Object o) {
391 if (this == o) return true;
392 if (o == null || getClass() != o.getClass()) return false;
393 if (!super.equals(o)) return false;
394 Square square = (Square) o;
395 return Objects.equal(getBottomLeft(), square.getBottomLeft()) &&
396 Objects.equal(getTopRight(), square.getTopRight());
397 }
398
399 }
400
401 @Table(name = "spheres")
402 static class Sphere extends Circle implements Shape3D {
403
404 // masks field Circle.center
405 @Frozen
406 private Point3D center;
407
408 private long writeTime;
409
410 public Sphere() {
411 }
412
413 public Sphere(Point3D center, double radius) {
414 this.center = center;
415 }
416
417 @Column(name = "sphere_id")
418 @Override
419 public UUID getId() {
420 return id;
421 }
422
423 // overrides field annotation in Circle,
424 // note that the property type is narrowed down to Point3D
425 @Column(name = "center3d")
426 @Frozen
427 @Override
428 public Point3D getCenter() {
429 return center;
430 }
431
432 public void setCenter(Point3D center) {
433 this.center = center;
434 }
435
436 // overridden builder-style setter
437 @Override
438 public Sphere setRadius(double radius) {
439 super.setRadius(radius);
440 return this;
441 }
442
443 @Override
444 public boolean equals(Object o) {
445 if (this == o) return true;
446 if (o == null || getClass() != o.getClass()) return false;
447 if (!super.equals(o)) return false;
448 Sphere sphere = (Sphere) o;
449 return Objects.equal(getCenter(), sphere.getCenter());
450 }
451
452 @Override
453 public int hashCode() {
454 return Objects.hashCode(super.hashCode(), getCenter());
455 }
456
457 // test annotation on implementation
458 @Override
459 @Transient
460 public double getVolume() {
461 return 4d / 3d * Math.PI * Math.pow(getRadius(), 3);
462 }
463
464 }
465
466 @Test(groups = "short")
467 public void should_save_and_retrieve_circle() throws Exception {
468 circleMapper.save(circle);
469 assertThat(circleMapper.get(circle.getId())).isEqualTo(circle);
470 }
471
472 @Test(groups = "short")
473 public void should_save_and_retrieve_rectangle() throws Exception {
474 rectangleMapper.save(rectangle);
475 assertThat(rectangleMapper.get(rectangle.getId())).isEqualTo(rectangle);
476 }
477
478 @Test(groups = "short")
479 public void should_save_and_retrieve_square() throws Exception {
480 squareMapper.save(square);
481 assertThat(squareMapper.get(square.getId())).isEqualTo(square);
482 }
483
484 @Test(groups = "short")
485 public void should_save_and_retrieve_sphere() throws Exception {
486 sphereMapper.save(sphere);
487 assertThat(sphereMapper.get(sphere.getId())).isEqualTo(sphere);
488 }
489
490}