Search in sources :

Example 71 with Extent

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);
    }
}
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)

Example 72 with Extent

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();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5