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