Search in sources :

Example 1 with CollectionFieldTester

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

the class PersistenceManagerTest method testJoinTableCollectionFieldPersistence1.

/**
 * This test was written as a result of a bug that was found. Test
 * persisting an object with a collection field, add objects to the
 * collection, make the owning object transient, and then persist it again.
 */
public void testJoinTableCollectionFieldPersistence1() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    CollectionFieldTester tester = new CollectionFieldTester();
    for (int i = 0; i < 3; i++) {
        Primitive p = new Primitive();
        setPrimitiveValues(p);
        tester.getPrimitiveCollection().add(p);
    }
    try {
        pm.currentTransaction().begin();
        pm.makePersistent(tester);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        pm.makeTransient(tester);
        pm.makePersistent(tester);
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().rollback();
        pm.close();
    }
}
Also used : CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 2 with CollectionFieldTester

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

the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence4.

/**
 * Test adding and removing elements from Collections whose members are defined as a non-PC superclass or interface
 */
public void testNormalFCOCollectionFieldPersistence4() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    Primitive p = new Primitive();
    setPrimitiveValues(p);
    CollectionFieldTester tester = new CollectionFieldTester();
    try {
        tester.getObjectCollection().add(mgr);
        tx.begin();
        pm.makePersistent(tester);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown in test", e);
        fail("Exception in test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ex = pm.getExtent(CollectionFieldTester.class, true);
        java.util.Iterator it = ex.iterator();
        assertTrue(it.hasNext());
        tester = (CollectionFieldTester) it.next();
        assertEquals(1, tester.getObjectCollection().size());
        mgr = (Manager) tester.getObjectCollection().iterator().next();
        assertEquals(0, mgr.getPersonNum());
        tx.commit();
    } catch (Exception e) {
        LOG.error(">> Exception thrown in test", e);
        fail("Exception thrown in ObjectCollection test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
            pm.close();
            fail("Failed to persist object and commit transaction");
        }
        pm.close();
    }
}
Also used : InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 3 with CollectionFieldTester

use of org.datanucleus.samples.widget.CollectionFieldTester 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 4 with CollectionFieldTester

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

PersistenceManager (javax.jdo.PersistenceManager)4 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)4 CollectionFieldTester (org.datanucleus.samples.widget.CollectionFieldTester)4 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)4 Iterator (java.util.Iterator)3 Extent (javax.jdo.Extent)3 Primitive (org.datanucleus.samples.widget.Primitive)3 Transaction (javax.jdo.Transaction)2 StoreManager (org.datanucleus.store.StoreManager)2 Manager (org.jpox.samples.models.company.Manager)2 Person (org.jpox.samples.models.company.Person)2 SQLException (java.sql.SQLException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 JDOException (javax.jdo.JDOException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)1 JDOUserCallbackException (javax.jdo.JDOUserCallbackException)1 JDOUserException (javax.jdo.JDOUserException)1