Search in sources :

Example 1 with InversePrimitive

use of org.datanucleus.samples.widget.InversePrimitive 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 InversePrimitive

use of org.datanucleus.samples.widget.InversePrimitive in project tests by datanucleus.

the class PersistenceManagerTest method testMakeCollectionFieldsPersistent.

/**
 * Test that updates of collection and inverse collection fields update
 * the underlying collection
 */
public void testMakeCollectionFieldsPersistent() throws Exception {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    CollectionFieldTester collectionTester = new CollectionFieldTester();
    Transaction tx = pm.currentTransaction();
    try {
        // data for testing collections
        Person[] personArray = new Person[3];
        InversePrimitive[] inversePrimitiveArray = new InversePrimitive[3];
        personArray[0] = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
        personArray[1] = new Person(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1]);
        personArray[2] = new Person(2, FIRSTNAME[2], LASTNAME[2], EMAIL[2]);
        inversePrimitiveArray[0] = new InversePrimitive(collectionTester);
        inversePrimitiveArray[1] = new InversePrimitive(collectionTester);
        inversePrimitiveArray[2] = new InversePrimitive(collectionTester);
        tx.begin();
        pm.makePersistent(collectionTester);
        tx.commit();
        tx.begin();
        // set up Set field data
        {
            HashSet<Person> s = new HashSet<Person>(3);
            s.add(personArray[0]);
            s.add(personArray[1]);
            s.add(personArray[2]);
            collectionTester.setPersonSet(s);
        }
        // set up inverse element data
        {
            HashSet<InversePrimitive> s = new HashSet<InversePrimitive>(3);
            s.add(inversePrimitiveArray[0]);
            s.add(inversePrimitiveArray[1]);
            s.add(inversePrimitiveArray[2]);
            collectionTester.setInversePrimitiveCollection(s);
        }
        tx.commit();
        collectionTester = null;
        // test results
        tx.begin();
        {
            Extent clnTest = pm.getExtent(CollectionFieldTester.class, false);
            // should only have one Primitive object
            java.util.Iterator it = clnTest.iterator();
            collectionTester = (CollectionFieldTester) it.next();
        }
        assertNotNull(collectionTester);
        // test that Set data was stored
        {
            Set s = collectionTester.getPersonSet();
            assertTrue(s.contains(personArray[0]));
            assertTrue(s.contains(personArray[1]));
            assertTrue(s.contains(personArray[2]));
            assertEquals(3, s.size());
            Collection c = collectionTester.getInversePrimitiveCollection();
            assertTrue(c.contains(inversePrimitiveArray[0]));
            assertTrue(c.contains(inversePrimitiveArray[1]));
            assertTrue(c.contains(inversePrimitiveArray[2]));
            assertEquals(3, c.size());
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person) HashSet(java.util.HashSet)

Aggregations

Iterator (java.util.Iterator)2 Extent (javax.jdo.Extent)2 PersistenceManager (javax.jdo.PersistenceManager)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 CollectionFieldTester (org.datanucleus.samples.widget.CollectionFieldTester)2 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)2 Person (org.jpox.samples.models.company.Person)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Transaction (javax.jdo.Transaction)1 InstanceCallbackContainer (org.datanucleus.samples.instancecallback.InstanceCallbackContainer)1 InstanceCallbackTester (org.datanucleus.samples.instancecallback.InstanceCallbackTester)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