Zoo zoo = new Zoo(zooName);
int sum = NB_ANIMAL;
zoo.setaKangaroo(new Kangaroo("Franck", NB_ANIMAL, true, (long) 1, (float) 2.0));
for(int i=0; i<NB_ANIMAL; i++) {
Kangaroo ka = new Kangaroo("kang" + i, i, true, (long) 1, (float) 2.0);
Koala ko = new Koala("koala" + i, i, true, (long) 1, 2);
sum += 2*i;
zoo.getKangaroos().add(ka);
zoo.getAnimals().add(ka);
zoo.getAnimals().add(ko);
}
Koala olivier = new Koala("Olivier", NB_ANIMAL + 1, true, (long) 1, 20);
sum += NB_ANIMAL + 1;
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
pm.makePersistent(zoo);
pm.makePersistent(olivier);
zoo = null;
olivier = null;
pm.currentTransaction().commit();
pm.evictAll();
pm.close();
pm = pmf.getPersistenceManager();
pm.evictAll();
try {
zoo = (Zoo) pm.getObjectById(
pm.newObjectIdInstance(Zoo.class, zooName), false);
} catch (Exception e) {
fail("Zoo not found");
}
assertNotNull("Null zoo", zoo);
Collection animals = zoo.getAnimals();
for(Iterator it = animals.iterator(); it.hasNext(); ) {
Animal a = (Animal) it.next();
logger.log(BasicLevel.INFO, "Animal: " + a.getName());
}
Object a = zoo.getaKangaroo();
assertNotNull("Null AnAnimal", a);
assertTrue("Bad Animal type", a instanceof Kangaroo);
Kangaroo ko = (Kangaroo) a;
assertEquals("Bad AnAnimal", "Franck", ko.getName());
assertNull("Null AnAnimal", zoo.getAnAnimal());
try {
ko = (Kangaroo) pm.getObjectById(
new AnimalId("Franck", Kangaroo.KANGAROO_SPECIES),
false);
logger.log(BasicLevel.INFO, "ko: " + ko.getName());
} catch (Exception e) {
logger.log(BasicLevel.ERROR, "Error the kangaroo fetching:",
ExceptionHelper.getNested(e));
fail("Kangaroo not found");
}
try {
olivier = (Koala) pm.getObjectById(
new AnimalId("Olivier", "koala"),
false);
logger.log(BasicLevel.INFO, "koala: " + olivier.getName());
} catch (Exception e) {
logger.log(BasicLevel.ERROR, "Error the koala fetching:",
ExceptionHelper.getNested(e));
fail("koala not found");
}