use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class AttachDetachTest method testDetachAllOnCommitViaQuery.
/**
* Test "DetachAllOnCommit" when we retrieve objects via query and commit the txn
*/
public void testDetachAllOnCommitViaQuery() {
try {
// Persist some objects
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
Account bugsAcct = new Account();
bugsAcct.setUsername("bugs");
bugsAcct.setEnabled(true);
bugs.setAccount(bugsAcct);
Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
donald.addSubordinate(woody);
donald.addSubordinate(bugs);
woody.setManager(donald);
bugs.setManager(donald);
pm.makePersistent(donald);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve Employees and check the detached states
pm = newPM();
pm.getFetchPlan().addGroup("groupSubordinates");
pm.getFetchPlan().addGroup("groupA");
pm.getFetchPlan().addGroup("groupC");
pm.getFetchPlan().setMaxFetchDepth(3);
((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Employee.class);
Collection results = (Collection) q.execute();
// Detach all of Employees on commit
tx.commit();
assertThat((Collection<?>) results).as("Number of Employees retrieved is incorrect").hasSize(3);
Iterator empIter = results.iterator();
while (empIter.hasNext()) {
Employee emp = (Employee) empIter.next();
assertTrue("Employee " + StringUtils.toJVMIDString(emp) + " is not detached!", JDOHelper.isDetached(emp));
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class AttachDetachTest method testDetachAllOnCommitViaFetch.
/**
* Test "DetachAllOnCommit" when we retrieve an object and commit the txn
*/
public void testDetachAllOnCommitViaFetch() {
try {
Object id = null;
// Persist some objects
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
Account bugsAcct = new Account();
bugsAcct.setUsername("bugs");
bugsAcct.setEnabled(true);
bugs.setAccount(bugsAcct);
Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
donald.addSubordinate(woody);
donald.addSubordinate(bugs);
woody.setManager(donald);
bugs.setManager(donald);
pm.makePersistent(donald);
tx.commit();
id = JDOHelper.getObjectId(donald);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve an object and check the detached states
pm = newPM();
pm.getFetchPlan().addGroup("groupSubordinates");
pm.getFetchPlan().addGroup("groupA");
pm.getFetchPlan().addGroup("groupC");
pm.getFetchPlan().setMaxFetchDepth(3);
((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
tx = pm.currentTransaction();
try {
tx.begin();
Manager donald = (Manager) pm.getObjectById(id);
tx.commit();
Set employees = donald.getSubordinates();
assertNotNull("Donald was not detached and should have been at commit", donald);
assertNotNull("Employees of Donald were not detached and should have been at commit", employees);
assertEquals("Donald Duck has incorrect number of employees after detach at commit", 2, donald.getSubordinates().size());
Employee bugs = null;
Employee woody = null;
Account bugsAcct = null;
Iterator emplIter = employees.iterator();
while (emplIter.hasNext()) {
Employee emp = (Employee) emplIter.next();
if (emp.getFirstName().equals("Bugs")) {
bugs = emp;
} else if (emp.getFirstName().equals("Woody")) {
woody = emp;
}
}
assertNotNull("Bugs Bunny was not detached and should have been at commit", bugs);
assertNotNull("Woody Woodpecker was not detached and should have been at commit", woody);
bugsAcct = bugs.getAccount();
assertNotNull("Account of Bugs Bunny was not detached and should have been at commit", bugsAcct);
// Check that all are now detached
if (!JDOHelper.isDetached(bugs) || JDOHelper.getObjectId(bugs) == null) {
fail("Bugs Bunny is not detached or hasn't been detached correctly after closing the PM");
}
if (!JDOHelper.isDetached(woody) || JDOHelper.getObjectId(woody) == null) {
fail("Woody Woodpecker is not detached or hasn't been detached correctly after closing the PM");
}
if (!JDOHelper.isDetached(donald) || JDOHelper.getObjectId(donald) == null) {
fail("Donald Duck is not detached or hasn't been detached correctly after closing the PM");
}
// Check that the relationships are intact
if (!woody.getFirstName().equals("Woody") || !woody.getLastName().equals("Woodpecker")) {
fail("Woody Woodpecker has lost his name after closing the PM");
}
assertNotNull("Woody has a null Manager after detach", woody.getManager());
assertEquals("Woody has lost the relation to his detached Manager after commit", woody.getManager(), donald);
if (!bugs.getFirstName().equals("Bugs") || !bugs.getLastName().equals("Bunny")) {
fail("Bugs Bunny has lost his name after closing the PM");
}
assertNotNull("Bugs has a null Manager after detach", bugs.getManager());
assertEquals("Bugs has lost the relation to his detached Manager after commit", bugs.getManager(), donald);
if (!donald.getFirstName().equals("Donald") || !donald.getLastName().equals("Duck")) {
fail("Donald Duck has lost his name after closing the PM");
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class AttachDetachTest method testDetachAllOnCommitViaFetchUnlimited.
/**
* Test "DetachAllOnCommit" when we retrieve an object and commit the txn.
* Slight variation on previous test in that this uses a max fetch depth of -1 so testing for stack overflow
*/
public void testDetachAllOnCommitViaFetchUnlimited() {
try {
Object id = null;
// Persist some objects
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
Account bugsAcct = new Account();
bugsAcct.setUsername("bugs");
bugsAcct.setEnabled(true);
bugs.setAccount(bugsAcct);
Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
donald.addSubordinate(woody);
donald.addSubordinate(bugs);
woody.setManager(donald);
bugs.setManager(donald);
pm.makePersistent(donald);
tx.commit();
id = JDOHelper.getObjectId(donald);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve an object and check the detached states
pm = newPM();
pm.getFetchPlan().addGroup("groupSubordinates");
pm.getFetchPlan().addGroup("groupA");
pm.getFetchPlan().addGroup("groupC");
pm.getFetchPlan().setMaxFetchDepth(-1);
((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
tx = pm.currentTransaction();
try {
tx.begin();
Manager donald = (Manager) pm.getObjectById(id);
tx.commit();
Set employees = donald.getSubordinates();
assertNotNull("Donald was not detached and should have been at commit", donald);
assertNotNull("Employees of Donald were not detached and should have been at commit", employees);
assertEquals("Donald Duck has incorrect number of employees after detach at commit", 2, donald.getSubordinates().size());
Employee bugs = null;
Employee woody = null;
Account bugsAcct = null;
Iterator emplIter = employees.iterator();
while (emplIter.hasNext()) {
Employee emp = (Employee) emplIter.next();
if (emp.getFirstName().equals("Bugs")) {
bugs = emp;
} else if (emp.getFirstName().equals("Woody")) {
woody = emp;
}
}
assertNotNull("Bugs Bunny was not detached and should have been at commit", bugs);
assertNotNull("Woody Woodpecker was not detached and should have been at commit", woody);
bugsAcct = bugs.getAccount();
assertNotNull("Account of Bugs Bunny was not detached and should have been at commit", bugsAcct);
// Check that all are now detached
if (!JDOHelper.isDetached(bugs) || JDOHelper.getObjectId(bugs) == null) {
fail("Bugs Bunny is not detached or hasn't been detached correctly after closing the PM");
}
if (!JDOHelper.isDetached(woody) || JDOHelper.getObjectId(woody) == null) {
fail("Woody Woodpecker is not detached or hasn't been detached correctly after closing the PM");
}
if (!JDOHelper.isDetached(donald) || JDOHelper.getObjectId(donald) == null) {
fail("Donald Duck is not detached or hasn't been detached correctly after closing the PM");
}
// Check that the relationships are intact
if (!woody.getFirstName().equals("Woody") || !woody.getLastName().equals("Woodpecker")) {
fail("Woody Woodpecker has lost his name after closing the PM");
}
assertNotNull("Woody has a null Manager after detach", woody.getManager());
assertEquals("Woody has lost the relation to his detached Manager after commit", woody.getManager(), donald);
if (!bugs.getFirstName().equals("Bugs") || !bugs.getLastName().equals("Bunny")) {
fail("Bugs Bunny has lost his name after closing the PM");
}
assertNotNull("Bugs has a null Manager after detach", bugs.getManager());
assertEquals("Bugs has lost the relation to his detached Manager after commit", bugs.getManager(), donald);
if (!donald.getFirstName().equals("Donald") || !donald.getLastName().equals("Duck")) {
fail("Donald Duck has lost his name after closing the PM");
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Account in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testInsertMultipleClassesToSameXPath.
public void testInsertMultipleClassesToSameXPath() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p = new Person();
p.setPersonNum(4);
p.setGlobalNum("4");
p.setFirstName("Bugs4");
p.setLastName("Bunny4");
Account a = new Account();
a.setId(1);
a.setUsername("testusername");
pm.makePersistent(p);
pm.makePersistent(a);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Person.class);
clean(Account.class);
}
}
Aggregations