/tests/com/google/appengine/datanucleus/jpa/JPABidirectionalOneToManySubclassTest.java
Java | 389 lines | 287 code | 58 blank | 44 comment | 0 complexity | 9f60c215fc55c6e362c479b5503bda41 MD5 | raw file
1/* 2 * Copyright (C) 2010 Google 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.google.appengine.datanucleus.jpa; 17 18import com.google.appengine.api.datastore.Entity; 19import com.google.appengine.api.datastore.EntityNotFoundException; 20import com.google.appengine.api.datastore.KeyFactory; 21import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example1; 22import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example2; 23import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example3; 24import com.google.appengine.datanucleus.test.jpa.BidirectionalOneToManySubclassesJPA.Example4; 25 26 27import java.util.Collections; 28 29/** 30 * @author Max Ross <max.ross@gmail.com> 31 */ 32public class JPABidirectionalOneToManySubclassTest extends JPATestCase { 33 34 public void testExample1Subclass() throws EntityNotFoundException { 35 // insertion 36 Example1.B parent = new Example1.B(); 37 parent.setAString("a string"); 38 parent.setBString("b string"); 39 40 Example1.X child = new Example1.X(); 41 child.setXString("x string"); 42 parent.getChildren().add(child); 43 44 beginTxn(); 45 em.persist(parent); 46 commitTxn(); 47 Entity parentEntity = 48 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 49 Entity childEntity = ds.get(child.getId()); 50 assertEquals(3, parentEntity.getProperties().size()); 51 assertEquals("a string", parentEntity.getProperty("aString")); 52 assertEquals("b string", parentEntity.getProperty("bString")); 53 assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children")); 54 assertEquals(1, childEntity.getProperties().size()); 55 assertEquals("x string", childEntity.getProperty("xString")); 56 57 // lookup 58 beginTxn(); 59 parent = em.find(parent.getClass(), parent.getId()); 60 assertEquals("a string", parent.getAString()); 61 assertEquals("b string", parent.getBString()); 62 assertEquals(1, parent.getChildren().size()); 63 assertEquals(child.getId(), parent.getChildren().get(0).getId()); 64 commitTxn(); 65 66 beginTxn(); 67 child = em.find(child.getClass(), child.getId()); 68 assertEquals("x string", child.getXString()); 69 commitTxn(); 70 71 // cascade delete 72 beginTxn(); 73 em.remove(em.merge(parent)); 74 commitTxn(); 75 76 assertEquals(0, countForClass(parent.getClass())); 77 assertEquals(0, countForClass(child.getClass())); 78 } 79 80 public void testExample1Superclass() throws EntityNotFoundException { 81 // insertion 82 Example1.A parent = new Example1.A(); 83 parent.setAString("a string"); 84 85 Example1.X child = new Example1.X(); 86 child.setXString("x string"); 87 parent.getChildren().add(child); 88 89 beginTxn(); 90 em.persist(parent); 91 commitTxn(); 92 Entity parentEntity = 93 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 94 Entity childEntity = ds.get(child.getId()); 95 assertEquals(2, parentEntity.getProperties().size()); 96 assertEquals("a string", parentEntity.getProperty("aString")); 97 assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children")); 98 assertEquals(1, childEntity.getProperties().size()); 99 assertEquals("x string", childEntity.getProperty("xString")); 100 101 // lookup 102 beginTxn(); 103 parent = em.find(parent.getClass(), parent.getId()); 104 assertEquals("a string", parent.getAString()); 105 assertEquals(1, parent.getChildren().size()); 106 assertEquals(child.getId(), parent.getChildren().get(0).getId()); 107 commitTxn(); 108 109 beginTxn(); 110 child = em.find(child.getClass(), child.getId()); 111 assertEquals("x string", child.getXString()); 112 commitTxn(); 113 114 // cascade delete 115 beginTxn(); 116 em.remove(em.merge(parent)); 117 commitTxn(); 118 119 assertEquals(0, countForClass(parent.getClass())); 120 assertEquals(0, countForClass(child.getClass())); 121 } 122 123 public void testExample2Subclass() throws EntityNotFoundException { 124 // insertion 125 Example2.B parent = new Example2.B(); 126 parent.setAString("a string"); 127 parent.setBString("b string"); 128 129 Example2.Y child = new Example2.Y(); 130 child.setXString("x string"); 131 child.setYString("y string"); 132 parent.getChildren().add(child); 133 134 beginTxn(); 135 em.persist(parent); 136 commitTxn(); 137 Entity parentEntity = 138 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 139 Entity childEntity = ds.get(child.getId()); 140 assertEquals(3, parentEntity.getProperties().size()); 141 assertEquals("a string", parentEntity.getProperty("aString")); 142 assertEquals("b string", parentEntity.getProperty("bString")); 143 assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children")); 144 assertEquals(2, childEntity.getProperties().size()); 145 assertEquals("x string", childEntity.getProperty("xString")); 146 assertEquals("y string", childEntity.getProperty("yString")); 147 148 // lookup 149 beginTxn(); 150 parent = em.find(parent.getClass(), parent.getId()); 151 assertEquals("a string", parent.getAString()); 152 assertEquals("b string", parent.getBString()); 153 assertEquals(1, parent.getChildren().size()); 154 assertEquals(child.getId(), parent.getChildren().get(0).getId()); 155 commitTxn(); 156 157 beginTxn(); 158 child = em.find(child.getClass(), child.getId()); 159 assertEquals("x string", child.getXString()); 160 assertEquals("y string", child.getYString()); 161 commitTxn(); 162 163 // cascade delete 164 beginTxn(); 165 em.remove(em.merge(parent)); 166 commitTxn(); 167 168 assertEquals(0, countForClass(parent.getClass())); 169 assertEquals(0, countForClass(child.getClass())); 170 } 171 172 public void testExample2Superclass() throws EntityNotFoundException { 173 // insertion 174 Example2.A parent = new Example2.A(); 175 parent.setAString("a string"); 176 177 beginTxn(); 178 em.persist(parent); 179 commitTxn(); 180 Entity parentEntity = 181 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 182 assertEquals(1, parentEntity.getProperties().size()); 183 assertEquals("a string", parentEntity.getProperty("aString")); 184 185 // lookup 186 beginTxn(); 187 parent = em.find(parent.getClass(), parent.getId()); 188 assertEquals("a string", parent.getAString()); 189 commitTxn(); 190 191 // delete 192 beginTxn(); 193 em.remove(em.merge(parent)); 194 commitTxn(); 195 196 assertEquals(0, countForClass(parent.getClass())); 197 } 198 199 public void testExample3Subclass() throws EntityNotFoundException { 200 // insertion 201 Example3.B parent = new Example3.B(); 202 parent.setAString("a string"); 203 parent.setBString("b string"); 204 205 Example3.Y child = new Example3.Y(); 206 child.setXString("x string"); 207 child.setYString("y string"); 208 parent.getChildren().add(child); 209 210 beginTxn(); 211 em.persist(parent); 212 commitTxn(); 213 Entity parentEntity = 214 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 215 Entity childEntity = ds.get(child.getId()); 216 assertEquals(3, parentEntity.getProperties().size()); 217 assertEquals("a string", parentEntity.getProperty("aString")); 218 assertEquals("b string", parentEntity.getProperty("bString")); 219 assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children")); 220 assertEquals(2, childEntity.getProperties().size()); 221 assertEquals("x string", childEntity.getProperty("xString")); 222 assertEquals("y string", childEntity.getProperty("yString")); 223 224 // lookup 225 beginTxn(); 226 parent = em.find(parent.getClass(), parent.getId()); 227 assertEquals("a string", parent.getAString()); 228 assertEquals("b string", parent.getBString()); 229 assertEquals(1, parent.getChildren().size()); 230 assertEquals(child.getId(), parent.getChildren().get(0).getId()); 231 commitTxn(); 232 233 beginTxn(); 234 child = em.find(child.getClass(), child.getId()); 235 assertEquals("x string", child.getXString()); 236 assertEquals("y string", child.getYString()); 237 commitTxn(); 238 239 // cascade delete 240 beginTxn(); 241 em.remove(em.merge(parent)); 242 commitTxn(); 243 244 assertEquals(0, countForClass(parent.getClass())); 245 assertEquals(0, countForClass(child.getClass())); 246 } 247 248 public void testExample3Superclass() throws EntityNotFoundException { 249 // insertion 250 Example3.A parent = new Example3.A(); 251 parent.setAString("a string"); 252 253 beginTxn(); 254 em.persist(parent); 255 commitTxn(); 256 Entity parentEntity = 257 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 258 assertEquals(1, parentEntity.getProperties().size()); 259 assertEquals("a string", parentEntity.getProperty("aString")); 260 261 // lookup 262 beginTxn(); 263 parent = em.find(parent.getClass(), parent.getId()); 264 assertEquals("a string", parent.getAString()); 265 commitTxn(); 266 267 // delete 268 beginTxn(); 269 em.remove(em.merge(parent)); 270 commitTxn(); 271 272 assertEquals(0, countForClass(parent.getClass())); 273 } 274 275 public void testExample4Subclass() throws EntityNotFoundException { 276 // insertion 277 Example4.B parent = new Example4.B(); 278 parent.setAString("a string"); 279 parent.setBString("b string"); 280 281 Example4.Y child = new Example4.Y(); 282 child.setXString("x string"); 283 child.setYString("y string"); 284 parent.getChildren().add(child); 285 286 beginTxn(); 287 em.persist(parent); 288 commitTxn(); 289 Entity parentEntity = 290 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 291 Entity childEntity = ds.get(child.getId()); 292 assertEquals(3, parentEntity.getProperties().size()); 293 assertEquals("a string", parentEntity.getProperty("aString")); 294 assertEquals("b string", parentEntity.getProperty("bString")); 295 assertEquals(Collections.singletonList(childEntity.getKey()), parentEntity.getProperty("children")); 296 assertEquals(2, childEntity.getProperties().size()); 297 assertEquals("x string", childEntity.getProperty("xString")); 298 assertEquals("y string", childEntity.getProperty("yString")); 299 300 // lookup 301 beginTxn(); 302 parent = em.find(parent.getClass(), parent.getId()); 303 assertEquals("a string", parent.getAString()); 304 assertEquals("b string", parent.getBString()); 305 assertEquals(1, parent.getChildren().size()); 306 assertEquals(child.getId(), parent.getChildren().get(0).getId()); 307 commitTxn(); 308 309 beginTxn(); 310 child = em.find(child.getClass(), child.getId()); 311 assertEquals("x string", child.getXString()); 312 assertEquals("y string", child.getYString()); 313 commitTxn(); 314 315 // cascade delete 316 beginTxn(); 317 em.remove(em.merge(parent)); 318 commitTxn(); 319 320 assertEquals(0, countForClass(parent.getClass())); 321 assertEquals(0, countForClass(child.getClass())); 322 } 323 324 public void testExample4Superclass() throws EntityNotFoundException { 325 // insertion 326 Example4.A parent = new Example4.A(); 327 parent.setAString("a string"); 328 329 beginTxn(); 330 em.persist(parent); 331 commitTxn(); 332 Entity parentEntity = 333 ds.get(KeyFactory.createKey(kindForClass(parent.getClass()), parent.getId())); 334 assertEquals(2, parentEntity.getProperties().size()); 335 assertEquals("a string", parentEntity.getProperty("aString")); 336 assertTrue(parentEntity.hasProperty("children")); 337 assertNull(parentEntity.getProperty("children")); 338 339 // lookup 340 beginTxn(); 341 parent = em.find(parent.getClass(), parent.getId()); 342 assertEquals("a string", parent.getAString()); 343 commitTxn(); 344 345 // delete 346 beginTxn(); 347 em.remove(em.merge(parent)); 348 commitTxn(); 349 350 assertEquals(0, countForClass(parent.getClass())); 351 } 352 353 public void testWrongChildType() { 354 Example1.A parent = new Example1.A(); 355 parent.setAString("a string"); 356 Example1.Y child = new Example1.Y(); 357 child.setXString("x string"); 358 parent.getChildren().add(child); 359 360 beginTxn(); 361 em.persist(parent); 362 try { 363 commitTxn(); 364 fail("expected exception"); 365 } catch (UnsupportedOperationException uoe) { 366 // good 367 } 368 } 369 370 public void testWrongChildType_Update() { 371 Example1.A parent = new Example1.A(); 372 parent.setAString("a string"); 373 beginTxn(); 374 em.persist(parent); 375 commitTxn(); 376 beginTxn(); 377 parent = em.find(parent.getClass(), parent.getId()); 378 Example1.Y child = new Example1.Y(); 379 child.setXString("x string"); 380 parent.getChildren().add(child); 381 382 try { 383 commitTxn(); 384 fail("expected exception"); 385 } catch (UnsupportedOperationException uoe) { 386 // good 387 } 388 } 389}