use of org.jpox.samples.one_many.bidir.Farm in project tests by datanucleus.
the class AttachDetachTest method testAttachOneManyBidirFromNewManySide.
/**
* Test of persisting a new object that has a N-1 bidir relation with a detached object.
*/
public void testAttachOneManyBidirFromNewManySide() {
try {
Farm detachedFarm = null;
PersistenceManager pm = newPM();
((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
Transaction tx = pm.currentTransaction();
try {
// Persist the 1 side
tx.begin();
Farm farm = new Farm("Sunnybrook Farm");
pm.makePersistent(farm);
tx.commit();
detachedFarm = farm;
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Create the N side and link to the detached 1 side
Animal animal = new Animal("Porky");
animal.setFarm(detachedFarm);
detachedFarm.addAnimal(animal);
pm = newPM();
((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
tx = pm.currentTransaction();
try {
// Persist from the N side
tx.begin();
pm.makePersistent(animal);
tx.commit();
// Check the detached results
assertTrue("Animal should be detached but isnt", JDOHelper.isDetached(animal));
Farm farm = animal.getFarm();
assertNotNull("Animal should have its Farm field set but is null", farm);
assertTrue("Animal.farm should be detached but isnt", JDOHelper.isDetached(farm));
assertNotNull("Animal.farm.animals should be set but is null", farm.getAnimals());
assertEquals("Animal.farm.animals has incorrect number of animals", 1, farm.getAnimals().size());
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of org.jpox.samples.one_many.bidir.Farm in project tests by datanucleus.
the class AttachDetachTest method testCopyOnAttachFalseOneToManyBidir.
/**
* Test for use of CopyOnAttach=false with a 1-N bidir relation.
*/
public void testCopyOnAttachFalseOneToManyBidir() {
try {
Farm farm1 = null;
// Persist the owner object
PersistenceManager pm = newPM();
pm.setDetachAllOnCommit(true);
pm.setCopyOnAttach(false);
pm.getFetchPlan().setGroup(FetchPlan.ALL).setMaxFetchDepth(2);
Transaction tx = pm.currentTransaction();
try {
// Persist
tx.begin();
farm1 = new Farm("Sunnybrook Farm");
pm.makePersistent(farm1);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Update the detached owner with new elements
assertEquals("Owner object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(farm1));
Animal dog = new Animal("Patch");
Animal cat = new Animal("Tabby");
farm1.addAnimal(dog);
dog.setFarm(farm1);
farm1.addAnimal(cat);
cat.setFarm(farm1);
// Attach the owner object
pm = newPM();
pm.setDetachAllOnCommit(true);
pm.setCopyOnAttach(false);
pm.getFetchPlan().setGroup(FetchPlan.ALL).setMaxFetchDepth(2);
tx = pm.currentTransaction();
try {
tx.begin();
Farm farm2 = (Farm) pm.makePersistent(farm1);
pm.flush();
assertEquals("Attached owner is different object to detached, yet with CopyOnAttach=false should be same", farm1, farm2);
assertTrue("Farm should be persistent but isnt", JDOHelper.isPersistent(farm1));
assertTrue("Cat should be persistent but isnt", JDOHelper.isPersistent(cat));
assertTrue("Dog should be persistent but isnt", JDOHelper.isPersistent(dog));
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
assertEquals("Owner object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(farm1));
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of org.jpox.samples.one_many.bidir.Farm 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);
}
}
use of org.jpox.samples.one_many.bidir.Farm in project tests by datanucleus.
the class PersistenceTest method testPersistOneToManyBidir.
/**
* Test the persistence/retrieval of class with 1-N relation.
* In addition the collection field is marked as dependent-element, so delete of an element
* should delete the related object.
*/
public void testPersistOneToManyBidir() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object farm1Id = null;
Object farm2Id = null;
Object animal1Id = null;
Object animal2Id = null;
Object animal3Id = null;
try {
tx.begin();
Farm farm1 = new Farm("Giles Farm");
Animal cow = new Animal("Big Brown Cow");
Animal pig = new Animal("Dirty Pig");
farm1.addAnimal(cow);
cow.setFarm(farm1);
farm1.addAnimal(pig);
pig.setFarm(farm1);
Farm farm2 = new Farm("Brook Farm");
Animal dog = new Animal("Yapping Dog");
farm2.addAnimal(dog);
dog.setFarm(farm2);
pm.makePersistent(farm1);
pm.makePersistent(farm2);
tx.commit();
farm1Id = JDOHelper.getObjectId(farm1);
farm2Id = JDOHelper.getObjectId(farm2);
animal1Id = JDOHelper.getObjectId(cow);
animal2Id = JDOHelper.getObjectId(pig);
animal3Id = JDOHelper.getObjectId(dog);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Clear L2 cache so we force load from spreadsheet
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Farm farm1 = (Farm) pm.getObjectById(farm1Id);
assertEquals("Name of farm1 is incorrect", "Giles Farm", farm1.getName());
Set<Animal> farm1Animals = farm1.getAnimals();
assertEquals("Number of animals in farm1 is incorrect", 2, farm1Animals.size());
Farm farm2 = (Farm) pm.getObjectById(farm2Id);
assertEquals("Name of farm2 is incorrect", "Brook Farm", farm2.getName());
Set<Animal> farm2Animals = farm2.getAnimals();
assertEquals("Number of animals in farm2 is incorrect", 1, farm2Animals.size());
Animal cow = (Animal) pm.getObjectById(animal1Id);
assertEquals("Farm of cow is incorrect", farm1, cow.getFarm());
Animal pig = (Animal) pm.getObjectById(animal2Id);
assertEquals("Farm of pig is incorrect", farm1, pig.getFarm());
Animal dog = (Animal) pm.getObjectById(animal3Id);
assertEquals("Farm of dog is incorrect", farm2, dog.getFarm());
// Remove the cow, should get deleted
farm1.removeAnimal(cow);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Clear L2 cache so we force load from spreadsheet
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Farm farm1 = (Farm) pm.getObjectById(farm1Id);
assertEquals("Name of farm1 is incorrect", "Giles Farm", farm1.getName());
Set<Animal> farm1Animals = farm1.getAnimals();
assertEquals("Number of animals in farm1 is incorrect", 1, farm1Animals.size());
Farm farm2 = (Farm) pm.getObjectById(farm2Id);
assertEquals("Name of farm2 is incorrect", "Brook Farm", farm2.getName());
Set<Animal> farm2Animals = farm2.getAnimals();
assertEquals("Number of animals in farm2 is incorrect", 1, farm2Animals.size());
try {
pm.getObjectById(animal1Id);
fail("Cow should have been deleted, but still exists in datastore");
} catch (JDOObjectNotFoundException onfe) {
// Expected
}
Animal pig = (Animal) pm.getObjectById(animal2Id);
assertEquals("Farm of pig is incorrect", farm1, pig.getFarm());
Animal dog = (Animal) pm.getObjectById(animal3Id);
assertEquals("Farm of dog is incorrect", farm2, dog.getFarm());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Farm.class);
}
}
Aggregations