Package org.xorm.tests

Source Code of org.xorm.tests.TestJDOEnhanced

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 TestJDOEnhanced extends XORMTestCase {
    public void testTransient() {
  PersistenceManager mgr = factory.getPersistenceManager();
  JDOEnhanced pc = (JDOEnhanced) XORM.newInstance(mgr, JDOEnhanced.class);
  assertTransient(pc);
  pc.setStr("x");
  assertTrue("IsDirty should be false",
        !JDOHelper.isDirty(pc));
  mgr.close();
    }

    public void testTransientTransactional() {
  PersistenceManager mgr = factory.getPersistenceManager();
  JDOEnhanced pc = (JDOEnhanced) XORM.newInstance(mgr, JDOEnhanced.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.setStr("a");
  assertTrue("IsDirty should be false",
        !JDOHelper.isDirty(pc));

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

  mgr.close();
    }

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

  JDOEnhanced pc = (JDOEnhanced) XORM.newInstance(mgr, JDOEnhanced.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.setStr("a");
  mgr.currentTransaction().rollback();

  // After rollback, should transition to TRANSIENT
  assertTrue("Value should be rolled back",
       !"a".equals(pc.getStr()));
  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 = {TestJDOEnhanced.class.getName()};
        junit.textui.TestRunner.main(testCaseName);
    }
     
     public static Test suite() {
        return new TestSuite(TestJDOEnhanced.class);
    }
 
}
TOP

Related Classes of org.xorm.tests.TestJDOEnhanced

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.