use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testRetrieve.
/**
* test of retrieve()
*/
public void testRetrieve() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = null;
Object id = null;
// Create a Department with its Manager (1-1 relationship)
try {
tx = pm.currentTransaction();
tx.begin();
Department d = new Department("dept1");
d.setManager(new Manager(new Random().nextLong(), "mgrFN", "mgrLN", "mgr@mgr.com", (float) 100.10, "mgrSERIAL"));
pm.makePersistent(d);
tx.commit();
id = pm.getObjectId(d);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Try making it transient without specifying the FetchPlan
// The Manager shouldn't become transient here since it isn't in the default "FetchPlan" for Department.
pm = pmf.getPersistenceManager();
Department d = null;
try {
tx = pm.currentTransaction();
tx.begin();
d = (Department) pm.getObjectById(id, true);
// Will retrieve all fields in "d"
pm.retrieve(d);
pm.makeTransient(d);
pm.makeTransient(d.getManager());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
assertTrue("The name attribute of Department wasn't retrieved", d.getName().trim().equals("dept1"));
// Make Department and Manager transient, using FetchPlan and check after the close of the PM.
pm = pmf.getPersistenceManager();
Department d2 = null;
try {
tx = pm.currentTransaction();
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
d2 = (Department) pm.getObjectById(id, true);
// Will retrieve all fields in "d2"
pm.retrieve(d2);
// Retrieve all fields in "d2.getManager()"
pm.retrieve(d2.getManager());
pm.makeTransient(d2);
pm.makeTransient(d2.getManager());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
assertTrue("The name attribute of Department wasn't retrieved", d2.getName().trim().equals("dept1"));
assertTrue("The Manager attribute of Department wasn't retrieved", d2.getManager() != null);
assertTrue("The serial number attribute of Department wasnt retrieved", d2.getManager().getSerialNo() != null);
assertTrue("The Manager attribute of Department wasn't retrieved correctly", d2.getManager().getSerialNo().trim().equals("mgrSERIAL"));
// Make Department and Manager transient, using FetchPlan and check after end of transaction but before close of PM.
pm = pmf.getPersistenceManager();
try {
tx = pm.currentTransaction();
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
d2 = (Department) pm.getObjectById(id, true);
pm.retrieve(d2);
pm.retrieve(d2.getManager());
pm.makeTransient(d2);
pm.makeTransient(d2.getManager());
tx.commit();
assertTrue("The name attribute of Department wasn't retrieved", d2.getName().trim().equals("dept1"));
assertTrue("The Manager attribute of Department wasn't retrieved", d2.getManager() != null);
assertTrue("The serial number attribute of Department wasnt retrieved", d2.getManager().getSerialNo() != null);
assertTrue("The Manager attribute of Department wasn't retrieved correctly", d2.getManager().getSerialNo().trim().equals("mgrSERIAL"));
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Manager.class);
clean(Department.class);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testQueryPM.
/**
* Tests that the persistence manager used to persist an object is the same
* as returned by jdoGetPersistenceManager()
*/
public void testQueryPM() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = null;
try {
tx.begin();
Department d = new Department("Engineering");
mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
mgr.addDepartment(d);
pm.makePersistent(mgr);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
mgr = (Manager) it.next();
assertSame(pm, JDOHelper.getPersistenceManager(mgr));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Manager.class);
clean(Department.class);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testElementDeletionRemovesFromFKCollection.
/**
* Test that deleting an object that is a member of a FK Collection also removes it from the Collection.
*/
public void testElementDeletionRemovesFromFKCollection() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Department d = new Department("Engineering");
d.setManager(mgr);
mgr.addDepartment(d);
tx.begin();
pm.makePersistent(d);
tx.commit();
tx.begin();
pm.deletePersistent(d);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown when deleting member of a FK collection", e);
fail("Exception thrown when deleting a member of a FK collection " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// get a fresh PM to ensure that any results aren't coming from the cache
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
Manager mgr = (Manager) it.next();
Collection c = mgr.getDepartments();
assertTrue("Departments should have been null or empty", c == null || c.size() == 0);
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
assertTrue(!(it.hasNext()));
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence2.
/**
* Test that deleting an object that is a member of a Collection field
* throws an exception
*/
public void testNormalFCOCollectionFieldPersistence2() {
/*
* If constraints aren't being used then the assumptions of this test don't hold.
*/
if (!getConfigurationForPMF(pmf).getBooleanProperty("datanucleus.validateConstraints")) {
return;
}
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
try {
mgr.addSubordinate(emp1);
tx.begin();
pm.makePersistent(mgr);
tx.commit();
tx.begin();
try {
pm.deletePersistent(emp1);
tx.commit();
fail("Commit transaction was successful but shouldn't have been");
} catch (javax.jdo.JDODataStoreException e) {
if (tx.isActive())
tx.rollback();
}
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to perform test");
}
pm.close();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testMakeTransientOwnerAndElements.
/**
* Test of makeTransient() for owner and makeTransientAll() for the owner elements.
*/
public void testMakeTransientOwnerAndElements() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// JoinTable 1-N relationship
// Create a Manager with subordinate number 0
createNewManager(pm, 0);
// Make the Manager transient with all subordinates
tx.begin();
Manager m = (Manager) pm.getExtent(Manager.class, true).iterator().next();
pm.retrieveAll(m.getSubordinates());
pm.retrieveAll(m.getDepartments());
pm.makeTransient(m);
pm.makeTransientAll(m.getSubordinates());
pm.makeTransientAll(m.getDepartments());
tx.commit();
// Check the result
assertNull(JDOHelper.getObjectId(m));
// Compare the managers
Manager m1 = queryManager(0, pm);
tx.begin();
Assert.assertEquals(m.getBestFriend(), m1.getBestFriend());
Assert.assertEquals(m.getLastName(), m1.getLastName());
Assert.assertEquals(m.getFirstName(), m1.getFirstName());
Assert.assertEquals(m.getEmailAddress(), m1.getEmailAddress());
Assert.assertEquals(m.getPersonNum(), m1.getPersonNum());
Assert.assertTrue("subordinates are not the same", Manager.compareSet(m.getSubordinates(), m1.getSubordinates()));
Assert.assertTrue("departments are not the same", Manager.compareSet(m.getDepartments(), m1.getDepartments()));
tx.commit();
// FK 1-N relationship
// Create owner with 2 elements.
tx = pm.currentTransaction();
tx.begin();
Farm farm = new Farm("Giles Farm");
Animal an1 = new Animal("Dog");
an1.setFarm(farm);
Animal an2 = new Animal("Cow");
an2.setFarm(farm);
farm.addAnimal(an1);
farm.addAnimal(an2);
pm.makePersistent(farm);
tx.commit();
Object id = pm.getObjectId(farm);
// Make the owner and its elements transient
tx = pm.currentTransaction();
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Farm transientFarm = (Farm) pm.getObjectById(id, true);
pm.retrieveAll(transientFarm.getAnimals());
pm.makeTransient(transientFarm);
pm.makeTransientAll(transientFarm.getAnimals());
tx.commit();
// Check the result
Set transientAnimals = transientFarm.getAnimals();
assertFalse("Owner object is not transient!", JDOHelper.isPersistent(transientFarm));
assertEquals("Number of elements in transient owner is incorrect", transientAnimals.size(), 2);
Iterator transientAnimalsIter = transientAnimals.iterator();
boolean dogPresent = false;
boolean cowPresent = false;
while (transientAnimalsIter.hasNext()) {
Animal transientAnimal = (Animal) transientAnimalsIter.next();
assertFalse("Element object is not transient!", JDOHelper.isPersistent(transientAnimal));
// Check that the Animal name field is the same as that persisted
if (transientAnimal.getName().equals("Dog")) {
dogPresent = true;
} else if (transientAnimal.getName().equals("Cow")) {
cowPresent = true;
}
// Check that the owner is the transient one
assertEquals("Owner of transient animal is incorrect", StringUtils.toJVMIDString(transientAnimal.getFarm()), StringUtils.toJVMIDString(transientFarm));
}
assertTrue("First value animal is not present in the transient set", dogPresent);
assertTrue("Second value animal is not present in the transient set", cowPresent);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
clean(Manager.class);
clean(Widget.class);
}
}
Aggregations