use of org.jpox.samples.one_many.bidir.DairyFarm in project tests by datanucleus.
the class JDOQLResultTest method testCountOnClassHierarchy.
/**
* Test of count(this) with inheritance using an Extent (inc subclasses).
*/
public void testCountOnClassHierarchy() {
try {
// Create objects
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create a base object
Farm baseobject = new Farm("base farm");
pm.makePersistent(baseobject);
// Create a sub object
DairyFarm subobject = new DairyFarm("dairy farm");
pm.makePersistent(subobject);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown during create of base and subclass objects for \"new-table\" strategy inheritance tree : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
Query q = pm.newQuery(pm.getExtent(Farm.class, true));
q.setResult("count(this)");
assertEquals(2, ((Long) q.execute()).longValue());
tx.commit();
} catch (Exception e) {
LOG.error("Exception during query", e);
fail("Exception thrown during count(this) query" + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Farm.class);
clean(DairyFarm.class);
}
}
use of org.jpox.samples.one_many.bidir.DairyFarm in project tests by datanucleus.
the class RelationshipTest method testDeletionWithInherited1toN.
public void testDeletionWithInherited1toN() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
DairyFarm farm = null;
Object farmId = null;
try {
tx.begin();
farm = new DairyFarm("Animal farm");
pm.makePersistent(farm);
farmId = JDOHelper.getObjectId(farm);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating Farm: " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
pm.deletePersistent(farm);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
try {
tx.begin();
assertNull(pm.getObjectById(farmId));
tx.commit();
fail("Expected JDOObjectNotFoundException thrown while retrieving farm");
} catch (JDOObjectNotFoundException e) {
// should come through here
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
} finally {
// Clean out our data
clean(DairyFarm.class);
}
}
use of org.jpox.samples.one_many.bidir.DairyFarm in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testPersistOneToManyBidi.
/**
* Test of persist of 1-N BIDIR relation (PC field).
*/
public void testPersistOneToManyBidi() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object dairyFarmId = null;
try {
tx.begin();
DairyFarm dairyFarm = new DairyFarm("theFarm");
Cattle cattle = new Cattle("theCattle");
cattle.setBreed("theBreedofCattle");
cattle.setFarm(dairyFarm);
dairyFarm.addAnimal(cattle);
Poultry poultry = new Poultry("thePoultry");
poultry.setLaysEggs(true);
poultry.setFarm(dairyFarm);
dairyFarm.addAnimal(poultry);
pm.makePersistent(dairyFarm);
tx.commit();
dairyFarmId = pm.getObjectId(dairyFarm);
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
DairyFarm theDairyFarm = (DairyFarm) pm.getObjectById(dairyFarmId);
assertEquals("DairyFarm name retrieved is incorrect", "theFarm", theDairyFarm.getName());
assertEquals("DairyFarm animals size retrieved is incorrect", 2, theDairyFarm.getAnimals().size());
Iterator iterator = theDairyFarm.getAnimals().iterator();
while (iterator.hasNext()) {
Animal animal = (Animal) iterator.next();
if (animal instanceof Cattle) {
Cattle cattle = (Cattle) animal;
assertEquals("Cattle name retrieved is incorrect", "theCattle", cattle.getName());
assertEquals("Cattle breed retrieved is incorrect", "theBreedofCattle", cattle.getBreed());
assertEquals("Cattle farm retrieved is incorrect", theDairyFarm, cattle.getFarm());
} else if (animal instanceof Poultry) {
Poultry poultry = (Poultry) animal;
assertEquals("Poultry name retrieved is incorrect", "thePoultry", poultry.getName());
assertEquals("Poultry layseggs retrieved is incorrect", true, poultry.getLaysEggs());
assertEquals("Poultry farm retrieved is incorrect", theDairyFarm, poultry.getFarm());
}
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check data using ALL fetch plan and infinite reach. Tests loading bidirs
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup(FetchPlan.ALL).setMaxFetchDepth(-1);
try {
tx.begin();
DairyFarm theDairyFarm = (DairyFarm) pm.getObjectById(dairyFarmId);
assertEquals("DairyFarm name retrieved is incorrect", "theFarm", theDairyFarm.getName());
assertEquals("DairyFarm animals size retrieved is incorrect", 2, theDairyFarm.getAnimals().size());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown retrieving data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
Extent ex = pm.getExtent(Farm.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
Farm f = (Farm) iter.next();
f.getAnimals().clear();
}
tx.commit();
clean(Farm.class);
clean(Animal.class);
}
}
Aggregations