Search in sources :

Example 96 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class AttachDetachTest method testDetachOnClose.

/**
 * Test "DetachOnClose" when we close the PM we detach all L1 cached objects.
 */
public void testDetachOnClose() {
    try {
        PersistenceManager pm = newPM();
        pm.getFetchPlan().addGroup("groupSubordinates");
        pm.getFetchPlan().addGroup("groupA");
        ((JDOPersistenceManager) pm).getExecutionContext().setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, true);
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
            Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
            Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
            donald.addSubordinate(woody);
            donald.addSubordinate(bugs);
            woody.setManager(donald);
            bugs.setManager(donald);
            pm.makePersistent(donald);
            tx.commit();
            // Close the PM and we should get our objects detached
            pm.close();
            // Check that all are now detached
            if (!JDOHelper.isDetached(bugs) || JDOHelper.getObjectId(bugs) == null) {
                fail("Bugs Bunny is not detached or hasn't been detached correctly after closing the PM");
            }
            if (!JDOHelper.isDetached(woody) || JDOHelper.getObjectId(woody) == null) {
                fail("Woody Woodpecker is not detached or hasn't been detached correctly after closing the PM");
            }
            if (!JDOHelper.isDetached(donald) || JDOHelper.getObjectId(donald) == null) {
                fail("Donald Duck is not detached or hasn't been detached correctly after closing the PM");
            }
            // Check that the relationships are intact
            if (!woody.getFirstName().equals("Woody") || !woody.getLastName().equals("Woodpecker")) {
                fail("Woody Woodpecker has lost his name after closing the PM");
            }
            if (woody.getManager() != donald) {
                fail("Woody Woodpecker has lost his relation to its Manager after closing the PM");
            }
            if (!bugs.getFirstName().equals("Bugs") || !bugs.getLastName().equals("Bunny")) {
                fail("Bugs Bunny has lost his name after closing the PM");
            }
            if (bugs.getManager() != donald) {
                fail("Bugs Bunny has lost his relation to its Manager after closing the PM");
            }
            if (!donald.getFirstName().equals("Donald") || !donald.getLastName().equals("Duck")) {
                fail("Donald Duck has lost his name after closing the PM");
            }
            if (donald.getSubordinates().size() != 2) {
                fail("Donald Duck has lost some or all of his employees after closing the PM");
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting objects and closing PM with detachOnClose : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 97 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class AttachDetachTest method testMaxFetchDepth.

/**
 * Test the specification of maximum fetch depth.
 */
public void testMaxFetchDepth() {
    try {
        Object e1Id = null;
        // Persist some data
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e1 = new Employee(1, "Yogi", "Bear", "yogi@warnerbros.com", 124, "10123");
            Employee e2 = new Employee(2, "Fred", "Flintstone", "fred.flintstone@hannabarbara.com", 167, "10019");
            Manager m1 = new Manager(3, "Wily", "Coyote", "wily.coyote@warnerbros.com", 202, "10067");
            Manager m2 = new Manager(4, "Mickey", "Mouse", "mickey.mouse@hollywood.com", 203, "10066");
            Manager m3 = new Manager(5, "Donald", "Duck", "donald.duck@hollywood.com", 204, "10065");
            m1.addSubordinate(e1);
            m1.addSubordinate(e2);
            e1.setManager(m1);
            e2.setManager(m1);
            m2.addSubordinate(m1);
            m1.setManager(m2);
            m3.addSubordinate(m2);
            m2.setManager(m3);
            Department d1 = new Department("Cartoon");
            m1.addDepartment(d1);
            d1.setManager(m1);
            // This should persist all objects
            pm.makePersistent(e1);
            tx.commit();
            e1Id = pm.getObjectId(e1);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting test data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve and detach some objects using default fetch-depth
        Employee e1Detached = null;
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e1 = (Employee) pm.getObjectById(e1Id);
            e1Detached = (Employee) pm.detachCopy(e1);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving/detaching test data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the detached objects (Employee.manager is in the DFG)
        try {
            // Basic fields of the detached object
            e1Detached.getSerialNo();
            e1Detached.getManager();
        } catch (JDODetachedFieldAccessException dfea) {
            fail("Detach of Employee has not detached its DFG fields! Should have been detached");
        }
        try {
            // Second level relation of the detached object
            e1Detached.getManager().getManager();
            fail("Detach of Employee has also detached Manager of the Manager! Should not have been detached since outside fetch-depth of 1");
        } catch (JDODetachedFieldAccessException ndfe) {
        // Expected
        }
        // Retrieve and detach some objects using extra level of fetch-depth
        pm = newPM();
        pm.getFetchPlan().setMaxFetchDepth(2);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e1 = (Employee) pm.getObjectById(e1Id);
            e1Detached = (Employee) pm.detachCopy(e1);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while retrieving/detaching test data : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the detached objects
        try {
            // Basic fields of the detached object
            e1Detached.getSerialNo();
            e1Detached.getManager();
        } catch (JDODetachedFieldAccessException dfea) {
            fail("Detach of Employee has not detached its DFG fields! Should have been detached");
        }
        try {
            // Basic fields of the detached object
            e1Detached.getManager().getManager();
        } catch (JDODetachedFieldAccessException dfea) {
            fail("Detach of Employee has not detached Manager of Manager! Should have been detached");
        }
        try {
            // Third level relation of the detached object
            e1Detached.getManager().getManager().getManager();
            fail("Detach of Employee has also detached Manager of the Manager of the Manager! Should not have been detached since outside fetch-depth of 1");
        } catch (JDODetachedFieldAccessException ndfe) {
        // Expected
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 98 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class AttachDetachTest method testDetachAttach_OneToMany_RelationConsistency.

/**
 * Test of detaching object and attaching relation between objects
 */
public void testDetachAttach_OneToMany_RelationConsistency() {
    try {
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            // Persist 1-N bidir relation
            Manager m = new Manager(1, "Homer", "Simpson", "homer@fox.com", 4, "serial 1");
            Department d = new Department("Nuclear");
            d.setManager(m);
            m.addDepartment(d);
            pm.makePersistent(m);
            pm.getFetchPlan().setMaxFetchDepth(2);
            Manager dm = (Manager) pm.detachCopy(m);
            Department dd = (Department) pm.detachCopy(d);
            dd.setManager(dm);
            Department ad = (Department) pm.makePersistent(dd);
            assertTrue(m.equals(ad.getManager()));
            tx.commit();
            tx.begin();
            pm.refresh(ad);
            assertTrue(m.equals(ad.getManager()));
        } catch (JDOUserException ue) {
            LOG.error(ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 99 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class AttachDetachTest method testCopyOnAttachFalseForUnchangedObject.

/**
 * Test "DetachAllOnCommit" when not "CopyOnAttach" on an unchanged object.
 */
public void testCopyOnAttachFalseForUnchangedObject() {
    try {
        Manager mgr = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
        PersistenceManager pm = newPM();
        pm.setDetachAllOnCommit(true);
        pm.setCopyOnAttach(false);
        Transaction tx = pm.currentTransaction();
        try {
            // mgr is transient-clean
            tx.begin();
            // mgr is now persistent-clean
            pm.makePersistent(mgr);
            // mgr is now detached-clean
            tx.commit();
            if (!JDOHelper.isDetached(mgr)) {
                fail("The object is not detached or hasn't been detached correctly after committing the TX");
            }
            // Attach the UNCHANGED (detached) object
            // mgr is detached-clean
            tx.begin();
            // mgr is now persistent-clean
            pm.makePersistent(mgr);
            // mgr is now detached-clean
            tx.commit();
            if (!JDOHelper.isDetached(mgr)) {
                fail("The object is not detached or hasn't been detached correctly after committing the TX");
            }
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 100 with Manager

use of org.jpox.samples.models.company.Manager in project tests by datanucleus.

the class AttachDetachTest method testDetachAttach_ManyToOne_NewPM.

/**
 * Test detach/attach using N-1 relations and a new PM for each operation.
 */
public void testDetachAttach_ManyToOne_NewPM() {
    try {
        Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        woody.setManager(bart);
        Department deptB = new Department("DeptB");
        deptB.setManager(bart);
        Employee woodyDetached = null;
        Employee woodyAttached = null;
        Employee woody2 = null;
        Object id = null;
        // -----------------------------------------------------------------------------------------
        // test 1 - test detach and attach
        // -----------------------------------------------------------------------------------------
        // store and detach objects
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woody);
            pm.makePersistent(deptB);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            id = pm.getObjectId(woody);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // change detached objects
        woodyDetached.getManager().setLastName("Simpson0");
        // attach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(woodyDetached);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // check attach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("expected change in attached instance", "Simpson0", woody2.getManager().getLastName());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 2 - test pc are the same after attach
        // -----------------------------------------------------------------------------------------
        // detach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // change detached objects
        woodyDetached.setLastName("Simpson1");
        // attach objects and check objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woody2 = (Employee) pm.getObjectById(id, true);
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached.getManager(), woody2.getManager());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 3 - test pc are the same after attach, now in different
        // order, first attach and later get object
        // -----------------------------------------------------------------------------------------
        // detach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // change detached objects
        woodyDetached.setLastName("Simpson1");
        // attach and check objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached.getManager(), woody2.getManager());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 4 - test changing aggregated pc. aggregated pc is not yet persistent
        // -----------------------------------------------------------------------------------------
        // detach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // change detached objects
        woodyDetached.setLastName("Simpson1");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        assertTrue("pc instance should not be already persistent", !JDOHelper.isPersistent(boss));
        woodyDetached.setManager(boss);
        // attach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("changed aggregated pc instance was not applied to the datastore", woodyAttached.getManager(), boss);
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 5 - test changing aggregated pc. aggregated pc is already persistent
        // -----------------------------------------------------------------------------------------
        // detach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
            pm.makePersistent(boss2);
            tx.commit();
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
            woodyDetached.setLastName("Simpson1");
            woodyDetached.setManager(boss2);
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertEquals("changed aggregated pc instance was not applied to the datastore", woodyAttached.getManager(), boss2);
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss2));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 6 - test setting aggregated pc to null
        // -----------------------------------------------------------------------------------------
        // detach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woody = (Employee) pm.getObjectById(id, true);
            woodyDetached = (Employee) pm.detachCopy(woody);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive())
                tx.rollback();
            pm.close();
        }
        // change detached objects
        woodyDetached.setLastName("Simpson1");
        woodyDetached.setManager(null);
        // attach objects
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            woodyAttached = (Employee) pm.makePersistent(woodyDetached);
            woody2 = (Employee) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", woodyAttached, woody2);
            assertNull("changed aggregated pc instance was not applied to the datastore. it should be null", woodyAttached.getManager());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)106 Manager (org.jpox.samples.models.company.Manager)106 Transaction (javax.jdo.Transaction)97 Department (org.jpox.samples.models.company.Department)50 Query (javax.jdo.Query)48 Employee (org.jpox.samples.models.company.Employee)45 JDOUserException (javax.jdo.JDOUserException)41 Collection (java.util.Collection)40 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)38 StoreManager (org.datanucleus.store.StoreManager)34 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)29 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)27 Iterator (java.util.Iterator)22 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)17 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)15 JDOException (javax.jdo.JDOException)14 Extent (javax.jdo.Extent)13 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)13 List (java.util.List)11 Properties (java.util.Properties)10