Search in sources :

Example 46 with Department

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

the class JDOQLResultTest method testSetResultCartesianProduct1.

/**
 * Test cartesian products
 */
public void testSetResultCartesianProduct1() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            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");
            Office office = new Office(5, "cubicle 1", "none");
            bart.addSubordinate(boss[0]);
            bart.addSubordinate(boss[1]);
            Department deptA = new Department("DeptA");
            Department deptB = new Department("DeptB");
            bart.getDepartments().add(deptA);
            boss[3].getDepartments().add(deptB);
            pm.makePersistent(bart);
            pm.makePersistentAll(boss);
            pm.makePersistent(office);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            tx.begin();
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
                fail("JDOUserException expected");
            } catch (JDOUserException e) {
            // expected;
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e");
                q.setResult("this");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
                fail("JDOUserException expected");
            } catch (JDOUserException e) {
            // expected;
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e");
                q.setResult("this, e");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e; Office o");
                q.setResult("this, e, o");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            Office office = new Office(6, "cubicle 2", "none");
            pm.makePersistent(office);
            pm.flush();
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Employee e; Office o");
                q.setResult("this, e, o");
                List results = (List) q.execute();
                assertEquals(24, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Department d");
                q.setResult("this, d");
                List results = (List) q.execute();
                assertEquals(4, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Department d");
                q.setResult("this, d");
                q.setFilter("d==this");
                List results = (List) q.execute();
                assertEquals(2, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Department d");
                q.setResult("this, d");
                q.setFilter("d!=this");
                List results = (List) q.execute();
                assertEquals(2, results.size());
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            try {
                Query q = pm.newQuery(pm.getExtent(Department.class, false));
                q.declareVariables("Manager e");
                q.setResult("this, e");
                List results = (List) q.execute();
                assertEquals(12, results.size());
                q.closeAll();
            } catch (RuntimeException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Office(org.jpox.samples.models.company.Office) 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) ArrayList(java.util.ArrayList) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 47 with Department

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

the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability2.

/**
 * Test that when an element with N-1 relation with FK collection is persisted, the owning PC is persisted also.
 * TODO Move to reachability tests.
 */
public void testFKCollectionFieldPersistenceByReachability2() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    try {
        Department d = new Department("Engineering");
        d.setManager(mgr);
        mgr.addDepartment(d);
        tx.begin();
        pm.makePersistent(d);
        tx.commit();
    } catch (Exception e) {
        LOG.error("Exception thrown when persisting FK collection using reachability", e);
        fail("Exception thrown when persisting FK collection using reachability " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    // get a fresh PM to ensure that any results aren't coming from the cache
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        mgr = (Manager) it.next();
        Collection c = mgr.getDepartments();
        assertEquals(1, c.size());
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Department d = (Department) it.next();
        assertTrue(c.contains(d));
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 48 with Department

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

the class PersistenceManagerTest method testLifecycleListenerForCollections.

/**
 * Test of basic lifecycle listener behaviour, listeneing to the changes in the lifecycle
 * of an object with a collection of other objects.
 */
public void testLifecycleListenerForCollections() {
    BasicListener listener = new BasicListener(true);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    int i = 0;
    try {
        pm.addInstanceLifecycleListener(listener, new Class[] { Manager.class, Department.class });
        Object managerId;
        Object dept2Id;
        tx.begin();
        // Persist related objects and check the events
        // Manager has a 1-N (FK) with Department
        Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
        Department dept1 = new Department("Invasions");
        Department dept2 = new Department("Propaganda");
        Department dept3 = new Department("Lies");
        manager.addDepartment(dept1);
        manager.addDepartment(dept2);
        manager.addDepartment(dept3);
        dept1.setManager(manager);
        dept2.setManager(manager);
        dept3.setManager(manager);
        pm.makePersistent(manager);
        pm.flush();
        Integer[] events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            int numPreStore = 0;
            int numPostStore = 0;
            int numEventsToProcess = i + 8;
            for (int j = i; j < numEventsToProcess; j++) {
                if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
                    numPreStore++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
                    numPostStore++;
                }
                i++;
            }
            assertEquals("Number of PreStore events is wrong", 4, numPreStore);
            assertEquals("Number of PostStore events is wrong", 4, numPostStore);
        } else {
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 1
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 3
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
        }
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        // Manager
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Manager
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Department 1
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Department 1
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Department 2
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Department 2
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        // Department 3
        assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
        // Department 3
        assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
        // Evict anything in the L2 cache so we know we are going to the
        // datastore, and hence get predictable callback ordering
        pmf.getDataStoreCache().evictAll();
        // Save the object ids
        managerId = pm.getObjectId(manager);
        dept2Id = pm.getObjectId(dept2);
        // Make sure the get goes to the DB
        pmf.getDataStoreCache().evictAll();
        // Make sure the get goes to the DB
        pm.evictAll();
        tx.begin();
        dept2 = (Department) pm.getObjectById(dept2Id);
        manager = (Manager) pm.getObjectById(managerId);
        // Remove manager of dept2 and check the events
        dept2.setManager(null);
        manager.removeDepartment(dept2);
        pm.flush();
        events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
            int numPostLoad = 0;
            int numPreDirty = 0;
            int numPostDirty = 0;
            int numEventsToProcess = i + 8;
            for (int j = i; j < numEventsToProcess; j++) {
                if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_LOAD) {
                    numPostLoad++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_DIRTY) {
                    numPreDirty++;
                } else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_DIRTY) {
                    numPostDirty++;
                }
                i++;
            }
            assertEquals("Number of PostLoad is wrong", 4, numPostLoad);
            // 1 for Department2 and 1 for Manager
            assertEquals("Number of PreDirty is wrong", 2, numPreDirty);
            // 1 for Department2 and 1 for Manager
            assertEquals("Number of PostDirty is wrong", 2, numPostDirty);
        } else {
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
            if (vendorID == null) {
                // Not needed on RDBMS for some reason
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            }
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
        }
        // Commit the changes and check the events
        tx.commit();
        events = listener.getRegisteredEventsAsArray();
        if (tx.getOptimistic()) {
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
        } else {
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            if (vendorID != null) {
                // RDBMS loads here
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
                assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
            }
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Manager
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
            // Department 2
            assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            if (vendorID == null) {
                // Clear the other 2 departments
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
                // Department 1
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
                // Department 3
                assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
            }
        }
        assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
    // TODO Add attach/detach of the Manager and its Departments.
    } catch (Exception e) {
        LOG.error("Exception thrown in test", e);
        fail("Exception thrown while running lifecycle listener collection test : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        listener.getRegisteredEvents().clear();
    }
}
Also used : BigInteger(java.math.BigInteger) Department(org.jpox.samples.models.company.Department) BasicListener(org.datanucleus.samples.lifecyclelistener.BasicListener) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 49 with Department

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

the class PersistenceManagerTest method testInverseFCOCollectionFieldPersistence4.

/**
 * Test that setting the inverse reference of an object implicitly adds it
 * to the inverse collection of the "owning" object
 */
public void testInverseFCOCollectionFieldPersistence4() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    try {
        Department d = new Department("Engineering");
        d.setManager(mgr);
        tx.begin();
        pm.makePersistent(d);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
            pm.close();
            fail("Failed to persist object and commit transaction");
        }
        pm.close();
    }
    // get a fresh PM to ensure that any results aren't coming from the cache
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        mgr = (Manager) it.next();
        Collection c = mgr.getDepartments();
        assertEquals(1, c.size());
        ext = pm.getExtent(Department.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Department d = (Department) it.next();
        assertTrue(c.contains(d));
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 50 with Department

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

the class ApplicationIdPersistenceTest method testOneToMany.

public void testOneToMany() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object dept1Id = null;
        try {
            tx.begin();
            Department dept1 = new Department("Finance");
            Project prj1 = new Project("Cost Cutting", 150000);
            Project prj2 = new Project("Restructuring", 100000);
            dept1.addProject(prj1);
            dept1.addProject(prj2);
            pm.makePersistent(dept1);
            tx.commit();
            dept1Id = pm.getObjectId(dept1);
        } catch (Exception e) {
            LOG.error("Exception during 1-N persist", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // No L2 cache interference
        pmf.getDataStoreCache().evictAll();
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            // Check number of objects present
            Query q1 = pm.newQuery(Department.class);
            List<Department> results1 = (List<Department>) q1.execute();
            assertEquals(1, results1.size());
            Query q2 = pm.newQuery(Project.class);
            List<Project> results2 = (List<Project>) q2.execute();
            assertEquals(2, results2.size());
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-N retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Department dept1 = null;
            try {
                dept1 = (Department) pm.getObjectById(dept1Id);
                assertEquals("Finance", dept1.getName());
            } catch (JDOObjectNotFoundException onfe) {
                fail("Department dept1 not found");
            }
            assertNotNull("Department projects should not be null", dept1.getProjects());
            Set<Project> projects = dept1.getProjects();
            assertEquals("Number of projects is incorrect", 2, projects.size());
            Iterator<Project> iter = projects.iterator();
            Project prj1 = iter.next();
            Project prj2 = iter.next();
            if (prj1.getName().equals("Cost Cutting")) {
                assertEquals("Budget of Cost Cutting incorrect", 150000, prj1.getBudget());
                assertEquals("Second project unexpected", "Restructuring", prj2.getName());
                assertEquals("Second project budget incorrect", 100000, prj2.getBudget());
            } else if (prj1.getName().equals("Restructuring")) {
                assertEquals("Budget of Restructring incorrect", 100000, prj1.getBudget());
                assertEquals("Second project unexpected", "Cost Cutting", prj2.getName());
                assertEquals("Second project budget incorrect", 150000, prj2.getBudget());
            } else {
                fail("First project unexpected");
            }
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception during 1-N retrieve and check", e);
            fail("Exception thrown when running test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Department.class);
        clean(Project.class);
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOException(javax.jdo.JDOException) JDOOptimisticVerificationException(javax.jdo.JDOOptimisticVerificationException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Project(org.jpox.samples.models.company.Project) Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) List(java.util.List)

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