use of org.jpox.samples.one_many.bidir_3.Sky in project tests by datanucleus.
the class RelationshipTest method test1toNAbstractBaseUsingSubclassTable.
/**
* Test case for 1-N relations with base container/element using subclass-table and single subclass.
*/
public void test1toNAbstractBaseUsingSubclassTable() throws Exception {
try {
// prepare data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Sky s = new Sky();
Cloud cl1 = new Cloud();
cl1.setId(101);
Cloud cl2 = new Cloud();
cl2.setId(102);
s.addCloud(cl1);
s.addCloud(cl2);
cl1.setSky(s);
cl2.setSky(s);
pm.makePersistent(s);
tx.commit();
id = JDOHelper.getObjectId(s);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Sky s = (Sky) pm.getObjectById(id);
List<Cloud> clouds = s.getClouds();
assertEquals("Number of clouds in sky was wrong", 2, clouds.size());
Cloud cl1 = clouds.get(0);
assertEquals("First cloud is wrong", 101, cl1.getId().longValue());
Cloud cl2 = clouds.get(1);
assertEquals("Second cloud is wrong", 102, cl2.getId().longValue());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// getExtent of Sky
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Sky.class);
Iterator iter = ex.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertEquals("Number of sky objects is incorrect from Extent", 1, size);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// getExtent of Cloud
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Cloud.class);
Iterator iter = ex.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertEquals("Number of clouds is incorrect from Extent", 2, size);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
for (Iterator iter = ((Collection) pm.newQuery(Sky.class).execute()).iterator(); iter.hasNext(); ) {
Sky sky = (Sky) iter.next();
Iterator<Cloud> clIter = sky.getClouds().iterator();
while (clIter.hasNext()) {
Cloud cl = clIter.next();
cl.setSky(null);
sky.removeCloud(cl);
}
}
tx.commit();
pm.close();
clean(Sky.class);
clean(Cloud.class);
}
}
Aggregations