Package org.xorm.tests

Source Code of org.xorm.tests.TestStateManagement

package org.xorm.tests;

import junit.framework.*;

import org.xorm.XORM;
import org.xorm.tests.model.*;
import javax.jdo.PersistenceManager;
import javax.jdo.JDOHelper;

public class TestStateManagement extends XORMTestCase {
    public void testTransient() {
        PersistenceManager mgr = factory.getPersistenceManager();
        StringInterface pc = (StringInterface) XORM.newInstance(mgr, StringInterface.class);
        assertTransient(pc);
        pc.setStringAttribute("x");
        assertTrue("IsDirty should be false",
                   !JDOHelper.isDirty(pc));
        mgr.close();
    }

    public void testTransientTransactional() {
        PersistenceManager mgr = factory.getPersistenceManager();
        StringInterface pc = (StringInterface) XORM.newInstance(mgr, StringInterface.class);
        mgr.makeTransactional(pc);
        assertNull("Object ID should be null",
                   JDOHelper.getObjectId(pc));
        assertNotNull("PersistenceManager should not be null",
                      JDOHelper.getPersistenceManager(pc));

        assertTrue("IsPersistent should be false",
                   !JDOHelper.isPersistent(pc));
        assertTrue("IsTransactional should be true",
                   JDOHelper.isTransactional(pc));
        assertTrue("IsNew should be false",
                   !JDOHelper.isNew(pc));
        assertTrue("IsDeleted should be false",
                   !JDOHelper.isDeleted(pc));

        pc.setStringAttribute("a");
        assertTrue("IsDirty should be false",
                   !JDOHelper.isDirty(pc));

        // Test commit and rollback
        mgr.currentTransaction().begin();
        assertTrue("Value from before transaction is retained",
                   pc.getStringAttribute().equals("a"));
        pc.setStringAttribute("b");
        assertTrue("Dirty should be true", JDOHelper.isDirty(pc));
        mgr.currentTransaction().commit();
        assertTrue("Value committed is retained",
                   pc.getStringAttribute().equals("b"));
        assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
        mgr.currentTransaction().begin();
        pc.setStringAttribute("c");
        mgr.currentTransaction().rollback();
        assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));
        assertTrue("Value should be rolled back",
                   pc.getStringAttribute().equals("b"));

        pc.setStringAttribute("d");
        assertTrue("Dirty should be false", !JDOHelper.isDirty(pc));

        mgr.close();
    }

    public void testPersistent() {
        PersistenceManager mgr = factory.getPersistenceManager();
        mgr.currentTransaction().begin();

        StringInterface pc = (StringInterface) XORM.newInstance(mgr, StringInterface.class);
        mgr.makePersistent(pc);
        assertNotNull("Object ID should not be null",
                      JDOHelper.getObjectId(pc));
        assertNotNull("PersistenceManager should not be null",
                      JDOHelper.getPersistenceManager(pc));

        assertTrue("IsPersistent should be true",
                   JDOHelper.isPersistent(pc));
        assertTrue("IsTransactional should be true",
                   JDOHelper.isTransactional(pc));
        assertTrue("IsNew should be true",
                   JDOHelper.isNew(pc));
        assertTrue("IsDeleted should be false",
                   !JDOHelper.isDeleted(pc));
        assertTrue("IsDirty should be true",
                   JDOHelper.isDirty(pc));
        pc.setStringAttribute("a");
        mgr.currentTransaction().rollback();

        // After rollback, should transition to TRANSIENT
        assertTrue("Value should be rolled back",
                   !"a".equals(pc.getStringAttribute()));
        assertTransient(pc);
    }

    /**
     * Asserts that an object fulfills all requirements
     * for being in the TRANSIENT state.
     */
    protected void assertTransient(Object pc) {
        assertNull("Object ID should be null",
                   JDOHelper.getObjectId(pc));
        assertNull("PersistenceManager should be null",
                   JDOHelper.getPersistenceManager(pc));

        assertTrue("IsPersistent should be false",
                   !JDOHelper.isPersistent(pc));
        assertTrue("IsTransactional should be false",
                   !JDOHelper.isTransactional(pc));
        assertTrue("IsNew should be false",
                   !JDOHelper.isNew(pc));
        assertTrue("IsDeleted should be false",
                   !JDOHelper.isDeleted(pc));
    }

    public static void main(String args[]) {
        String[] testCaseName = {TestStateManagement.class.getName()};
        junit.textui.TestRunner.main(testCaseName);
    }
     
    public static Test suite() {
        return new TestSuite(TestStateManagement.class);
    }
 
}
TOP

Related Classes of org.xorm.tests.TestStateManagement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.