001: package org.xorm.tests;
002:
003: import junit.framework.*;
004:
005: import org.xorm.XORM;
006: import org.xorm.tests.model.*;
007: import javax.jdo.PersistenceManager;
008: import javax.jdo.JDOHelper;
009:
010: public class TestJDOEnhanced extends XORMTestCase {
011: public void testTransient() {
012: PersistenceManager mgr = factory.getPersistenceManager();
013: JDOEnhanced pc = (JDOEnhanced) XORM.newInstance(mgr,
014: JDOEnhanced.class);
015: assertTransient(pc);
016: pc.setStr("x");
017: assertTrue("IsDirty should be false", !JDOHelper.isDirty(pc));
018: mgr.close();
019: }
020:
021: public void testTransientTransactional() {
022: PersistenceManager mgr = factory.getPersistenceManager();
023: JDOEnhanced pc = (JDOEnhanced) XORM.newInstance(mgr,
024: JDOEnhanced.class);
025: mgr.makeTransactional(pc);
026: assertNull("Object ID should be null", JDOHelper
027: .getObjectId(pc));
028: assertNotNull("PersistenceManager should not be null",
029: JDOHelper.getPersistenceManager(pc));
030:
031: assertTrue("IsPersistent should be false", !JDOHelper
032: .isPersistent(pc));
033: assertTrue("IsTransactional should be true", JDOHelper
034: .isTransactional(pc));
035: assertTrue("IsNew should be false", !JDOHelper.isNew(pc));
036: assertTrue("IsDeleted should be false", !JDOHelper
037: .isDeleted(pc));
038:
039: pc.setStr("a");
040: assertTrue("IsDirty should be false", !JDOHelper.isDirty(pc));
041:
042: // Test commit and rollback
043: mgr.currentTransaction().begin();
044: assertTrue("Value from before transaction is retained", pc
045: .getStr().equals("a"));
046: pc.setStr("b");
047: assertTrue("Dirty should be true", JDOHelper.isDirty(pc));
048: mgr.currentTransaction().commit();
049: assertTrue("Value committed is retained", pc.getStr().equals(
050: "b"));
051: assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
052: mgr.currentTransaction().begin();
053: pc.setStr("c");
054: mgr.currentTransaction().rollback();
055: assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
056: assertTrue("Value should be rolled back", pc.getStr().equals(
057: "b"));
058:
059: mgr.close();
060: }
061:
062: public void testPersistent() {
063: PersistenceManager mgr = factory.getPersistenceManager();
064: mgr.currentTransaction().begin();
065:
066: JDOEnhanced pc = (JDOEnhanced) XORM.newInstance(mgr,
067: JDOEnhanced.class);
068: mgr.makePersistent(pc);
069: assertNotNull("Object ID should not be null", JDOHelper
070: .getObjectId(pc));
071: assertNotNull("PersistenceManager should not be null",
072: JDOHelper.getPersistenceManager(pc));
073:
074: assertTrue("IsPersistent should be true", JDOHelper
075: .isPersistent(pc));
076: assertTrue("IsTransactional should be true", JDOHelper
077: .isTransactional(pc));
078: assertTrue("IsNew should be true", JDOHelper.isNew(pc));
079: assertTrue("IsDeleted should be false", !JDOHelper
080: .isDeleted(pc));
081: assertTrue("IsDirty should be true", JDOHelper.isDirty(pc));
082: pc.setStr("a");
083: mgr.currentTransaction().rollback();
084:
085: // After rollback, should transition to TRANSIENT
086: assertTrue("Value should be rolled back", !"a".equals(pc
087: .getStr()));
088: assertTransient(pc);
089: }
090:
091: /**
092: * Asserts that an object fulfills all requirements
093: * for being in the TRANSIENT state.
094: */
095: protected void assertTransient(Object pc) {
096: assertNull("Object ID should be null", JDOHelper
097: .getObjectId(pc));
098: assertNull("PersistenceManager should be null", JDOHelper
099: .getPersistenceManager(pc));
100:
101: assertTrue("IsPersistent should be false", !JDOHelper
102: .isPersistent(pc));
103: assertTrue("IsTransactional should be false", !JDOHelper
104: .isTransactional(pc));
105: assertTrue("IsNew should be false", !JDOHelper.isNew(pc));
106: assertTrue("IsDeleted should be false", !JDOHelper
107: .isDeleted(pc));
108: }
109:
110: public static void main(String args[]) {
111: String[] testCaseName = { TestJDOEnhanced.class.getName() };
112: junit.textui.TestRunner.main(testCaseName);
113: }
114:
115: public static Test suite() {
116: return new TestSuite(TestJDOEnhanced.class);
117: }
118:
119: }
|