| 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.Column; |
| 20 | import com.datastax.driver.mapping.annotations.PartitionKey; |
| 21 | import com.datastax.driver.mapping.annotations.Table; |
| 22 | import com.datastax.driver.mapping.annotations.UDT; |
| 23 | import com.google.common.base.Objects; |
| 24 | import org.testng.annotations.BeforeMethod; |
| 25 | import org.testng.annotations.Test; |
| 26 | |
| 27 | import java.util.UUID; |
| 28 | |
| 29 | import static org.assertj.core.api.Assertions.assertThat; |
| 30 | |
| 31 | @SuppressWarnings("unused") |
| 32 | public class MapperPolymorphismTest extends CCMTestsSupport { |
| 33 | |
| 34 | private final Circle circle = new Circle(new Point2D(11, 22), 12.34); |
| 35 | private final Rectangle rectangle = new Rectangle(new Point2D(20, 30), new Point2D(50, 60)); |
| 36 | private final Sphere sphere = new Sphere(new Point3D(11, 22, 33), 34.56); |
| 37 | private Mapper<Circle> circleMapper; |
| 38 | private Mapper<Rectangle> rectangleMapper; |
| 39 | private Mapper<Sphere> sphereMapper; |
| 40 | |
| 41 | @Override |
| 42 | public void onTestContextInitialized() { |
| 43 | execute( |
| 44 | "CREATE TYPE point2d (x int, y int)", |
| 45 | "CREATE TYPE point3d (x int, y int, z int)", |
| 46 | "CREATE TABLE circles (id uuid PRIMARY KEY, center frozen<point2d>, radius double)", |
| 47 | "CREATE TABLE rectangles (id uuid PRIMARY KEY, bottom_left frozen<point2d>, top_right frozen<point2d>)", |
| 48 | "CREATE TABLE spheres (id uuid PRIMARY KEY, center frozen<point3d>, radius double)"); |
| 49 | } |
| 50 | |
| 51 | @BeforeMethod(groups = "short") |
| 52 | public void clean() { |
| 53 | execute("TRUNCATE circles", "TRUNCATE rectangles", "TRUNCATE spheres"); |
| 54 | } |
| 55 | |
| 56 | @UDT(name = "point2d") |
| 57 | static class Point2D { |
| 58 | |
| 59 | private int x; |
| 60 | |
| 61 | private int y; |
| 62 | |
| 63 | public Point2D() { |
| 64 | } |
| 65 | |
| 66 | public Point2D(int x, int y) { |
| 67 | this.x = x; |
| 68 | this.y = y; |
| 69 | } |
| 70 | |
| 71 | public int getX() { |
| 72 | return x; |
| 73 | } |
| 74 | |
| 75 | public void setX(int x) { |
| 76 | this.x = x; |
| 77 | } |
| 78 | |
| 79 | public int getY() { |
| 80 | return y; |
| 81 | } |
| 82 | |
| 83 | public void setY(int y) { |
| 84 | this.y = 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 | public Point3D() { |
| 108 | } |
| 109 | |
| 110 | public Point3D(int x, int y, int z) { |
| 111 | super(x, y); |
| 112 | this.z = z; |
| 113 | } |
| 114 | |
| 115 | public int getZ() { |
| 116 | return z; |
| 117 | } |
| 118 | |
| 119 | public void setZ(int z) { |
| 120 | this.z = 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 | static class Shape { |
| 139 | |
| 140 | @PartitionKey |
| 141 | private UUID id; |
| 142 | |
| 143 | public Shape() { |
| 144 | this.id = UUID.randomUUID(); |
| 145 | } |
| 146 | |
| 147 | public UUID getId() { |
| 148 | return id; |
| 149 | } |
| 150 | |
| 151 | public void setId(UUID id) { |
| 152 | this.id = id; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public boolean equals(Object o) { |
| 157 | if (this == o) return true; |
| 158 | if (o == null || getClass() != o.getClass()) return false; |
| 159 | Shape shape = (Shape) o; |
| 160 | return Objects.equal(getId(), shape.getId()); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public int hashCode() { |
| 165 | return Objects.hashCode(getId()); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | @Table(name = "circles") |
| 170 | static class Circle extends Shape { |
| 171 | |
| 172 | private Point2D center; |
| 173 | |
| 174 | private double radius; |
| 175 | |
| 176 | public Circle() { |
| 177 | } |
| 178 | |
| 179 | public Circle(Point2D center, double radius) { |
| 180 | this.center = center; |
| 181 | this.radius = radius; |
| 182 | } |
| 183 | |
| 184 | public Point2D getCenter() { |
| 185 | return center; |
| 186 | } |
| 187 | |
| 188 | public void setCenter(Point2D center) { |
| 189 | this.center = center; |
| 190 | } |
| 191 | |
| 192 | public double getRadius() { |
| 193 | return radius; |
| 194 | } |
| 195 | |
| 196 | public void setRadius(double radius) { |
| 197 | this.radius = radius; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean equals(Object o) { |
| 202 | if (this == o) return true; |
| 203 | if (o == null || getClass() != o.getClass()) return false; |
| 204 | if (!super.equals(o)) return false; |
| 205 | Circle circle = (Circle) o; |
| 206 | return Double.compare(circle.getRadius(), getRadius()) == 0 && |
| 207 | Objects.equal(getCenter(), circle.getCenter()); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public int hashCode() { |
| 212 | return Objects.hashCode(super.hashCode(), getCenter(), getRadius()); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | @Table(name = "rectangles") |
| 217 | static class Rectangle extends Shape { |
| 218 | |
| 219 | @Column(name = "bottom_left") |
| 220 | private Point2D bottomLeft; |
| 221 | |
| 222 | @Column(name = "top_right") |
| 223 | private Point2D topRight; |
| 224 | |
| 225 | public Rectangle() { |
| 226 | } |
| 227 | |
| 228 | public Rectangle(Point2D bottomLeft, Point2D topRight) { |
| 229 | this.bottomLeft = bottomLeft; |
| 230 | this.topRight = topRight; |
| 231 | } |
| 232 | |
| 233 | public Point2D getBottomLeft() { |
| 234 | return bottomLeft; |
| 235 | } |
| 236 | |
| 237 | public void setBottomLeft(Point2D bottomLeft) { |
| 238 | this.bottomLeft = bottomLeft; |
| 239 | } |
| 240 | |
| 241 | public Point2D getTopRight() { |
| 242 | return topRight; |
| 243 | } |
| 244 | |
| 245 | public void setTopRight(Point2D topRight) { |
| 246 | this.topRight = topRight; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public boolean equals(Object o) { |
| 251 | if (this == o) return true; |
| 252 | if (o == null || getClass() != o.getClass()) return false; |
| 253 | if (!super.equals(o)) return false; |
| 254 | Rectangle rectangle = (Rectangle) o; |
| 255 | return Objects.equal(getBottomLeft(), rectangle.getBottomLeft()) && |
| 256 | Objects.equal(getTopRight(), rectangle.getTopRight()); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public int hashCode() { |
| 261 | return Objects.hashCode(super.hashCode(), getBottomLeft(), getTopRight()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | @Table(name = "spheres") |
| 266 | static class Sphere extends Circle { |
| 267 | |
| 268 | private Point3D center; |
| 269 | |
| 270 | public Sphere() { |
| 271 | } |
| 272 | |
| 273 | public Sphere(Point3D center, double radius) { |
| 274 | this.center = center; |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public Point3D getCenter() { |
| 279 | return center; |
| 280 | } |
| 281 | |
| 282 | public void setCenter(Point3D center) { |
| 283 | this.center = center; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public boolean equals(Object o) { |
| 288 | if (this == o) return true; |
| 289 | if (o == null || getClass() != o.getClass()) return false; |
| 290 | if (!super.equals(o)) return false; |
| 291 | Sphere sphere = (Sphere) o; |
| 292 | return Objects.equal(getCenter(), sphere.getCenter()); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public int hashCode() { |
| 297 | return Objects.hashCode(super.hashCode(), getCenter()); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | @BeforeMethod(groups = "short") |
| 302 | public void createMappers() throws Exception { |
| 303 | MappingManager mappingManager = new MappingManager(session()); |
| 304 | circleMapper = mappingManager.mapper(Circle.class); |
| 305 | rectangleMapper = mappingManager.mapper(Rectangle.class); |
| 306 | sphereMapper = mappingManager.mapper(Sphere.class); |
| 307 | } |
| 308 | |
| 309 | @Test(groups = "short") |
| 310 | public void should_save_and_retrieve_circle() throws Exception { |
| 311 | circleMapper.save(circle); |
| 312 | assertThat(circleMapper.get(circle.getId())).isEqualTo(circle); |
| 313 | } |
| 314 | |
| 315 | @Test(groups = "short") |
| 316 | public void should_save_and_retrieve_rectangle() throws Exception { |
| 317 | rectangleMapper.save(rectangle); |
| 318 | assertThat(rectangleMapper.get(rectangle.getId())).isEqualTo(rectangle); |
| 319 | } |
| 320 | |
| 321 | @Test(groups = "short") |
| 322 | public void should_save_and_retrieve_sphere() throws Exception { |
| 323 | sphereMapper.save(sphere); |
| 324 | assertThat(sphereMapper.get(sphere.getId())).isEqualTo(sphere); |
| 325 | } |
| 326 | |
| 327 | } |