use of org.jpox.samples.interfaces.Steak in project tests by datanucleus.
the class InterfacesTest method testMappingStrategyIdentity1To1Query.
/**
* Test for use of mapping-strategy="identity" for 1-1 relation, with a query taking in the string form of the id.
*/
public void testMappingStrategyIdentity1To1Query() throws Exception {
try {
addClassesToSchema(new Class[] { Diet.class });
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object steakId = null;
try {
// Create some objects
tx.begin();
Diet diet = new Diet(1);
Food fave = new Steak();
diet.setFavouriteFood(fave);
pm.makePersistent(diet);
tx.commit();
steakId = JDOHelper.getObjectId(fave);
} catch (JDOUserException ue) {
fail("Exception thrown during create of interface objects.");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Try a query
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Pass in a parameter using a String form of the id
Query q = pm.newQuery("SELECT FROM " + Diet.class.getName() + " WHERE favouriteFood == :param");
q.execute(Steak.class.getName() + ":" + steakId.toString());
// TODO Compare the results
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception thrown during query", ue);
fail("Exception thrown during query of objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Diet.class);
clean(Steak.class);
}
}
use of org.jpox.samples.interfaces.Steak in project tests by datanucleus.
the class InterfacesTest method testMappingStrategyIdentity1To1.
/**
* Test for use of mapping-strategy="identity" for 1-1 relation
*/
public void testMappingStrategyIdentity1To1() 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);
Food fave = new Steak();
diet.setFavouriteFood(fave);
pm.makePersistent(diet);
tx.commit();
id = JDOHelper.getObjectId(diet);
} catch (JDOUserException ue) {
fail("Exception thrown during create of interface objects.");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Diet diet = (Diet) pm.getObjectById(id);
Food fave = diet.getFavouriteFood();
assertNotNull("Favourite food is null!!", fave);
assertTrue("Favourite should be Steak!", fave instanceof Steak);
// Set to null
diet.setFavouriteFood(null);
tx.commit();
} catch (JDOUserException ue) {
fail("Exception thrown during retrieval of objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Diet diet = (Diet) pm.getObjectById(id);
Food fave = diet.getFavouriteFood();
assertNull("Favourite food is not null!!", fave);
tx.commit();
} catch (JDOUserException ue) {
fail("Exception thrown during retrieval of objects");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Diet.class);
clean(Steak.class);
}
}
use of org.jpox.samples.interfaces.Steak 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