use of org.jpox.samples.interfaces.Cereal in project tests by datanucleus.
the class InterfacesTest method testMappingStrategyIdentity1ToN.
/**
* Test for use of mapping-strategy="identity" for 1-N relation
*/
public void testMappingStrategyIdentity1ToN() throws Exception {
try {
addClassesToSchema(new Class[] { Diet.class });
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some objects.
tx.begin();
Diet diet = new Diet(1);
Steak steak = new Steak();
Salad salad = new Salad();
diet.getFoods().add(steak);
diet.getFoods().add(salad);
pm.makePersistent(diet);
tx.commit();
id = JDOHelper.getObjectId(diet);
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of interface objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Diet diet = (Diet) pm.getObjectById(id);
assertNotNull("Diet foods is null!!", diet.getFoods());
assertEquals("Number of foods in diet is wrong", 2, diet.getFoods().size());
Set foods = diet.getFoods();
boolean steakFound = false;
boolean saladFound = false;
Iterator iter = foods.iterator();
while (iter.hasNext()) {
Food food = (Food) iter.next();
if (food instanceof Steak) {
steakFound = true;
}
if (food instanceof Salad) {
saladFound = true;
}
}
assertTrue("Steak was not retrieved as a food!", steakFound);
assertTrue("Salad was not retrieved as a food!", saladFound);
// Go on a healthy diet!
foods.clear();
foods.add(new Cereal());
foods.add(new Salad());
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of interface objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Diet diet = (Diet) pm.getObjectById(id);
assertNotNull("Diet foods is null!!", diet.getFoods());
assertEquals("Number of foods in diet is wrong", 2, diet.getFoods().size());
Set foods = diet.getFoods();
boolean cerealFound = false;
boolean saladFound = false;
Iterator iter = foods.iterator();
while (iter.hasNext()) {
Food food = (Food) iter.next();
if (food instanceof Cereal) {
cerealFound = true;
}
if (food instanceof Salad) {
saladFound = true;
}
}
assertTrue("Cereal was not retrieved as a food!", cerealFound);
assertTrue("Salad was not retrieved as a food!", saladFound);
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during create of interface objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Diet.class);
clean(Steak.class);
clean(Salad.class);
clean(Cereal.class);
}
}
Aggregations