Search in sources :

Example 1 with InstanceCallbackContainer

use of org.datanucleus.samples.instancecallback.InstanceCallbackContainer in project tests by datanucleus.

the class PersistenceManagerTest method tearDown.

/**
 * Invoked after each test is run
 */
public void tearDown() throws java.lang.Exception {
    super.tearDown();
    // TODO Remove all of this when each test cleans out its own data.
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    try {
        // delete all InstanceCallbackContainer objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(InstanceCallbackContainer.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
            pm.deletePersistent(owner);
        }
        pm.currentTransaction().commit();
        // delete all InstanceCallbackTester objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(InstanceCallbackTester.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
            // necesaary to avoid exception from jdoPreDelete() for this class only
            tester.setTransientValue("");
            pm.deletePersistent(tester);
        }
        pm.currentTransaction().commit();
        // delete all InversePrimitive objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            InversePrimitive ip = (InversePrimitive) it.next();
            ip.setTester(null);
            pm.deletePersistent(ip);
        }
        pm.currentTransaction().commit();
        // delete all CollectionFieldTester objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(CollectionFieldTester.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            CollectionFieldTester t = (CollectionFieldTester) it.next();
            if (t.getPrimitiveCollection() != null) {
                t.getPrimitiveCollection().clear();
            }
            if (t.getInversePrimitiveCollection() != null) {
                t.getInversePrimitiveCollection().clear();
            }
            pm.deletePersistent(t);
        }
        pm.currentTransaction().commit();
        // delete all Primative objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Primitive p = (Primitive) it.next();
            pm.deletePersistent(p);
        }
        pm.currentTransaction().commit();
        // disassociate all Employees and Departments from their Managers
        pm.currentTransaction().begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            if (mgr.getSubordinates() != null) {
                mgr.getSubordinates().clear();
            }
            if (mgr.getDepartments() != null) {
                mgr.getDepartments().clear();
            }
        }
        pm.currentTransaction().commit();
        // delete all Employee objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        // dekete all Department objects
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        pm.currentTransaction().commit();
        // delete all Manager objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        pm.currentTransaction().commit();
        // delete all Person objects
        pm.currentTransaction().begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().commit();
        pm.close();
    }
}
Also used : Iterator(java.util.Iterator) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Extent(javax.jdo.Extent) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackContainer(org.datanucleus.samples.instancecallback.InstanceCallbackContainer) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackTester(org.datanucleus.samples.instancecallback.InstanceCallbackTester) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Person(org.jpox.samples.models.company.Person)

Example 2 with InstanceCallbackContainer

use of org.datanucleus.samples.instancecallback.InstanceCallbackContainer in project tests by datanucleus.

the class PersistenceManagerTest method testInstanceCallbacksInSCO.

/**
 * Tests the InstanceCallback interface methods for elements in SCO fields
 */
public void testInstanceCallbacksInSCO() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    InstanceCallbackContainer owner = new InstanceCallbackContainer();
    InstanceCallbackTester tester;
    tester = new InstanceCallbackTester();
    tester.setPersistentValue("value Set 1");
    owner.addIcTesterToSet(tester);
    tester = new InstanceCallbackTester();
    tester.setPersistentValue("value Set 2");
    owner.addIcTesterToSet(tester);
    tester = new InstanceCallbackTester();
    tester.setPersistentValue("value Map 1");
    owner.addIcTesterToMap(tester);
    tester = new InstanceCallbackTester();
    tester.setPersistentValue("value Map 2");
    owner.addIcTesterToMap(tester);
    try {
        tx.begin();
        pm.makePersistent(owner);
        tx.commit();
        pm.close();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        // ////////////////////////////////////////
        // test jdoPostLoad with evicted instances
        // ////////////////////////////////////////
        tx.begin();
        Query q = pm.newQuery(pm.getExtent(InstanceCallbackContainer.class, false));
        Collection c = (Collection) q.execute();
        try {
            assertEquals(1, c.size());
            owner = (InstanceCallbackContainer) c.iterator().next();
            Iterator it = owner.getIcTesters().iterator();
            while (it.hasNext()) {
                tester = ((InstanceCallbackTester) it.next());
                assertNull(tester.getPersistentValue());
                assertNotNull(tester.getTransientValue());
                assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
            }
            Iterator itMap = owner.getIcTestersByPersistentValue().values().iterator();
            while (itMap.hasNext()) {
                tester = ((InstanceCallbackTester) itMap.next());
                assertNull(tester.getPersistentValue());
                assertNotNull(tester.getTransientValue());
                assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
            }
        } finally {
            q.closeAll();
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) InstanceCallbackContainer(org.datanucleus.samples.instancecallback.InstanceCallbackContainer) InstanceCallbackTester(org.datanucleus.samples.instancecallback.InstanceCallbackTester)

Aggregations

Iterator (java.util.Iterator)2 PersistenceManager (javax.jdo.PersistenceManager)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 InstanceCallbackContainer (org.datanucleus.samples.instancecallback.InstanceCallbackContainer)2 InstanceCallbackTester (org.datanucleus.samples.instancecallback.InstanceCallbackTester)2 Collection (java.util.Collection)1 Extent (javax.jdo.Extent)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 CollectionFieldTester (org.datanucleus.samples.widget.CollectionFieldTester)1 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)1 Primitive (org.datanucleus.samples.widget.Primitive)1 StoreManager (org.datanucleus.store.StoreManager)1 Department (org.jpox.samples.models.company.Department)1 Employee (org.jpox.samples.models.company.Employee)1 Manager (org.jpox.samples.models.company.Manager)1 Person (org.jpox.samples.models.company.Person)1