Search in sources :

Example 1 with Cattle

use of org.jpox.samples.one_many.bidir.Cattle 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);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Cattle(org.jpox.samples.one_many.bidir.Cattle) Poultry(org.jpox.samples.one_many.bidir.Poultry) Extent(javax.jdo.Extent) Animal(org.jpox.samples.one_many.bidir.Animal) DairyFarm(org.jpox.samples.one_many.bidir.DairyFarm) Farm(org.jpox.samples.one_many.bidir.Farm) Iterator(java.util.Iterator) DairyFarm(org.jpox.samples.one_many.bidir.DairyFarm) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

Iterator (java.util.Iterator)1 Extent (javax.jdo.Extent)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 Animal (org.jpox.samples.one_many.bidir.Animal)1 Cattle (org.jpox.samples.one_many.bidir.Cattle)1 DairyFarm (org.jpox.samples.one_many.bidir.DairyFarm)1 Farm (org.jpox.samples.one_many.bidir.Farm)1 Poultry (org.jpox.samples.one_many.bidir.Poultry)1