0001: /*
0002: * Copyright 2004 (C) TJDO.
0003: * All rights reserved.
0004: *
0005: * This software is distributed under the terms of the TJDO License version 1.0.
0006: * See the terms of the TJDO License in the documentation provided with this software.
0007: *
0008: * $Id: PersistenceManagerImplFullTest.java,v 1.11 2004/01/18 03:01:07 jackknifebarber Exp $
0009: */
0010:
0011: package com.triactive.jdo.test;
0012:
0013: import com.triactive.jdo.PersistenceManagerFactoryImpl;
0014:
0015: import javax.jdo.PersistenceManager;
0016: import javax.jdo.Extent;
0017: import javax.jdo.Query;
0018: import javax.jdo.JDODataStoreException;
0019: import javax.jdo.JDOFatalDataStoreException;
0020: import javax.jdo.JDOUserException;
0021: import javax.jdo.Transaction;
0022: import javax.jdo.JDOHelper;
0023:
0024: import java.util.Iterator;
0025: import java.util.Set;
0026: import java.util.HashSet;
0027: import java.util.Collection;
0028:
0029: import junit.framework.Test;
0030: import junit.framework.TestCase;
0031:
0032: import org.apache.log4j.Category;
0033:
0034: /**
0035: * This class is a JUnit test class for unit testing
0036: * <code>com.triactive.jdo.PersistenceManagerImpl</code>.
0037: *
0038: */
0039: public class PersistenceManagerImplFullTest extends PersistenceTestCase {
0040: private static final Category LOG = Category
0041: .getInstance(PersistenceManagerImplFullTest.class);
0042:
0043: // Person data
0044: private static String EMAIL[] = { "jon.doe@msn.com",
0045: "jane.smith@msn.com", "tom.jones@aol.com" };
0046: private static String FIRSTNAME[] = { "Jon", "Jane", "Tom" };
0047: private static String LASTNAME[] = { "Doe", "Smith", "Jones" };
0048:
0049: // Employee data
0050: private static float EMP_SALARY[] = { 75000.00F, 40000.00F,
0051: 35000.00F, 25000.00F };
0052: private static String EMP_SERIAL[] = { "683687A", "439293A",
0053: "384018D", "102938X" };
0054:
0055: // Manager data
0056: private static String MGR_DEPT[] = { "Marketing", "Sales",
0057: "Purchasing", "Accounting" };
0058:
0059: private boolean schemaInitialized = false;
0060:
0061: /**
0062: * Used by the JUnit framework to construct tests.
0063: *
0064: * @param name Name of the <tt>TestCase</tt>.
0065: */
0066: public PersistenceManagerImplFullTest(String name) {
0067: super (name);
0068: }
0069:
0070: /**
0071: * Tests storage of various datatypes using the makePersistent method.
0072: */
0073: public void testMakePersistent() throws Exception {
0074: PersistenceManager pm = pmf.getPersistenceManager();
0075: Primitive p = new Primitive();
0076:
0077: Transaction tx = pm.currentTransaction();
0078:
0079: try {
0080: // Test insert of native and native wrapper data types
0081: /* BigDecimal bd = new BigDecimal("12345.12345");
0082: BigInteger bi = new BigInteger("12345");*/
0083: java.util.Date date1 = (new java.util.GregorianCalendar())
0084: .getTime();
0085: java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
0086: java.sql.Timestamp timestamp = java.sql.Timestamp
0087: .valueOf("2001-01-01 23:23:23.050500000");
0088:
0089: tx.begin();
0090:
0091: setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43,
0092: 123456789L, 123.456F, 123.456, "fixed", "normal",
0093: "huge", date1, date2, timestamp);
0094:
0095: // BigDecimal and BigInteger types not yet supported
0096: /* p.setBigDecimal(bd);
0097: p.setBigInteger(bi);*/
0098:
0099: pm.makePersistent(p);
0100:
0101: tx.commit();
0102:
0103: p = null;
0104:
0105: tx.begin();
0106:
0107: Extent clnPrimitive = pm.getExtent(Primitive.class, false);
0108: java.util.Iterator it = clnPrimitive.iterator(); // should only have one Primitive object
0109: p = (Primitive) it.next();
0110:
0111: assertNotNull(p);
0112:
0113: // test that the data was stored properly
0114: assertPrimitiveValues(p, true, (byte) 23, 'z', 33,
0115: (short) 43, 123456789L, 123.456F, 123.456, "fixed",
0116: "normal", "huge", date1, date2, timestamp);
0117:
0118: tx.commit();
0119: } finally {
0120: if (tx.isActive())
0121: tx.rollback();
0122: pm.close();
0123: }
0124:
0125: }
0126:
0127: /**
0128: * Test that updates of collection and inverse collection fields update
0129: * the underlying collection
0130: */
0131: public void testMakeCollectionFieldsPersistent() throws Exception {
0132: PersistenceManager pm = pmf.getPersistenceManager();
0133: CollectionFieldTester collectionTester = new CollectionFieldTester();
0134:
0135: Transaction tx = pm.currentTransaction();
0136:
0137: try {
0138: // data for testing collections
0139: Person[] personArray = new Person[3];
0140: InversePrimitive[] inversePrimitiveArray = new InversePrimitive[3];
0141:
0142: personArray[0] = new Person(0, FIRSTNAME[0], LASTNAME[0],
0143: EMAIL[0]);
0144: personArray[1] = new Person(1, FIRSTNAME[1], LASTNAME[1],
0145: EMAIL[1]);
0146: personArray[2] = new Person(2, FIRSTNAME[2], LASTNAME[2],
0147: EMAIL[2]);
0148:
0149: inversePrimitiveArray[0] = new InversePrimitive();
0150: inversePrimitiveArray[1] = new InversePrimitive();
0151: inversePrimitiveArray[2] = new InversePrimitive();
0152:
0153: tx.begin();
0154: pm.makePersistent(collectionTester);
0155: tx.commit();
0156:
0157: tx.begin();
0158:
0159: // set up Set field data
0160: {
0161: HashSet s = new HashSet(3);
0162: s.add(personArray[0]);
0163: s.add(personArray[1]);
0164: s.add(personArray[2]);
0165: collectionTester.setPersonSet(s);
0166: }
0167:
0168: // set up inverse element data
0169: {
0170: HashSet s = new HashSet(3);
0171: s.add(inversePrimitiveArray[0]);
0172: s.add(inversePrimitiveArray[1]);
0173: s.add(inversePrimitiveArray[2]);
0174:
0175: collectionTester.setInversePrimitiveCollection(s);
0176: }
0177:
0178: tx.commit();
0179:
0180: collectionTester = null;
0181:
0182: // test results
0183: tx.begin();
0184:
0185: {
0186: Extent clnTest = pm.getExtent(
0187: CollectionFieldTester.class, false);
0188: java.util.Iterator it = clnTest.iterator(); // should only have one Primitive object
0189: collectionTester = (CollectionFieldTester) it.next();
0190: }
0191:
0192: assertNotNull(collectionTester);
0193:
0194: // test that Set data was stored
0195: {
0196: Set s = collectionTester.getPersonSet();
0197: assertTrue(s.contains(personArray[0]));
0198: assertTrue(s.contains(personArray[1]));
0199: assertTrue(s.contains(personArray[2]));
0200: assertEquals(3, s.size());
0201:
0202: Collection c = collectionTester
0203: .getInversePrimitiveCollection();
0204:
0205: assertEquals(collectionTester, inversePrimitiveArray[0]
0206: .getTester());
0207: assertEquals(collectionTester, inversePrimitiveArray[1]
0208: .getTester());
0209: assertEquals(collectionTester, inversePrimitiveArray[2]
0210: .getTester());
0211:
0212: assertTrue(c.contains(inversePrimitiveArray[0]));
0213: assertTrue(c.contains(inversePrimitiveArray[1]));
0214: assertTrue(c.contains(inversePrimitiveArray[2]));
0215: assertEquals(3, c.size());
0216: }
0217:
0218: tx.commit();
0219:
0220: tx.begin();
0221:
0222: /*
0223: * Modify the owner field of element 2 to point to a new transient
0224: * this should make that object persistent (after commit).
0225: */
0226: CollectionFieldTester ct1 = new CollectionFieldTester();
0227: inversePrimitiveArray[2].setTester(ct1);
0228:
0229: tx.commit();
0230:
0231: tx.begin();
0232:
0233: assertTrue(JDOHelper.isPersistent(ct1));
0234:
0235: /*
0236: * Assert that element 2 no longer is in the set of its former
0237: * owner ...
0238: */
0239: Collection c = collectionTester
0240: .getInversePrimitiveCollection();
0241: assertTrue(!c.contains(inversePrimitiveArray[2]));
0242:
0243: /*
0244: * ... and that it is in the set of its new owner.
0245: */
0246: Collection c1 = ct1.getInversePrimitiveCollection();
0247: assertTrue(c1.contains(inversePrimitiveArray[2]));
0248:
0249: /*
0250: * Move element 1 from one set to the other with Set.add().
0251: */
0252: c1.add(inversePrimitiveArray[1]);
0253: assertEquals(1, c.size());
0254: assertEquals(2, c1.size());
0255:
0256: tx.commit();
0257:
0258: /*
0259: * Remove element 2 with Set.remove() and assert that it gets
0260: * deleted from the DB.
0261: */
0262: tx.begin();
0263:
0264: c1 = ct1.getInversePrimitiveCollection();
0265: c1.remove(inversePrimitiveArray[2]);
0266: assertTrue(JDOHelper.isDeleted(inversePrimitiveArray[2]));
0267:
0268: tx.commit();
0269:
0270: /*
0271: * Clear c1 with Set.clear() and assert that element 1 (the only
0272: * one left) gets deleted from the DB.
0273: */
0274: tx.begin();
0275:
0276: c1 = ct1.getInversePrimitiveCollection();
0277: c1.clear();
0278: assertTrue(JDOHelper.isDeleted(inversePrimitiveArray[1]));
0279: assertTrue(c1.isEmpty());
0280:
0281: tx.commit();
0282: } finally {
0283: if (tx.isActive())
0284: tx.rollback();
0285: pm.close();
0286: }
0287: }
0288:
0289: /**
0290: * Simple test of transactional PC field updates
0291: */
0292: public void testUpdatePersistentFields() throws Exception {
0293: PersistenceManager pm = pmf.getPersistenceManager();
0294: Primitive p = new Primitive();
0295:
0296: Transaction tx = pm.currentTransaction();
0297:
0298: try {
0299: // Test update of native and native wrapper data types
0300: /* BigDecimal bd = new BigDecimal("12345.12345");
0301: BigInteger bi = new BigInteger("12345");*/
0302: java.util.Date date1 = (new java.util.GregorianCalendar())
0303: .getTime();
0304: java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
0305: java.sql.Timestamp timestamp = java.sql.Timestamp
0306: .valueOf("2001-01-01 23:23:23.050500000");
0307: tx.begin();
0308: setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43,
0309: 123456789L, 123.456F, 123.456, "fixed", "normal",
0310: "huge", date1, date2, timestamp);
0311:
0312: // BigDecimal and BigInteger types not yet supported
0313: /* p.setBigDecimal(bd);
0314: p.setBigInteger(bi);*/
0315: pm.makePersistent(p);
0316: tx.commit();
0317:
0318: p = null;
0319:
0320: // perform the update
0321: tx.begin();
0322:
0323: Extent clnPrimitive = pm.getExtent(Primitive.class, false);
0324: java.util.Iterator it = clnPrimitive.iterator(); // should only have one Primitive object
0325: p = (Primitive) it.next();
0326:
0327: // replacement SCOs
0328: date1 = new java.util.Date(date1.getTime() + 10000);
0329: date2 = java.sql.Date.valueOf("2001-01-02");
0330: timestamp = java.sql.Timestamp
0331: .valueOf("2001-01-02 23:23:23.050500000");
0332:
0333: setPrimitiveValues(p, false, (byte) 22, 'a', 34,
0334: (short) 44, 1234567890, 456.456F, 456.456,
0335: "fixedlength", "normalsize", "hugexyzabc", date1,
0336: date2, timestamp);
0337:
0338: tx.commit();
0339:
0340: // test the results
0341: tx.begin();
0342:
0343: clnPrimitive = pm.getExtent(Primitive.class, false);
0344: it = clnPrimitive.iterator(); // should only have one Primitive object
0345: p = (Primitive) it.next();
0346:
0347: assertPrimitiveValues(p, false, (byte) 22, 'a', 34,
0348: (short) 44, 1234567890, 456.456F, 456.456,
0349: "fixedlength", "normalsize", "hugexyzabc", date1,
0350: date2, timestamp);
0351: } finally {
0352: if (tx.isActive())
0353: tx.rollback();
0354: pm.close();
0355: }
0356: }
0357:
0358: /**
0359: * Tests attempts to update PC fields on a P-new-del or P-del PC object
0360: */
0361: public void testUpdatePersistentFieldsExceptions() throws Exception {
0362: PersistenceManager pm = pmf.getPersistenceManager();
0363: Transaction tx = null;
0364:
0365: try {
0366: Person p = new Person(0, FIRSTNAME[0], LASTNAME[0],
0367: EMAIL[0]);
0368:
0369: // test attempt to update P-new-del PC
0370: tx = pm.currentTransaction();
0371:
0372: tx.begin();
0373: pm.makePersistent(p);
0374: pm.deletePersistent(p);
0375:
0376: try {
0377: p.setLastName(LASTNAME[1]);
0378: fail("should have thrown JDOUserException");
0379: } catch (JDOUserException e) {
0380: }
0381:
0382: tx.rollback();
0383:
0384: // test attempt to update P-del
0385: tx.begin();
0386: pm.makePersistent(p);
0387: tx.commit();
0388:
0389: tx.begin();
0390: pm.deletePersistent(p);
0391:
0392: try {
0393: p.setLastName(LASTNAME[1]);
0394: fail("should have thrown JDOUserException");
0395: } catch (JDOUserException e) {
0396: }
0397: } finally {
0398: if (tx.isActive())
0399: tx.rollback();
0400: pm.close();
0401: }
0402: }
0403:
0404: /**
0405: * Simple test of makeTransient()
0406: */
0407: public void testMakeTransient() throws Exception {
0408: PersistenceManager pm = pmf.getPersistenceManager();
0409: Transaction tx = null;
0410:
0411: try {
0412: tx = pm.currentTransaction();
0413: Person x = createNewPerson(pm, 0);
0414: Person y = new Person(0, FIRSTNAME[0], LASTNAME[0],
0415: EMAIL[0]);
0416:
0417: // test making transient object transient (no effect)
0418: tx.begin();
0419: pm.makeTransient(y);
0420: tx.commit();
0421:
0422: // test making persistent object transient
0423: tx.begin();
0424: pm.makeTransient(x);
0425: x.setLastName(LASTNAME[1]);
0426: tx.commit();
0427:
0428: assertNull(JDOHelper.getObjectId(x));
0429:
0430: Person queryResult = queryPerson(0, pm);
0431:
0432: tx.begin();
0433: assertEquals(LASTNAME[0], queryResult.getLastName());
0434:
0435: tx.commit();
0436: } finally {
0437: if (tx.isActive())
0438: tx.rollback();
0439: pm.close();
0440: }
0441: }
0442:
0443: /**
0444: * Simple test of makeTransientAll()
0445: */
0446: public void testMakeTransientAll() throws Exception {
0447: PersistenceManager pm = pmf.getPersistenceManager();
0448: Transaction tx = null;
0449:
0450: try {
0451: tx = pm.currentTransaction();
0452: Person x = createNewPerson(pm, 0);
0453: Person y = createNewPerson(pm, 1);
0454: Person[] personArray = new Person[2];
0455:
0456: personArray[0] = x;
0457: personArray[1] = y;
0458:
0459: // test making persistent object transient
0460: tx.begin();
0461: pm.makeTransientAll(personArray);
0462:
0463: x.setLastName(LASTNAME[1]);
0464: y.setLastName(LASTNAME[0]);
0465: tx.commit();
0466:
0467: x = queryPerson(0, pm);
0468: y = queryPerson(1, pm);
0469:
0470: tx.begin();
0471:
0472: // changes applied after objects were made transient should not have persisted
0473: assertEquals(LASTNAME[0], x.getLastName());
0474: assertEquals(LASTNAME[1], y.getLastName());
0475:
0476: tx.commit();
0477: } finally {
0478: if (tx.isActive())
0479: tx.rollback();
0480: pm.close();
0481: }
0482: }
0483:
0484: /**
0485: * Tests attempts to call makeTransient outside a transaction
0486: */
0487: public void testMakeTransientExceptions() throws Exception {
0488: PersistenceManager pm = pmf.getPersistenceManager();
0489:
0490: try {
0491: Person x = createNewPerson(pm, 0);
0492:
0493: // test makeTransient outside of transaction
0494: pm.makeTransient(x);
0495: assertTrue("Object still persistent after makeTransient()",
0496: !JDOHelper.isPersistent(x));
0497: assertTrue(
0498: "Object still transactional after makeTransient()",
0499: !JDOHelper.isTransactional(x));
0500: } finally {
0501: pm.close();
0502: }
0503: }
0504:
0505: /**
0506: * Simple test of deletePersistent method
0507: */
0508: public void testDeletePersistent() throws Exception {
0509: PersistenceManager pm = pmf.getPersistenceManager();
0510: Transaction tx = null;
0511:
0512: try {
0513: tx = pm.currentTransaction();
0514: Person x = createNewPerson(pm, 0);
0515:
0516: // test deletion of persistent-clean object
0517: tx.begin();
0518: pm.deletePersistent(x);
0519: tx.commit();
0520:
0521: Person queryResult = queryPerson(0, pm);
0522: assertNull(queryResult);
0523:
0524: // test deletion of persistent-new objects
0525: tx.begin();
0526: x = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
0527: pm.makePersistent(x);
0528: pm.deletePersistent(x);
0529: tx.commit();
0530: } finally {
0531: if (tx.isActive())
0532: tx.rollback();
0533: pm.close();
0534: }
0535:
0536: }
0537:
0538: /**
0539: * Tests attempts to delete a transient object
0540: */
0541: public void testDeletePersistentExceptions() throws Exception {
0542: PersistenceManager pm = pmf.getPersistenceManager();
0543: Transaction tx = null;
0544:
0545: try {
0546: tx = pm.currentTransaction();
0547: Person p = new Person(0, FIRSTNAME[0], LASTNAME[0],
0548: EMAIL[0]);
0549:
0550: // test attempt to redelete transient object
0551: try {
0552: tx.begin();
0553: pm.deletePersistent(p);
0554: fail("calling deletePersistent on transient object should fail");
0555: } catch (JDOUserException e) {
0556: } finally {
0557: tx.rollback();
0558: }
0559:
0560: // test attempt to delete a P-new-del
0561: tx.begin();
0562: pm.makePersistent(p);
0563: pm.deletePersistent(p);
0564: try {
0565: pm.deletePersistent(p);
0566: } catch (JDOUserException e) {
0567: fail("calling deletePersistent on a P-del object should work");
0568: }
0569:
0570: tx.rollback();
0571:
0572: // test attempt to delete a P-del
0573: p = createNewPerson(pm, 0);
0574:
0575: tx.begin();
0576: pm.deletePersistent(p);
0577:
0578: try {
0579: pm.deletePersistent(p);
0580: } catch (JDOUserException e) {
0581: fail("calling deletePersistent on a P-del object should work");
0582: }
0583:
0584: tx.rollback();
0585: } finally {
0586: if (tx.isActive())
0587: tx.rollback();
0588: pm.close();
0589: }
0590:
0591: }
0592:
0593: public void testStateTransitions() throws Exception {
0594: PersistenceManager pm = pmf.getPersistenceManager();
0595: Transaction tx = null;
0596: Object id = null;
0597:
0598: try {
0599: Person x = new Person(0, FIRSTNAME[0], LASTNAME[0],
0600: EMAIL[0]);
0601: tx = pm.currentTransaction();
0602:
0603: tx.begin();
0604:
0605: // 1. transient to persistent-new
0606: assertTransient(x);
0607: pm.makePersistent(x);
0608: assertPersistentNew(x);
0609:
0610: // 15. persistent-new to transient
0611: tx.rollback();
0612: assertTransient(x);
0613:
0614: tx.begin();
0615: pm.makePersistent(x);
0616: assertPersistentNew(x);
0617:
0618: // 16. persistent-new to persistent-new-deleted
0619: pm.deletePersistent(x);
0620: assertPersistentNewDeleted(x);
0621:
0622: // 18. persistent-new-deleted to transient
0623: tx.commit();
0624: assertTransient(x);
0625:
0626: // 2. persistent-new to hollow
0627: tx.begin();
0628: pm.makePersistent(x);
0629: assertPersistentNew(x);
0630: id = pm.getObjectId(x);
0631: tx.commit();
0632: assertHollow(x);
0633:
0634: // 6. persistent-clean to hollow
0635: tx.begin();
0636: x = (Person) pm.getObjectById(id, false);
0637: assertHollow(x);
0638: x.getFirstName();
0639: assertPersistentClean(x);
0640: tx.commit();
0641: assertHollow(x);
0642:
0643: // 3. hollow to persistent-clean
0644: tx.begin();
0645: assertHollow(x);
0646: x.getPersonNum();
0647: assertPersistentClean(x);
0648: tx.commit();
0649:
0650: // 11. hollow to persistent-dirty
0651: tx.begin();
0652: assertHollow(x);
0653: x.setLastName(this .LASTNAME[1]);
0654: assertPersistentDirty(x);
0655: // 5. persistent-dirty to hollow via rollback
0656: tx.rollback();
0657: assertHollow(x);
0658:
0659: tx.begin();
0660: // 19. hollow to persistent-deleted
0661: assertHollow(x);
0662: pm.deletePersistent(x);
0663: assertPersistentDeleted(x);
0664: // 21. persistent-deleted to hollow
0665: tx.rollback();
0666: assertHollow(x);
0667:
0668: tx.begin();
0669: x.getPersonNum();
0670: // 4. persistent-clean to persistent-dirty
0671: assertPersistentClean(x);
0672: x.setLastName(this .LASTNAME[1]);
0673: assertPersistentDirty(x);
0674:
0675: // 19. persistent-dirty to persistent-deleted
0676: pm.deletePersistent(x);
0677: assertPersistentDeleted(x);
0678: // 21. persistent-deleted to hollow
0679: tx.rollback();
0680: assertHollow(x);
0681:
0682: tx.begin();
0683: // 5. persistent-dirty to hollow via commit
0684: x.setLastName(this .LASTNAME[1]);
0685: assertPersistentDirty(x);
0686: tx.commit();
0687: assertHollow(x);
0688:
0689: tx.begin();
0690: x.setLastName(this .LASTNAME[2]);
0691: assertPersistentDirty(x);
0692: // 19. persistent-dirty to persistent-deleted
0693: pm.deletePersistent(x);
0694: assertPersistentDeleted(x);
0695: // 20. persistent-deleted to transient
0696: tx.commit();
0697: assertTransient(x);
0698:
0699: x = new Person(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1]);
0700: tx.begin();
0701: pm.makePersistent(x);
0702: pm.deletePersistent(x);
0703: assertPersistentNewDeleted(x);
0704: // 18. persistent-new-deleted to transient via commit
0705: tx.commit();
0706: assertTransient(x);
0707:
0708: x = new Person(2, FIRSTNAME[2], LASTNAME[2], EMAIL[2]);
0709: tx.begin();
0710: pm.makePersistent(x);
0711: pm.deletePersistent(x);
0712: assertPersistentNewDeleted(x);
0713: // 17. persistent-new-deleted to transient via rollback
0714: tx.rollback();
0715: assertTransient(x);
0716:
0717: x = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
0718: tx.begin();
0719: pm.makePersistent(x);
0720: tx.commit();
0721: assertHollow(x);
0722: tx.begin();
0723: x.getPersonNum();
0724: assertPersistentClean(x);
0725: // 12. persistent-clean to persistent-nontransactional
0726: pm.makeNontransactional(x);
0727: assertPersistentNontransactional(x);
0728: tx.commit();
0729:
0730: tx.begin();
0731: // 13. persistent-nontransactional to persistent-clean
0732: pm.makeTransactional(x);
0733: assertPersistentClean(x);
0734: tx.commit();
0735:
0736: tx.begin();
0737: pm.makeNontransactional(x);
0738: assertPersistentNontransactional(x);
0739: // 14. persistent-nontransactional to persistent-dirty
0740: x.setLastName(this .LASTNAME[1]);
0741: assertPersistentDirty(x);
0742: tx.rollback();
0743:
0744: tx.setNontransactionalRead(true);
0745: assertHollow(x);
0746: // 22. hollow to persistent-nontransactional
0747: x.getPersonNum();
0748: assertPersistentNontransactional(x);
0749: } finally {
0750: if (tx.isActive())
0751: tx.rollback();
0752:
0753: pm.close();
0754: }
0755: }
0756:
0757: /**
0758: * Test that inherited fields are persisted
0759: */
0760: public void testInheritedFieldsPersisted() {
0761: PersistenceManager pm = pmf.getPersistenceManager();
0762: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
0763: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
0764:
0765: try {
0766: pm.currentTransaction().begin();
0767: pm.makePersistent(mgr);
0768: pm.currentTransaction().commit();
0769:
0770: pm.currentTransaction().begin();
0771: Extent ext = pm.getExtent(
0772: com.triactive.jdo.test.Manager.class, false);
0773: java.util.Iterator it = ext.iterator();
0774:
0775: assertTrue(it.hasNext());
0776:
0777: mgr = (Manager) it.next();
0778:
0779: assertEquals(FIRSTNAME[0], mgr.getFirstName());
0780: assertEquals(LASTNAME[0], mgr.getLastName());
0781: assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
0782: pm.currentTransaction().commit();
0783: } finally {
0784: if (pm.currentTransaction().isActive()) {
0785: pm.currentTransaction().rollback();
0786: pm.close();
0787: fail();
0788: }
0789:
0790: pm.close();
0791: }
0792:
0793: try {
0794: // get a fresh pm to ensure that data is coming from the store, not the cache
0795: pm = pmf.getPersistenceManager();
0796: pm.currentTransaction().begin();
0797:
0798: Extent ext = pm.getExtent(
0799: com.triactive.jdo.test.Manager.class, false);
0800: java.util.Iterator it = ext.iterator();
0801:
0802: assertTrue(it.hasNext());
0803:
0804: mgr = (Manager) it.next();
0805:
0806: assertEquals(FIRSTNAME[0], mgr.getFirstName());
0807: assertEquals(LASTNAME[0], mgr.getLastName());
0808: assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
0809: } finally {
0810: if (pm.currentTransaction().isActive())
0811: pm.currentTransaction().rollback();
0812:
0813: pm.close();
0814: }
0815: }
0816:
0817: /**
0818: * Test that persisting a PC will also persist all FCOs that are referenced
0819: * by that PC
0820: */
0821: public void testFCOReferencedObjectsPersistence() {
0822: Manager manager = new Manager(0, FIRSTNAME[0], LASTNAME[0],
0823: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
0824:
0825: Employee employee = new Employee(1, FIRSTNAME[1], LASTNAME[1],
0826: EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
0827:
0828: PersistenceManager pm = pmf.getPersistenceManager();
0829:
0830: try {
0831: pm.currentTransaction().begin();
0832: pm.makePersistent(employee);
0833: employee.setManager(manager);
0834: pm.currentTransaction().commit();
0835: } finally {
0836: if (pm.currentTransaction().isActive()) {
0837: pm.currentTransaction().rollback();
0838: pm.close();
0839: fail();
0840: }
0841:
0842: pm.close();
0843: }
0844:
0845: // test that persisting a PC will also persist FCOs referenced by that PC
0846: try {
0847: pm = pmf.getPersistenceManager();
0848: pm.currentTransaction().begin();
0849: Extent clnManager = pm.getExtent(Manager.class, false);
0850: Iterator it = clnManager.iterator(); // should only have one Primitive object
0851: assertTrue(it.hasNext());
0852:
0853: Manager m = (Manager) it.next();
0854: assertEquals(pm.getObjectId(manager), pm.getObjectId(m));
0855: pm.currentTransaction().commit();
0856: } finally {
0857: if (pm.currentTransaction().isActive())
0858: pm.currentTransaction().rollback();
0859: pm.close();
0860: }
0861: }
0862:
0863: /**
0864: * Test that transient fields are accessible both inside and outside transactiosns
0865: */
0866: public void testPCFieldAccess() throws Exception {
0867: PersistenceManager pm = pmf.getPersistenceManager();
0868: Primitive p = new Primitive();
0869:
0870: Transaction tx = pm.currentTransaction();
0871:
0872: try {
0873: p.setTransientField(100);
0874:
0875: tx.begin();
0876: p.setTransientField(200);
0877: pm.makePersistent(p);
0878: p.setTransientField(300);
0879: tx.commit();
0880:
0881: p.setTransientField(400);
0882: } finally {
0883: if (tx.isActive())
0884: tx.rollback();
0885: pm.close();
0886: }
0887: }
0888:
0889: public void testExtentSubclasses() throws Exception {
0890: PersistenceManager pm = pmf.getPersistenceManager();
0891: Transaction tx = pm.currentTransaction();
0892:
0893: Employee empl = new Employee(0, FIRSTNAME[0], LASTNAME[0],
0894: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
0895: Manager mgr = new Manager(1, FIRSTNAME[1], LASTNAME[1],
0896: EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
0897:
0898: try {
0899: tx.begin();
0900: pm.makePersistent(empl);
0901: pm.makePersistent(mgr);
0902: Extent extent = pm.getExtent(Employee.class, false);
0903:
0904: // test subclasses argument == false
0905: java.util.Iterator it = extent.iterator();
0906: Employee empl2 = (Employee) it.next();
0907: assertEquals(empl.getPersonNum(), empl2.getPersonNum());
0908: assertEquals(false, it.hasNext());
0909:
0910: tx.commit();
0911:
0912: // test subclasses argument == true
0913: tx.begin();
0914: extent = pm.getExtent(Employee.class, true);
0915: it = extent.iterator();
0916:
0917: // the extent should contain both the Manager and Employee that we persisted
0918: empl2 = (Employee) it.next();
0919:
0920: if (empl2 instanceof Manager) {
0921: assertEquals(1, empl2.getPersonNum());
0922: pm.deletePersistent(empl2);
0923:
0924: empl2 = (Employee) it.next();
0925: assertEquals(0, empl2.getPersonNum());
0926: pm.deletePersistent(empl2);
0927: } else {
0928: assertEquals(0, empl2.getPersonNum());
0929: pm.deletePersistent(empl2);
0930:
0931: empl2 = (Manager) it.next();
0932: assertEquals(1, empl2.getPersonNum());
0933: pm.deletePersistent(empl2);
0934: }
0935:
0936: tx.commit();
0937: } finally {
0938: if (tx.isActive())
0939: tx.rollback();
0940:
0941: pm.close();
0942: }
0943: }
0944:
0945: /**
0946: * Test that getObjectId() returns the same value when called on a newly
0947: * persistent PC as when called on a query result that should return that same
0948: * persistent object
0949: */
0950: public void testJavaIdentity() {
0951: Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
0952: PersistenceManager pm = pmf.getPersistenceManager();
0953: pm.currentTransaction().begin();
0954: pm.makePersistent(p);
0955: Object oid = pm.getObjectId(p);
0956: pm.currentTransaction().commit();
0957:
0958: try {
0959: pm.currentTransaction().begin();
0960: Extent ext = pm.getExtent(Person.class, false);
0961: Iterator it = ext.iterator(); // should only have one Person object
0962: assertTrue(it.hasNext());
0963:
0964: p = (Person) it.next();
0965: assertTrue(!it.hasNext());
0966: assertEquals(oid, pm.getObjectId(p));
0967: } finally {
0968: if (pm.currentTransaction().isActive())
0969: pm.currentTransaction().rollback();
0970: }
0971: }
0972:
0973: /**
0974: * Test that FCOs added to a Collection field are persisted when the owning
0975: * PC is persisted.
0976: */
0977: public void testNormalFCOCollectionFieldPersistence1() {
0978: PersistenceManager pm = pmf.getPersistenceManager();
0979:
0980: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
0981: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
0982:
0983: Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1],
0984: EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
0985:
0986: try {
0987: pm.currentTransaction().begin();
0988: mgr.addSubordinate(emp1);
0989: pm.makePersistent(mgr);
0990: pm.currentTransaction().commit();
0991: } finally {
0992: if (pm.currentTransaction().isActive()) {
0993: pm.currentTransaction().rollback();
0994: pm.close();
0995: fail();
0996: }
0997:
0998: pm.close();
0999: }
1000:
1001: // get a fresh PM to ensure that any results aren't coming from the cache
1002: try {
1003: pm = pmf.getPersistenceManager();
1004: pm.currentTransaction().begin();
1005: Extent ext = pm.getExtent(
1006: com.triactive.jdo.test.Manager.class, false);
1007: java.util.Iterator it = ext.iterator();
1008:
1009: assertTrue(it.hasNext());
1010: mgr = (Manager) it.next();
1011: Collection c = mgr.getSubordinates();
1012: assertEquals(1, c.size());
1013:
1014: ext = pm.getExtent(com.triactive.jdo.test.Employee.class,
1015: false);
1016: it = ext.iterator();
1017: assertTrue(it.hasNext());
1018: Employee emp = (Employee) it.next();
1019:
1020: assertTrue(c.contains(emp));
1021: } finally {
1022: if (pm.currentTransaction().isActive())
1023: pm.currentTransaction().rollback();
1024:
1025: pm.close();
1026: }
1027: }
1028:
1029: /**
1030: * Test that deleting an object that is a member of a Collection field
1031: * throws an exception
1032: */
1033: public void testNormalFCOCollectionFieldPersistence2() {
1034: LOG.info("testNormalFCOCollectionFieldPersistence2 entry");
1035:
1036: /*
1037: * If constraints aren't being used then the assumptions of this test
1038: * don't hold.
1039: */
1040: if (!((PersistenceManagerFactoryImpl) pmf)
1041: .getValidateConstraints())
1042: return;
1043:
1044: PersistenceManager pm = pmf.getPersistenceManager();
1045: Transaction tx = pm.currentTransaction();
1046:
1047: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1048: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1049:
1050: Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1],
1051: EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
1052:
1053: try {
1054: tx.begin();
1055: mgr.addSubordinate(emp1);
1056: pm.makePersistent(mgr);
1057: tx.commit();
1058:
1059: try {
1060: tx.begin();
1061: pm.deletePersistent(emp1);
1062: tx.commit();
1063: fail("Removal of referenced object should have failed due to a constraint violation: "
1064: + emp1);
1065: } catch (JDODataStoreException e) {
1066: // Allow constraint violations to be considered either fatal or non-fatal
1067: assertTrue("Transaction should still be active", tx
1068: .isActive());
1069: } catch (JDOFatalDataStoreException e) {
1070: // Allow constraint violations to be considered either fatal or non-fatal
1071: }
1072: } finally {
1073: try {
1074: if (tx.isActive())
1075: tx.rollback();
1076:
1077: pm.close();
1078: } finally {
1079: LOG
1080: .info("testNormalFCOCollectionFieldPersistence2 exit");
1081: }
1082: }
1083: }
1084:
1085: /**
1086: * Test that removing a member of a normal Collection field does NOT delete
1087: * the object that was removed
1088: */
1089: public void testNormalFCOCollectionFieldPersistence3() {
1090: PersistenceManager pm = pmf.getPersistenceManager();
1091: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1092: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1093:
1094: Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1],
1095: EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
1096:
1097: try {
1098: mgr.addSubordinate(emp1);
1099: pm.currentTransaction().begin();
1100: pm.makePersistent(mgr);
1101: pm.currentTransaction().commit();
1102:
1103: pm.currentTransaction().begin();
1104: mgr.getSubordinates().remove(emp1);
1105: pm.currentTransaction().commit();
1106: } finally {
1107: if (pm.currentTransaction().isActive()) {
1108: pm.currentTransaction().rollback();
1109: pm.close();
1110: fail();
1111: }
1112:
1113: pm.close();
1114: }
1115:
1116: try {
1117: pm = pmf.getPersistenceManager();
1118:
1119: pm.currentTransaction().begin();
1120: Extent ex = pm.getExtent(
1121: com.triactive.jdo.test.Employee.class, false);
1122: java.util.Iterator it = ex.iterator();
1123: assertTrue(it.hasNext());
1124: Employee emp = (Employee) it.next();
1125:
1126: assertEquals(1, emp.getPersonNum());
1127: pm.currentTransaction().commit();
1128: } finally {
1129: if (pm.currentTransaction().isActive()) {
1130: pm.currentTransaction().rollback();
1131: pm.close();
1132: fail();
1133: }
1134:
1135: pm.close();
1136: }
1137: }
1138:
1139: /**
1140: * Test adding and removing elements from Collections whose members are
1141: * defined as a non-PC superclass or interface
1142: */
1143: public void testNormalFCOCollectionFieldPersistence4() {
1144: PersistenceManager pm = pmf.getPersistenceManager();
1145:
1146: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1147: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1148:
1149: Primitive p = new Primitive();
1150: setPrimitiveValues(p);
1151:
1152: CollectionFieldTester tester = new CollectionFieldTester();
1153:
1154: try {
1155: tester.getObjectCollection().add(mgr);
1156: tester.getInterfaceCollection().add(p);
1157: pm.currentTransaction().begin();
1158: pm.makePersistent(tester);
1159: pm.currentTransaction().commit();
1160: } finally {
1161: if (pm.currentTransaction().isActive()) {
1162: pm.currentTransaction().rollback();
1163: pm.close();
1164: fail();
1165: }
1166:
1167: pm.close();
1168: }
1169:
1170: try {
1171: pm = pmf.getPersistenceManager();
1172:
1173: pm.currentTransaction().begin();
1174: Extent ex = pm.getExtent(CollectionFieldTester.class, true);
1175: java.util.Iterator it = ex.iterator();
1176: assertTrue(it.hasNext());
1177: tester = (CollectionFieldTester) it.next();
1178:
1179: assertEquals(1, tester.getObjectCollection().size());
1180: mgr = (Manager) tester.getObjectCollection().iterator()
1181: .next();
1182: assertEquals(0, mgr.getPersonNum());
1183:
1184: assertEquals(1, tester.getInterfaceCollection().size());
1185:
1186: pm.currentTransaction().commit();
1187: } finally {
1188: if (pm.currentTransaction().isActive()) {
1189: pm.currentTransaction().rollback();
1190: pm.close();
1191: fail();
1192: }
1193:
1194: pm.close();
1195: }
1196: }
1197:
1198: /**
1199: * Test that FCOs added to a Collection field of an inverse Collection are
1200: * persisted when the owning PC is persisted. <BR>
1201: */
1202: public void testInverseFCOCollectionFieldPersistence1() {
1203: PersistenceManager pm = pmf.getPersistenceManager();
1204:
1205: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1206: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1207:
1208: try {
1209: Department d = new Department("Engineering");
1210: d.setManager(mgr);
1211:
1212: pm.currentTransaction().begin();
1213: mgr.addDepartment(d);
1214:
1215: pm.makePersistent(mgr);
1216: pm.currentTransaction().commit();
1217: } finally {
1218: if (pm.currentTransaction().isActive()) {
1219: pm.currentTransaction().rollback();
1220: pm.close();
1221: fail();
1222: }
1223:
1224: pm.close();
1225: }
1226:
1227: // get a fresh PM to ensure that any results aren't coming from the cache
1228: try {
1229: pm = pmf.getPersistenceManager();
1230: pm.currentTransaction().begin();
1231: Extent ext = pm.getExtent(
1232: com.triactive.jdo.test.Manager.class, false);
1233: java.util.Iterator it = ext.iterator();
1234:
1235: assertTrue(it.hasNext());
1236: mgr = (Manager) it.next();
1237: Collection c = mgr.getDepartments();
1238: assertEquals(1, c.size());
1239:
1240: ext = pm.getExtent(com.triactive.jdo.test.Department.class,
1241: false);
1242: it = ext.iterator();
1243: assertTrue(it.hasNext());
1244: Department d = (Department) it.next();
1245:
1246: assertTrue(c.contains(d));
1247: } finally {
1248: if (pm.currentTransaction().isActive())
1249: pm.currentTransaction().rollback();
1250:
1251: pm.close();
1252: }
1253: }
1254:
1255: /**
1256: * Test that when inverse FCOs are persisted, the owning PC is persisted
1257: * also
1258: */
1259: public void testInverseFCOCollectionFieldPersistence2() {
1260: PersistenceManager pm = pmf.getPersistenceManager();
1261:
1262: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1263: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1264:
1265: try {
1266: Department d = new Department("Engineering");
1267: d.setManager(mgr);
1268:
1269: pm.currentTransaction().begin();
1270: mgr.addDepartment(d);
1271:
1272: pm.makePersistent(d);
1273: pm.currentTransaction().commit();
1274: } finally {
1275: if (pm.currentTransaction().isActive()) {
1276: pm.currentTransaction().rollback();
1277: pm.close();
1278: fail();
1279: }
1280:
1281: pm.close();
1282: }
1283:
1284: // get a fresh PM to ensure that any results aren't coming from the cache
1285: try {
1286: pm = pmf.getPersistenceManager();
1287: pm.currentTransaction().begin();
1288: Extent ext = pm.getExtent(
1289: com.triactive.jdo.test.Manager.class, false);
1290: java.util.Iterator it = ext.iterator();
1291:
1292: assertTrue(it.hasNext());
1293: mgr = (Manager) it.next();
1294: Collection c = mgr.getDepartments();
1295: assertEquals(1, c.size());
1296:
1297: ext = pm.getExtent(com.triactive.jdo.test.Department.class,
1298: false);
1299: it = ext.iterator();
1300: assertTrue(it.hasNext());
1301: Department d = (Department) it.next();
1302:
1303: assertTrue(c.contains(d));
1304: pm.currentTransaction().commit();
1305: } finally {
1306: if (pm.currentTransaction().isActive())
1307: pm.currentTransaction().rollback();
1308:
1309: pm.close();
1310: }
1311: }
1312:
1313: /**
1314: * Test that deleting an object that is a member of an inverse Collection field
1315: * also removes it from the Collection
1316: */
1317: public void testInverseFCOCollectionFieldPersistence3() {
1318: PersistenceManager pm = pmf.getPersistenceManager();
1319:
1320: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1321: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1322:
1323: try {
1324: Department d = new Department("Engineering");
1325: d.setManager(mgr);
1326:
1327: pm.currentTransaction().begin();
1328: mgr.addDepartment(d);
1329:
1330: pm.makePersistent(d);
1331: pm.currentTransaction().commit();
1332:
1333: pm.currentTransaction().begin();
1334: pm.deletePersistent(d);
1335: pm.currentTransaction().commit();
1336: } finally {
1337: if (pm.currentTransaction().isActive()) {
1338: pm.currentTransaction().rollback();
1339: pm.close();
1340: fail();
1341: }
1342:
1343: pm.close();
1344: }
1345:
1346: // get a fresh PM to ensure that any results aren't coming from the cache
1347: try {
1348: pm = pmf.getPersistenceManager();
1349: pm.currentTransaction().begin();
1350: Extent ext = pm.getExtent(
1351: com.triactive.jdo.test.Manager.class, false);
1352: java.util.Iterator it = ext.iterator();
1353:
1354: assertTrue(it.hasNext());
1355: mgr = (Manager) it.next();
1356: Collection c = mgr.getDepartments();
1357: assertEquals(0, c.size());
1358:
1359: ext = pm.getExtent(com.triactive.jdo.test.Department.class,
1360: false);
1361: it = ext.iterator();
1362: assertTrue(!(it.hasNext()));
1363: pm.currentTransaction().commit();
1364: } finally {
1365: if (pm.currentTransaction().isActive())
1366: pm.currentTransaction().rollback();
1367:
1368: pm.close();
1369: }
1370: }
1371:
1372: /**
1373: * Test that setting the inverse reference of an object implicitly adds it
1374: * to the inverse collection of the "owning" object
1375: */
1376: public void testInverseFCOCollectionFieldPersistence4() {
1377: PersistenceManager pm = pmf.getPersistenceManager();
1378:
1379: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1380: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1381:
1382: try {
1383: Department d = new Department("Engineering");
1384: d.setManager(mgr);
1385:
1386: pm.currentTransaction().begin();
1387:
1388: pm.makePersistent(d);
1389: pm.currentTransaction().commit();
1390: } finally {
1391: if (pm.currentTransaction().isActive()) {
1392: pm.currentTransaction().rollback();
1393: pm.close();
1394: fail();
1395: }
1396:
1397: pm.close();
1398: }
1399:
1400: // get a fresh PM to ensure that any results aren't coming from the cache
1401: try {
1402: pm = pmf.getPersistenceManager();
1403: pm.currentTransaction().begin();
1404: Extent ext = pm.getExtent(
1405: com.triactive.jdo.test.Manager.class, false);
1406: java.util.Iterator it = ext.iterator();
1407:
1408: assertTrue(it.hasNext());
1409: mgr = (Manager) it.next();
1410: Collection c = mgr.getDepartments();
1411: assertEquals(1, c.size());
1412:
1413: ext = pm.getExtent(com.triactive.jdo.test.Department.class,
1414: false);
1415: it = ext.iterator();
1416: assertTrue(it.hasNext());
1417: Department d = (Department) it.next();
1418: assertTrue(c.contains(d));
1419: pm.currentTransaction().commit();
1420: } finally {
1421: if (pm.currentTransaction().isActive())
1422: pm.currentTransaction().rollback();
1423:
1424: pm.close();
1425: }
1426: }
1427:
1428: /**
1429: * Tests that the persistence manager used to persist an object is the same
1430: * as returned by jdoGetPersistenceManager()
1431: */
1432: public void testQueryPM() {
1433: PersistenceManager pm = pmf.getPersistenceManager();
1434:
1435: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1436: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1437:
1438: try {
1439: Department d = new Department("Engineering");
1440:
1441: pm.currentTransaction().begin();
1442: mgr.addDepartment(d);
1443: pm.makePersistent(mgr);
1444: pm.currentTransaction().commit();
1445: } finally {
1446: if (pm.currentTransaction().isActive()) {
1447: pm.currentTransaction().rollback();
1448: pm.close();
1449: fail();
1450: }
1451:
1452: pm.close();
1453: }
1454:
1455: try {
1456: pm = pmf.getPersistenceManager();
1457: pm.currentTransaction().begin();
1458: Extent ext = pm.getExtent(
1459: com.triactive.jdo.test.Manager.class, false);
1460: java.util.Iterator it = ext.iterator();
1461: mgr = (Manager) it.next();
1462:
1463: assertSame(pm, JDOHelper.getPersistenceManager(mgr));
1464: pm.currentTransaction().commit();
1465: } finally {
1466: if (pm.currentTransaction().isActive())
1467: pm.currentTransaction().rollback();
1468:
1469: pm.close();
1470: }
1471: }
1472:
1473: /**
1474: * Tests the InstanceCallback interface methods
1475: * @todo Add test for jdoPreClear()
1476: */
1477: public void testInstanceCallbacks() {
1478: PersistenceManager pm = pmf.getPersistenceManager();
1479: Transaction tx = pm.currentTransaction();
1480:
1481: InstanceCallbackTester tester = new InstanceCallbackTester();
1482: tester.setPersistentValue("value");
1483:
1484: try {
1485: tx.begin();
1486: pm.makePersistent(tester);
1487: tx.commit();
1488:
1489: Query q = pm.newQuery(pm.getExtent(
1490: InstanceCallbackTester.class, false));
1491: tx.begin();
1492:
1493: Collection c = (Collection) q.execute();
1494:
1495: try {
1496: assertEquals(1, c.size());
1497: tester = (InstanceCallbackTester) c.iterator().next();
1498:
1499: // test jdPreStore()
1500: assertNull(tester.getPersistentValue());
1501:
1502: // test jdoPostLoad()
1503: assertNotNull(tester.getTransientValue());
1504: assertEquals(InstanceCallbackTester.POST_LOAD_VALUE,
1505: tester.getTransientValue());
1506:
1507: // test jdoPreDelete()
1508: try {
1509: tester.setTransientValue(null);
1510: pm.deletePersistent(tester);
1511: fail();
1512: } catch (PreDeleteException e) {
1513: }
1514: } finally {
1515: q.closeAll();
1516: }
1517:
1518: tx.commit();
1519: } finally {
1520: if (tx.isActive())
1521: tx.rollback();
1522:
1523: pm.close();
1524: }
1525: }
1526:
1527: /**
1528: * This test was written as a result of a bug that was found. Test
1529: * persisting an object with a collection field, add objects to the
1530: * collection, make the owning object transient, and then persist it again.
1531: */
1532: public void testNormalCollectionFieldPersistence1() {
1533: PersistenceManager pm = pmf.getPersistenceManager();
1534: Transaction tx = pm.currentTransaction();
1535: tx.setRetainValues(true);
1536:
1537: CollectionFieldTester tester = new CollectionFieldTester();
1538:
1539: for (int i = 0; i < 3; i++) {
1540: Primitive p = new Primitive();
1541: setPrimitiveValues(p);
1542: tester.getPrimitiveCollection().add(p);
1543: }
1544:
1545: try {
1546: tx.begin();
1547: pm.makePersistent(tester);
1548: Object id1 = JDOHelper.getObjectId(tester);
1549: tx.commit();
1550:
1551: tx.begin();
1552: pm.makeTransient(tester);
1553: pm.makePersistent(tester);
1554: Object id2 = JDOHelper.getObjectId(tester);
1555: tx.commit();
1556:
1557: assertTrue("Object should have been given a new ID", !id1
1558: .equals(id2));
1559: } finally {
1560: if (tx.isActive())
1561: tx.rollback();
1562:
1563: pm.close();
1564: }
1565: }
1566:
1567: public static void main(String args[]) {
1568: junit.textui.TestRunner
1569: .run(PersistenceManagerImplFullTest.class);
1570: }
1571:
1572: /**
1573: * Invoked before each test is run
1574: */
1575: public void setUp() throws java.lang.Exception {
1576: super .setUp();
1577:
1578: if (!schemaInitialized) {
1579: addClassesToSchema(new Class[] { InversePrimitive.class,
1580: CollectionFieldTester.class,
1581: InstanceCallbackTester.class, Person.class,
1582: Manager.class, Primitive.class });
1583:
1584: schemaInitialized = true;
1585: }
1586:
1587: cleanup();
1588: }
1589:
1590: /**
1591: * Invoked after each test is run
1592: */
1593: public void tearDown() throws java.lang.Exception {
1594: super .tearDown();
1595: cleanup();
1596: }
1597:
1598: /**
1599: * Delete all data from all test tables
1600: */
1601: public void cleanup() {
1602: Extent ext = null;
1603: java.util.Iterator it = null;
1604: PersistenceManager pm = pmf.getPersistenceManager();
1605:
1606: try {
1607: // delete all InstanceCallbackTester objects
1608: pm.currentTransaction().begin();
1609:
1610: ext = pm.getExtent(InstanceCallbackTester.class, false);
1611: it = ext.iterator();
1612:
1613: while (it.hasNext()) {
1614: InstanceCallbackTester tester = (InstanceCallbackTester) it
1615: .next();
1616: tester.setTransientValue(""); // necesaary to avoid exception from jdoPreDelete() for this class only
1617: pm.deletePersistent(tester);
1618: }
1619:
1620: pm.currentTransaction().commit();
1621:
1622: // delete all InversePrimitive objects
1623: pm.currentTransaction().begin();
1624:
1625: ext = pm.getExtent(InversePrimitive.class, false);
1626: it = ext.iterator();
1627:
1628: while (it.hasNext()) {
1629: InversePrimitive ip = (InversePrimitive) it.next();
1630: pm.deletePersistent(ip);
1631: }
1632:
1633: pm.currentTransaction().commit();
1634:
1635: // delete all CollectionFieldTester objects
1636: pm.currentTransaction().begin();
1637:
1638: ext = pm.getExtent(CollectionFieldTester.class, false);
1639: it = ext.iterator();
1640:
1641: while (it.hasNext()) {
1642: CollectionFieldTester t = (CollectionFieldTester) it
1643: .next();
1644: t.getPrimitiveCollection().clear();
1645: pm.deletePersistent(t);
1646: }
1647:
1648: pm.currentTransaction().commit();
1649:
1650: // delete all Primative objects
1651: pm.currentTransaction().begin();
1652:
1653: ext = pm.getExtent(Primitive.class, false);
1654: it = ext.iterator();
1655:
1656: while (it.hasNext()) {
1657: Primitive p = (Primitive) it.next();
1658: pm.deletePersistent(p);
1659: }
1660:
1661: pm.currentTransaction().commit();
1662:
1663: // disassociate all Employees and Departments from their Managers
1664: pm.currentTransaction().begin();
1665:
1666: ext = pm.getExtent(com.triactive.jdo.test.Manager.class,
1667: false);
1668: it = ext.iterator();
1669:
1670: while (it.hasNext()) {
1671: Manager mgr = (Manager) it.next();
1672: mgr.getSubordinates().clear();
1673: mgr.getDepartments().clear();
1674: }
1675:
1676: pm.currentTransaction().commit();
1677:
1678: // delete all Employee objects
1679: pm.currentTransaction().begin();
1680:
1681: ext = pm.getExtent(Employee.class, false);
1682: it = ext.iterator();
1683:
1684: while (it.hasNext()) {
1685: Employee emp = (Employee) it.next();
1686: pm.deletePersistent(emp);
1687: }
1688:
1689: pm.currentTransaction().commit();
1690: pm.currentTransaction().begin();
1691:
1692: // dekete all Department objects
1693: ext = pm.getExtent(Department.class, false);
1694: it = ext.iterator();
1695:
1696: while (it.hasNext()) {
1697: Department d = (Department) it.next();
1698: pm.deletePersistent(d);
1699: }
1700:
1701: pm.currentTransaction().commit();
1702:
1703: // delete all Manager objects
1704: pm.currentTransaction().begin();
1705:
1706: ext = pm.getExtent(Manager.class, false);
1707: it = ext.iterator();
1708:
1709: while (it.hasNext()) {
1710: Manager mgr = (Manager) it.next();
1711: pm.deletePersistent(mgr);
1712: }
1713:
1714: pm.currentTransaction().commit();
1715:
1716: // delete all Person objects
1717: pm.currentTransaction().begin();
1718:
1719: ext = pm.getExtent(Person.class, true);
1720: it = ext.iterator();
1721:
1722: while (it.hasNext()) {
1723: Person person = (Person) it.next();
1724: pm.deletePersistent(person);
1725: }
1726:
1727: pm.currentTransaction().commit();
1728: } finally {
1729: if (pm.currentTransaction().isActive())
1730: pm.currentTransaction().commit();
1731:
1732: pm.close();
1733: }
1734: }
1735:
1736: /**
1737: * Tests that objects can be added to a Collection owned by a persistent
1738: * object made transient.
1739: */
1740: public void testTransientObjectCollections() {
1741: PersistenceManager pm = pmf.getPersistenceManager();
1742:
1743: Employee emp = new Employee(1, FIRSTNAME[1], LASTNAME[1],
1744: EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
1745:
1746: Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0],
1747: EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
1748:
1749: pm.currentTransaction().begin();
1750: pm.makePersistent(mgr);
1751: pm.currentTransaction().commit();
1752:
1753: pm.currentTransaction().begin();
1754: /* mgr is hollow, must retrieve fields before making transient. */
1755: pm.retrieve(mgr);
1756: pm.makeTransient(mgr);
1757: mgr.addSubordinate(emp);
1758:
1759: pm.currentTransaction().commit();
1760: }
1761:
1762: private Person createNewPerson(PersistenceManager pm, int dataset)
1763: throws Exception {
1764: boolean successful = false;
1765:
1766: Person p = new Person(dataset, FIRSTNAME[dataset],
1767: LASTNAME[dataset], EMAIL[dataset]);
1768: Transaction tx = pm.currentTransaction();
1769:
1770: try {
1771: tx.begin();
1772: pm.makePersistent(p);
1773:
1774: successful = true;
1775: } finally {
1776: if (successful)
1777: tx.commit();
1778: else
1779: tx.rollback();
1780: }
1781:
1782: return p;
1783: }
1784:
1785: private Person queryPerson(long personNum, PersistenceManager pm) {
1786: Transaction tx = pm.currentTransaction();
1787:
1788: try {
1789: tx.begin();
1790:
1791: Extent clnPerson = pm.getExtent(Person.class, true);
1792: Query q = pm.newQuery(clnPerson);
1793:
1794: try {
1795: Collection people = (Collection) q.execute();
1796:
1797: Iterator i = people.iterator();
1798:
1799: while (i.hasNext()) {
1800: Person p = (Person) i.next();
1801:
1802: if (p.getPersonNum() == personNum) {
1803: return p;
1804: }
1805: }
1806: } finally {
1807: q.closeAll();
1808: }
1809: } finally {
1810: tx.commit();
1811: }
1812:
1813: return null;
1814: }
1815:
1816: static void setPrimitiveValues(Primitive p, boolean b, byte y,
1817: char c, int i, short s, long l, float f, double d,
1818: String fstr, String nstr, String hstr, java.util.Date dt1,
1819: java.sql.Date dt2, java.sql.Timestamp tm) {
1820: p.setBoolean(b);
1821: p.setBooleanObject(new Boolean(b));
1822: p.setByte(y);
1823: p.setByteObject(new Byte(y));
1824: p.setChar(c);
1825: p.setCharObject(new Character(c));
1826: p.setInt(i);
1827: p.setIntObject(new Integer(i));
1828: p.setShort(s);
1829: p.setShortObject(new Short(s));
1830: p.setLong(l);
1831: p.setLongObject(new Long(l));
1832: p.setFloat(f);
1833: p.setFloatObject(new Float(f));
1834: p.setDouble(d);
1835: p.setDoubleObject(new Double(d));
1836: p.setFixedLengthString(fstr);
1837: p.setNormalString(nstr);
1838: p.setHugeString(hstr);
1839: p.setUtilDateField(dt1);
1840: p.setSqlDateField(dt2);
1841: p.setSqlTimestamp(tm);
1842: }
1843:
1844: /**
1845: * assign default values
1846: */
1847: private void setPrimitiveValues(Primitive p) {
1848: java.util.Date date1 = (new java.util.GregorianCalendar())
1849: .getTime();
1850: java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
1851: java.sql.Timestamp timestamp = java.sql.Timestamp
1852: .valueOf("2001-01-01 23:23:23.050500000");
1853:
1854: setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43,
1855: 123456789L, 123.456F, 123.456, "fixed", "normal",
1856: "huge", date1, date2, timestamp);
1857: }
1858:
1859: private void assertPrimitiveValues(Primitive p, boolean b, byte y,
1860: char c, int i, short s, long l, float f, double d,
1861: String fstr, String nstr, String hstr, java.util.Date dt1,
1862: java.sql.Date dt2, java.sql.Timestamp tm) {
1863: assertEquals(b, p.getBoolean());
1864: assertEquals(new Boolean(b), p.getBooleanObject());
1865: assertEquals(y, p.getByte());
1866: assertEquals(new Byte(y), p.getByteObject());
1867: assertEquals(c, p.getChar());
1868: assertEquals(new Character(c), p.getCharObject());
1869: assertEquals(i, p.getInt());
1870: assertEquals(new Integer(i), p.getIntObject());
1871: assertEquals(s, p.getShort());
1872: assertEquals(new Short(s), p.getShortObject());
1873: assertEquals(l, p.getLong());
1874: assertEquals(new Long(l), p.getLongObject());
1875: assertEquals(f, p.getFloat(), 0.0F);
1876: assertEquals(f, p.getFloatObject().floatValue(), 0.0F);
1877: assertEquals(d, p.getDouble(), 0.0);
1878: assertEquals(d, p.getDoubleObject().doubleValue(), 0.0);
1879: assertEquals(fstr, p.getFixedLengthString().trim());
1880: assertEquals(nstr, p.getNormalString());
1881: assertEquals(hstr, p.getHugeString());
1882: // assertEquals(bd, p.getBigDecimal());
1883: // assertEquals(bi, p.getBigInteger());
1884:
1885: // note: date storage is only accurate to the second - the rest is truncated
1886: assertEquals(dt1.getTime() / 1000, p.getUtilDateField()
1887: .getTime() / 1000);
1888: assertEquals(dt2.getTime(), p.getSqlDateField().getTime());
1889: assertEquals(tm.getTime() / 1000,
1890: p.getSqlTimestamp().getTime() / 1000);
1891: }
1892:
1893: private void assertState(Object o, boolean persistent,
1894: boolean transactional, boolean dirty, boolean isNew,
1895: boolean deleted) {
1896: assertEquals(deleted, JDOHelper.isDeleted(o));
1897: assertEquals(dirty, JDOHelper.isDirty(o));
1898: assertEquals(isNew, JDOHelper.isNew(o));
1899: assertEquals(persistent, JDOHelper.isPersistent(o));
1900: assertEquals(transactional, JDOHelper.isTransactional(o));
1901: }
1902:
1903: private void assertTransient(Object o) {
1904: assertNull(JDOHelper.getPersistenceManager(o));
1905: assertState(o, false, false, false, false, false);
1906: }
1907:
1908: private void assertPersistentNew(Object o) {
1909: assertNotNull(JDOHelper.getPersistenceManager(o));
1910: assertState(o, true, true, true, true, false);
1911: }
1912:
1913: private void assertPersistentNontransactional(Object o) {
1914: assertNotNull(JDOHelper.getPersistenceManager(o));
1915: assertState(o, true, false, false, false, false);
1916: }
1917:
1918: private void assertPersistentClean(Object o) {
1919: assertNotNull(JDOHelper.getPersistenceManager(o));
1920: assertState(o, true, true, false, false, false);
1921: }
1922:
1923: private void assertPersistentDirty(Object o) {
1924: assertNotNull(JDOHelper.getPersistenceManager(o));
1925: assertState(o, true, true, true, false, false);
1926: }
1927:
1928: private void assertHollow(Object o) {
1929: assertNotNull(JDOHelper.getPersistenceManager(o));
1930: assertState(o, true, false, false, false, false);
1931: }
1932:
1933: private void assertPersistentDeleted(Object o) {
1934: assertNotNull(JDOHelper.getPersistenceManager(o));
1935: assertState(o, true, true, true, false, true);
1936: }
1937:
1938: private void assertPersistentNewDeleted(Object o) {
1939: assertNotNull(JDOHelper.getPersistenceManager(o));
1940: assertState(o, true, true, true, true, true);
1941: }
1942: }
|