Search in sources :

Example 1 with CompoundConcreteSub

use of org.jpox.samples.compoundidentity.CompoundConcreteSub in project tests by datanucleus.

the class CompoundIdentityTest method testOneToOneUniInheritance.

/**
 * Test of inheritance with compound identity (1-1 uni).
 */
public void testOneToOneUniInheritance() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        CompoundConcreteSub[] n = new CompoundConcreteSub[6];
        Object[] ids = new Object[6];
        try {
            tx.begin();
            CompoundRelated rel1 = new CompoundRelated("First Related");
            CompoundRelated rel2 = new CompoundRelated("Second Related");
            CompoundRelated rel3 = new CompoundRelated("Third Related");
            n[0] = new CompoundConcreteSub(rel3, "1", "1");
            n[1] = new CompoundConcreteSub(rel3, "2", "2");
            n[2] = new CompoundConcreteSub(rel2, "3", "3");
            n[3] = new CompoundConcreteSub(rel2, "4", "4");
            n[4] = new CompoundConcreteSub(rel1, "5", "5");
            n[5] = new CompoundConcreteSub(rel1, "6", "6");
            pm.makePersistentAll(n);
            tx.commit();
            for (int i = 0; i < 6; i++) {
                ids[i] = JDOHelper.getObjectId(n[i]);
            }
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            for (int i = 0; i < 6; i++) {
                CompoundConcreteSub sub = (CompoundConcreteSub) pm.getObjectById(ids[i]);
                if (i == 0 || i == 1) {
                    assertEquals("Name of related of target is incorrect", "Third Related", sub.getRelated().getName());
                }
                if (i == 2 || i == 3) {
                    assertEquals("Name of related of target is incorrect", "Second Related", sub.getRelated().getName());
                }
                if (i == 4 || i == 5) {
                    assertEquals("Name of related of target is incorrect", "First Related", sub.getRelated().getName());
                }
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error(e);
            e.printStackTrace();
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean up our data
        clean(CompoundConcreteSub.class);
        clean(CompoundRelated.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) CompoundRelated(org.jpox.samples.compoundidentity.CompoundRelated) CompoundConcreteSub(org.jpox.samples.compoundidentity.CompoundConcreteSub)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 CompoundConcreteSub (org.jpox.samples.compoundidentity.CompoundConcreteSub)1 CompoundRelated (org.jpox.samples.compoundidentity.CompoundRelated)1