use of org.jpox.samples.one_many.collection.PCFKListElementShared in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirListSharedFK.
/**
* Test case for 1-N unidirectional relationships using a shared foreign-key (List).
*/
public void test1toNUnidirListSharedFK() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data
Object holderId = null;
try {
tx.begin();
ListHolder holder = new ListHolder();
PCFKListElementShared elem1 = new PCFKListElementShared("First");
PCFKListElementShared elem2 = new PCFKListElementShared("Second");
PCFKListElementShared elem3 = new PCFKListElementShared("Third");
holder.getFkListPCShared1().add(elem1);
holder.getFkListPCShared1().add(elem3);
holder.getFkListPCShared2().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 foreign-key : " + 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.getFkListPCShared1();
List coll2 = holder.getFkListPCShared2();
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()) {
PCFKListElementShared elem = (PCFKListElementShared) iter1.next();
if (elem.getName().equals("First")) {
hasElem1 = true;
} else if (elem.getName().equals("Third")) {
hasElem3 = true;
}
}
Iterator iter2 = coll2.iterator();
while (iter2.hasNext()) {
PCFKListElementShared elem = (PCFKListElementShared) 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 foreign-key : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ListHolder.class);
clean(PCFKListElementShared.class);
}
}
Aggregations