1 18 package org.objectweb.speedo.runtime.basic; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.lang.reflect.Method ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Locale ; 30 31 import javax.jdo.JDOException; 32 import javax.jdo.JDOUserException; 33 import javax.jdo.PersistenceManager; 34 import javax.jdo.JDOHelper; 35 import javax.jdo.listener.AttachLifecycleListener; 36 import javax.jdo.listener.ClearLifecycleListener; 37 import javax.jdo.listener.CreateLifecycleListener; 38 import javax.jdo.listener.DeleteLifecycleListener; 39 import javax.jdo.listener.DetachLifecycleListener; 40 import javax.jdo.listener.DirtyLifecycleListener; 41 import javax.jdo.listener.InstanceLifecycleEvent; 42 import javax.jdo.listener.InstanceLifecycleListener; 43 import javax.jdo.listener.LoadLifecycleListener; 44 import javax.jdo.listener.StoreLifecycleListener; 45 import javax.jdo.spi.JDOImplHelper; 46 47 import junit.framework.Assert; 48 49 import org.objectweb.speedo.SpeedoTestHelper; 50 import org.objectweb.speedo.api.ExceptionHelper; 51 import org.objectweb.speedo.pobjects.basic.BasicA; 52 import org.objectweb.speedo.pobjects.basic.BasicType; 53 import org.objectweb.speedo.pobjects.basic.Product; 54 import org.objectweb.util.monolog.api.BasicLevel; 55 56 60 public class TestBasicA extends SpeedoTestHelper { 61 62 public TestBasicA(String s) { 63 super(s); 64 } 65 66 protected String getLoggerName() { 67 return LOG_NAME + ".rt.basic.TestBasicA"; 68 } 69 70 public void testgetPMF() { 71 assertTrue("Not the same PMF", 72 pmf == JDOHelper.getPersistenceManagerFactory(getPMFProperties())); 73 } 74 public void testCreateInTxAndRemoveInTx2PM() { 75 logger.log(BasicLevel.DEBUG, "testCreateInTxAndRemoveInTx2PM"); 76 BasicA ba = new BasicA(); 77 ba.writeF1("testCreateInTxAndRemoveInTx2PM"); 78 ba.writeF2(2); 79 Locale l = new Locale ("en"); ba.setLocale(l); 81 PersistenceManager pm = pmf.getPersistenceManager(); 82 pm.currentTransaction().begin(); 83 pm.makePersistent(ba); 84 Object id = pm.getObjectId(ba); 85 Assert.assertNotNull("Null object identifier", id); 86 assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoStore()); 87 assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoLoad()); 88 assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoClear()); 89 assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoDelete()); 90 assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage()); 91 assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry()); 92 assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant()); 93 pm.currentTransaction().commit(); 94 assertEquals(2, ba.getCheckJdoStore()); 95 pm.close(); 96 97 ba = null; 98 99 pm = pmf.getPersistenceManager(); 100 pmf.getDataStoreCache().evictAll(); 101 pm.currentTransaction().begin(); 102 ba = (BasicA) pm.getObjectById(id, true); 103 Assert.assertNotNull("Object not found", ba); 104 Assert.assertEquals("Bad f1 value", "testCreateInTxAndRemoveInTx2PM", ba.readF1()); 105 Assert.assertEquals("Bad f1 value", 2, ba.readF2()); 106 assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage()); 107 assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry()); 108 assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant()); 109 assertEquals(BasicA.CALLBACK_INIT, ba.getCheckJdoDelete()); 111 pm.deletePersistent(ba); 112 assertEquals(BasicA.CALLBACK_INIT + 1, ba.getCheckJdoDelete()); 113 pm.currentTransaction().commit(); 114 115 try { 116 Object o = pm.getObjectById(id, true); 117 if (o != null) 118 fail("Removed object always availlable: " + o); 119 } catch (JDOUserException e) { 120 } catch (Exception e) { 122 e.printStackTrace(); 123 fail("Bad exception thrown when an object does not exist: " + e); 124 } 125 126 pm.close(); 127 } 128 129 public void testCreateAndRemoveInTx2PM() { 130 logger.log(BasicLevel.DEBUG, "testCreateAndRemoveInTx2PM"); 131 BasicA ba = new BasicA(); 132 ba.writeF1("testCreateAndRemoveInTx2PM"); 133 ba.writeF2(2); 134 Locale l = new Locale ("de", "de"); ba.setLocale(l); 136 PersistenceManager pm = pmf.getPersistenceManager(); 137 pm.makePersistent(ba); 138 Object id = pm.getObjectId(ba); 139 Assert.assertNotNull("Null object identifier", id); 140 pm.close(); 141 142 ba = null; 143 144 pm = pmf.getPersistenceManager(); 145 pm.currentTransaction().begin(); 146 ba = (BasicA) pm.getObjectById(id, true); 147 Assert.assertNotNull("Object not found", ba); 148 Assert.assertEquals("Bad f1 value", "testCreateAndRemoveInTx2PM", ba.readF1()); 149 Assert.assertEquals("Bad f1 value", 2, ba.readF2()); 150 assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage()); 151 assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry()); 152 assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant()); 153 154 pm.deletePersistent(ba); 155 pm.currentTransaction().commit(); 156 pm.close(); 157 } 158 159 160 public void testCreateAndRemoveInTx1PM() { 161 logger.log(BasicLevel.DEBUG, "testCreateAndRemoveInTx1PM"); 162 BasicA ba = new BasicA(); 163 ba.writeF1("testCreateAndRemoveInTx1PM"); 164 ba.writeF2(2); 165 PersistenceManager pm = pmf.getPersistenceManager(); 166 pm.makePersistent(ba); 167 pm.currentTransaction().begin(); 168 pm.deletePersistent(ba); 169 pm.currentTransaction().commit(); 170 pm.close(); 171 } 172 173 public void testCreateAndRemoveInSameTx1PM() { 174 logger.log(BasicLevel.DEBUG, "testCreateAndRemoveInSameTx1PM"); 175 BasicA ba = new BasicA(); 176 ba.writeF1("testCreateAndRemoveInSameTx1PM"); 177 ba.writeF2(2); 178 PersistenceManager pm = pmf.getPersistenceManager(); 179 pm.currentTransaction().begin(); 180 pm.makePersistent(ba); 181 ba.writeF2(3); 182 pm.deletePersistent(ba); 183 try { 184 pm.currentTransaction().commit(); 185 } catch (JDOException e) { 186 logger.log(BasicLevel.ERROR, "testCreateAndRemoveInSameTx1PM", ExceptionHelper.getNested(e)); 187 fail("Jorm does not support export following by an unexport"); 188 } 189 pm.close(); 190 } 191 192 public void testCreateInTxAndRemoveInTx1PM() { 193 logger.log(BasicLevel.DEBUG, "testCreateInTxAndRemove1PM"); 194 BasicA ba = new BasicA(); 195 ba.writeF1("testCreateInTxAndRemove1PM"); 196 ba.writeF2(2); 197 PersistenceManager pm = pmf.getPersistenceManager(); 198 pm.currentTransaction().begin(); 199 pm.makePersistent(ba); 200 pm.currentTransaction().commit(); 201 pm.currentTransaction().begin(); 202 pm.deletePersistent(ba); 203 pm.currentTransaction().commit(); 204 try { 205 pm.close(); 206 } catch (JDOException e) { 207 logger.log(BasicLevel.ERROR, "testCreateInTxAndRemove1PM", ExceptionHelper.getNested(e)); 208 fail("Speedo does not support the use of a PM after a transaction commit"); 209 } 210 } 211 212 public void testTransientAfterTx() { 213 logger.log(BasicLevel.DEBUG, "testTransientAfterTx"); 214 BasicA ba = new BasicA(); 215 ba.writeF1("testTransient"); 216 ba.writeF2(2); 217 Locale l = new Locale ("GB"); ba.setLocale(l); 219 PersistenceManager pm = pmf.getPersistenceManager(); 220 pm.currentTransaction().begin(); 221 pm.makePersistent(ba); 222 pm.currentTransaction().commit(); 223 Object oid = pm.getObjectId(ba); 224 try { 225 pm.makeTransient(ba); 226 pm.close(); 227 Assert.assertEquals("Bad f1 value", "testTransient", ba.readF1()); 228 assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage()); 229 assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry()); 230 assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant()); 231 ba = null; 232 } catch (JDOException e) { 233 logger.log(BasicLevel.ERROR, "testTransient", ExceptionHelper.getNested(e)); 234 fail("Speedo does not support the makeTransient operation after a Tx"); 235 } finally { 236 pm = pmf.getPersistenceManager(); 237 ba = (BasicA) pm.getObjectById(oid, false); 238 pm.currentTransaction().begin(); 239 pm.deletePersistent(ba); 240 pm.currentTransaction().commit(); 241 pm.close(); 242 } 243 } 244 245 public void testTransientNoTx() { 246 logger.log(BasicLevel.DEBUG, "testTransientNoTx"); 247 BasicA ba = new BasicA(); 248 ba.writeF1("testTransient"); 249 ba.writeF2(2); 250 PersistenceManager pm = pmf.getPersistenceManager(); 251 pm.currentTransaction().begin(); 252 pm.makePersistent(ba); 253 pm.currentTransaction().commit(); 254 Object oid = pm.getObjectId(ba); 255 pm.close(); 256 ba = null; 257 pm = pmf.getPersistenceManager(); 258 try { 259 ba = (BasicA) pm.getObjectById(oid, false); 260 pm.makeTransient(ba); 261 pm.close(); 262 Assert.assertEquals("Bad f1 value", "testTransient", ba.readF1()); 263 ba = null; 264 } catch (JDOException e) { 265 logger.log(BasicLevel.ERROR, "testTransientNoTx", ExceptionHelper.getNested(e)); 266 fail("Speedo does not support the makeTransient operation (notx)"); 267 } finally { 268 pm = pmf.getPersistenceManager(); 269 ba = (BasicA) pm.getObjectById(oid, false); 270 pm.currentTransaction().begin(); 271 pm.deletePersistent(ba); 272 pm.currentTransaction().commit(); 273 pm.close(); 274 } 275 } 276 277 public void testImbricatedPM() { 278 logger.log(BasicLevel.DEBUG, "testImbricatedPM"); 279 BasicA ba = new BasicA(); 280 ba.writeF1("testImbricatedPM"); 281 ba.writeF2(2); 282 PersistenceManager pm1 = pmf.getPersistenceManager(); 283 PersistenceManager pm2 = pmf.getPersistenceManager(); 284 Assert.assertTrue("Same persistence manager", pm1!=pm2); 285 pm2.makePersistent(ba); 286 pm2.close(); 287 pm1.getUserObject(); 289 ba.readF1(); 290 ba.readF2(); 291 pm1.currentTransaction().begin(); 292 pm1.deletePersistent(ba); 293 pm1.currentTransaction().commit(); 294 try { 295 pm1.close(); 296 } catch (JDOException e) { 297 logger.log(BasicLevel.ERROR, "testImbricatedPM", ExceptionHelper.getNested(e)); 298 fail("Speedo does not support the use of a PM after a transaction commit"); 299 } 300 } 301 302 public void testRollBackTxSamePM() { 303 logger.log(BasicLevel.DEBUG, "testRollBackTxSamePM"); 304 BasicA ba = new BasicA(); 305 ba.writeF1("testRollBack1"); 306 ba.writeF2(2); 307 PersistenceManager pm = pmf.getPersistenceManager(); 308 309 pm.currentTransaction().begin(); 310 pm.makePersistent(ba); 311 pm.currentTransaction().commit(); 312 313 pm.currentTransaction().begin(); 314 ba.writeF2(3); 315 Assert.assertEquals("Bad f2 value before roolback", 3, ba.readF2()); 316 pm.currentTransaction().rollback(); 317 Assert.assertEquals("Bad f2 value after roolback", 2, ba.readF2()); 318 319 pm.currentTransaction().begin(); 320 pm.deletePersistent(ba); 321 pm.currentTransaction().commit(); 322 pm.close(); 323 } 324 325 public void testRollBack2() { 326 logger.log(BasicLevel.DEBUG, "testRollBack2"); 327 BasicA ba = new BasicA(); 328 ba.writeF1("testRollBack1"); 329 ba.writeF2(2); 330 PersistenceManager pm = pmf.getPersistenceManager(); 331 332 pm.currentTransaction().begin(); 333 pm.makePersistent(ba); 334 pm.currentTransaction().commit(); 335 336 pm.currentTransaction().begin(); 337 ba.writeF2(3); 338 Assert.assertEquals("Bad f2 value before roolback", 3, ba.readF2()); 339 pm.currentTransaction().rollback(); 340 Assert.assertEquals("Bad f2 value after roolback", 2, ba.readF2()); 341 ba.writeF2(4); 342 Assert.assertEquals("Bad f2 value after roolback", 4, ba.readF2()); 343 pm.close(); 344 345 pm = pmf.getPersistenceManager(); 346 Assert.assertEquals("Bad f2 value after roolback", 4, ba.readF2()); 347 348 pm.currentTransaction().begin(); 349 pm.deletePersistent(ba); 350 pm.currentTransaction().commit(); 351 pm.close(); 352 } 353 354 public void testBasicType(long longval) { 355 logger.log(BasicLevel.DEBUG, "testBasicType" + longval); 356 BasicType bt = new BasicType(); 357 bt.assign(longval); 358 PersistenceManager pm = pmf.getPersistenceManager(); 359 360 pm.currentTransaction().begin(); 361 pm.makePersistent(bt); 362 Object oid = pm.getObjectId(bt); 363 pm.currentTransaction().commit(); 364 365 bt = null; 366 pmf.getDataStoreCache().evictAll(); 367 368 pm.currentTransaction().begin(); 369 bt = (BasicType) pm.getObjectById(oid, false); 370 Assert.assertNotNull("bt is null", bt); 371 bt.check(longval); 372 pm.deletePersistent(bt); 373 pm.currentTransaction().commit(); 374 pm.close(); 375 } 376 public void testBasicType35() { 377 testBasicType(35); 378 } 379 public void _testBasicType0() { 380 testBasicType(0); 381 } 382 383 public void testSerialization() { 384 logger.log(BasicLevel.DEBUG, "testSerialization"); 385 BasicA ba = new BasicA(); 386 ba.writeF1("testSerialization"); 387 ba.writeF2(5); 388 Locale l = new Locale ("en"); ba.setLocale(l); 390 PersistenceManager pm = pmf.getPersistenceManager(); 391 392 pm.currentTransaction().begin(); 393 pm.makePersistent(ba); 394 Object oid = pm.getObjectId(ba); 395 pm.currentTransaction().commit(); 396 pm.close(); 397 File f = new File (new File (new File ("output"),"test"), "testSerialization"); 398 if (f.exists()) { 399 f.delete(); 400 } 401 try { 402 ObjectOutputStream oos = new ObjectOutputStream ( 403 new FileOutputStream (f)); 404 oos.writeObject(ba); 405 oos.close(); 406 407 ba = null; 408 ObjectInputStream ois = new ObjectInputStream ( 409 new FileInputStream (f)); 410 ba = (BasicA) ois.readObject(); 411 ois.close(); 412 ba.readF1_F2(); 413 assertEquals("Bad locale language value", l.getLanguage(), ba.getLocale().getLanguage()); 414 assertEquals("Bad locale country value", l.getCountry(), ba.getLocale().getCountry()); 415 assertEquals("Bad locale variant value", l.getVariant(), ba.getLocale().getVariant()); 416 417 } catch (Exception e) { 418 logger.log(BasicLevel.ERROR, "testSerialization fails: ", e); 419 fail(e.getMessage()); 420 } finally { 421 pm = pmf.getPersistenceManager(); 422 pm.currentTransaction().begin(); 423 pm.deletePersistent(pm.getObjectById(oid, false)); 424 pm.currentTransaction().commit(); 425 pm.close(); 426 } 427 } 428 429 public void testStaticFinder() { 430 logger.log(BasicLevel.DEBUG, "testStaticFinder"); 431 ArrayList al = new ArrayList (); 432 for(int i =0; i<50; i++) { 433 BasicA ba = new BasicA(); 434 ba.writeF1("testStaticFinder" + i); 435 ba.writeF2(i); 436 al.add(ba); 437 } 438 PersistenceManager pm = pmf.getPersistenceManager(); 439 pm.makePersistentAll(al); 440 pm.close(); 441 List l = BasicA.getAllBasicA(pmf); 442 pm = pmf.getPersistenceManager(); 443 try { 444 assertSameCollection("bad query result", al, l); 445 } finally { 446 pm.currentTransaction().begin(); 447 pm.deletePersistentAll(al); 448 pm.currentTransaction().commit(); 449 pm.close(); 450 } 451 } 452 453 public void testNonNullField() { 454 logger.log(BasicLevel.DEBUG, "testNonNullField"); 455 BasicA ba = new BasicA(); 456 ba.writeF1("testNonNullField"); 457 PersistenceManager pm = pmf.getPersistenceManager(); 458 pm.currentTransaction().begin(); 459 pm.makePersistent(ba); 460 pm.currentTransaction().commit(); 461 pm.currentTransaction().begin(); 462 ba.setNonullField(null); 463 try { 464 pm.currentTransaction().commit(); 465 fail("no exception thrown when a null value try to be store for a persistent field marked as non null"); 466 } catch (JDOUserException e) { 467 } catch (Exception e) { 468 fail("Bad exception thrown when a null value try to be store for a persistent field marked as non null: " + e.getClass()); 469 } finally { 470 pm.currentTransaction().begin(); 471 pm.deletePersistent(ba); 472 pm.currentTransaction().commit(); 473 pm.close(); 474 } 475 } 476 public void testRefresh() { 477 logger.log(BasicLevel.DEBUG, "testRefresh"); 478 BasicA ba = new BasicA(); 479 ba.writeF1("testRefresh"); 480 ba.writeF2(2); 481 PersistenceManager pm = pmf.getPersistenceManager(); 482 pm.currentTransaction().begin(); 483 pm.makePersistent(ba); 484 pm.currentTransaction().commit(); 485 486 pm.currentTransaction().begin(); 487 pm.refresh(ba); 488 int f2 = ba.readF2(); 489 assertTrue("bad F2 value:" + f2, f2 < 4); 490 pm.currentTransaction().commit(); 491 492 pm.currentTransaction().begin(); 493 pm.deletePersistent(ba); 494 pm.currentTransaction().commit(); 495 pm.close(); 496 } 497 498 public void testRefreshDirty() { 499 logger.log(BasicLevel.DEBUG, "testRefreshDirty"); 500 BasicA ba = new BasicA(); 501 ba.writeF1("testRefreshDirty"); 502 ba.writeF2(2); 503 PersistenceManager pm = pmf.getPersistenceManager(); 504 pm.currentTransaction().begin(); 505 pm.makePersistent(ba); 506 pm.currentTransaction().commit(); 507 508 pm.currentTransaction().begin(); 509 ba.writeF2(4); 510 pm.refresh(ba); 511 int f2 = ba.readF2(); 512 assertTrue("bad F2 value:" + f2, f2 < 4); 513 pm.currentTransaction().commit(); 514 515 pm.currentTransaction().begin(); 516 pm.deletePersistent(ba); 517 pm.currentTransaction().commit(); 518 pm.close(); 519 } 520 521 522 public void testPONEICompatible() { 523 logger.log(BasicLevel.DEBUG, "testPONEICompatible"); 524 final String [] FIELD_NAMES = new String []{ 525 "locale", 526 "d", 527 "f2", 528 "privateField", 529 "protectedField", 530 "f1", 531 "myArrayOfchar", 532 "noModifierField", 533 "undeclaredField", 534 "l", 535 "d2", 536 "nonullField" 537 }; 538 final Class [] FIELD_TYPES = new Class []{ 539 java.util.Locale .class, 540 java.util.Date .class, 541 Integer.TYPE, 542 Integer.TYPE, 543 Integer.TYPE, 544 java.lang.String .class, 545 char[].class, 546 Integer.TYPE, 547 java.lang.String .class, 548 Long.TYPE, 549 java.util.Date .class, 550 java.lang.String .class 551 }; 552 assertTrue("Class not registered: ", 553 JDOImplHelper.getInstance().getRegisteredClasses().contains(BasicA.class)); 554 final String [] fns = JDOImplHelper.getInstance().getFieldNames(BasicA.class); 555 assertNotNull("Null array of field names", fns); 556 assertEquals("Bad length of the array of field names", FIELD_NAMES.length, fns.length); 557 final Class [] fts = JDOImplHelper.getInstance().getFieldTypes(BasicA.class); 558 assertNotNull("Null array of field types", fts); 559 assertEquals("Bad length of the array of field types", FIELD_TYPES.length, fts.length); 560 final HashMap expected = new HashMap (); 561 final HashMap found = new HashMap (); 562 for(int i=0; i<FIELD_NAMES.length; i++) { 563 expected.put(FIELD_NAMES[i], FIELD_TYPES[i]); 564 found.put(fns[i], fts[i]); 565 } 566 assertEquals("Bad content", expected, found); 567 final Class superClass = JDOImplHelper.getInstance().getPersistenceCapableSuperclass(BasicA.class); 568 assertNull("Bad super class name", superClass); 569 570 BasicA ba = new BasicA(); 571 ba.writeF1("testPONEICompatible"); 572 ba.writeF2(23242); 573 PersistenceManager pm = pmf.getPersistenceManager(); 574 pm.currentTransaction().begin(); 575 pm.makePersistent(ba); 576 pm.currentTransaction().commit(); 577 578 pm.currentTransaction().begin(); 580 assertEquals("Bad value on getter", new Integer (23242), 581 invokeStaticMethod(BasicA.class, "jdoGetf2", 582 new Class []{BasicA.class}, 583 new Object []{ba})); 584 assertNull("Bad value on getter", 585 invokeStaticMethod(BasicA.class, "jdoSetf2", 586 new Class []{BasicA.class, Integer.TYPE}, 587 new Object []{ba, new Integer (1)})); 588 assertEquals("Bad value on getter", new Integer (1), 589 invokeStaticMethod(BasicA.class, "jdoGetf2", 590 new Class []{BasicA.class}, 591 new Object []{ba})); 592 pm.currentTransaction().commit(); 593 594 assertEquals("Bad value on getter", new Integer (1), 596 invokeStaticMethod(BasicA.class, "jdoGetf2", 597 new Class []{BasicA.class}, 598 new Object []{ba})); 599 assertNull("Bad value on getter", 600 invokeStaticMethod(BasicA.class, "jdoSetf2", 601 new Class []{BasicA.class, Integer.TYPE}, 602 new Object []{ba, new Integer (2)})); 603 assertEquals("Bad value on getter", new Integer (2), 604 invokeStaticMethod(BasicA.class, "jdoGetf2", 605 new Class []{BasicA.class}, 606 new Object []{ba})); 607 608 pm.close(); 609 pm = pmf.getPersistenceManager(); 610 pm.currentTransaction().begin(); 611 pm.deletePersistent(ba); 612 pm.currentTransaction().commit(); 613 pm.close(); 614 615 } 616 617 public void testDeleteMakePersistent() { 619 long pIdentifier = 1; 620 PersistenceManager pm = pmf.getPersistenceManager(); 621 try { 622 Product p = new Product(pIdentifier); 623 p.setCost("cost1"); 624 p.setProductName("product1"); 625 pm.currentTransaction().begin(); 627 pm.makePersistent(p); 628 Object pId = pm.getObjectId(p); 629 Assert.assertNotNull("null object identifier", pId); 630 pm.currentTransaction().commit(); 631 632 p = null; 633 p = (Product) pm.getObjectById(pId, true); 634 Assert.assertNotNull("null instance returned by getObjectById", p); 635 Assert.assertEquals("Bad product id", pIdentifier, p.getId()); 636 pm.currentTransaction().begin(); 639 pm.deletePersistent(p); 640 Product newP = new Product(pIdentifier); 641 newP.setCost("cost2"); 642 newP.setProductName("product2"); 643 pm.makePersistent(newP); 644 pm.currentTransaction().commit(); 645 } catch (Exception e) { 646 e.printStackTrace(); 647 fail(e.getMessage()); 648 } finally { 649 if (pm.currentTransaction().isActive()) 650 pm.currentTransaction().rollback(); 651 pm.close(); 652 } 653 pm = pmf.getPersistenceManager(); 654 pm.currentTransaction().begin(); 655 pm.deletePersistent(pm.getObjectById( 656 pm.newObjectIdInstance(Product.class, "" + pIdentifier), false)); 657 pm.currentTransaction().commit(); 658 pm.close(); 659 } 660 661 662 public void testDeleteRollback() { 664 logger.log(BasicLevel.DEBUG, "testDeleteRollBack"); 665 BasicA ba = new BasicA(); 666 ba.writeF1("testDeleteRollBack"); 667 ba.writeF2(2); 668 PersistenceManager pm = pmf.getPersistenceManager(); 669 670 pm.currentTransaction().begin(); 671 pm.makePersistent(ba); 672 pm.currentTransaction().commit(); 673 674 pm.currentTransaction().begin(); 675 pm.deletePersistent(ba); 676 pm.currentTransaction().rollback(); 677 678 pm.currentTransaction().begin(); 679 assertTrue("The object should be persistent", JDOHelper.isPersistent(ba)); 680 assertEquals("F2 should be 2", 2, ba.readF2()); 681 pm.deletePersistent(ba); 682 pm.currentTransaction().commit(); 683 684 pm.close(); 685 } 686 public void testDeleteAndUse() { 687 logger.log(BasicLevel.DEBUG, "testRefreshDirty"); 688 BasicA ba = new BasicA(); 689 ba.writeF1("testRefreshDirty"); 690 ba.writeF2(2); 691 PersistenceManager pm = pmf.getPersistenceManager(); 692 pm.currentTransaction().begin(); 693 pm.makePersistent(ba); 694 pm.currentTransaction().commit(); 695 696 pm.currentTransaction().begin(); 697 pm.deletePersistent(ba); 698 assertEquals("bad F2 value after delete:", 2, ba.readF2()); 699 ba.writeF2(3); 700 assertEquals("bad F2 value after delete and set:", 3, ba.readF2()); 701 pm.currentTransaction().commit(); 702 assertEquals("bad F2 value after commit:", 3, ba.readF2()); 703 ba.writeF2(4); 704 assertEquals("bad F2 value after commit and set:", 4, ba.readF2()); 705 pm.close(); 706 assertEquals("bad F2 value after close:", 4, ba.readF2()); 707 ba.writeF2(5); 708 assertEquals("bad F2 value after close and set:", 5, ba.readF2()); 709 } 710 711 public void testInstanceLifecycleListener() { 712 final int PRE = 0; 713 final int POST = 1; 714 final InstanceLifecycleEvent[][] marks = new InstanceLifecycleEvent[8][2]; 715 final int[] nbEvent = new int[1]; 716 nbEvent[0] = 0; 717 InstanceLifecycleListener[] listeners = new InstanceLifecycleListener[8]; 718 listeners[InstanceLifecycleEvent.ATTACH] = new AttachLifecycleListener() { 719 public void preAttach(InstanceLifecycleEvent ev) { 720 marks[InstanceLifecycleEvent.ATTACH][PRE] = ev; 721 nbEvent[0] ++; 722 } 723 public void postAttach(InstanceLifecycleEvent ev) { 724 marks[InstanceLifecycleEvent.ATTACH][POST] = ev; 725 nbEvent[0] ++; 726 } 727 }; 728 listeners[InstanceLifecycleEvent.DETACH] = new DetachLifecycleListener() { 729 public void preDetach(InstanceLifecycleEvent ev) { 730 marks[InstanceLifecycleEvent.DETACH][PRE] = ev; 731 nbEvent[0] ++; 732 } 733 public void postDetach(InstanceLifecycleEvent ev) { 734 marks[InstanceLifecycleEvent.DETACH][POST] = ev; 735 nbEvent[0] ++; 736 } 737 }; 738 listeners[InstanceLifecycleEvent.CREATE] = new CreateLifecycleListener() { 739 public void postCreate(InstanceLifecycleEvent ev) { 740 marks[InstanceLifecycleEvent.CREATE][POST] = ev; 741 nbEvent[0] ++; 742 } 743 }; 744 listeners[InstanceLifecycleEvent.DELETE] = new DeleteLifecycleListener() { 745 public void preDelete(InstanceLifecycleEvent ev) { 746 marks[InstanceLifecycleEvent.DELETE][PRE] = ev; 747 nbEvent[0] ++; 748 } 749 public void postDelete(InstanceLifecycleEvent ev) { 750 marks[InstanceLifecycleEvent.DELETE][POST] = ev; 751 nbEvent[0] ++; 752 } 753 }; 754 listeners[InstanceLifecycleEvent.LOAD] = new LoadLifecycleListener() { 755 public void preLoad(InstanceLifecycleEvent ev) { 756 marks[InstanceLifecycleEvent.LOAD][PRE] = ev; 757 nbEvent[0] ++; 758 } 759 public void postLoad(InstanceLifecycleEvent ev) { 760 marks[InstanceLifecycleEvent.LOAD][POST] = ev; 761 nbEvent[0] ++; 762 } 763 }; 764 listeners[InstanceLifecycleEvent.STORE] = new StoreLifecycleListener() { 765 public void preStore(InstanceLifecycleEvent ev) { 766 marks[InstanceLifecycleEvent.STORE][PRE] = ev; 767 nbEvent[0] ++; 768 } 769 public void postStore(InstanceLifecycleEvent ev) { 770 marks[InstanceLifecycleEvent.STORE][POST] = ev; 771 nbEvent[0] ++; 772 } 773 }; 774 listeners[InstanceLifecycleEvent.DIRTY] = new DirtyLifecycleListener() { 775 public void preDirty(InstanceLifecycleEvent ev) { 776 marks[InstanceLifecycleEvent.DIRTY][PRE] = ev; 777 nbEvent[0] ++; 778 } 779 public void postDirty(InstanceLifecycleEvent ev) { 780 marks[InstanceLifecycleEvent.DIRTY][POST] = ev; 781 nbEvent[0] ++; 782 } 783 }; 784 listeners[InstanceLifecycleEvent.CLEAR] = new ClearLifecycleListener() { 785 public void preClear(InstanceLifecycleEvent ev) { 786 marks[InstanceLifecycleEvent.CLEAR][PRE] = ev; 787 nbEvent[0] ++; 788 } 789 public void postClear(InstanceLifecycleEvent ev) { 790 marks[InstanceLifecycleEvent.CLEAR][POST] = ev; 791 nbEvent[0] ++; 792 } 793 }; 794 PersistenceManager pm = pmf.getPersistenceManager(); 795 for (int i = 0; i < listeners.length; i++) { 796 pmf.addInstanceLifecycleListener(listeners[i], new Class []{BasicA.class}); 797 } 798 BasicA ba = new BasicA(); 799 assertEquals("unexpect event on create ", 0, nbEvent[0]); 800 ba.writeF1("testInstanceLifecycleListener_1"); 801 assertEquals("unexpect event on write ", 0, nbEvent[0]); 802 pm.currentTransaction().begin(); 803 pm.makePersistent(ba); 804 ba.writeF1("testInstanceLifecycleListener_1"); 805 assertEquals("Bad event number on make persistent: ", 1, nbEvent[0]); 806 assertNotNull("Not postCreate event", marks[InstanceLifecycleEvent.CREATE][POST]); 807 assertTrue("Bad source ", marks[InstanceLifecycleEvent.CREATE][POST].getSource() == ba); 808 809 clear(nbEvent, marks); 810 pm.currentTransaction().commit(); 811 pm.currentTransaction().begin(); 812 assertTrue("Bad event number on commit: " + nbEvent[0], 2 == nbEvent[0] | 3 ==nbEvent[0]); 813 assertNotNull("Not preStore event", marks[InstanceLifecycleEvent.STORE][PRE]); 814 assertTrue("Bad source on preStore", marks[InstanceLifecycleEvent.STORE][PRE].getSource() == ba); 815 assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.STORE][POST]); 816 assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.STORE][POST].getSource() == ba); 817 pm.currentTransaction().commit(); 818 819 pm.evict(ba); 820 821 pm.currentTransaction().begin(); 822 clear(nbEvent, marks); 823 assertEquals("Bad f1 value" , "testInstanceLifecycleListener_1", ba.readF1()); 824 assertTrue("Bad event number on load: ", 2 == nbEvent[0] || 1 == nbEvent[0]); 825 assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.LOAD][POST]); 826 assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.LOAD][POST].getSource() == ba); 827 836 pm.currentTransaction().rollback(); 837 838 pm.currentTransaction().begin(); 839 clear(nbEvent, marks); 840 pm.deletePersistent(ba); 841 assertTrue("Bad event number on delete persistent: " + nbEvent[0], 2 == nbEvent[0] || 3 == nbEvent[0]); 842 assertNotNull("Not preStore event", marks[InstanceLifecycleEvent.DELETE][PRE]); 843 assertTrue("Bad source on preStore", marks[InstanceLifecycleEvent.DELETE][PRE].getSource() == ba); 844 assertNotNull("Not postStore event", marks[InstanceLifecycleEvent.DELETE][POST]); 845 assertTrue("Bad source on postStore", marks[InstanceLifecycleEvent.DELETE][POST].getSource() == ba); 846 847 pm.currentTransaction().commit(); 848 pm.close(); 849 for (int i = 0; i < listeners.length; i++) { 850 pmf.removeInstanceLifecycleListener(listeners[i]); 851 } 852 } 853 854 private void clear(int[] nbEvent, Object [][] marks) { 855 nbEvent[0] = 0; 856 for(int i=0; i<8; i++) { 857 marks[i][0] = null; 858 marks[i][1] = null; 859 } 860 } 861 862 private Object invokeStaticMethod(Class clazz, 863 String methodName, 864 Class [] paramTypes, 865 Object [] paramValues) { 866 try { 867 Method m = clazz.getMethod(methodName, paramTypes); 868 return m.invoke(null, paramValues); 869 } catch (Exception e) { 870 logger.log(BasicLevel.ERROR, "Exception during invocation:", e); 871 fail(e.getMessage()); 872 return null; 873 } 874 } 875 876 } 877 | Popular Tags |