use of org.jpox.samples.one_many.collection.PCFKListElementSub2 in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKListInheritanceTarget.
/**
* Test 1-N uni FK List relation having subclasses on the target side (1=source;N=target)
*/
public void test1toNUnidirFKListInheritanceTarget() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Persist some objects
try {
tx.begin();
ListHolder holder = new ListHolder();
PCFKListElementSub1 sub1 = new PCFKListElementSub1("Element 1");
PCFKListElementSub2 sub2 = new PCFKListElementSub2("Element 2");
holder.getFkListPC().add(sub1);
holder.getFkListPC().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();
ListHolder holder = (ListHolder) pm.getObjectById(holderId, true);
List elements = holder.getFkListPC();
assertEquals("Number of elements in 1-N unidir FK with inheritance is incorrect", 2, elements.size());
Object element = null;
element = elements.get(0);
assertTrue("First element of 1-N uni FK list is incorrect type", element instanceof PCFKListElementSub1);
element = elements.get(1);
assertTrue("Second element of 1-N uni FK list is incorrect type", element instanceof PCFKListElementSub2);
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(ListHolder.class);
clean(PCFKListElementSub1.class);
clean(PCFKListElementSub1.class);
}
}
Aggregations