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