/tests/com/google/appengine/datanucleus/jdo/JDOBatchInsertTest.java
Java | 415 lines | 364 code | 29 blank | 22 comment | 1 complexity | 66f830f42dc613368cf3bb7724847523 MD5 | raw file
1/********************************************************************** 2Copyright (c) 2009 Google Inc. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15**********************************************************************/ 16package com.google.appengine.datanucleus.jdo; 17 18import com.google.appengine.api.datastore.DatastoreServiceConfig; 19import com.google.appengine.api.datastore.Entity; 20import com.google.appengine.api.datastore.EntityNotFoundException; 21import com.google.appengine.api.datastore.Key; 22import com.google.appengine.api.datastore.KeyFactory; 23import com.google.appengine.datanucleus.Utils; 24import com.google.appengine.datanucleus.test.jdo.BidirectionalChildListJDO; 25import com.google.appengine.datanucleus.test.jdo.Flight; 26import com.google.appengine.datanucleus.test.jdo.HasKeyAncestorKeyPkJDO; 27import com.google.appengine.datanucleus.test.jdo.HasOneToManyListJDO; 28import com.google.appengine.datanucleus.test.jdo.HasOneToOneJDO; 29 30import java.lang.reflect.Method; 31import java.util.List; 32 33import javax.jdo.JDOHelper; 34import javax.jdo.JDOUserException; 35import javax.jdo.ObjectState; 36 37/** 38 * @author Max Ross <maxr@google.com> 39 */ 40public class JDOBatchInsertTest extends JDOBatchTestCase { 41 42 private static Flight newFlight() { 43 Flight f1 = new Flight(); 44 f1.setOrigin("BOS"); 45 f1.setDest("MIA"); 46 f1.setMe(2); 47 f1.setYou(4); 48 f1.setName("Harold"); 49 50 return f1; 51 } 52 53 BatchRecorder newBatchRecorder() { 54 DatastoreServiceConfig config = getStoreManager().getDefaultDatastoreServiceConfigForReads(); 55 return new BatchRecorder(config) { 56 boolean isBatchMethod(Method method) { 57 return method.getName().equals("put") && List.class.isAssignableFrom(method.getReturnType()); 58 } 59 }; 60 } 61 62 public void testMakePersistentAll_NoTxn() { 63 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.nontransactional); 64 Flight f1 = newFlight(); 65 Flight f2 = newFlight(); 66 pm.makePersistentAll(f1, f2); 67 assertEquals(2, countForClass(Flight.class)); 68 assertEquals(1, batchRecorder.batchOps); 69 70 Flight f3 = newFlight(); 71 Flight f4 = newFlight(); 72 pm.makePersistentAll(Utils.newArrayList(f3, f4)); 73 assertEquals(4, countForClass(Flight.class)); 74 assertEquals(2, batchRecorder.batchOps); 75 } 76 77 public void testMakePersistentAll_OneEntity_NoTxn() { 78 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.nontransactional); 79 Flight f1 = newFlight(); 80 pm.makePersistentAll(f1); 81 assertEquals(1, countForClass(Flight.class)); 82 assertEquals(0, batchRecorder.batchOps); 83 84 Flight f2 = newFlight(); 85 pm.makePersistentAll(Utils.newArrayList(f2)); 86 assertEquals(2, countForClass(Flight.class)); 87 assertEquals(0, batchRecorder.batchOps); 88 } 89 90 public void testMakePersistentAll_Txn_MultipleEntityGroups() { 91 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 92 Flight f1 = newFlight(); 93 Flight f2 = newFlight(); 94 beginTxn(); 95 try { 96 pm.makePersistentAll(f1, f2); 97 fail("expected iae"); 98 } catch (JDOUserException nue) { 99 // good 100 } 101 rollbackTxn(); 102 assertEquals(0, countForClass(Flight.class)); 103 assertEquals(1, batchRecorder.batchOps); 104 105 Flight f3 = newFlight(); 106 Flight f4 = newFlight(); 107 beginTxn(); 108 try { 109 pm.makePersistentAll(Utils.newArrayList(f3, f4)); 110 fail("expected iae"); 111 } catch (JDOUserException nue) { 112 // good 113 } 114 assertEquals(0, countForClass(Flight.class)); 115 assertEquals(2, batchRecorder.batchOps); 116 } 117 118 public void testMakePersistentAll_Txn_OneEntityGroup() { 119 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 120 Key parentKey = KeyFactory.createKey("yar", 24); 121 HasKeyAncestorKeyPkJDO pojo1 = new HasKeyAncestorKeyPkJDO(); 122 pojo1.setAncestorKey(parentKey); 123 HasKeyAncestorKeyPkJDO pojo2 = new HasKeyAncestorKeyPkJDO(); 124 pojo2.setAncestorKey(parentKey); 125 beginTxn(); 126 pm.makePersistentAll(pojo1, pojo2); 127 commitTxn(); 128 assertEquals(2, countForClass(HasKeyAncestorKeyPkJDO.class)); 129 assertEquals(1, batchRecorder.batchOps); 130 } 131 132 public void testMakePersistentAll_OneEntity_Txn() { 133 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 134 Flight f1 = newFlight(); 135 beginTxn(); 136 pm.makePersistentAll(f1); 137 commitTxn(); 138 assertEquals(1, countForClass(Flight.class)); 139 assertEquals(0, batchRecorder.batchOps); 140 141 Flight f2 = newFlight(); 142 beginTxn(); 143 pm.makePersistentAll(Utils.newArrayList(f2)); 144 commitTxn(); 145 assertEquals(2, countForClass(Flight.class)); 146 assertEquals(0, batchRecorder.batchOps); 147 } 148 149 public void testMakePersistentAll_CascadeInsert_OneToOne_NoTxn() throws EntityNotFoundException { 150 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.nontransactional); 151 Flight f1 = newFlight(); 152 Flight f2 = newFlight(); 153 HasOneToOneJDO parent1 = new HasOneToOneJDO(); 154 parent1.setFlight(f1); 155 HasOneToOneJDO parent2 = new HasOneToOneJDO(); 156 parent2.setFlight(f2); 157 pm.makePersistentAll(parent1, parent2); 158 assertEquals(2, countForClass(HasOneToOneJDO.class)); 159 assertEquals(2, countForClass(Flight.class)); 160 assertEquals(1, batchRecorder.batchOps); 161 162 Entity parent1Entity = ds.get(KeyFactory.stringToKey(parent1.getId())); 163 Entity flight1Entity = ds.get(KeyFactory.stringToKey(f1.getId())); 164 assertEquals(6, parent1Entity.getProperties().size()); 165 assertTrue(parent1Entity.hasProperty("hasKeyPK_key_OID")); 166 assertNull(parent1Entity.getProperty("hasKeyPK_key_OID")); 167 assertTrue(parent1Entity.hasProperty("hasParent_key_OID")); 168 assertNull(parent1Entity.getProperty("hasParent_key_OID")); 169 assertTrue(parent1Entity.hasProperty("hasParentKeyPK_key_OID")); 170 assertNull(parent1Entity.getProperty("hasParentKeyPK_key_OID")); 171 assertTrue(parent1Entity.hasProperty("notDependent_id_OID")); 172 assertNull(parent1Entity.getProperty("notDependent_id_OID")); 173 assertEquals(flight1Entity.getKey(), parent1Entity.getProperty("flight_id_OID")); 174 Entity parent2Entity = ds.get(KeyFactory.stringToKey(parent2.getId())); 175 Entity flight2Entity = ds.get(KeyFactory.stringToKey(f2.getId())); 176 assertEquals(6, parent2Entity.getProperties().size()); 177 assertTrue(parent2Entity.hasProperty("hasKeyPK_key_OID")); 178 assertNull(parent2Entity.getProperty("hasKeyPK_key_OID")); 179 assertTrue(parent2Entity.hasProperty("hasParent_key_OID")); 180 assertNull(parent2Entity.getProperty("hasParent_key_OID")); 181 assertTrue(parent2Entity.hasProperty("hasParentKeyPK_key_OID")); 182 assertNull(parent2Entity.getProperty("hasParentKeyPK_key_OID")); 183 assertTrue(parent2Entity.hasProperty("notDependent_id_OID")); 184 assertNull(parent2Entity.getProperty("notDependent_id_OID")); 185 assertEquals(flight2Entity.getKey(), parent2Entity.getProperty("flight_id_OID")); 186 } 187 188 public void testMakePersistentAll_CascadeInsert_OneToOne_MultipleEntityGroups_Txn() { 189 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 190 Flight f1 = newFlight(); 191 Flight f2 = newFlight(); 192 HasOneToOneJDO parent1 = new HasOneToOneJDO(); 193 parent1.setFlight(f1); 194 HasOneToOneJDO parent2 = new HasOneToOneJDO(); 195 parent2.setFlight(f2); 196 beginTxn(); 197 try { 198 pm.makePersistentAll(parent1, parent2); 199 fail("expected exception"); 200 } catch (JDOUserException nue) { 201 // good 202 } 203 } 204 205 public void testMakePersistentAll_CascadeInsert_OneToOne_OneEntityGroup_Txn() 206 throws EntityNotFoundException { 207 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 208 Flight f1 = newFlight(); 209 Flight f2 = newFlight(); 210 HasOneToOneJDO parent1 = new HasOneToOneJDO(); 211 parent1.setId(new KeyFactory.Builder("Yar", 43).addChild(HasOneToOneJDO.class.getSimpleName(), "k1").getString()); 212 parent1.setFlight(f1); 213 HasOneToOneJDO parent2 = new HasOneToOneJDO(); 214 parent2.setId(new KeyFactory.Builder("Yar", 43).addChild(HasOneToOneJDO.class.getSimpleName(), "k2").getString()); 215 parent2.setFlight(f2); 216 beginTxn(); 217 pm.makePersistentAll(parent1, parent2); 218 commitTxn(); 219 assertEquals(2, countForClass(HasOneToOneJDO.class)); 220 assertEquals(2, countForClass(Flight.class)); 221 assertEquals(1, batchRecorder.batchOps); 222 223 Entity parent1Entity = ds.get(KeyFactory.stringToKey(parent1.getId())); 224 Entity flight1Entity = ds.get(KeyFactory.stringToKey(f1.getId())); 225 assertEquals(6, parent1Entity.getProperties().size()); 226 assertTrue(parent1Entity.hasProperty("hasKeyPK_key_OID")); 227 assertNull(parent1Entity.getProperty("hasKeyPK_key_OID")); 228 assertTrue(parent1Entity.hasProperty("hasParent_key_OID")); 229 assertNull(parent1Entity.getProperty("hasParent_key_OID")); 230 assertTrue(parent1Entity.hasProperty("hasParentKeyPK_key_OID")); 231 assertNull(parent1Entity.getProperty("hasParentKeyPK_key_OID")); 232 assertTrue(parent1Entity.hasProperty("notDependent_id_OID")); 233 assertNull(parent1Entity.getProperty("notDependent_id_OID")); 234 assertEquals(flight1Entity.getKey(), parent1Entity.getProperty("flight_id_OID")); 235 Entity parent2Entity = ds.get(KeyFactory.stringToKey(parent2.getId())); 236 Entity flight2Entity = ds.get(KeyFactory.stringToKey(f2.getId())); 237 assertEquals(6, parent2Entity.getProperties().size()); 238 assertTrue(parent2Entity.hasProperty("hasKeyPK_key_OID")); 239 assertNull(parent2Entity.getProperty("hasKeyPK_key_OID")); 240 assertTrue(parent2Entity.hasProperty("hasParent_key_OID")); 241 assertNull(parent2Entity.getProperty("hasParent_key_OID")); 242 assertTrue(parent2Entity.hasProperty("hasParentKeyPK_key_OID")); 243 assertNull(parent2Entity.getProperty("hasParentKeyPK_key_OID")); 244 assertTrue(parent2Entity.hasProperty("notDependent_id_OID")); 245 assertNull(parent2Entity.getProperty("notDependent_id_OID")); 246 assertEquals(flight2Entity.getKey(), parent2Entity.getProperty("flight_id_OID")); 247 } 248 249 public void testMakePersistentAll_CascadeInsert_OneToMany_NoTxn() throws EntityNotFoundException { 250 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.nontransactional); 251 Flight f1 = newFlight(); 252 Flight f2 = newFlight(); 253 Flight f3 = newFlight(); 254 Flight f4 = newFlight(); 255 BidirectionalChildListJDO bidirChild1 = new BidirectionalChildListJDO(); 256 BidirectionalChildListJDO bidirChild2 = new BidirectionalChildListJDO(); 257 BidirectionalChildListJDO bidirChild3 = new BidirectionalChildListJDO(); 258 BidirectionalChildListJDO bidirChild4 = new BidirectionalChildListJDO(); 259 HasOneToManyListJDO parent1 = new HasOneToManyListJDO(); 260 parent1.getFlights().add(f1); 261 parent1.getFlights().add(f2); 262 parent1.getBidirChildren().add(bidirChild1); 263 parent1.getBidirChildren().add(bidirChild2); 264 HasOneToManyListJDO parent2 = new HasOneToManyListJDO(); 265 parent2.getFlights().add(f3); 266 parent2.getFlights().add(f4); 267 parent2.getBidirChildren().add(bidirChild3); 268 parent2.getBidirChildren().add(bidirChild4); 269 pm.makePersistentAll(parent1, parent2); 270 assertEquals(2, countForClass(HasOneToManyListJDO.class)); 271 assertEquals(4, countForClass(Flight.class)); 272 assertEquals(4, countForClass(BidirectionalChildListJDO.class)); 273 assertEquals(1, batchRecorder.batchOps); 274 275 Entity parent1Entity = ds.get(KeyFactory.stringToKey(parent1.getId())); 276 Entity parent2Entity = ds.get(KeyFactory.stringToKey(parent2.getId())); 277 Entity flight1Entity = ds.get(KeyFactory.stringToKey(f1.getId())); 278 Entity flight2Entity = ds.get(KeyFactory.stringToKey(f2.getId())); 279 Entity flight3Entity = ds.get(KeyFactory.stringToKey(f3.getId())); 280 Entity flight4Entity = ds.get(KeyFactory.stringToKey(f4.getId())); 281 Entity bidir1Entity = ds.get(KeyFactory.stringToKey(bidirChild1.getId())); 282 Entity bidir2Entity = ds.get(KeyFactory.stringToKey(bidirChild2.getId())); 283 Entity bidir3Entity = ds.get(KeyFactory.stringToKey(bidirChild3.getId())); 284 Entity bidir4Entity = ds.get(KeyFactory.stringToKey(bidirChild4.getId())); 285 assertEquals(4, parent1Entity.getProperties().size()); 286 assertTrue(parent1Entity.hasProperty("hasKeyPks")); 287 assertNull(parent1Entity.getProperty("hasKeyPks")); 288 assertEquals( 289 Utils.newArrayList(flight1Entity.getKey(), flight2Entity.getKey()), 290 parent1Entity.getProperty("flights")); 291 assertEquals( 292 Utils.newArrayList(bidir1Entity.getKey(), bidir2Entity.getKey()), 293 parent1Entity.getProperty("bidirChildren")); 294 295 assertEquals(4, parent2Entity.getProperties().size()); 296 assertTrue(parent2Entity.hasProperty("hasKeyPks")); 297 assertNull(parent2Entity.getProperty("hasKeyPks")); 298 assertEquals( 299 Utils.newArrayList(flight3Entity.getKey(), flight4Entity.getKey()), 300 parent2Entity.getProperty("flights")); 301 assertEquals( 302 Utils.newArrayList(bidir3Entity.getKey(), bidir4Entity.getKey()), 303 parent2Entity.getProperty("bidirChildren")); 304 } 305 306 public void testMakePersistentAll_CascadeInsert_OneToMany_MultipleEntityGroups_Txn() { 307 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 308 Flight f1 = newFlight(); 309 Flight f2 = newFlight(); 310 Flight f3 = newFlight(); 311 Flight f4 = newFlight(); 312 BidirectionalChildListJDO bidirChild1 = new BidirectionalChildListJDO(); 313 BidirectionalChildListJDO bidirChild2 = new BidirectionalChildListJDO(); 314 BidirectionalChildListJDO bidirChild3 = new BidirectionalChildListJDO(); 315 BidirectionalChildListJDO bidirChild4 = new BidirectionalChildListJDO(); 316 HasOneToManyListJDO parent1 = new HasOneToManyListJDO(); 317 parent1.getFlights().add(f1); 318 parent1.getFlights().add(f2); 319 parent1.getBidirChildren().add(bidirChild1); 320 parent1.getBidirChildren().add(bidirChild2); 321 HasOneToManyListJDO parent2 = new HasOneToManyListJDO(); 322 parent2.getFlights().add(f3); 323 parent2.getFlights().add(f4); 324 parent2.getBidirChildren().add(bidirChild3); 325 parent2.getBidirChildren().add(bidirChild4); 326 beginTxn(); 327 try { 328 pm.makePersistentAll(parent1, parent2); 329 fail("expected exception"); 330 } catch (JDOUserException nue) { 331 // good 332 } 333 } 334 335 public void testMakePersistentAll_CascadeInsert_OneToMany_OneEntityGroup_Txn() 336 throws EntityNotFoundException { 337 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.transactional); 338 Flight f1 = newFlight(); 339 Flight f2 = newFlight(); 340 Flight f3 = newFlight(); 341 Flight f4 = newFlight(); 342 BidirectionalChildListJDO bidirChild1 = new BidirectionalChildListJDO(); 343 BidirectionalChildListJDO bidirChild2 = new BidirectionalChildListJDO(); 344 BidirectionalChildListJDO bidirChild3 = new BidirectionalChildListJDO(); 345 BidirectionalChildListJDO bidirChild4 = new BidirectionalChildListJDO(); 346 HasOneToManyListJDO parent1 = new HasOneToManyListJDO(); 347 parent1.setId(new KeyFactory.Builder("Yar", 43).addChild(HasOneToManyListJDO.class.getSimpleName(), "k1").getString()); 348 parent1.getFlights().add(f1); 349 parent1.getFlights().add(f2); 350 parent1.getBidirChildren().add(bidirChild1); 351 parent1.getBidirChildren().add(bidirChild2); 352 HasOneToManyListJDO parent2 = new HasOneToManyListJDO(); 353 parent2.setId(new KeyFactory.Builder("Yar", 43).addChild(HasOneToManyListJDO.class.getSimpleName(), "k2").getString()); 354 parent2.getFlights().add(f3); 355 parent2.getFlights().add(f4); 356 parent2.getBidirChildren().add(bidirChild3); 357 parent2.getBidirChildren().add(bidirChild4); 358 beginTxn(); 359 pm.makePersistentAll(parent1, parent2); 360 commitTxn(); 361 assertEquals(2, countForClass(HasOneToManyListJDO.class)); 362 assertEquals(4, countForClass(Flight.class)); 363 assertEquals(4, countForClass(BidirectionalChildListJDO.class)); 364 assertEquals(1, batchRecorder.batchOps); 365 366 Entity parent1Entity = ds.get(KeyFactory.stringToKey(parent1.getId())); 367 Entity parent2Entity = ds.get(KeyFactory.stringToKey(parent2.getId())); 368 Entity flight1Entity = ds.get(KeyFactory.stringToKey(f1.getId())); 369 Entity flight2Entity = ds.get(KeyFactory.stringToKey(f2.getId())); 370 Entity flight3Entity = ds.get(KeyFactory.stringToKey(f3.getId())); 371 Entity flight4Entity = ds.get(KeyFactory.stringToKey(f4.getId())); 372 Entity bidir1Entity = ds.get(KeyFactory.stringToKey(bidirChild1.getId())); 373 Entity bidir2Entity = ds.get(KeyFactory.stringToKey(bidirChild2.getId())); 374 Entity bidir3Entity = ds.get(KeyFactory.stringToKey(bidirChild3.getId())); 375 Entity bidir4Entity = ds.get(KeyFactory.stringToKey(bidirChild4.getId())); 376 assertEquals(4, parent1Entity.getProperties().size()); 377 assertTrue(parent1Entity.hasProperty("hasKeyPks")); 378 assertNull(parent1Entity.getProperty("hasKeyPks")); 379 assertEquals( 380 Utils.newArrayList(flight1Entity.getKey(), flight2Entity.getKey()), 381 parent1Entity.getProperty("flights")); 382 assertEquals( 383 Utils.newArrayList(bidir1Entity.getKey(), bidir2Entity.getKey()), 384 parent1Entity.getProperty("bidirChildren")); 385 386 assertEquals(4, parent2Entity.getProperties().size()); 387 assertTrue(parent2Entity.hasProperty("hasKeyPks")); 388 assertNull(parent2Entity.getProperty("hasKeyPks")); 389 assertEquals( 390 Utils.newArrayList(flight3Entity.getKey(), flight4Entity.getKey()), 391 parent2Entity.getProperty("flights")); 392 assertEquals( 393 Utils.newArrayList(bidir3Entity.getKey(), bidir4Entity.getKey()), 394 parent2Entity.getProperty("bidirChildren")); 395 } 396 397 public void testCombineInsertAndUpdate_NoTxn() { 398 switchDatasource(JDOTestCase.PersistenceManagerFactoryName.nontransactional); 399 Flight f1 = newFlight(); 400 pm.makePersistent(f1); 401 f1 = pm.detachCopy(f1); 402 assertEquals(1, countForClass(Flight.class)); 403 assertEquals(0, batchRecorder.batchOps); 404 pm.close(); 405 assertEquals(ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(f1)); 406 f1.setName("jimmy"); 407 pm = pmf.getPersistenceManager(); 408 Flight f2 = new Flight(); 409 Flight f3 = new Flight(); 410 pm.makePersistentAll(f1, f2, f3); 411 pm.close(); 412 assertEquals(3, countForClass(Flight.class)); 413 assertEquals(1, batchRecorder.batchOps); 414 } 415}