Search in sources :

Example 1 with ListHolder

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

the class RelationshipTest method test1toNUnidirFKListToSelf.

/**
 * Test case for 1-N inverse unidirectional, using a List.
 */
public void test1toNUnidirFKListToSelf() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holder1Id = null;
        Object holder2Id = null;
        Object holder3Id = null;
        // Check the persistence of owner and elements
        try {
            tx.begin();
            ListHolder holder1 = new ListHolder();
            ListHolder holder2 = new ListHolder();
            ListHolder holder3 = new ListHolder();
            holder1.getFkListPC2().add(holder2);
            holder1.getFkListPC2().add(holder3);
            pm.makePersistent(holder1);
            tx.commit();
            holder1Id = pm.getObjectId(holder1);
            holder2Id = pm.getObjectId(holder2);
            holder3Id = pm.getObjectId(holder3);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while creating 1-N unidir FK self-referring relationships (List) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the retrieval of the owner and elements
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ListHolder holder = (ListHolder) pm.getObjectById(holder1Id);
            assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship (List)", holder);
            Collection innerHolders = holder.getFkListPC2();
            assertEquals("Number of elements in 1-N unidir FK relationship (List) is incorrect", 2, innerHolders.size());
            Iterator iter = innerHolders.iterator();
            int i = 0;
            while (iter.hasNext()) {
                ListHolder innerHolder = (ListHolder) iter.next();
                if (i == 0) {
                    assertEquals("The first element in the List is wrong", holder2Id, JDOHelper.getObjectId(innerHolder));
                } else if (i == 1) {
                    assertEquals("The second element in the List is wrong", holder3Id, JDOHelper.getObjectId(innerHolder));
                }
                i++;
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while querying 1-N unidir FK relationships (List) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        // Clean out our data
        clean(ListHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) ListHolder(org.jpox.samples.one_many.collection.ListHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 2 with ListHolder

use of org.jpox.samples.one_many.collection.ListHolder 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 3 with ListHolder

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

the class RelationshipTest method test1toNUnidirOrderedListDetachAttach.

/**
 * Test case for 1-N unidirectional relationships using an ordered List with detachment.
 */
public void test1toNUnidirOrderedListDetachAttach() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        // Create sample data and detach it
        Object holderId = null;
        ListHolder detachedHolder = null;
        try {
            tx.begin();
            ListHolder holder = new ListHolder();
            PCFKListElement elem1 = new PCFKListElement("First");
            PCFKListElement elem2 = new PCFKListElement("Middle");
            PCFKListElement elem3 = new PCFKListElement("Last");
            holder.getFkListPCOrdered().add(elem1);
            holder.getFkListPCOrdered().add(elem2);
            holder.getFkListPCOrdered().add(elem3);
            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 ordered List : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve holder and detach it
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.getFetchPlan().setMaxFetchDepth(-1);
            pm.getFetchPlan().setGroup(FetchPlan.ALL);
            ListHolder holder = (ListHolder) pm.getObjectById(holderId);
            detachedHolder = (ListHolder) pm.detachCopy(holder);
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while detaching data for 1-N unidirectional ordered List : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Delete an element while detached
        assertEquals("Number of elements whilst detached is wrong", 3, detachedHolder.getFkListPCOrdered().size());
        detachedHolder.getFkListPCOrdered().remove(1);
        // Reattach
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(detachedHolder);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown during reattach", e);
            fail("Exception thrown while attaching data for 1-N unidirectional ordered List : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve and check it
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Retrieve the holder and check the ordered data
            ListHolder holder = (ListHolder) pm.getObjectById(holderId);
            List coll1 = holder.getFkListPCOrdered();
            assertTrue("Collection should have elements but is null!", coll1 != null);
            assertEquals("Collection has incorrect number of elements", coll1.size(), 2);
            PCFKListElement elem1 = (PCFKListElement) coll1.get(0);
            PCFKListElement elem2 = (PCFKListElement) coll1.get(1);
            assertEquals("First retrieved element (after remove) is incorrect", "First", elem1.getName());
            assertEquals("Second retrieved element (after remove) is incorrect", "Middle", elem2.getName());
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while interrogating data for 1-N unidirectional ordered List : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ListHolder.class);
        clean(PCFKListElement.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PCFKListElement(org.jpox.samples.one_many.collection.PCFKListElement) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) ArrayList(java.util.ArrayList) ListHolder(org.jpox.samples.one_many.collection.ListHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 4 with ListHolder

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

the class RelationshipTest method test1toNUnidirFKList.

/**
 * Test case for 1-N inverse unidirectional, using a List.
 */
public void test1toNUnidirFKList() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holderId = null;
        // Check the persistence of owner and elements
        try {
            tx.begin();
            // Create some data
            ListHolder holder = new ListHolder();
            PCFKListElement elem1 = new PCFKListElement("Element 1");
            PCFKListElement elem2 = new PCFKListElement("Element 2");
            holder.getFkListPC().add(elem1);
            holder.getFkListPC().add(elem2);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while creating 1-N unidir FK relationships (List) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        // Check the retrieval of the owner and elements
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ListHolder holder = (ListHolder) pm.getObjectById(holderId);
            assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship (List)", holder);
            Collection elements = holder.getFkListPC();
            assertNotNull("Elements in Holder 1-N unidir FK List is null", elements);
            assertEquals("Number of elements in 1-N unidir FK relationship (List) is wrong", 2, elements.size());
            Iterator iter = elements.iterator();
            int i = 0;
            while (iter.hasNext()) {
                PCFKListElement elem = (PCFKListElement) iter.next();
                if (i == 0) {
                    assertEquals("The first element in the List is wrong", elem.getName(), "Element 1");
                } else if (i == 1) {
                    assertEquals("The second element in the List is wrong", elem.getName(), "Element 2");
                }
                i++;
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            fail("Exception thrown while querying 1-N unidir FK relationships (List) : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        pm.close();
    } finally {
        // Clean out our data
        clean(ListHolder.class);
        clean(PCFKListElement.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PCFKListElement(org.jpox.samples.one_many.collection.PCFKListElement) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Collection(java.util.Collection) ListHolder(org.jpox.samples.one_many.collection.ListHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 5 with ListHolder

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

the class PersistenceModelsTest method testClassWithOnlyContainerFields.

/**
 * Test if a class with only a container (Collection or List) fields works with
 * query, delete, getObjectById, persistent.
 */
public void testClassWithOnlyContainerFields() {
    try {
        ListHolder[] objs = new ListHolder[5];
        for (int i = 0; i < objs.length; i++) {
            objs[i] = new ListHolder(i + 1);
        }
        Object[] ids = new Object[objs.length];
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Persist the objects
            tx.begin();
            pm.makePersistentAll(objs);
            tx.commit();
            for (int i = 0; i < objs.length; i++) {
                ids[i] = pm.getObjectId(objs[i]);
            }
            // Retrieve an object (getObjectById)
            tx.begin();
            for (int i = 0; i < objs.length; i++) {
                assertEquals(JDOHelper.getObjectId(objs[i]).toString(), JDOHelper.getObjectId(pm.getObjectById(ids[i], true)).toString());
            }
            tx.commit();
            // Query the objects
            tx.begin();
            Collection c = (Collection) pm.newQuery(ListHolder.class).execute();
            assertEquals(c.size(), objs.length);
            tx.commit();
            // Delete an object
            ListHolder objToDel = new ListHolder(6);
            tx.begin();
            pm.makePersistent(objToDel);
            tx.commit();
            Object idToDel = pm.getObjectId(objToDel);
            tx.begin();
            pm.deletePersistent(pm.getObjectById(idToDel, true));
            tx.commit();
            tx.begin();
            boolean success = false;
            try {
                pm.getObjectById(idToDel, true);
            } catch (JDOObjectNotFoundException e) {
                success = true;
            }
            assertTrue("should have been raised exception", success);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ListHolder.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) ListHolder(org.jpox.samples.one_many.collection.ListHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)9 PersistenceManager (javax.jdo.PersistenceManager)9 Transaction (javax.jdo.Transaction)9 ListHolder (org.jpox.samples.one_many.collection.ListHolder)9 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Collection (java.util.Collection)4 Iterator (java.util.Iterator)4 PCFKListElement (org.jpox.samples.one_many.collection.PCFKListElement)3 PCFKListElementShared (org.jpox.samples.one_many.collection.PCFKListElementShared)1 PCFKListElementSub1 (org.jpox.samples.one_many.collection.PCFKListElementSub1)1 PCFKListElementSub2 (org.jpox.samples.one_many.collection.PCFKListElementSub2)1 PCJoinElement (org.jpox.samples.one_many.collection.PCJoinElement)1