Search in sources :

Example 51 with Department

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

the class JDOQLBasicTest method testQueryUnboundVariablesInheritance1.

public void testQueryUnboundVariablesInheritance1() {
    try {
        Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
        bart.addSubordinate(boss);
        bart.addSubordinate(boss2);
        homer.addSubordinate(boss4);
        Department deptA = new Department("DeptA");
        Department deptB = new Department("DeptB");
        bart.addDepartment(deptB);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(bart);
            pm.makePersistent(deptA);
            pm.makePersistent(deptB);
            tx.commit();
            tx.begin();
            Query q = pm.newQuery(Manager.class);
            Collection c = q.filter("emp.lastName == \"WakesUp2\" && this.subordinates.contains(emp)").variables("Employee emp").imports("import org.jpox.samples.models.company.Employee").executeList();
            assertEquals(1, c.size());
            assertEquals(((Manager) c.iterator().next()).getFirstName(), "Bart");
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Department(org.jpox.samples.models.company.Department) InsuranceDepartment(org.jpox.samples.models.company.InsuranceDepartment) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 52 with Department

use of org.jpox.samples.models.company.Department 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 53 with Department

use of org.jpox.samples.models.company.Department 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 54 with Department

use of org.jpox.samples.models.company.Department 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)

Example 55 with Department

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

the class AttachDetachTest method testDetachAttach_OneToMany_NewPM.

/**
 * test pc objects aggregating other pcs. associations 1-n, with a new PM for each operation
 */
public void testDetachAttach_OneToMany_NewPM() {
    try {
        Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
        Manager[] boss = new Manager[5];
        boss[0] = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
        boss[1] = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
        boss[2] = new Manager(5, "Boss", "WakesUp3", "boss3@wakes.up", 6, "serial 5");
        boss[3] = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
        boss[4] = new Manager(7, "Boss", "WakesUp5", "boss5@wakes.up", 8, "serial 7");
        bart.addSubordinate(boss[0]);
        bart.addSubordinate(boss[1]);
        Department deptB = new Department("DeptB");
        bart.addDepartment(deptB);
        Manager bartAttached = null;
        Manager bartDetached = null;
        Manager bart2;
        Object id = null;
        Object[] bossIds = new Object[boss.length];
        Object id2;
        // -----------------------------------------------------------------------------------------
        // start data for tests
        // -----------------------------------------------------------------------------------------
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            pm.makePersistent(bart);
            tx.commit();
            id = pm.getObjectId(bart);
            bossIds[0] = pm.getObjectId(boss[0]);
            bossIds[1] = pm.getObjectId(boss[1]);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 1 - test detach and attach
        // -----------------------------------------------------------------------------------------
        bartDetached = getDetachedManager(id, "groupSubordinates");
        Employee employeeChanged = (Employee) bartDetached.getSubordinates().iterator().next();
        employeeChanged.setLastName("Simpson0");
        id2 = JDOHelper.getObjectId(employeeChanged);
        attachDetachedManager(bartDetached);
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            bart2 = (Manager) pm.getObjectById(id, true);
            employeeChanged = (Employee) pm.getObjectById(id2, true);
            assertEquals(2, bart2.getSubordinates().size());
            assertEquals("expected change in attached instance", "Simpson0", employeeChanged.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
        // -----------------------------------------------------------------------------------------
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.setLastName("Simpson1");
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            bart2 = (Manager) pm.getObjectById(id, true);
            bartAttached = (Manager) pm.makePersistent(bartDetached);
            assertEquals("attached instance returned must be the one already enlisted in the PM", bartAttached, bart2);
            assertTrue(Manager.compareElementsContained(bartDetached.getSubordinates(), bart2.getSubordinates()));
            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
        // -----------------------------------------------------------------------------------------
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.setLastName("Simpson1");
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            bartAttached = (Manager) pm.makePersistent(bartDetached);
            bart2 = (Manager) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", bartAttached, bart2);
            assertTrue("attached instance returned must be the one already enlisted in the PM", bartDetached.getSubordinates().containsAll(bart2.getSubordinates()));
            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. add element pc which is not yet
        // persistent
        // -----------------------------------------------------------------------------------------
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.setLastName("Simpson1");
        assertTrue("pc instance should not be already persistent", !JDOHelper.isPersistent(boss[2]));
        bartDetached.addSubordinate(boss[2]);
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            bartAttached = (Manager) pm.makePersistent(bartDetached);
            pm.flush();
            bart2 = (Manager) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", bartAttached, bart2);
            assertTrue("add element to collection was not applied to the datastore", bartAttached.getSubordinates().contains(boss[2]));
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss[2]));
            // verify if previous boss were not lost
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[0], false)));
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[1], false)));
            tx.commit();
            bossIds[2] = pm.getObjectId(boss[2]);
        } 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. add element pc which is already
        // persistent
        // -----------------------------------------------------------------------------------------
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        Employee detachedBoss3 = null;
        try {
            tx.begin();
            pm.makePersistent(boss[3]);
            detachedBoss3 = (Employee) pm.detachCopy(boss[3]);
            tx.commit();
            bossIds[3] = pm.getObjectId(boss[3]);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.setLastName("Simpson1");
        bartDetached.addSubordinate(detachedBoss3);
        JDOHelper.makeDirty(bartDetached, "subordinates");
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            bartAttached = (Manager) pm.makePersistent(bartDetached);
            bart2 = (Manager) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", bartAttached, bart2);
            assertTrue("add element to collection was not applied to the datastore", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[3], false)));
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(pm.getObjectById(bossIds[3], false)));
            // verify if previous boss were not lost
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[0], false)));
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[1], false)));
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[2], false)));
            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 changing aggregated pc. remove element
        // -----------------------------------------------------------------------------------------
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.setLastName("Simpson1");
        bartDetached.removeSubordinate(getDetachedManager(bossIds[3], "groupSubordinates"));
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            bartAttached = (Manager) pm.makePersistent(bartDetached);
            bart2 = (Manager) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", bartAttached, bart2);
            assertTrue("remove element in aggregated pc instance was not applied to the datastore", !bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[3], false)));
            assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(pm.getObjectById(bossIds[3], false)));
            // verify if previous boss were not lost
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[0], false)));
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[1], false)));
            assertTrue("previous aggregated pc instances were lost", bartAttached.getSubordinates().contains(pm.getObjectById(bossIds[2], false)));
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 7 - test changing aggregated pc. aggregated pc is cleared
        // -----------------------------------------------------------------------------------------
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.setLastName("Simpson1");
        bartDetached.clearSubordinates();
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            bartAttached = (Manager) pm.makePersistent(bartDetached);
            bart2 = (Manager) pm.getObjectById(id, true);
            assertEquals("attached instance returned must be the one already enlisted in the PM", bartAttached, bart2);
            assertTrue("clear Collection with aggregated pc instance was not applied to the datastore", bartAttached.getSubordinates().size() == 0);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            bart2 = (Manager) pm.getObjectById(id, true);
            assertTrue("clear Collection with aggregated pc instance was not applied to the datastore", bart2.getSubordinates().size() == 0);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // -----------------------------------------------------------------------------------------
        // test 8 - test sco fields made dirty
        // -----------------------------------------------------------------------------------------
        pm = newPM();
        tx = pm.currentTransaction();
        pm.getFetchPlan().addGroup("groupSubordinates");
        try {
            tx.begin();
            pm.makePersistent(boss[4]);
            tx.commit();
            bossIds[4] = pm.getObjectId(boss[4]);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        bartDetached = getDetachedManager(id, "groupSubordinates");
        bartDetached.addSubordinate(getDetachedManager(bossIds[4], "groupSubordinates"));
        JDOHelper.makeDirty(bartDetached, "subordinates");
        attachDetachedManager(bartDetached);
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            bart2 = (Manager) pm.getObjectById(id, true);
            assertEquals(1, bart2.getSubordinates().size());
            assertTrue("SCO field should is missing element", bart2.getSubordinates().contains(pm.getObjectById(bossIds[4], false)));
            assertTrue("element of SCO field is not persistent", JDOHelper.isPersistent(pm.getObjectById(bossIds[4], false)));
            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

Department (org.jpox.samples.models.company.Department)56 PersistenceManager (javax.jdo.PersistenceManager)55 Transaction (javax.jdo.Transaction)54 Manager (org.jpox.samples.models.company.Manager)50 Query (javax.jdo.Query)33 InsuranceDepartment (org.jpox.samples.models.company.InsuranceDepartment)32 Collection (java.util.Collection)31 JDOUserException (javax.jdo.JDOUserException)20 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)19 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)13 StoreManager (org.datanucleus.store.StoreManager)13 Employee (org.jpox.samples.models.company.Employee)12 Iterator (java.util.Iterator)11 JDOException (javax.jdo.JDOException)11 Extent (javax.jdo.Extent)8 List (java.util.List)7 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)7 ArrayList (java.util.ArrayList)6 Office (org.jpox.samples.models.company.Office)6 SQLException (java.sql.SQLException)5