use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class AttachDetachReplicateTest method testMoveAcrossDatastores_company_check.
/**
* Convenience method to check the detached state of a graph of "Company" objects.
* @param messagePrefix Prefix to any failure messages
* @param manager The detached manager that starts the graph
* @param detached Whether we should test for detached objects
*/
private void testMoveAcrossDatastores_company_check(String messagePrefix, Manager manager, boolean detached) {
Employee detached_employee1 = null;
Employee detached_employee2 = null;
Department detached_department1 = null;
Department detached_department2 = null;
if (!JDOHelper.isDetached(manager) && detached) {
fail(messagePrefix + "Manager is not detached!");
}
assertEquals("Manager.personNum is not correct", 1L, manager.getPersonNum());
assertEquals("Manager.firstName is not correct", "Lucifer", manager.getFirstName());
assertEquals("Manager.firstName is not correct", "Satan", manager.getLastName());
assertEquals("Manager.emailAddress is not correct", "Lucifer.Satan@microsoft.hell", manager.getEmailAddress());
assertEquals("Manager.serialNo is not correct", "jsdkhf8z23", manager.getSerialNo().trim());
assertTrue("Manager.salary is not correct : expected " + 33666.99f + " but is " + manager.getSalary(), Math.abs(33666.99f - manager.getSalary()) <= 0.02f);
assertEquals("Number of manager's departments has been changed", 2, manager.getDepartments().size());
assertEquals("Number of manager's subordinates has been changed", 2, manager.getSubordinates().size());
for (Iterator it = manager.getDepartments().iterator(); it.hasNext(); ) {
Department dep = (Department) it.next();
if (!JDOHelper.isDetached(dep) && detached) {
fail(messagePrefix + "Department is not detached!");
}
if ("Brainwashing".equals(dep.getName())) {
detached_department1 = dep;
} else if ("Torture".equals(dep.getName())) {
detached_department2 = dep;
} else {
fail(messagePrefix + "The Name of at least one Department is wrong: " + dep.getName());
}
}
if (detached_department1 == null || detached_department2 == null) {
fail(messagePrefix + "Both detached departments have the same name!");
}
for (Iterator it = manager.getSubordinates().iterator(); it.hasNext(); ) {
Employee emp = (Employee) it.next();
if (!JDOHelper.isDetached(emp) && detached) {
fail(messagePrefix + "Employee is not detached!");
}
if (!JDOHelper.isDetached(emp.getManager()) && detached) {
fail(messagePrefix + "Manager reference of Employee is not detached!");
}
switch((int) emp.getPersonNum()) {
case 9593:
detached_employee1 = emp;
break;
case 8723:
detached_employee2 = emp;
break;
default:
fail(messagePrefix + "Employee number is wrong: " + emp.getPersonNum());
}
}
if (detached_employee1 == null || detached_employee2 == null) {
fail(messagePrefix + "Both employees have the same personNum!");
}
}
use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class AttachDetachReplicateTest method testMoveAcrossDatastores_company.
/**
* This is a complex testcase using the classes from org.jpox.samples.models.company.
* It stores a Manager into datastore 1. This Manager has a mapped-by-Set of
* his employees and a join-Set of his departments. Hence, this test checks for
* the behaviour of Sets when copied from one
*/
public void testMoveAcrossDatastores_company() {
PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
try {
Transaction tx1 = null;
Transaction tx2 = null;
// Create a Manager with two Departments and two Employees and persist it
// into datastore 1.
PersistenceManager pm1 = pmf.getPersistenceManager();
Object managerId = null;
try {
tx1 = pm1.currentTransaction();
tx1.begin();
Manager ds1_manager = new Manager(1L, "Lucifer", "Satan", "Lucifer.Satan@microsoft.hell", 33666.99f, "jsdkhf8z23");
Employee ds1_employee1 = new Employee(9593L, "McCreevy", "Charlie", "Charlie.McCreevy@microsoft.hell", 9948.57f, "8967bjjhg", new Integer(94));
Employee ds1_employee2 = new Employee(8723L, "Gates", "Bill", "Bill.Gates@microsoft.hell", 11835.17f, "3894lknsd", new Integer(42));
Department ds1_department1 = new Department("Brainwashing");
ds1_department1.setManager(ds1_manager);
ds1_manager.addDepartment(ds1_department1);
// TODO Change this to be an inherited type to show up an error
Department ds1_department2 = new Department("Torture");
ds1_department2.setManager(ds1_manager);
ds1_manager.addDepartment(ds1_department2);
ds1_employee1.setManager(ds1_manager);
ds1_manager.addSubordinate(ds1_employee1);
ds1_employee2.setManager(ds1_manager);
ds1_manager.addSubordinate(ds1_employee2);
try {
pm1.makePersistent(ds1_manager);
} catch (Exception x) {
LOG.error("Persisting Manager with Departments and Employees into datastore1 failed!", x);
fail("Persisting Manager with Departments and Employees into datastore1 failed: " + x.getMessage());
}
tx1.commit();
managerId = JDOHelper.getObjectId(ds1_manager);
} finally {
if (tx1 != null && tx1.isActive()) {
tx1.rollback();
}
pm1.close();
}
// Detach the Manager (with FetchPlan.ALL) from datastore 1.
Manager detached_manager = null;
pm1 = pmf.getPersistenceManager();
try {
tx1 = pm1.currentTransaction();
tx1.begin();
pm1.getFetchPlan().setGroup(FetchPlan.ALL);
pm1.getFetchPlan().setMaxFetchDepth(-1);
try {
Manager ds1_manager = (Manager) pm1.getObjectById(managerId);
detached_manager = (Manager) pm1.detachCopy(ds1_manager);
} catch (Exception x) {
LOG.error("Loading and detaching Manager from datastore1 failed!", x);
fail("Loading and detaching Manager from datastore1 failed: " + x.getMessage());
}
tx1.commit();
} finally {
if (tx1 != null && tx1.isActive()) {
tx1.rollback();
}
pm1.close();
}
// check, whether the detached data equals the original data
testMoveAcrossDatastores_company_check("makePersistent or detachCopy (with datastore1) has corrupted data: ", detached_manager, true);
// put the detached manager into datastore2 using makePersistent
PersistenceManager pm2 = pmf2.getPersistenceManager();
try {
tx2 = pm2.currentTransaction();
tx2.begin();
try {
pm2.makePersistent(detached_manager);
} catch (Exception x) {
LOG.error("makePersistent failed on datastore2!", x);
fail("makePersistent failed on datastore2: " + x.getMessage());
}
tx2.commit();
} finally {
if (tx2 != null && tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
// load the manager from datastore2 and check whether data is still correct
pm2 = pmf2.getPersistenceManager();
try {
tx2 = pm2.currentTransaction();
tx2.begin();
Manager ds2_manager = null;
try {
Extent ex = pm2.getExtent(Manager.class);
Iterator exIter = ex.iterator();
ds2_manager = (Manager) exIter.next();
if (exIter.hasNext()) {
fail("Returned more than 1 Manager object in second datastore when should only have 1");
}
} catch (Exception e) {
e.printStackTrace();
LOG.error("Loading Manager from datastore2 failed!", e);
fail("Loading Manager from datastore2 failed: " + e.getMessage());
}
testMoveAcrossDatastores_company_check("makePersistent on datastore2 has corrupted data: ", ds2_manager, false);
tx2.commit();
} finally {
if (tx2 != null && tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
} finally {
// Clean out our data
PersistenceManagerFactory[] pmfs = new PersistenceManagerFactory[] { pmf, pmf2 };
for (int i = 0; i < pmfs.length; ++i) {
PersistenceManagerFactory pmf = pmfs[i];
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
for (Iterator it = pm.getExtent(Employee.class, false).iterator(); it.hasNext(); ) {
Employee e = (Employee) it.next();
e.setManager(null);
}
tx.commit();
tx.begin();
for (Iterator it = pm.getExtent(Department.class, false).iterator(); it.hasNext(); ) {
Department d = (Department) it.next();
d.setManager(null);
}
tx.commit();
pm.close();
clean(pmf, Manager.class);
clean(pmf, Employee.class);
clean(pmf, Department.class);
}
}
}
use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class AttachDetachTest method testDetachLoadUnloadFields.
/**
* Test of DETACH_LOAD_FIELDS, DETACH_UNLOAD_FIELDS flags.
*/
public void testDetachLoadUnloadFields() {
try {
Manager detachedM1a = null;
Manager detachedM1b = 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");
m1.addSubordinate(e1);
m1.addSubordinate(e2);
e1.setManager(m1);
e2.setManager(m1);
Department d1 = new Department("Cartoon");
m1.addDepartment(d1);
d1.setManager(m1);
// This should persist all objects
pm.makePersistent(e1);
// Detach just the FetchPlan fields
pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS | FetchPlan.DETACH_UNLOAD_FIELDS);
detachedM1a = (Manager) pm.detachCopy(m1);
pm.getFetchPlan().setDetachmentOptions(FetchPlan.DETACH_LOAD_FIELDS);
detachedM1b = (Manager) pm.detachCopy(m1);
tx.commit();
} 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();
}
// Check what has been detached - when detaching just fetch plan fields
try {
detachedM1a.getEmailAddress();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.emailAddress hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1a.getSerialNo();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.serialNo hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1a.getDepartments();
fail("Field Manager.departments has been detached yet this should not have been since wasn't in fetch-plan");
} catch (JDODetachedFieldAccessException dfae) {
// Expected
}
try {
detachedM1a.getSubordinates();
fail("Field Manager.subordinates has been detached yet this should not have been since wasn't in fetch-plan");
} catch (JDODetachedFieldAccessException dfae) {
// Expected
}
// Check what has been detached - when detaching all loaded fields
try {
detachedM1b.getEmailAddress();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.emailAddress hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1b.getSerialNo();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.serialNo hasn't been detached yet this should have been since was in fetch-plan");
}
try {
detachedM1b.getDepartments();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.departments hasn't been detached yet this should have been since was loaded at detach");
}
try {
detachedM1b.getSubordinates();
} catch (JDODetachedFieldAccessException dfae) {
fail("Field Manager.subordinates hasn't been detached yet this should have been since was loaded at detach");
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class AttachDetachTest method testDetachAttach_OneToManyFK.
/**
* test pc objects aggregating other pcs. associations 1-N FK
*/
public void testDetachAttach_OneToManyFK() {
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");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
woody.setManager(bart);
Manager bossDetached;
Object idBoss;
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
// -----------------------------------------------------------------------------------------
// test 1 - test detach and attach
// -----------------------------------------------------------------------------------------
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(boss);
pm.getFetchPlan().addGroup("groupDepartments");
bossDetached = (Manager) pm.detachCopy(boss);
tx.commit();
idBoss = pm.getObjectId(boss);
Department deptB = new Department("DeptB");
deptB.setManager(bossDetached);
bossDetached.addDepartment(deptB);
tx.begin();
pm.makePersistent(bossDetached);
tx.commit();
System.gc();
tx.begin();
Manager theBoss = (Manager) pm.getObjectById(idBoss, true);
Assert.assertEquals(1, theBoss.getDepartments().size());
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);
}
}
use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class AttachDetachTest method testDetachAttach_OneToMany.
/**
* test pc objects aggregating other pcs. associations 1-n
*/
public void testDetachAttach_OneToMany() {
try {
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 boss3 = new Manager(5, "Boss", "WakesUp3", "boss3@wakes.up", 6, "serial 5");
Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
Manager boss5 = new Manager(7, "Boss", "WakesUp5", "boss5@wakes.up", 8, "serial 7");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Department deptB = new Department("DeptB");
bart.addDepartment(deptB);
Manager bartDetached;
Manager bart2;
Object id;
Object id2;
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
pm.getFetchPlan().addGroup("groupSubordinates");
// This is a fix for the fact that Person hashCode/equals rely on non-PK fields and so use of
// this class outside of a txn will try to load these fields
tx.setNontransactionalRead(true);
try {
// test detach and attach
tx.begin();
pm.makePersistent(bart);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
id = pm.getObjectId(bart);
Employee employeeChanged = (Employee) bartDetached.getSubordinates().iterator().next();
employeeChanged.setLastName("Simpson0");
id2 = JDOHelper.getObjectId(employeeChanged);
tx.begin();
bart2 = (Manager) pm.makePersistent(bartDetached);
tx.commit();
System.gc();
tx.begin();
bart2 = (Manager) pm.getObjectById(id, true);
employeeChanged = (Employee) pm.getObjectById(id2, true);
assertEquals(bart2.getSubordinates().size(), 2);
assertEquals("expected change in attached instance", "Simpson0", employeeChanged.getLastName());
tx.commit();
// test pc are the same after attach
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.setLastName("Simpson1");
tx.begin();
bart2 = (Manager) pm.getObjectById(id, true);
assertEquals("attached instance returned must be the one already enlisted in the PM", bartDetached, bart2);
assertTrue("attached instance returned must be the one already enlisted in the PM", bartDetached.getSubordinates().containsAll(bart2.getSubordinates()));
tx.commit();
// test pc are the same after attach, now in different order, first attach and later get object
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.setLastName("Simpson1");
tx.begin();
bartDetached = (Manager) pm.makePersistent(bartDetached);
bart2 = (Manager) pm.getObjectById(id, true);
assertEquals("attached instance returned must be the one already enlisted in the PM", bartDetached, bart2);
assertTrue("attached instance returned must be the one already enlisted in the PM", bartDetached.getSubordinates().containsAll(bart2.getSubordinates()));
tx.commit();
// test changing aggregated pc. add element pc which is not yet persistent
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.setLastName("Simpson1");
assertTrue("pc instance should not be already persistent", !JDOHelper.isPersistent(boss3));
bartDetached.addSubordinate(boss3);
tx.begin();
bartDetached = (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", bartDetached, bart2);
assertTrue("add element to collection was not applied to the datastore", bartDetached.getSubordinates().contains(boss3));
assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss3));
// verify if previous boss were not lost
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss));
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss2));
tx.commit();
// test changing aggregated pc. add element pc which is already persistent
tx.begin();
pm.makePersistent(boss4);
tx.commit();
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.setLastName("Simpson1");
bartDetached.addSubordinate(boss4);
tx.begin();
bartDetached = (Manager) pm.makePersistent(bartDetached);
bart2 = (Manager) pm.getObjectById(id, true);
assertEquals("attached instance returned must be the one already enlisted in the PM", bartDetached, bart2);
assertTrue("add element to collection was not applied to the datastore", bartDetached.getSubordinates().contains(boss4));
assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss4));
// verify if previous boss were not lost
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss));
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss2));
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss3));
tx.commit();
// test changing aggregated pc. remove element
tx.begin();
pm.makePersistent(boss4);
tx.commit();
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.setLastName("Simpson1");
bartDetached.removeSubordinate(boss4);
tx.begin();
bartDetached = (Manager) pm.makePersistent(bartDetached);
bart2 = (Manager) pm.getObjectById(id, true);
assertEquals("attached instance returned must be the one already enlisted in the PM", bartDetached, bart2);
assertTrue("remove element in aggregated pc instance was not applied to the datastore", !bartDetached.getSubordinates().contains(boss4));
assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss4));
// verify if previous boss were not lost
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss));
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss2));
assertTrue("previous aggregated pc instances were lost", bartDetached.getSubordinates().contains(boss3));
tx.commit();
// test changing aggregated pc. aggregated pc is cleared
tx.begin();
pm.makePersistent(boss4);
tx.commit();
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.setLastName("Simpson1");
bartDetached.clearSubordinates();
tx.begin();
bartDetached = (Manager) pm.makePersistent(bartDetached);
bart2 = (Manager) pm.getObjectById(id, true);
assertEquals("attached instance returned must be the one already enlisted in the PM", bartDetached, bart2);
assertTrue("clear Collection with aggregated pc instance was not applied to the datastore", bartDetached.getSubordinates().size() == 0);
tx.commit();
// test sco fields made dirty
tx.begin();
pm.makePersistent(boss5);
tx.commit();
tx.begin();
bart = (Manager) pm.getObjectById(id, true);
bartDetached = (Manager) pm.detachCopy(bart);
tx.commit();
bartDetached.addSubordinate(boss5);
JDOHelper.makeDirty(bartDetached, "subordinates");
tx.begin();
bartDetached = (Manager) pm.makePersistent(bartDetached);
tx.commit();
tx.begin();
bart2 = (Manager) pm.getObjectById(id, true);
assertEquals(1, bart2.getSubordinates().size());
assertTrue("SCO field should is missing element", bart2.getSubordinates().contains(boss5));
assertTrue("element of SCO field is not persistent", JDOHelper.isPersistent(boss5));
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);
}
}
Aggregations