use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence1.
/**
* Test that FCOs added to a Collection field are persisted when the owning PC is persisted.
*/
public void testNormalFCOCollectionFieldPersistence1() {
PersistenceManager pm = pmf.getPersistenceManager();
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 {
pm.currentTransaction().begin();
mgr.addSubordinate(emp1);
pm.makePersistent(mgr);
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
// get a fresh PM to ensure that any results aren't coming from the cache
try {
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
mgr = (Manager) it.next();
Collection c = mgr.getSubordinates();
assertEquals(1, c.size());
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
assertTrue(it.hasNext());
Employee emp = (Employee) it.next();
assertTrue(c.contains(emp));
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testInheritedFieldsPersisted.
/**
* Test that inherited fields are persisted.
*/
public void testInheritedFieldsPersisted() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = null;
try {
tx.begin();
mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
pm.makePersistent(mgr);
tx.commit();
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
mgr = (Manager) it.next();
assertEquals(FIRSTNAME[0], mgr.getFirstName());
assertEquals(LASTNAME[0], mgr.getLastName());
assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
pm.currentTransaction().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 {
// get a fresh pm to ensure that data is coming from the store,
// not the cache
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
mgr = (Manager) it.next();
assertEquals(FIRSTNAME[0], mgr.getFirstName());
assertEquals(LASTNAME[0], mgr.getLastName());
assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Manager.class);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence3.
/**
* Test that removing a member of a normal Collection field does NOT delete
* the object that was removed
*/
public void testNormalFCOCollectionFieldPersistence3() {
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();
mgr.getSubordinates().remove(emp1);
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 ex = pm.getExtent(Employee.class, false);
java.util.Iterator it = ex.iterator();
assertTrue(it.hasNext());
Employee emp = (Employee) it.next();
assertEquals(1, emp.getPersonNum());
tx.commit();
} 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 testInverseFCOCollectionFieldPersistence4.
/**
* Test that setting the inverse reference of an object implicitly adds it
* to the inverse collection of the "owning" object
*/
public void testInverseFCOCollectionFieldPersistence4() {
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]);
try {
Department d = new Department("Engineering");
d.setManager(mgr);
tx.begin();
pm.makePersistent(d);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
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());
mgr = (Manager) it.next();
Collection c = mgr.getDepartments();
assertEquals(1, c.size());
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
assertTrue(it.hasNext());
Department d = (Department) it.next();
assertTrue(c.contains(d));
tx.commit();
} 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 testTransientObjectCollections.
/**
* Tests that objects can be added to a Collection owned by a persistent object made transient.
*/
public void testTransientObjectCollections() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Employee emp = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
try {
tx.begin();
pm.makePersistent(mgr);
tx.commit();
tx = pm.currentTransaction();
tx.begin();
pm.retrieve(mgr);
pm.retrieveAll(mgr.getSubordinates());
pm.retrieveAll(mgr.getDepartments());
pm.makeTransient(mgr);
mgr.addSubordinate(emp);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while making object with collection transient : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations