Search in sources :

Example 1 with PCJoinElement

use of org.jpox.samples.one_many.collection.PCJoinElement in project tests by datanucleus.

the class RelationshipTest method test1toNUnidirListSharedJoin.

/**
 * Test case for 1-N unidirectional relationships using a shared join table (List).
 */
public void test1toNUnidirListSharedJoin() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Create sample data
        Object holderId = null;
        try {
            tx.begin();
            ListHolder holder = new ListHolder();
            PCJoinElement elem1 = new PCJoinElement("First");
            PCJoinElement elem2 = new PCJoinElement("Second");
            PCJoinElement elem3 = new PCJoinElement("Third");
            holder.getJoinListPCShared1().add(elem1);
            holder.getJoinListPCShared1().add(elem3);
            holder.getJoinListPCShared2().add(elem2);
            pm.makePersistent(holder);
            holderId = JDOHelper.getObjectId(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while creating data for 1-N unidirectional List with shared join table : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the record and check the data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ListHolder holder = (ListHolder) pm.getObjectById(holderId);
            List coll1 = holder.getJoinListPCShared1();
            List coll2 = holder.getJoinListPCShared2();
            assertTrue("Collection 1 should have elements but is null!", coll1 != null);
            assertEquals("Collection 1 has incorrect number of elements", coll1.size(), 2);
            assertTrue("Collection 2 should have elements but is null!", coll2 != null);
            assertEquals("Collection 2 has incorrect number of elements", coll2.size(), 1);
            boolean hasElem1 = false;
            boolean hasElem2 = false;
            boolean hasElem3 = false;
            Iterator iter1 = coll1.iterator();
            while (iter1.hasNext()) {
                PCJoinElement elem = (PCJoinElement) iter1.next();
                if (elem.getName().equals("First")) {
                    hasElem1 = true;
                } else if (elem.getName().equals("Third")) {
                    hasElem3 = true;
                }
            }
            Iterator iter2 = coll2.iterator();
            while (iter2.hasNext()) {
                PCJoinElement elem = (PCJoinElement) iter2.next();
                if (elem.getName().equals("Second")) {
                    hasElem2 = true;
                }
            }
            assertTrue("Collection 1 is missing element 1", hasElem1);
            assertTrue("Collection 1 is missing element 3", hasElem3);
            assertTrue("Collection 2 is missing element 2", hasElem2);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while interrogating data for 1-N unidirectional List with shared join table : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ListHolder.class);
        clean(PCJoinElement.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) PCJoinElement(org.jpox.samples.one_many.collection.PCJoinElement) ListHolder(org.jpox.samples.one_many.collection.ListHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 2 with PCJoinElement

use of org.jpox.samples.one_many.collection.PCJoinElement in project tests by datanucleus.

the class RelationshipTest method test1toNUnidirSetSharedJoin.

/**
 * Test case for 1-N unidirectional relationships using a shared join table (Set).
 */
public void test1toNUnidirSetSharedJoin() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Create sample data
        Object holderId = null;
        try {
            tx.begin();
            SetHolder holder = new SetHolder();
            PCJoinElement elem1 = new PCJoinElement("First");
            PCJoinElement elem2 = new PCJoinElement("Second");
            PCJoinElement elem3 = new PCJoinElement("Third");
            holder.getJoinSetPCShared1().add(elem1);
            holder.getJoinSetPCShared1().add(elem3);
            holder.getJoinSetPCShared2().add(elem2);
            pm.makePersistent(holder);
            holderId = JDOHelper.getObjectId(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while creating data for 1-N unidirectional Set with shared join table : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the record and check the data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SetHolder holder = (SetHolder) pm.getObjectById(holderId);
            Set coll1 = holder.getJoinSetPCShared1();
            Set coll2 = holder.getJoinSetPCShared2();
            assertTrue("Collection 1 should have elements but is null!", coll1 != null);
            assertEquals("Collection 1 has incorrect number of elements", coll1.size(), 2);
            assertTrue("Collection 2 should have elements but is null!", coll2 != null);
            assertEquals("Collection 2 has incorrect number of elements", coll2.size(), 1);
            boolean hasElem1 = false;
            boolean hasElem2 = false;
            boolean hasElem3 = false;
            Iterator iter1 = coll1.iterator();
            while (iter1.hasNext()) {
                PCJoinElement elem = (PCJoinElement) iter1.next();
                if (elem.getName().equals("First")) {
                    hasElem1 = true;
                } else if (elem.getName().equals("Third")) {
                    hasElem3 = true;
                }
            }
            Iterator iter2 = coll2.iterator();
            while (iter2.hasNext()) {
                PCJoinElement elem = (PCJoinElement) iter2.next();
                if (elem.getName().equals("Second")) {
                    hasElem2 = true;
                }
            }
            assertTrue("Collection 1 is missing element 1", hasElem1);
            assertTrue("Collection 1 is missing element 3", hasElem3);
            assertTrue("Collection 2 is missing element 2", hasElem2);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while interrogating data for 1-N unidirectional Set with shared join table : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(SetHolder.class);
        clean(PCJoinElement.class);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) SetHolder(org.jpox.samples.one_many.collection.SetHolder) PCJoinElement(org.jpox.samples.one_many.collection.PCJoinElement) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 3 with PCJoinElement

use of org.jpox.samples.one_many.collection.PCJoinElement in project tests by datanucleus.

the class SerializationTest method testSerialisedCollectionElements.

/**
 * Test for serialisation of collection elements.
 */
public void testSerialisedCollectionElements() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    try {
        Object holderId = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Persist the object with serialised fields
        try {
            tx.begin();
            SetHolder holder = new SetHolder("Holder(3)");
            PCJoinElement elem = new PCJoinElement("Element 1");
            holder.getJoinSetPCSerial().add(elem);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while persisted object with serialised collection elements field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SetHolder holder = (SetHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised collection elements could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(3)"));
            assertEquals("Number of serialised elements is incorrect", holder.getJoinSetPCSerial().size(), 1);
            PCJoinElement elem = holder.getJoinSetPCSerial().iterator().next();
            assertEquals("Serialised collection element has incorrect description", elem.getName(), "Element 1");
            // Add 2 new elements and remove original
            holder.getJoinSetPCSerial().clear();
            holder.getJoinSetPCSerial().add(new PCJoinElement("Element 2"));
            holder.getJoinSetPCSerial().add(new PCJoinElement("Element 3"));
            holder.setName("Holder(4)");
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised collection elements : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object again to check the most recent update
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SetHolder holder = (SetHolder) pm.getObjectById(holderId);
            assertTrue("Holder of serialised collection elements could not be retrieved!", holder != null);
            assertTrue("Holder name is incorrect", holder.getName().equals("Holder(4)"));
            assertEquals("Number of serialised elements is incorrect", holder.getJoinSetPCSerial().size(), 2);
            Iterator elementsIter = holder.getJoinSetPCSerial().iterator();
            boolean containsElem2 = false;
            boolean containsElem3 = false;
            while (elementsIter.hasNext()) {
                PCJoinElement elem = (PCJoinElement) elementsIter.next();
                if (elem.getName().equals("Element 2")) {
                    containsElem2 = true;
                } else if (elem.getName().equals("Element 3")) {
                    containsElem3 = true;
                }
            }
            assertTrue("Element 2 is missing from collection with serialised elements", containsElem2);
            assertTrue("Element 3 is missing from collection with serialised elements", containsElem3);
            tx.commit();
        } catch (Exception e) {
            LOG.error(">> Exception thrown in test", e);
            fail("Exception thrown while retrieving object with serialised collection elements field : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data
        clean(SetHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) SerialisedObject(org.datanucleus.samples.serialised.SerialisedObject) SetHolder(org.jpox.samples.one_many.collection.SetHolder) PCJoinElement(org.jpox.samples.one_many.collection.PCJoinElement) IOException(java.io.IOException)

Aggregations

Iterator (java.util.Iterator)3 PersistenceManager (javax.jdo.PersistenceManager)3 Transaction (javax.jdo.Transaction)3 PCJoinElement (org.jpox.samples.one_many.collection.PCJoinElement)3 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)2 SetHolder (org.jpox.samples.one_many.collection.SetHolder)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 SerialisedObject (org.datanucleus.samples.serialised.SerialisedObject)1 ListHolder (org.jpox.samples.one_many.collection.ListHolder)1