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);
}
}
Aggregations