Search in sources :

Example 11 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class AttachDetachTest method testFetchRecurse.

/**
 * Test for recursive relations in fetching (use of recursion-depth)
 */
public void testFetchRecurse() {
    try {
        Object dir1id = null;
        Object dir5id = null;
        Directory detachedDir = null;
        // Persist sample data
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Directory /usr
            Directory dir1 = new Directory("/usr");
            // Directory /usr/local
            Directory dir2 = new Directory("/local");
            dir2.setParent(dir1);
            dir1.addChild(dir2);
            // Directory /usr/local/audio
            Directory dir3 = new Directory("/audio");
            dir3.setParent(dir2);
            dir2.addChild(dir3);
            // Directory /usr/local/audio/mp3
            Directory dir4 = new Directory("/mp3");
            dir4.setParent(dir3);
            dir3.addChild(dir4);
            // Directory /usr/local/audio/flac
            Directory dir5 = new Directory("/flac");
            dir5.setParent(dir3);
            dir3.addChild(dir5);
            pm.makePersistent(dir1);
            tx.commit();
            dir1id = pm.getObjectId(dir1);
            dir5id = pm.getObjectId(dir5);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while creating sample data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Test 1 - detach and check the detach from the bottom directory ... checking the parents (unlimited recursion)
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Directory dir = (Directory) pm.getObjectById(dir5id);
            pm.getFetchPlan().addGroup("groupA");
            // Max big enough to not cause any limit
            pm.getFetchPlan().setMaxFetchDepth(5);
            detachedDir = (Directory) pm.detachCopy(dir);
            tx.commit();
        } catch (Exception e) {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
            fail("Exception thrown while detaching top-down recursive tree : " + e.getMessage());
        }
        try {
            // Check that all parents are detached
            Directory parent = detachedDir;
            while (parent != null) {
                parent = parent.getParent();
            }
        } catch (JDODetachedFieldAccessException dfae) {
            fail("Exception thrown while inspecting detached bottom-up recursive tree : " + dfae.getMessage());
        }
        // Test 2 - detach and check the detach from the main parent ... checking the children (limited recursion)
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Directory dir = (Directory) pm.getObjectById(dir1id);
            pm.getFetchPlan().addGroup("groupA");
            // Max big enough to not cause any limit
            pm.getFetchPlan().setMaxFetchDepth(5);
            detachedDir = (Directory) pm.detachCopy(dir);
            tx.commit();
        } catch (Exception e) {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
            fail("Exception thrown while detaching top-down recursive tree : " + e.getMessage());
        }
        try {
            // Check that we have the 1st level of children
            Set level1children = detachedDir.getChildren();
            Iterator level1childIter = level1children.iterator();
            while (level1childIter.hasNext()) {
                Directory child = (Directory) level1childIter.next();
                child.getParent();
                try {
                    // Check that we have the 2nd level of children
                    Set level2children = child.getChildren();
                    Iterator level2childIter = level2children.iterator();
                    while (level2childIter.hasNext()) {
                        Directory grandchild = (Directory) level2childIter.next();
                        try {
                            // Check that we dont have the 3rd level of children
                            Set level3children = grandchild.getChildren();
                            Iterator level3childIter = level3children.iterator();
                            while (level3childIter.hasNext()) {
                                Directory greatgrandchild = (Directory) level3childIter.next();
                                // Should throw exception
                                LOG.info(">> Detached 3rd level child " + greatgrandchild.getName());
                                fail("Managed to detach 3rd level of directories children - recursion should have stopped at 2 levels");
                            }
                        } catch (JDODetachedFieldAccessException e) {
                        // To be expected
                        }
                    }
                } catch (JDODetachedFieldAccessException e) {
                    LOG.error("Exception in test", e);
                    fail("One of the objects in a recursive relation that should have been detached hasn't! : " + e.getMessage());
                }
            }
        } catch (JDODetachedFieldAccessException e) {
            LOG.error("Exception in test", e);
            fail("One of the objects in a recursive relation that should have been detached hasn't! : " + e.getMessage());
        }
    } finally {
        // Clean out our data
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(Directory.class, false);
            Iterator it = ext.iterator();
            while (it.hasNext()) {
                Directory dir = (Directory) it.next();
                dir.setParent(null);
                dir.clearChildren();
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        clean(Directory.class);
    }
}
Also used : JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) Set(java.util.Set) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Directory(org.datanucleus.samples.detach.fetchdepth.Directory)

Example 12 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class CacheTest method clearEmployeeData.

protected void clearEmployeeData(PersistenceManagerFactory pmf) {
    Extent ext = null;
    java.util.Iterator it = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // disassociate all Employees and Departments from their Managers
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            mgr.clearSubordinates();
            mgr.clearDepartments();
        }
        tx.commit();
        // delete all Employee objects
        tx.begin();
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Employee emp = (Employee) it.next();
            pm.deletePersistent(emp);
        }
        tx.commit();
        // delete all Qualification objects
        tx.begin();
        ext = pm.getExtent(Qualification.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Qualification q = (Qualification) it.next();
            pm.deletePersistent(q);
        }
        tx.commit();
        // delete all Department objects
        tx.begin();
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Department d = (Department) it.next();
            pm.deletePersistent(d);
        }
        tx.commit();
        // delete all Manager objects
        tx.begin();
        ext = pm.getExtent(Manager.class, false);
        it = ext.iterator();
        while (it.hasNext()) {
            Manager mgr = (Manager) it.next();
            pm.deletePersistent(mgr);
        }
        tx.commit();
        // delete all Person objects
        tx.begin();
        ext = pm.getExtent(Person.class, true);
        it = ext.iterator();
        while (it.hasNext()) {
            Person person = (Person) it.next();
            pm.deletePersistent(person);
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Iterator(java.util.Iterator) Transaction(javax.jdo.Transaction) Extent(javax.jdo.Extent) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person)

Example 13 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class SchemaTest method testFixedDatastore.

/**
 * Test of the fixed datastore facility.
 * Should prevent all attempts to change tables in the datastore, yet allow insert/delete of rows.
 */
public void testFixedDatastore() {
    try {
        // Create the necessary table and create a few objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm.makePersistent(e);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create a PMF for our read-only schema
        Properties userProps = new Properties();
        userProps.setProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES, "false");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        assertFalse("The PMF should have had the AutoCreate property as false, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES));
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        // a). Try makePersistent
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245D");
            pm2.makePersistent(e);
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when trying makePersistent on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // b). Try deletePersistent
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                pm2.deletePersistent(e);
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when trying deletePersistent on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // c). Try update
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                e.setAge(21);
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should not have thrown an exception when modifying an object on fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // d). Try query
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Query q = pm2.newQuery(Employee.class);
            Collection results = (Collection) q.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                resultsIter.next();
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should have been able to access objects on a fixed datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        pm2.close();
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException)

Example 14 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class SchemaTest method testReadOnlyDatastore.

/**
 * Test of the Read-Only datastore facility.
 * Should prevent all attempts to write to the datastore.
 */
public void testReadOnlyDatastore() {
    try {
        // Create the necessary table and create a few objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Make sure our read-write PMF has schema for this class
            pm.getExtent(Developer.class);
            // Make sure our read-write PMF has schema for this class
            pm.getExtent(Manager.class);
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm.makePersistent(e);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Create a PMF for our read-only schema
        Properties userProps = new Properties();
        userProps.setProperty(PropertyNames.PROPERTY_DATASTORE_READONLY, "true");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        assertTrue("The PMF should have had the ReadOnlyDatastore property, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY));
        PersistenceManager pm2 = pmf2.getPersistenceManager();
        // a). Try makePersistent
        Transaction tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
            pm2.makePersistent(e);
            tx2.commit();
            assertTrue("Should have thrown an exception when trying makePersistent on ReadOnly datastore", false);
        } catch (Exception e) {
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // b). Try deletePersistent
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                pm2.deletePersistent(e);
            }
            tx2.commit();
            assertTrue("Should have thrown an exception when trying deletePersistent on ReadOnly datastore", false);
        } catch (Exception e) {
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // c). Try update
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Extent ex = pm2.getExtent(Employee.class, true);
            Iterator iter = ex.iterator();
            while (iter.hasNext()) {
                Employee e = (Employee) iter.next();
                e.setAge(23);
            }
            tx2.commit();
            assertTrue("Should have thrown an exception when modifying an object on ReadOnly datastore", false);
        } catch (Exception e) {
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        // d). Try query
        tx2 = pm2.currentTransaction();
        try {
            tx2.begin();
            Query q = pm2.newQuery(Employee.class);
            Collection results = (Collection) q.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                resultsIter.next();
            }
            tx2.commit();
        } catch (Exception e) {
            assertTrue("Should have been able to access objects on a ReadOnly datastore", false);
            LOG.error(e);
        } finally {
            if (tx2.isActive()) {
                tx2.rollback();
            }
        }
        pm2.close();
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Collection(java.util.Collection) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 15 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class PersistenceTest method testExtent.

public void testExtent() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ex = pm.getExtent(Person.class, false);
        Iterator it = ex.iterator();
        boolean personFound = false;
        boolean employeeFound = false;
        int number = 0;
        while (it.hasNext()) {
            Object obj = it.next();
            Object id = JDOHelper.getObjectId(obj);
            if (id.equals(id1)) {
                personFound = true;
            } else if (id.equals(id2)) {
                employeeFound = true;
            }
            number++;
        }
        assertEquals("Number of objects in Extent was wrong", 1, number);
        assertTrue("Should have found Person but didnt", personFound);
        assertFalse("Shouldnt have found Employee but did", employeeFound);
        tx.commit();
    } 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)

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