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