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 TestStateManagement extends XORMTestCase {
011: public void testTransient() {
012: PersistenceManager mgr = factory.getPersistenceManager();
013: StringInterface pc = (StringInterface) XORM.newInstance(mgr,
014: StringInterface.class);
015: assertTransient(pc);
016: pc.setStringAttribute("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: StringInterface pc = (StringInterface) XORM.newInstance(mgr,
024: StringInterface.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.setStringAttribute("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: .getStringAttribute().equals("a"));
046: pc.setStringAttribute("b");
047: assertTrue("Dirty should be true", JDOHelper.isDirty(pc));
048: mgr.currentTransaction().commit();
049: assertTrue("Value committed is retained", pc
050: .getStringAttribute().equals("b"));
051: assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
052: mgr.currentTransaction().begin();
053: pc.setStringAttribute("c");
054: mgr.currentTransaction().rollback();
055: assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
056: assertTrue("Value should be rolled back", pc
057: .getStringAttribute().equals("b"));
058:
059: pc.setStringAttribute("d");
060: assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
061:
062: mgr.close();
063: }
064:
065: public void testPersistent() {
066: PersistenceManager mgr = factory.getPersistenceManager();
067: mgr.currentTransaction().begin();
068:
069: StringInterface pc = (StringInterface) XORM.newInstance(mgr,
070: StringInterface.class);
071: mgr.makePersistent(pc);
072: assertNotNull("Object ID should not be null", JDOHelper
073: .getObjectId(pc));
074: assertNotNull("PersistenceManager should not be null",
075: JDOHelper.getPersistenceManager(pc));
076:
077: assertTrue("IsPersistent should be true", JDOHelper
078: .isPersistent(pc));
079: assertTrue("IsTransactional should be true", JDOHelper
080: .isTransactional(pc));
081: assertTrue("IsNew should be true", JDOHelper.isNew(pc));
082: assertTrue("IsDeleted should be false", !JDOHelper
083: .isDeleted(pc));
084: assertTrue("IsDirty should be true", JDOHelper.isDirty(pc));
085: pc.setStringAttribute("a");
086: mgr.currentTransaction().rollback();
087:
088: // After rollback, should transition to TRANSIENT
089: assertTrue("Value should be rolled back", !"a".equals(pc
090: .getStringAttribute()));
091: assertTransient(pc);
092: }
093:
094: /**
095: * Asserts that an object fulfills all requirements
096: * for being in the TRANSIENT state.
097: */
098: protected void assertTransient(Object pc) {
099: assertNull("Object ID should be null", JDOHelper
100: .getObjectId(pc));
101: assertNull("PersistenceManager should be null", JDOHelper
102: .getPersistenceManager(pc));
103:
104: assertTrue("IsPersistent should be false", !JDOHelper
105: .isPersistent(pc));
106: assertTrue("IsTransactional should be false", !JDOHelper
107: .isTransactional(pc));
108: assertTrue("IsNew should be false", !JDOHelper.isNew(pc));
109: assertTrue("IsDeleted should be false", !JDOHelper
110: .isDeleted(pc));
111: }
112:
113: public static void main(String args[]) {
114: String[] testCaseName = { TestStateManagement.class.getName() };
115: junit.textui.TestRunner.main(testCaseName);
116: }
117:
118: public static Test suite() {
119: return new TestSuite(TestStateManagement.class);
120: }
121:
122: }
|