use of org.jpox.samples.models.company.Employee 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.Employee in project tests by datanucleus.
the class AttachDetachTest method testAttachDetachNonTransactionalRead.
public void testAttachDetachNonTransactionalRead() {
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");
Object id;
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(boss);
pm.makePersistent(bart);
tx.commit();
// non transactional read
Collection c = (Collection) pm.newQuery(Employee.class).execute();
Employee detachedEmployee = (Employee) pm.detachCopy(c.iterator().next());
// test with String
detachedEmployee.setFirstName("detached guy");
id = JDOHelper.getObjectId(detachedEmployee);
tx.begin();
pm.makePersistent(detachedEmployee);
tx.commit();
System.gc();
tx.begin();
Employee emp = (Employee) pm.getObjectById(id, true);
assertEquals("expected change in attached instance", "detached guy", emp.getFirstName());
tx.commit();
// non transactional read
c = (Collection) pm.newQuery(Employee.class).execute();
detachedEmployee = (Employee) pm.detachCopy(c.iterator().next());
// test with Integer (Object) fields non DFG
detachedEmployee.setYearsInCompany(new Integer(33));
id = JDOHelper.getObjectId(detachedEmployee);
tx.begin();
pm.makePersistent(detachedEmployee);
tx.commit();
System.gc();
tx.begin();
emp = (Employee) pm.getObjectById(id, true);
assertEquals("expected change in attached instance", 33, emp.getYearsInCompany().intValue());
tx.commit();
// non transactional read
c = (Collection) pm.newQuery(Employee.class).execute();
detachedEmployee = (Employee) pm.detachCopy(c.iterator().next());
// test with long (Primitive)
detachedEmployee.setPersonNum(546);
id = JDOHelper.getObjectId(detachedEmployee);
tx.begin();
pm.makePersistent(detachedEmployee);
tx.commit();
System.gc();
tx.begin();
emp = (Employee) pm.getObjectById(id, true);
assertEquals("expected change in attached instance", 546, emp.getPersonNum());
tx.commit();
// non transactional read
c = (Collection) pm.newQuery(Employee.class).execute();
detachedEmployee = (Employee) pm.detachCopy(c.iterator().next());
// test with PC (Object)
detachedEmployee.setManager(bart);
id = JDOHelper.getObjectId(detachedEmployee);
tx.begin();
pm.makePersistent(detachedEmployee);
tx.commit();
System.gc();
tx.begin();
emp = (Employee) pm.getObjectById(id, true);
assertEquals("expected change in attached instance", bart.getFirstName(), emp.getManager().getFirstName());
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.Employee 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);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class AttachDetachTest method testDetachAttach_ManyToOne.
/**
* test pc objects aggregating other pcs. associations N-1
*/
public void testDetachAttach_ManyToOne() {
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");
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");
woody.setManager(bart);
Department deptB = new Department("DeptB");
deptB.setManager(bart);
Employee woodyDetached;
Employee bossDetached;
Object id;
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.makePersistent(deptB);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
id = pm.getObjectId(woody);
idBoss = pm.getObjectId(boss);
woodyDetached.getManager().setLastName("Simpson0");
tx.begin();
pm.makePersistent(woodyDetached);
tx.commit();
System.gc();
tx.begin();
Employee woody2 = (Employee) pm.getObjectById(id, true);
assertEquals("expected change in attached instance", "Simpson0", woody2.getManager().getLastName());
tx.commit();
// -----------------------------------------------------------------------------------------
// test 2 - test pc are the same after attach
// -----------------------------------------------------------------------------------------
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
woodyDetached.setLastName("Simpson1");
tx.begin();
woody2 = (Employee) pm.getObjectById(id, true);
Employee 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();
// -----------------------------------------------------------------------------------------
// test 3 - test pc are the same after attach, now in different order, first attach and later get object
// -----------------------------------------------------------------------------------------
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
woodyDetached.setLastName("Simpson1");
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();
// -----------------------------------------------------------------------------------------
// test 4 - test changing aggregated pc. aggregated pc is not yet persistent
// -----------------------------------------------------------------------------------------
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
woodyDetached.setLastName("Simpson1");
assertTrue("pc instance should not be already persistent", !JDOHelper.isPersistent(boss3));
woodyDetached.setManager(boss3);
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(), boss3);
assertTrue("aggregated pc instance was expected to be made persistent", JDOHelper.isPersistent(boss3));
tx.commit();
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
assertEquals("changed aggregated pc instance was not applied to the datastore", woody.getManager(), boss3);
tx.commit();
// -----------------------------------------------------------------------------------------
// test 5 - test changing aggregated pc. aggregated pc is already persistent
// -----------------------------------------------------------------------------------------
tx.begin();
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();
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
assertEquals("changed aggregated pc instance was not applied to the datastore", woody.getManager(), boss2);
tx.commit();
// -----------------------------------------------------------------------------------------
// test 6 - test setting aggregated pc to null
// -----------------------------------------------------------------------------------------
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
woodyDetached = (Employee) pm.detachCopy(woody);
tx.commit();
woodyDetached.setLastName("Simpson1");
woodyDetached.setManager(null);
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 has incorrect last name", "Simpson1", woody2.getLastName());
assertNull("changed aggregated pc instance was not applied to the datastore. it should be null", woodyAttached.getManager());
tx.commit();
tx.begin();
woody = (Employee) pm.getObjectById(id, true);
assertNull("changed aggregated pc instance was not applied to the datastore. it should be null", woody.getManager());
tx.commit();
// -----------------------------------------------------------------------------------------
// test 7 - test detach and read aggregated pc field when its null
// -----------------------------------------------------------------------------------------
tx.begin();
boss = (Manager) pm.getObjectById(idBoss, true);
bossDetached = (Manager) pm.detachCopy(boss);
tx.commit();
assertNull("pc field should be null", bossDetached.getManager());
} 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.Employee in project tests by datanucleus.
the class AttachDetachTest method testFetchDepthOnDetachCopyAll.
/**
* Test of detachCopyAll with fetch-depth. See JIRA "NUCCORE-24"
*/
public void testFetchDepthOnDetachCopyAll() {
try {
Manager bob = new Manager(1, "Bob", "Woodpecker", "bob@woodpecker.com", 13, "serial 1");
Manager dave = new Manager(1, "Dave", "Woodpecker", "dave@woodpecker.com", 13, "serial 2");
Employee mary = new Employee(1, "Mary", "Woodpecker", "mary@woodpecker.com", 13, "serial 3");
mary.setManager(dave);
dave.setManager(bob);
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
// Save Mary will also save Dave and Bob.
tx.begin();
pm.makePersistent(mary);
tx.commit();
tx.begin();
// Group A has firstName and manager, amount other things.
// No max fetch-depth is defined, so they default to 1.
pm.getFetchPlan().setGroup("groupA");
Query<Employee> query = pm.newQuery(pm.getExtent(Employee.class, true));
query.setOrdering("firstName descending");
Collection<Employee> c = query.executeList();
c = pm.detachCopyAll(c);
tx.commit();
assertEquals("c.size() == 3", c.size(), 3);
for (Iterator i = c.iterator(); i.hasNext(); ) {
Employee em = (Employee) i.next();
try {
em.getManager();
} catch (Exception e) {
fail("Manager must be returned for maxFetchDepth of 1 : employee " + em.getFirstName() + " has no manager");
}
}
} 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