Search in sources :

Example 56 with Department

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

the class PersistenceManagerTest method testMakeTransientOwnerAndElementsUsingFetchPlan.

/**
 * Test of makeTransient(Object, boolean) to use the fetchplan for makeTransient operation.
 */
public void testMakeTransientOwnerAndElementsUsingFetchPlan() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object mgrId = null;
        try {
            // Persist Manager -> Departments -> Projects
            tx.begin();
            Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
            Department dept1 = new Department("Sales");
            Department dept2 = new Department("Marketing");
            dept1.setManager(mgr);
            dept2.setManager(mgr);
            mgr.addDepartment(dept1);
            mgr.addDepartment(dept2);
            Project prj1 = new Project("Christmas Sales drive", 100000);
            Project prj2 = new Project("XFactor special offer", 30000);
            Project prj3 = new Project("Press Releases", 25000);
            dept1.addProject(prj1);
            dept1.addProject(prj2);
            dept2.addProject(prj3);
            pm.makePersistent(mgr);
            tx.commit();
            mgrId = JDOHelper.getObjectId(mgr);
        } catch (Exception e) {
            LOG.error("Exception indata setup for makeTransient", e);
            fail("Exception thrown setting up data for makeTransient test " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        Manager mgr = null;
        try {
            // Make the Manager transient with all subordinates
            // Large enough depth for all of graph
            pm.getFetchPlan().addGroup("all").setMaxFetchDepth(3);
            tx.begin();
            mgr = (Manager) pm.getObjectById(mgrId);
            ((JDOPersistenceManager) pm).makeTransient(mgr, true);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in makeTransient for graph", e);
            fail("Exception thrown making graph transient " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        try {
            // Check the result
            assertNotNull("Transient manager is null!", mgr);
            assertEquals("Transient manager is in wrong state", ObjectState.TRANSIENT, JDOHelper.getObjectState(mgr));
            assertEquals("Transient manager has wrong first name", mgr.getFirstName(), FIRSTNAME[0]);
            assertEquals("Transient manager has wrong last name", mgr.getLastName(), LASTNAME[0]);
            Set depts = mgr.getDepartments();
            assertNotNull("Transient manager has no departments!", depts);
            assertEquals("Transient manager has incorrect number of departments", 2, depts.size());
            Iterator deptIter = depts.iterator();
            Department dept1 = (Department) deptIter.next();
            Department dept2 = (Department) deptIter.next();
            Department sales = null;
            Department marketing = null;
            if (dept1.getName().equals("Sales")) {
                sales = dept1;
                if (dept2.getName().equals("Marketing")) {
                    marketing = dept2;
                } else {
                    fail("Marketing department not found");
                }
            } else if (dept1.getName().equals("Marketing")) {
                marketing = dept1;
                if (dept2.getName().equals("Sales")) {
                    sales = dept2;
                } else {
                    fail("Sales department not found");
                }
            }
            // Sales dept
            assertEquals("Transient Sales Department is in wrong state", ObjectState.TRANSIENT, JDOHelper.getObjectState(sales));
            Set projects = sales.getProjects();
            assertNotNull("Projects of sales department is null", projects);
            assertEquals("Number of projects of sales department is incorrect", 2, projects.size());
            Iterator iter = projects.iterator();
            boolean hasPrj1 = false;
            boolean hasPrj2 = false;
            boolean hasPrj3 = false;
            while (iter.hasNext()) {
                Project prj = (Project) iter.next();
                assertEquals("State of project of sales dept is wrong", ObjectState.TRANSIENT, JDOHelper.getObjectState(prj));
                if (prj.getName().equals("Christmas Sales drive")) {
                    hasPrj1 = true;
                    assertEquals("Budget of project 1 is incorrect", 100000, prj.getBudget());
                } else if (prj.getName().equals("XFactor special offer")) {
                    hasPrj2 = true;
                    assertEquals("Budget of project 2 is incorrect", 30000, prj.getBudget());
                }
            }
            if (!hasPrj1 || !hasPrj2) {
                fail("One of two projects in Sales department was missing!");
            }
            // Marketing dept
            assertEquals("Transient Marketing Department is in wrong state", ObjectState.TRANSIENT, JDOHelper.getObjectState(marketing));
            projects = marketing.getProjects();
            assertNotNull("Projects of marketing department is null", projects);
            assertEquals("Number of projects of marketing department is incorrect", 1, projects.size());
            iter = projects.iterator();
            while (iter.hasNext()) {
                Project prj = (Project) iter.next();
                assertEquals("State of project of marketing dept is wrong", ObjectState.TRANSIENT, JDOHelper.getObjectState(prj));
                if (prj.getName().equals("Press Releases")) {
                    hasPrj3 = true;
                    assertEquals("Budget of project 1 is incorrect", 25000, prj.getBudget());
                }
            }
            if (!hasPrj3) {
                fail("Project in marketing department was missing!");
            }
        } catch (Exception e) {
            LOG.error("Exception in check of makeTransient", e);
            fail("Exception thrown checking transient graph " + e.getMessage());
        } finally {
            pm.close();
        }
    } finally {
        clean(Manager.class);
        clean(Department.class);
        clean(Project.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) Project(org.jpox.samples.models.company.Project) Department(org.jpox.samples.models.company.Department) Set(java.util.Set) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Manager(org.jpox.samples.models.company.Manager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

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