1 10 11 package com.triactive.jdo.test; 12 13 import com.triactive.jdo.PersistenceManagerFactoryImpl; 14 15 import javax.jdo.PersistenceManager; 16 import javax.jdo.Extent; 17 import javax.jdo.Query; 18 import javax.jdo.JDODataStoreException; 19 import javax.jdo.JDOFatalDataStoreException; 20 import javax.jdo.JDOUserException; 21 import javax.jdo.Transaction; 22 import javax.jdo.JDOHelper; 23 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Collection ; 28 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 32 import org.apache.log4j.Category; 33 34 35 40 public class PersistenceManagerImplFullTest extends PersistenceTestCase 41 { 42 private static final Category LOG = Category.getInstance(PersistenceManagerImplFullTest.class); 43 44 private static String EMAIL[] = { "jon.doe@msn.com", "jane.smith@msn.com", 46 "tom.jones@aol.com" }; 47 private static String FIRSTNAME[] = { "Jon", "Jane", "Tom" }; 48 private static String LASTNAME[] = { "Doe", "Smith", "Jones" }; 49 50 private static float EMP_SALARY[] = { 75000.00F, 40000.00F, 35000.00F, 25000.00F }; 52 private static String EMP_SERIAL[] = { "683687A", "439293A", "384018D", "102938X" }; 53 54 private static String MGR_DEPT[] = { "Marketing", "Sales", "Purchasing", "Accounting" }; 56 57 private boolean schemaInitialized = false; 58 59 64 public PersistenceManagerImplFullTest(String name) 65 { 66 super(name); 67 } 68 69 70 73 public void testMakePersistent() throws Exception 74 { 75 PersistenceManager pm = pmf.getPersistenceManager(); 76 Primitive p = new Primitive(); 77 78 Transaction tx = pm.currentTransaction(); 79 80 try 81 { 82 85 java.util.Date date1 = (new java.util.GregorianCalendar ()).getTime(); 86 java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01"); 87 java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000"); 88 89 tx.begin(); 90 91 setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 92 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", 93 date1, date2, timestamp); 94 95 98 99 pm.makePersistent(p); 100 101 tx.commit(); 102 103 p = null; 104 105 tx.begin(); 106 107 Extent clnPrimitive = pm.getExtent(Primitive.class, false); 108 java.util.Iterator it = clnPrimitive.iterator(); p = (Primitive) it.next(); 110 111 assertNotNull(p); 112 113 assertPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 115 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", 116 date1, date2, timestamp); 117 118 tx.commit(); 119 } 120 finally 121 { 122 if (tx.isActive()) 123 tx.rollback(); 124 pm.close(); 125 } 126 127 } 128 129 130 134 public void testMakeCollectionFieldsPersistent() throws Exception 135 { 136 PersistenceManager pm = pmf.getPersistenceManager(); 137 CollectionFieldTester collectionTester = new CollectionFieldTester(); 138 139 Transaction tx = pm.currentTransaction(); 140 141 try 142 { 143 Person[] personArray = new Person[3]; 145 InversePrimitive[] inversePrimitiveArray = new InversePrimitive[3]; 146 147 personArray[0] = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 148 personArray[1] = new Person(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1]); 149 personArray[2] = new Person(2, FIRSTNAME[2], LASTNAME[2], EMAIL[2]); 150 151 inversePrimitiveArray[0] = new InversePrimitive(); 152 inversePrimitiveArray[1] = new InversePrimitive(); 153 inversePrimitiveArray[2] = new InversePrimitive(); 154 155 tx.begin(); 156 pm.makePersistent(collectionTester); 157 tx.commit(); 158 159 tx.begin(); 160 161 { 163 HashSet s = new HashSet (3); 164 s.add(personArray[0]); 165 s.add(personArray[1]); 166 s.add(personArray[2]); 167 collectionTester.setPersonSet(s); 168 } 169 170 { 172 HashSet s = new HashSet (3); 173 s.add(inversePrimitiveArray[0]); 174 s.add(inversePrimitiveArray[1]); 175 s.add(inversePrimitiveArray[2]); 176 177 collectionTester.setInversePrimitiveCollection(s); 178 } 179 180 tx.commit(); 181 182 collectionTester = null; 183 184 tx.begin(); 186 187 { 188 Extent clnTest = pm.getExtent(CollectionFieldTester.class, false); 189 java.util.Iterator it = clnTest.iterator(); collectionTester = (CollectionFieldTester) it.next(); 191 } 192 193 assertNotNull(collectionTester); 194 195 { 197 Set s = collectionTester.getPersonSet(); 198 assertTrue(s.contains(personArray[0])); 199 assertTrue(s.contains(personArray[1])); 200 assertTrue(s.contains(personArray[2])); 201 assertEquals(3, s.size()); 202 203 Collection c = collectionTester.getInversePrimitiveCollection(); 204 205 assertEquals(collectionTester, inversePrimitiveArray[0].getTester()); 206 assertEquals(collectionTester, inversePrimitiveArray[1].getTester()); 207 assertEquals(collectionTester, inversePrimitiveArray[2].getTester()); 208 209 assertTrue(c.contains(inversePrimitiveArray[0])); 210 assertTrue(c.contains(inversePrimitiveArray[1])); 211 assertTrue(c.contains(inversePrimitiveArray[2])); 212 assertEquals(3, c.size()); 213 } 214 215 tx.commit(); 216 217 tx.begin(); 218 219 223 CollectionFieldTester ct1 = new CollectionFieldTester(); 224 inversePrimitiveArray[2].setTester(ct1); 225 226 tx.commit(); 227 228 tx.begin(); 229 230 assertTrue(JDOHelper.isPersistent(ct1)); 231 232 236 Collection c = collectionTester.getInversePrimitiveCollection(); 237 assertTrue(!c.contains(inversePrimitiveArray[2])); 238 239 242 Collection c1 = ct1.getInversePrimitiveCollection(); 243 assertTrue(c1.contains(inversePrimitiveArray[2])); 244 245 248 c1.add(inversePrimitiveArray[1]); 249 assertEquals(1, c.size()); 250 assertEquals(2, c1.size()); 251 252 tx.commit(); 253 254 258 tx.begin(); 259 260 c1 = ct1.getInversePrimitiveCollection(); 261 c1.remove(inversePrimitiveArray[2]); 262 assertTrue(JDOHelper.isDeleted(inversePrimitiveArray[2])); 263 264 tx.commit(); 265 266 270 tx.begin(); 271 272 c1 = ct1.getInversePrimitiveCollection(); 273 c1.clear(); 274 assertTrue(JDOHelper.isDeleted(inversePrimitiveArray[1])); 275 assertTrue(c1.isEmpty()); 276 277 tx.commit(); 278 } 279 finally 280 { 281 if (tx.isActive()) 282 tx.rollback(); 283 pm.close(); 284 } 285 } 286 287 288 289 290 293 public void testUpdatePersistentFields() throws Exception 294 { 295 PersistenceManager pm = pmf.getPersistenceManager(); 296 Primitive p = new Primitive(); 297 298 Transaction tx = pm.currentTransaction(); 299 300 try 301 { 302 305 java.util.Date date1 = (new java.util.GregorianCalendar ()).getTime(); 306 java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01"); 307 java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000"); 308 tx.begin(); 309 setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 310 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", 311 date1, date2, timestamp); 312 313 316 pm.makePersistent(p); 317 tx.commit(); 318 319 p = null; 320 321 tx.begin(); 323 324 Extent clnPrimitive = pm.getExtent(Primitive.class, false); 325 java.util.Iterator it = clnPrimitive.iterator(); p = (Primitive) it.next(); 327 328 date1 = new java.util.Date (date1.getTime() + 10000); 330 date2 = java.sql.Date.valueOf("2001-01-02"); 331 timestamp = java.sql.Timestamp.valueOf("2001-01-02 23:23:23.050500000"); 332 333 setPrimitiveValues(p, false, (byte) 22, 'a', 34, (short) 44, 334 1234567890, 456.456F, 456.456, "fixedlength", "normalsize", 335 "hugexyzabc", date1, date2, timestamp); 336 337 tx.commit(); 338 339 tx.begin(); 341 342 clnPrimitive = pm.getExtent(Primitive.class, false); 343 it = clnPrimitive.iterator(); p = (Primitive) it.next(); 345 346 assertPrimitiveValues(p, false, (byte) 22, 'a', 34, (short) 44, 347 1234567890, 456.456F, 456.456, "fixedlength", "normalsize", 348 "hugexyzabc", date1, date2, timestamp); 349 } 350 finally 351 { 352 if (tx.isActive()) 353 tx.rollback(); 354 pm.close(); 355 } 356 } 357 358 359 362 public void testUpdatePersistentFieldsExceptions() throws Exception 363 { 364 PersistenceManager pm = pmf.getPersistenceManager(); 365 Transaction tx = null; 366 367 try 368 { 369 Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 370 371 tx = pm.currentTransaction(); 373 374 tx.begin(); 375 pm.makePersistent(p); 376 pm.deletePersistent(p); 377 378 try 379 { 380 p.setLastName(LASTNAME[1]); 381 fail("should have thrown JDOUserException"); 382 } 383 catch (JDOUserException e) 384 { } 385 386 tx.rollback(); 387 388 tx.begin(); 390 pm.makePersistent(p); 391 tx.commit(); 392 393 tx.begin(); 394 pm.deletePersistent(p); 395 396 try 397 { 398 p.setLastName(LASTNAME[1]); 399 fail("should have thrown JDOUserException"); 400 } 401 catch (JDOUserException e) 402 { } 403 } 404 finally 405 { 406 if (tx.isActive()) 407 tx.rollback(); 408 pm.close(); 409 } 410 } 411 412 413 416 public void testMakeTransient() throws Exception 417 { 418 PersistenceManager pm = pmf.getPersistenceManager(); 419 Transaction tx = null; 420 421 try 422 { 423 tx = pm.currentTransaction(); 424 Person x = createNewPerson(pm, 0); 425 Person y = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 426 427 tx.begin(); 429 pm.makeTransient(y); 430 tx.commit(); 431 432 tx.begin(); 434 pm.makeTransient(x); 435 x.setLastName(LASTNAME[1]); 436 tx.commit(); 437 438 assertNull(JDOHelper.getObjectId(x)); 439 440 Person queryResult = queryPerson(0, pm); 441 442 tx.begin(); 443 assertEquals(LASTNAME[0], queryResult.getLastName()); 444 445 tx.commit(); 446 } 447 finally 448 { 449 if (tx.isActive()) 450 tx.rollback(); 451 pm.close(); 452 } 453 } 454 455 456 459 public void testMakeTransientAll() throws Exception 460 { 461 PersistenceManager pm = pmf.getPersistenceManager(); 462 Transaction tx = null; 463 464 try 465 { 466 tx = pm.currentTransaction(); 467 Person x = createNewPerson(pm, 0); 468 Person y = createNewPerson(pm, 1); 469 Person[] personArray = new Person[2]; 470 471 personArray[0] = x; 472 personArray[1] = y; 473 474 tx.begin(); 476 pm.makeTransientAll(personArray); 477 478 x.setLastName(LASTNAME[1]); 479 y.setLastName(LASTNAME[0]); 480 tx.commit(); 481 482 x = queryPerson(0, pm); 483 y = queryPerson(1, pm); 484 485 tx.begin(); 486 487 assertEquals(LASTNAME[0], x.getLastName()); 489 assertEquals(LASTNAME[1], y.getLastName()); 490 491 tx.commit(); 492 } 493 finally 494 { 495 if (tx.isActive()) 496 tx.rollback(); 497 pm.close(); 498 } 499 } 500 501 502 503 504 507 public void testMakeTransientExceptions() throws Exception 508 { 509 PersistenceManager pm = pmf.getPersistenceManager(); 510 511 try 512 { 513 Person x = createNewPerson(pm, 0); 514 515 pm.makeTransient(x); 517 assertTrue("Object still persistent after makeTransient()", !JDOHelper.isPersistent(x)); 518 assertTrue("Object still transactional after makeTransient()", !JDOHelper.isTransactional(x)); 519 } 520 finally 521 { 522 pm.close(); 523 } 524 } 525 526 527 530 public void testDeletePersistent() throws Exception 531 { 532 PersistenceManager pm = pmf.getPersistenceManager(); 533 Transaction tx = null; 534 535 try 536 { 537 tx = pm.currentTransaction(); 538 Person x = createNewPerson(pm, 0); 539 540 tx.begin(); 542 pm.deletePersistent(x); 543 tx.commit(); 544 545 Person queryResult = queryPerson(0, pm); 546 assertNull(queryResult); 547 548 tx.begin(); 550 x = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 551 pm.makePersistent(x); 552 pm.deletePersistent(x); 553 tx.commit(); 554 } 555 finally 556 { 557 if (tx.isActive()) 558 tx.rollback(); 559 pm.close(); 560 } 561 562 } 563 564 565 568 public void testDeletePersistentExceptions() throws Exception 569 { 570 PersistenceManager pm = pmf.getPersistenceManager(); 571 Transaction tx = null; 572 573 try 574 { 575 tx = pm.currentTransaction(); 576 Person p = new Person(0, FIRSTNAME[0], 577 LASTNAME[0], EMAIL[0]); 578 579 try 581 { 582 tx.begin(); 583 pm.deletePersistent(p); 584 fail("calling deletePersistent on transient object should fail"); 585 } 586 catch (JDOUserException e) 587 { } 588 finally 589 { 590 tx.rollback(); 591 } 592 593 tx.begin(); 595 pm.makePersistent(p); 596 pm.deletePersistent(p); 597 try 598 { 599 pm.deletePersistent(p); 600 } 601 catch (JDOUserException e) 602 { 603 fail("calling deletePersistent on a P-del object should work"); 604 } 605 606 tx.rollback(); 607 608 p = createNewPerson(pm, 0); 610 611 tx.begin(); 612 pm.deletePersistent(p); 613 614 try 615 { 616 pm.deletePersistent(p); 617 } 618 catch (JDOUserException e) 619 { 620 fail("calling deletePersistent on a P-del object should work"); 621 } 622 623 tx.rollback(); 624 } 625 finally 626 { 627 if (tx.isActive()) 628 tx.rollback(); 629 pm.close(); 630 } 631 632 } 633 634 635 public void testStateTransitions() throws Exception 636 { 637 PersistenceManager pm = pmf.getPersistenceManager(); 638 Transaction tx = null; 639 Object id = null; 640 641 try 642 { 643 Person x = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 644 tx = pm.currentTransaction(); 645 646 tx.begin(); 647 648 assertTransient(x); 650 pm.makePersistent(x); 651 assertPersistentNew(x); 652 653 tx.rollback(); 655 assertTransient(x); 656 657 tx.begin(); 658 pm.makePersistent(x); 659 assertPersistentNew(x); 660 661 pm.deletePersistent(x); 663 assertPersistentNewDeleted(x); 664 665 tx.commit(); 667 assertTransient(x); 668 669 tx.begin(); 671 pm.makePersistent(x); 672 assertPersistentNew(x); 673 id = pm.getObjectId(x); 674 tx.commit(); 675 assertHollow(x); 676 677 tx.begin(); 679 x = (Person)pm.getObjectById(id, false); 680 assertHollow(x); 681 x.getFirstName(); 682 assertPersistentClean(x); 683 tx.commit(); 684 assertHollow(x); 685 686 tx.begin(); 688 assertHollow(x); 689 x.getPersonNum(); 690 assertPersistentClean(x); 691 tx.commit(); 692 693 tx.begin(); 695 assertHollow(x); 696 x.setLastName(this.LASTNAME[1]); 697 assertPersistentDirty(x); 698 tx.rollback(); 700 assertHollow(x); 701 702 tx.begin(); 703 assertHollow(x); 705 pm.deletePersistent(x); 706 assertPersistentDeleted(x); 707 tx.rollback(); 709 assertHollow(x); 710 711 tx.begin(); 712 x.getPersonNum(); 713 assertPersistentClean(x); 715 x.setLastName(this.LASTNAME[1]); 716 assertPersistentDirty(x); 717 718 pm.deletePersistent(x); 720 assertPersistentDeleted(x); 721 tx.rollback(); 723 assertHollow(x); 724 725 tx.begin(); 726 x.setLastName(this.LASTNAME[1]); 728 assertPersistentDirty(x); 729 tx.commit(); 730 assertHollow(x); 731 732 tx.begin(); 733 x.setLastName(this.LASTNAME[2]); 734 assertPersistentDirty(x); 735 pm.deletePersistent(x); 737 assertPersistentDeleted(x); 738 tx.commit(); 740 assertTransient(x); 741 742 x = new Person(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1]); 743 tx.begin(); 744 pm.makePersistent(x); 745 pm.deletePersistent(x); 746 assertPersistentNewDeleted(x); 747 tx.commit(); 749 assertTransient(x); 750 751 x = new Person(2, FIRSTNAME[2], LASTNAME[2], EMAIL[2]); 752 tx.begin(); 753 pm.makePersistent(x); 754 pm.deletePersistent(x); 755 assertPersistentNewDeleted(x); 756 tx.rollback(); 758 assertTransient(x); 759 760 x = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 761 tx.begin(); 762 pm.makePersistent(x); 763 tx.commit(); 764 assertHollow(x); 765 tx.begin(); 766 x.getPersonNum(); 767 assertPersistentClean(x); 768 pm.makeNontransactional(x); 770 assertPersistentNontransactional(x); 771 tx.commit(); 772 773 tx.begin(); 774 pm.makeTransactional(x); 776 assertPersistentClean(x); 777 tx.commit(); 778 779 tx.begin(); 780 pm.makeNontransactional(x); 781 assertPersistentNontransactional(x); 782 x.setLastName(this.LASTNAME[1]); 784 assertPersistentDirty(x); 785 tx.rollback(); 786 787 tx.setNontransactionalRead(true); 788 assertHollow(x); 789 x.getPersonNum(); 791 assertPersistentNontransactional(x); 792 } 793 finally 794 { 795 if (tx.isActive()) 796 tx.rollback(); 797 798 pm.close(); 799 } 800 } 801 802 805 public void testInheritedFieldsPersisted() 806 { 807 PersistenceManager pm = pmf.getPersistenceManager(); 808 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 809 EMP_SALARY[0], EMP_SERIAL[0]); 810 811 try 812 { 813 pm.currentTransaction().begin(); 814 pm.makePersistent(mgr); 815 pm.currentTransaction().commit(); 816 817 pm.currentTransaction().begin(); 818 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 819 java.util.Iterator it = ext.iterator(); 820 821 assertTrue(it.hasNext()); 822 823 mgr = (Manager) it.next(); 824 825 assertEquals(FIRSTNAME[0], mgr.getFirstName()); 826 assertEquals(LASTNAME[0], mgr.getLastName()); 827 assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F); 828 pm.currentTransaction().commit(); 829 } 830 finally 831 { 832 if (pm.currentTransaction().isActive()) 833 { 834 pm.currentTransaction().rollback(); 835 pm.close(); 836 fail(); 837 } 838 839 pm.close(); 840 } 841 842 try 843 { 844 pm = pmf.getPersistenceManager(); 846 pm.currentTransaction().begin(); 847 848 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 849 java.util.Iterator it = ext.iterator(); 850 851 assertTrue(it.hasNext()); 852 853 mgr = (Manager) it.next(); 854 855 assertEquals(FIRSTNAME[0], mgr.getFirstName()); 856 assertEquals(LASTNAME[0], mgr.getLastName()); 857 assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F); 858 } 859 finally 860 { 861 if (pm.currentTransaction().isActive()) 862 pm.currentTransaction().rollback(); 863 864 pm.close(); 865 } 866 } 867 868 869 873 public void testFCOReferencedObjectsPersistence() 874 { 875 Manager manager = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 876 EMP_SALARY[0], EMP_SERIAL[0]); 877 878 Employee employee = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], 879 EMP_SALARY[1], EMP_SERIAL[1]); 880 881 PersistenceManager pm = pmf.getPersistenceManager(); 882 883 try 884 { 885 pm.currentTransaction().begin(); 886 pm.makePersistent(employee); 887 employee.setManager(manager); 888 pm.currentTransaction().commit(); 889 } 890 finally 891 { 892 if (pm.currentTransaction().isActive()) 893 { 894 pm.currentTransaction().rollback(); 895 pm.close(); 896 fail(); 897 } 898 899 pm.close(); 900 } 901 902 try 904 { 905 pm = pmf.getPersistenceManager(); 906 pm.currentTransaction().begin(); 907 Extent clnManager = pm.getExtent(Manager.class, false); 908 Iterator it = clnManager.iterator(); assertTrue(it.hasNext()); 910 911 Manager m = (Manager) it.next(); 912 assertEquals(pm.getObjectId(manager), pm.getObjectId(m)); 913 pm.currentTransaction().commit(); 914 } 915 finally 916 { 917 if (pm.currentTransaction().isActive()) 918 pm.currentTransaction().rollback(); 919 pm.close(); 920 } 921 } 922 923 924 927 public void testPCFieldAccess() throws Exception 928 { 929 PersistenceManager pm = pmf.getPersistenceManager(); 930 Primitive p = new Primitive(); 931 932 Transaction tx = pm.currentTransaction(); 933 934 try 935 { 936 p.setTransientField(100); 937 938 tx.begin(); 939 p.setTransientField(200); 940 pm.makePersistent(p); 941 p.setTransientField(300); 942 tx.commit(); 943 944 p.setTransientField(400); 945 } 946 finally 947 { 948 if (tx.isActive()) 949 tx.rollback(); 950 pm.close(); 951 } 952 } 953 954 955 public void testExtentSubclasses() throws Exception 956 { 957 PersistenceManager pm = pmf.getPersistenceManager(); 958 Transaction tx = pm.currentTransaction(); 959 960 Employee empl = new Employee(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 961 EMP_SALARY[0], EMP_SERIAL[0]); 962 Manager mgr = new Manager(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], 963 EMP_SALARY[1], EMP_SERIAL[1]); 964 965 try 966 { 967 tx.begin(); 968 pm.makePersistent(empl); 969 pm.makePersistent(mgr); 970 Extent extent = pm.getExtent(Employee.class, false); 971 972 java.util.Iterator it = extent.iterator(); 974 Employee empl2 = (Employee) it.next(); 975 assertEquals(empl.getPersonNum(), empl2.getPersonNum()); 976 assertEquals(false, it.hasNext()); 977 978 tx.commit(); 979 980 tx.begin(); 982 extent = pm.getExtent(Employee.class, true); 983 it = extent.iterator(); 984 985 empl2 = (Employee) it.next(); 987 988 if (empl2 instanceof Manager) 989 { 990 assertEquals(1, empl2.getPersonNum()); 991 pm.deletePersistent(empl2); 992 993 empl2 = (Employee) it.next(); 994 assertEquals(0, empl2.getPersonNum()); 995 pm.deletePersistent(empl2); 996 } 997 else 998 { 999 assertEquals(0, empl2.getPersonNum()); 1000 pm.deletePersistent(empl2); 1001 1002 empl2 = (Manager) it.next(); 1003 assertEquals(1, empl2.getPersonNum()); 1004 pm.deletePersistent(empl2); 1005 } 1006 1007 tx.commit(); 1008 } 1009 finally 1010 { 1011 if (tx.isActive()) 1012 tx.rollback(); 1013 1014 pm.close(); 1015 } 1016 } 1017 1018 1019 1020 1025 public void testJavaIdentity() 1026 { 1027 Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]); 1028 PersistenceManager pm = pmf.getPersistenceManager(); 1029 pm.currentTransaction().begin(); 1030 pm.makePersistent(p); 1031 Object oid = pm.getObjectId(p); 1032 pm.currentTransaction().commit(); 1033 1034 try 1035 { 1036 pm.currentTransaction().begin(); 1037 Extent ext = pm.getExtent(Person.class, false); 1038 Iterator it = ext.iterator(); assertTrue(it.hasNext()); 1040 1041 p = (Person) it.next(); 1042 assertTrue(!it.hasNext()); 1043 assertEquals(oid, pm.getObjectId(p)); 1044 } 1045 finally 1046 { 1047 if (pm.currentTransaction().isActive()) 1048 pm.currentTransaction().rollback(); 1049 } 1050 } 1051 1052 1053 1057 public void testNormalFCOCollectionFieldPersistence1() 1058 { 1059 PersistenceManager pm = pmf.getPersistenceManager(); 1060 1061 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1062 EMP_SALARY[0], EMP_SERIAL[0]); 1063 1064 Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], 1065 EMP_SALARY[1], EMP_SERIAL[1]); 1066 1067 try 1068 { 1069 pm.currentTransaction().begin(); 1070 mgr.addSubordinate(emp1); 1071 pm.makePersistent(mgr); 1072 pm.currentTransaction().commit(); 1073 } 1074 finally 1075 { 1076 if (pm.currentTransaction().isActive()) 1077 { 1078 pm.currentTransaction().rollback(); 1079 pm.close(); 1080 fail(); 1081 } 1082 1083 pm.close(); 1084 } 1085 1086 try 1088 { 1089 pm = pmf.getPersistenceManager(); 1090 pm.currentTransaction().begin(); 1091 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1092 java.util.Iterator it = ext.iterator(); 1093 1094 assertTrue(it.hasNext()); 1095 mgr = (Manager) it.next(); 1096 Collection c = mgr.getSubordinates(); 1097 assertEquals(1, c.size()); 1098 1099 ext = pm.getExtent(com.triactive.jdo.test.Employee.class, false); 1100 it = ext.iterator(); 1101 assertTrue(it.hasNext()); 1102 Employee emp = (Employee) it.next(); 1103 1104 assertTrue(c.contains(emp)); 1105 } 1106 finally 1107 { 1108 if (pm.currentTransaction().isActive()) 1109 pm.currentTransaction().rollback(); 1110 1111 pm.close(); 1112 } 1113 } 1114 1115 1116 1120 public void testNormalFCOCollectionFieldPersistence2() 1121 { 1122 LOG.info("testNormalFCOCollectionFieldPersistence2 entry"); 1123 1124 1128 if (!((PersistenceManagerFactoryImpl)pmf).getValidateConstraints()) 1129 return; 1130 1131 PersistenceManager pm = pmf.getPersistenceManager(); 1132 Transaction tx = pm.currentTransaction(); 1133 1134 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1135 EMP_SALARY[0], EMP_SERIAL[0]); 1136 1137 Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], 1138 EMP_SALARY[1], EMP_SERIAL[1]); 1139 1140 try 1141 { 1142 tx.begin(); 1143 mgr.addSubordinate(emp1); 1144 pm.makePersistent(mgr); 1145 tx.commit(); 1146 1147 try 1148 { 1149 tx.begin(); 1150 pm.deletePersistent(emp1); 1151 tx.commit(); 1152 fail("Removal of referenced object should have failed due to a constraint violation: " + emp1); 1153 } 1154 catch (JDODataStoreException e) 1155 { 1156 assertTrue("Transaction should still be active", tx.isActive()); 1158 } 1159 catch (JDOFatalDataStoreException e) 1160 { 1161 } 1163 } 1164 finally 1165 { 1166 try 1167 { 1168 if (tx.isActive()) 1169 tx.rollback(); 1170 1171 pm.close(); 1172 } 1173 finally 1174 { 1175 LOG.info("testNormalFCOCollectionFieldPersistence2 exit"); 1176 } 1177 } 1178 } 1179 1180 1181 1185 public void testNormalFCOCollectionFieldPersistence3() 1186 { 1187 PersistenceManager pm = pmf.getPersistenceManager(); 1188 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1189 EMP_SALARY[0], EMP_SERIAL[0]); 1190 1191 Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], 1192 EMP_SALARY[1], EMP_SERIAL[1]); 1193 1194 try 1195 { 1196 mgr.addSubordinate(emp1); 1197 pm.currentTransaction().begin(); 1198 pm.makePersistent(mgr); 1199 pm.currentTransaction().commit(); 1200 1201 pm.currentTransaction().begin(); 1202 mgr.getSubordinates().remove(emp1); 1203 pm.currentTransaction().commit(); 1204 } 1205 finally 1206 { 1207 if (pm.currentTransaction().isActive()) 1208 { 1209 pm.currentTransaction().rollback(); 1210 pm.close(); 1211 fail(); 1212 } 1213 1214 pm.close(); 1215 } 1216 1217 try 1218 { 1219 pm = pmf.getPersistenceManager(); 1220 1221 pm.currentTransaction().begin(); 1222 Extent ex = pm.getExtent(com.triactive.jdo.test.Employee.class, false); 1223 java.util.Iterator it = ex.iterator(); 1224 assertTrue(it.hasNext()); 1225 Employee emp = (Employee) it.next(); 1226 1227 assertEquals(1, emp.getPersonNum()); 1228 pm.currentTransaction().commit(); 1229 } 1230 finally 1231 { 1232 if (pm.currentTransaction().isActive()) 1233 { 1234 pm.currentTransaction().rollback(); 1235 pm.close(); 1236 fail(); 1237 } 1238 1239 pm.close(); 1240 } 1241 } 1242 1243 1244 1248 public void testNormalFCOCollectionFieldPersistence4() 1249 { 1250 PersistenceManager pm = pmf.getPersistenceManager(); 1251 1252 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1253 EMP_SALARY[0], EMP_SERIAL[0]); 1254 1255 Primitive p = new Primitive(); 1256 setPrimitiveValues(p); 1257 1258 CollectionFieldTester tester = new CollectionFieldTester(); 1259 1260 try 1261 { 1262 tester.getObjectCollection().add(mgr); 1263 tester.getInterfaceCollection().add(p); 1264 pm.currentTransaction().begin(); 1265 pm.makePersistent(tester); 1266 pm.currentTransaction().commit(); 1267 } 1268 finally 1269 { 1270 if (pm.currentTransaction().isActive()) 1271 { 1272 pm.currentTransaction().rollback(); 1273 pm.close(); 1274 fail(); 1275 } 1276 1277 pm.close(); 1278 } 1279 1280 try 1281 { 1282 pm = pmf.getPersistenceManager(); 1283 1284 pm.currentTransaction().begin(); 1285 Extent ex = pm.getExtent(CollectionFieldTester.class, true); 1286 java.util.Iterator it = ex.iterator(); 1287 assertTrue(it.hasNext()); 1288 tester = (CollectionFieldTester) it.next(); 1289 1290 assertEquals(1, tester.getObjectCollection().size()); 1291 mgr = (Manager) tester.getObjectCollection().iterator().next(); 1292 assertEquals(0, mgr.getPersonNum()); 1293 1294 assertEquals(1, tester.getInterfaceCollection().size()); 1295 1296 pm.currentTransaction().commit(); 1297 } 1298 finally 1299 { 1300 if (pm.currentTransaction().isActive()) 1301 { 1302 pm.currentTransaction().rollback(); 1303 pm.close(); 1304 fail(); 1305 } 1306 1307 pm.close(); 1308 } 1309 } 1310 1311 1312 1313 1314 1315 1319 public void testInverseFCOCollectionFieldPersistence1() 1320 { 1321 PersistenceManager pm = pmf.getPersistenceManager(); 1322 1323 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1324 EMP_SALARY[0], EMP_SERIAL[0]); 1325 1326 try 1327 { 1328 Department d = new Department("Engineering"); 1329 d.setManager(mgr); 1330 1331 pm.currentTransaction().begin(); 1332 mgr.addDepartment(d); 1333 1334 pm.makePersistent(mgr); 1335 pm.currentTransaction().commit(); 1336 } 1337 finally 1338 { 1339 if (pm.currentTransaction().isActive()) 1340 { 1341 pm.currentTransaction().rollback(); 1342 pm.close(); 1343 fail(); 1344 } 1345 1346 pm.close(); 1347 } 1348 1349 try 1351 { 1352 pm = pmf.getPersistenceManager(); 1353 pm.currentTransaction().begin(); 1354 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1355 java.util.Iterator it = ext.iterator(); 1356 1357 assertTrue(it.hasNext()); 1358 mgr = (Manager) it.next(); 1359 Collection c = mgr.getDepartments(); 1360 assertEquals(1, c.size()); 1361 1362 ext = pm.getExtent(com.triactive.jdo.test.Department.class, false); 1363 it = ext.iterator(); 1364 assertTrue(it.hasNext()); 1365 Department d = (Department) it.next(); 1366 1367 assertTrue(c.contains(d)); 1368 } 1369 finally 1370 { 1371 if (pm.currentTransaction().isActive()) 1372 pm.currentTransaction().rollback(); 1373 1374 pm.close(); 1375 } 1376 } 1377 1378 1382 public void testInverseFCOCollectionFieldPersistence2() 1383 { 1384 PersistenceManager pm = pmf.getPersistenceManager(); 1385 1386 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1387 EMP_SALARY[0], EMP_SERIAL[0]); 1388 1389 try 1390 { 1391 Department d = new Department("Engineering"); 1392 d.setManager(mgr); 1393 1394 pm.currentTransaction().begin(); 1395 mgr.addDepartment(d); 1396 1397 pm.makePersistent(d); 1398 pm.currentTransaction().commit(); 1399 } 1400 finally 1401 { 1402 if (pm.currentTransaction().isActive()) 1403 { 1404 pm.currentTransaction().rollback(); 1405 pm.close(); 1406 fail(); 1407 } 1408 1409 pm.close(); 1410 } 1411 1412 try 1414 { 1415 pm = pmf.getPersistenceManager(); 1416 pm.currentTransaction().begin(); 1417 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1418 java.util.Iterator it = ext.iterator(); 1419 1420 assertTrue(it.hasNext()); 1421 mgr = (Manager) it.next(); 1422 Collection c = mgr.getDepartments(); 1423 assertEquals(1, c.size()); 1424 1425 ext = pm.getExtent(com.triactive.jdo.test.Department.class, false); 1426 it = ext.iterator(); 1427 assertTrue(it.hasNext()); 1428 Department d = (Department) it.next(); 1429 1430 assertTrue(c.contains(d)); 1431 pm.currentTransaction().commit(); 1432 } 1433 finally 1434 { 1435 if (pm.currentTransaction().isActive()) 1436 pm.currentTransaction().rollback(); 1437 1438 pm.close(); 1439 } 1440 } 1441 1442 1443 1447 public void testInverseFCOCollectionFieldPersistence3() 1448 { 1449 PersistenceManager pm = pmf.getPersistenceManager(); 1450 1451 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1452 EMP_SALARY[0], EMP_SERIAL[0]); 1453 1454 try 1455 { 1456 Department d = new Department("Engineering"); 1457 d.setManager(mgr); 1458 1459 pm.currentTransaction().begin(); 1460 mgr.addDepartment(d); 1461 1462 pm.makePersistent(d); 1463 pm.currentTransaction().commit(); 1464 1465 pm.currentTransaction().begin(); 1466 pm.deletePersistent(d); 1467 pm.currentTransaction().commit(); 1468 } 1469 finally 1470 { 1471 if (pm.currentTransaction().isActive()) 1472 { 1473 pm.currentTransaction().rollback(); 1474 pm.close(); 1475 fail(); 1476 } 1477 1478 pm.close(); 1479 } 1480 1481 try 1483 { 1484 pm = pmf.getPersistenceManager(); 1485 pm.currentTransaction().begin(); 1486 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1487 java.util.Iterator it = ext.iterator(); 1488 1489 assertTrue(it.hasNext()); 1490 mgr = (Manager) it.next(); 1491 Collection c = mgr.getDepartments(); 1492 assertEquals(0, c.size()); 1493 1494 ext = pm.getExtent(com.triactive.jdo.test.Department.class, false); 1495 it = ext.iterator(); 1496 assertTrue(!(it.hasNext())); 1497 pm.currentTransaction().commit(); 1498 } 1499 finally 1500 { 1501 if (pm.currentTransaction().isActive()) 1502 pm.currentTransaction().rollback(); 1503 1504 pm.close(); 1505 } 1506 } 1507 1508 1509 1513 public void testInverseFCOCollectionFieldPersistence4() 1514 { 1515 PersistenceManager pm = pmf.getPersistenceManager(); 1516 1517 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1518 EMP_SALARY[0], EMP_SERIAL[0]); 1519 1520 try 1521 { 1522 Department d = new Department("Engineering"); 1523 d.setManager(mgr); 1524 1525 pm.currentTransaction().begin(); 1526 1527 pm.makePersistent(d); 1528 pm.currentTransaction().commit(); 1529 } 1530 finally 1531 { 1532 if (pm.currentTransaction().isActive()) 1533 { 1534 pm.currentTransaction().rollback(); 1535 pm.close(); 1536 fail(); 1537 } 1538 1539 pm.close(); 1540 } 1541 1542 try 1544 { 1545 pm = pmf.getPersistenceManager(); 1546 pm.currentTransaction().begin(); 1547 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1548 java.util.Iterator it = ext.iterator(); 1549 1550 assertTrue(it.hasNext()); 1551 mgr = (Manager) it.next(); 1552 Collection c = mgr.getDepartments(); 1553 assertEquals(1, c.size()); 1554 1555 ext = pm.getExtent(com.triactive.jdo.test.Department.class, false); 1556 it = ext.iterator(); 1557 assertTrue(it.hasNext()); 1558 Department d = (Department) it.next(); 1559 assertTrue(c.contains(d)); 1560 pm.currentTransaction().commit(); 1561 } 1562 finally 1563 { 1564 if (pm.currentTransaction().isActive()) 1565 pm.currentTransaction().rollback(); 1566 1567 pm.close(); 1568 } 1569 } 1570 1571 1572 1573 1577 public void testQueryPM() 1578 { 1579 PersistenceManager pm = pmf.getPersistenceManager(); 1580 1581 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1582 EMP_SALARY[0], EMP_SERIAL[0]); 1583 1584 try 1585 { 1586 Department d = new Department("Engineering"); 1587 1588 pm.currentTransaction().begin(); 1589 mgr.addDepartment(d); 1590 pm.makePersistent(mgr); 1591 pm.currentTransaction().commit(); 1592 } 1593 finally 1594 { 1595 if (pm.currentTransaction().isActive()) 1596 { 1597 pm.currentTransaction().rollback(); 1598 pm.close(); 1599 fail(); 1600 } 1601 1602 pm.close(); 1603 } 1604 1605 try 1606 { 1607 pm = pmf.getPersistenceManager(); 1608 pm.currentTransaction().begin(); 1609 Extent ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1610 java.util.Iterator it = ext.iterator(); 1611 mgr = (Manager) it.next(); 1612 1613 assertSame(pm, JDOHelper.getPersistenceManager(mgr)); 1614 pm.currentTransaction().commit(); 1615 } 1616 finally 1617 { 1618 if (pm.currentTransaction().isActive()) 1619 pm.currentTransaction().rollback(); 1620 1621 pm.close(); 1622 } 1623 } 1624 1625 1626 1630 public void testInstanceCallbacks() 1631 { 1632 PersistenceManager pm = pmf.getPersistenceManager(); 1633 Transaction tx = pm.currentTransaction(); 1634 1635 InstanceCallbackTester tester = new InstanceCallbackTester(); 1636 tester.setPersistentValue("value"); 1637 1638 try 1639 { 1640 tx.begin(); 1641 pm.makePersistent(tester); 1642 tx.commit(); 1643 1644 Query q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false)); 1645 tx.begin(); 1646 1647 Collection c = (Collection )q.execute(); 1648 1649 try 1650 { 1651 assertEquals(1, c.size()); 1652 tester = (InstanceCallbackTester) c.iterator().next(); 1653 1654 assertNull(tester.getPersistentValue()); 1656 1657 assertNotNull(tester.getTransientValue()); 1659 assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue()); 1660 1661 try 1663 { 1664 tester.setTransientValue(null); 1665 pm.deletePersistent(tester); 1666 fail(); 1667 } 1668 catch (PreDeleteException e) 1669 { 1670 } 1671 } 1672 finally 1673 { 1674 q.closeAll(); 1675 } 1676 1677 tx.commit(); 1678 } 1679 finally 1680 { 1681 if (tx.isActive()) 1682 tx.rollback(); 1683 1684 pm.close(); 1685 } 1686 } 1687 1688 1689 1694 public void testNormalCollectionFieldPersistence1() 1695 { 1696 PersistenceManager pm = pmf.getPersistenceManager(); 1697 Transaction tx = pm.currentTransaction(); 1698 tx.setRetainValues(true); 1699 1700 CollectionFieldTester tester = new CollectionFieldTester(); 1701 1702 for (int i = 0; i < 3; i++) 1703 { 1704 Primitive p = new Primitive(); 1705 setPrimitiveValues(p); 1706 tester.getPrimitiveCollection().add(p); 1707 } 1708 1709 try 1710 { 1711 tx.begin(); 1712 pm.makePersistent(tester); 1713 Object id1 = JDOHelper.getObjectId(tester); 1714 tx.commit(); 1715 1716 tx.begin(); 1717 pm.makeTransient(tester); 1718 pm.makePersistent(tester); 1719 Object id2 = JDOHelper.getObjectId(tester); 1720 tx.commit(); 1721 1722 assertTrue("Object should have been given a new ID", !id1.equals(id2)); 1723 } 1724 finally 1725 { 1726 if (tx.isActive()) 1727 tx.rollback(); 1728 1729 pm.close(); 1730 } 1731 } 1732 1733 1734 1735 1736 public static void main(String args[]) 1737 { 1738 junit.textui.TestRunner.run(PersistenceManagerImplFullTest.class); 1739 } 1740 1741 1742 1745 public void setUp() throws java.lang.Exception 1746 { 1747 super.setUp(); 1748 1749 if (!schemaInitialized) 1750 { 1751 addClassesToSchema(new Class [] 1752 { 1753 InversePrimitive.class, 1754 CollectionFieldTester.class, 1755 InstanceCallbackTester.class, 1756 Person.class, 1757 Manager.class, 1758 Primitive.class 1759 } 1760 ); 1761 1762 schemaInitialized = true; 1763 } 1764 1765 cleanup(); 1766 } 1767 1768 1771 public void tearDown() throws java.lang.Exception 1772 { 1773 super.tearDown(); 1774 cleanup(); 1775 } 1776 1777 1778 1781 public void cleanup() 1782 { 1783 Extent ext = null; 1784 java.util.Iterator it = null; 1785 PersistenceManager pm = pmf.getPersistenceManager(); 1786 1787 try 1788 { 1789 pm.currentTransaction().begin(); 1791 1792 ext = pm.getExtent(InstanceCallbackTester.class, false); 1793 it = ext.iterator(); 1794 1795 while (it.hasNext()) 1796 { 1797 InstanceCallbackTester tester = (InstanceCallbackTester) it.next(); 1798 tester.setTransientValue(""); pm.deletePersistent(tester); 1800 } 1801 1802 pm.currentTransaction().commit(); 1803 1804 pm.currentTransaction().begin(); 1806 1807 ext = pm.getExtent(InversePrimitive.class, false); 1808 it = ext.iterator(); 1809 1810 while (it.hasNext()) 1811 { 1812 InversePrimitive ip = (InversePrimitive) it.next(); 1813 pm.deletePersistent(ip); 1814 } 1815 1816 pm.currentTransaction().commit(); 1817 1818 pm.currentTransaction().begin(); 1820 1821 ext = pm.getExtent(CollectionFieldTester.class, false); 1822 it = ext.iterator(); 1823 1824 while (it.hasNext()) 1825 { 1826 CollectionFieldTester t = (CollectionFieldTester) it.next(); 1827 t.getPrimitiveCollection().clear(); 1828 pm.deletePersistent(t); 1829 } 1830 1831 pm.currentTransaction().commit(); 1832 1833 1834 pm.currentTransaction().begin(); 1836 1837 ext = pm.getExtent(Primitive.class, false); 1838 it = ext.iterator(); 1839 1840 while (it.hasNext()) 1841 { 1842 Primitive p = (Primitive) it.next(); 1843 pm.deletePersistent(p); 1844 } 1845 1846 pm.currentTransaction().commit(); 1847 1848 pm.currentTransaction().begin(); 1850 1851 ext = pm.getExtent(com.triactive.jdo.test.Manager.class, false); 1852 it = ext.iterator(); 1853 1854 while (it.hasNext()) 1855 { 1856 Manager mgr = (Manager) it.next(); 1857 mgr.getSubordinates().clear(); 1858 mgr.getDepartments().clear(); 1859 } 1860 1861 pm.currentTransaction().commit(); 1862 1863 pm.currentTransaction().begin(); 1865 1866 ext = pm.getExtent(Employee.class, false); 1867 it = ext.iterator(); 1868 1869 while (it.hasNext()) 1870 { 1871 Employee emp = (Employee) it.next(); 1872 pm.deletePersistent(emp); 1873 } 1874 1875 pm.currentTransaction().commit(); 1876 pm.currentTransaction().begin(); 1877 1878 ext = pm.getExtent(Department.class, false); 1880 it = ext.iterator(); 1881 1882 while (it.hasNext()) 1883 { 1884 Department d = (Department) it.next(); 1885 pm.deletePersistent(d); 1886 } 1887 1888 pm.currentTransaction().commit(); 1889 1890 pm.currentTransaction().begin(); 1892 1893 ext = pm.getExtent(Manager.class, false); 1894 it = ext.iterator(); 1895 1896 while (it.hasNext()) 1897 { 1898 Manager mgr = (Manager) it.next(); 1899 pm.deletePersistent(mgr); 1900 } 1901 1902 pm.currentTransaction().commit(); 1903 1904 pm.currentTransaction().begin(); 1906 1907 ext = pm.getExtent(Person.class, true); 1908 it = ext.iterator(); 1909 1910 while (it.hasNext()) 1911 { 1912 Person person = (Person) it.next(); 1913 pm.deletePersistent(person); 1914 } 1915 1916 pm.currentTransaction().commit(); 1917 } 1918 finally 1919 { 1920 if (pm.currentTransaction().isActive()) 1921 pm.currentTransaction().commit(); 1922 1923 pm.close(); 1924 } 1925 } 1926 1927 1928 1932 public void testTransientObjectCollections() 1933 { 1934 PersistenceManager pm = pmf.getPersistenceManager(); 1935 1936 Employee emp = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], 1937 EMP_SALARY[1], EMP_SERIAL[1]); 1938 1939 Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], 1940 EMP_SALARY[0], EMP_SERIAL[0]); 1941 1942 pm.currentTransaction().begin(); 1943 pm.makePersistent(mgr); 1944 pm.currentTransaction().commit(); 1945 1946 pm.currentTransaction().begin(); 1947 1948 pm.retrieve(mgr); 1949 pm.makeTransient(mgr); 1950 mgr.addSubordinate(emp); 1951 1952 pm.currentTransaction().commit(); 1953 } 1954 1955 1956 private Person createNewPerson(PersistenceManager pm, int dataset) throws Exception 1957 { 1958 boolean successful = false; 1959 1960 Person p = new Person(dataset, FIRSTNAME[dataset], LASTNAME[dataset], EMAIL[dataset]); 1961 Transaction tx = pm.currentTransaction(); 1962 1963 try 1964 { 1965 tx.begin(); 1966 pm.makePersistent(p); 1967 1968 successful = true; 1969 } 1970 finally 1971 { 1972 if (successful) 1973 tx.commit(); 1974 else 1975 tx.rollback(); 1976 } 1977 1978 return p; 1979 } 1980 1981 1982 1983 private Person queryPerson(long personNum, PersistenceManager pm) 1984 { 1985 Transaction tx = pm.currentTransaction(); 1986 1987 try 1988 { 1989 tx.begin(); 1990 1991 Extent clnPerson = pm.getExtent(Person.class, true); 1992 Query q = pm.newQuery(clnPerson); 1993 1994 try 1995 { 1996 Collection people = (Collection ) q.execute(); 1997 1998 Iterator i = people.iterator(); 1999 2000 while (i.hasNext()) 2001 { 2002 Person p = (Person) i.next(); 2003 2004 if (p.getPersonNum() == personNum) 2005 { 2006 return p; 2007 } 2008 } 2009 } 2010 finally 2011 { 2012 q.closeAll(); 2013 } 2014 } 2015 finally 2016 { 2017 tx.commit(); 2018 } 2019 2020 return null; 2021 } 2022 2023 2024 static void setPrimitiveValues(Primitive p, boolean b, byte y, char c, int i, 2025 short s, long l, float f, double d, String fstr, String nstr, String hstr, 2026 java.util.Date dt1, java.sql.Date dt2, java.sql.Timestamp tm) 2027 { 2028 p.setBoolean(b); 2029 p.setBooleanObject(new Boolean (b)); 2030 p.setByte(y); 2031 p.setByteObject(new Byte (y)); 2032 p.setChar(c); 2033 p.setCharObject(new Character (c)); 2034 p.setInt(i); 2035 p.setIntObject(new Integer (i)); 2036 p.setShort(s); 2037 p.setShortObject(new Short (s)); 2038 p.setLong(l); 2039 p.setLongObject(new Long (l)); 2040 p.setFloat(f); 2041 p.setFloatObject(new Float (f)); 2042 p.setDouble(d); 2043 p.setDoubleObject(new Double (d)); 2044 p.setFixedLengthString(fstr); 2045 p.setNormalString(nstr); 2046 p.setHugeString(hstr); 2047 p.setUtilDateField(dt1); 2048 p.setSqlDateField(dt2); 2049 p.setSqlTimestamp(tm); 2050 } 2051 2052 2055 private void setPrimitiveValues(Primitive p) 2056 { 2057 java.util.Date date1 = (new java.util.GregorianCalendar ()).getTime(); 2058 java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01"); 2059 java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000"); 2060 2061 setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 2062 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", 2063 date1, date2, timestamp); 2064 } 2065 2066 2067 private void assertPrimitiveValues(Primitive p, boolean b, byte y, char c, int i, 2068 short s, long l, float f, double d, String fstr, String nstr, String hstr, 2069 java.util.Date dt1, java.sql.Date dt2, java.sql.Timestamp tm) 2070 { 2071 assertEquals(b, p.getBoolean()); 2072 assertEquals(new Boolean (b), p.getBooleanObject()); 2073 assertEquals(y, p.getByte()); 2074 assertEquals(new Byte (y), p.getByteObject()); 2075 assertEquals(c, p.getChar()); 2076 assertEquals(new Character (c), p.getCharObject()); 2077 assertEquals(i, p.getInt()); 2078 assertEquals(new Integer (i), p.getIntObject()); 2079 assertEquals(s, p.getShort()); 2080 assertEquals(new Short (s), p.getShortObject()); 2081 assertEquals(l, p.getLong()); 2082 assertEquals(new Long (l), p.getLongObject()); 2083 assertEquals(f, p.getFloat(), 0.0F); 2084 assertEquals(f, p.getFloatObject().floatValue(), 0.0F); 2085 assertEquals(d, p.getDouble(), 0.0); 2086 assertEquals(d, p.getDoubleObject().doubleValue(), 0.0); 2087 assertEquals(fstr, p.getFixedLengthString().trim()); 2088 assertEquals(nstr, p.getNormalString()); 2089 assertEquals(hstr, p.getHugeString()); 2090 2093 assertEquals(dt1.getTime() / 1000, p.getUtilDateField().getTime() / 1000); 2095 assertEquals(dt2.getTime(), p.getSqlDateField().getTime()); 2096 assertEquals(tm.getTime() / 1000, p.getSqlTimestamp().getTime() / 1000); 2097 } 2098 2099 2100 private void assertState(Object o, 2101 boolean persistent, 2102 boolean transactional, 2103 boolean dirty, 2104 boolean isNew, 2105 boolean deleted) 2106 { 2107 assertEquals(deleted, JDOHelper.isDeleted(o)); 2108 assertEquals(dirty, JDOHelper.isDirty(o)); 2109 assertEquals(isNew, JDOHelper.isNew(o)); 2110 assertEquals(persistent, JDOHelper.isPersistent(o)); 2111 assertEquals(transactional, JDOHelper.isTransactional(o)); 2112 } 2113 2114 private void assertTransient(Object o) 2115 { 2116 assertNull(JDOHelper.getPersistenceManager(o)); 2117 assertState(o, false, false, false, false, false); 2118 } 2119 2120 private void assertPersistentNew(Object o) 2121 { 2122 assertNotNull(JDOHelper.getPersistenceManager(o)); 2123 assertState(o, true, true, true, true, false); 2124 } 2125 2126 private void assertPersistentNontransactional(Object o) 2127 { 2128 assertNotNull(JDOHelper.getPersistenceManager(o)); 2129 assertState(o, true, false, false, false, false); 2130 } 2131 2132 private void assertPersistentClean(Object o) 2133 { 2134 assertNotNull(JDOHelper.getPersistenceManager(o)); 2135 assertState(o, true, true, false, false, false); 2136 } 2137 2138 private void assertPersistentDirty(Object o) 2139 { 2140 assertNotNull(JDOHelper.getPersistenceManager(o)); 2141 assertState(o, true, true, true, false, false); 2142 } 2143 2144 private void assertHollow(Object o) 2145 { 2146 assertNotNull(JDOHelper.getPersistenceManager(o)); 2147 assertState(o, true, false, false, false, false); 2148 } 2149 2150 private void assertPersistentDeleted(Object o) 2151 { 2152 assertNotNull(JDOHelper.getPersistenceManager(o)); 2153 assertState(o, true, true, true, false, true); 2154 } 2155 2156 private void assertPersistentNewDeleted(Object o) 2157 { 2158 assertNotNull(JDOHelper.getPersistenceManager(o)); 2159 assertState(o, true, true, true, true, true); 2160 } 2161} 2162 | Popular Tags |