Search in sources :

Example 1 with PCFKSetElementSub1

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

the class RelationshipTest method test1toNUnidirFKSetInheritanceTarget.

/**
 * Test 1-N uni FK Set relation having subclasses on the target side (1=source;N=target)
 */
public void test1toNUnidirFKSetInheritanceTarget() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object holderId = null;
        // Persist some objects
        try {
            tx.begin();
            SetHolder holder = new SetHolder("First");
            PCFKSetElementSub1 sub1 = new PCFKSetElementSub1("Element 1");
            PCFKSetElementSub2 sub2 = new PCFKSetElementSub2("Element 2");
            holder.getFkSetPC().add(sub1);
            holder.getFkSetPC().add(sub2);
            pm.makePersistent(holder);
            tx.commit();
            holderId = pm.getObjectId(holder);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception in test : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query the objects
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            SetHolder holder = (SetHolder) pm.getObjectById(holderId, true);
            Collection elements = holder.getFkSetPC();
            assertEquals("Number of elements in 1-N unidir FK with inheritance is incorrect", 2, elements.size());
            boolean containsElem1 = false;
            boolean containsElem2 = false;
            for (Iterator iter = elements.iterator(); iter.hasNext(); ) {
                Object element = iter.next();
                if (element instanceof PCFKSetElementSub1) {
                    containsElem1 = true;
                } else if (element instanceof PCFKSetElementSub2) {
                    containsElem2 = true;
                }
            }
            assertTrue(containsElem1);
            assertTrue(containsElem2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(SetHolder.class);
        clean(PCFKSetElementSub1.class);
        clean(PCFKSetElementSub1.class);
    }
}
Also used : PCFKSetElementSub1(org.jpox.samples.one_many.collection.PCFKSetElementSub1) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) PCFKSetElementSub2(org.jpox.samples.one_many.collection.PCFKSetElementSub2) Iterator(java.util.Iterator) Collection(java.util.Collection) SetHolder(org.jpox.samples.one_many.collection.SetHolder) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 PCFKSetElementSub1 (org.jpox.samples.one_many.collection.PCFKSetElementSub1)1 PCFKSetElementSub2 (org.jpox.samples.one_many.collection.PCFKSetElementSub2)1 SetHolder (org.jpox.samples.one_many.collection.SetHolder)1