use of javax.jdo.Extent 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 javax.jdo.Extent in project tests by datanucleus.
the class StorageTester method iterateUsingExtent.
protected void iterateUsingExtent(Class c) throws Exception {
// Iterate over them using an Extent and verify that they're all returned.
TestHelper.LOG.info("Iterating over " + TEST_OBJECT_COUNT + " " + c.getName() + " objects with an Extent");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent extent = pm.getExtent(c, true);
Iterator ei = extent.iterator();
try {
HashSet<TestObject> returned = new HashSet<TestObject>();
while (ei.hasNext()) {
TestObject obj = (TestObject) ei.next();
Assert.assertTrue("Object returned twice from Extent iterator: " + obj, returned.add(obj));
}
Assert.assertEquals(TEST_OBJECT_COUNT, returned.size());
for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
TestObject obj = (TestObject) pm.getObjectById(ids[i], true);
Assert.assertTrue("Object never returned from Extent iterator: " + obj, returned.remove(obj));
}
} finally {
extent.close(ei);
}
tx.commit();
} catch (Exception e) {
TestHelper.LOG.error("StorageTester.iterateUsingExtent exception thrown", e);
throw e;
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
Aggregations