use of org.jpox.samples.models.company.Department 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.Department 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.Department in project tests by datanucleus.
the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability1.
/**
* Test that when a FK collection is persisted the element is persisted also.
* TODO Move to reachability tests.
*/
public void testFKCollectionFieldPersistenceByReachability1() {
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);
mgr.addDepartment(d);
tx.begin();
pm.makePersistent(mgr);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown when persisting FK collection using reachability", e);
fail("Exception thrown when persisting FK collection using reachability " + 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());
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));
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class PersistenceManagerTest method tearDown.
/**
* Invoked after each test is run
*/
public void tearDown() throws java.lang.Exception {
super.tearDown();
// TODO Remove all of this when each test cleans out its own data.
Extent ext = null;
java.util.Iterator it = null;
PersistenceManager pm = pmf.getPersistenceManager();
try {
// delete all InstanceCallbackContainer objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackContainer.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
pm.deletePersistent(owner);
}
pm.currentTransaction().commit();
// delete all InstanceCallbackTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
// necesaary to avoid exception from jdoPreDelete() for this class only
tester.setTransientValue("");
pm.deletePersistent(tester);
}
pm.currentTransaction().commit();
// delete all InversePrimitive objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
InversePrimitive ip = (InversePrimitive) it.next();
ip.setTester(null);
pm.deletePersistent(ip);
}
pm.currentTransaction().commit();
// delete all CollectionFieldTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(CollectionFieldTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
CollectionFieldTester t = (CollectionFieldTester) it.next();
if (t.getPrimitiveCollection() != null) {
t.getPrimitiveCollection().clear();
}
if (t.getInversePrimitiveCollection() != null) {
t.getInversePrimitiveCollection().clear();
}
pm.deletePersistent(t);
}
pm.currentTransaction().commit();
// delete all Primative objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
Primitive p = (Primitive) it.next();
pm.deletePersistent(p);
}
pm.currentTransaction().commit();
// disassociate all Employees and Departments from their Managers
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
if (mgr.getSubordinates() != null) {
mgr.getSubordinates().clear();
}
if (mgr.getDepartments() != null) {
mgr.getDepartments().clear();
}
}
pm.currentTransaction().commit();
// delete all Employee objects
pm.currentTransaction().begin();
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
pm.deletePersistent(emp);
}
pm.currentTransaction().commit();
pm.currentTransaction().begin();
// dekete all Department objects
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
while (it.hasNext()) {
Department d = (Department) it.next();
pm.deletePersistent(d);
}
pm.currentTransaction().commit();
// delete all Manager objects
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
pm.deletePersistent(mgr);
}
pm.currentTransaction().commit();
// delete all Person objects
pm.currentTransaction().begin();
ext = pm.getExtent(Person.class, true);
it = ext.iterator();
while (it.hasNext()) {
Person person = (Person) it.next();
pm.deletePersistent(person);
}
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().commit();
pm.close();
}
}
use of org.jpox.samples.models.company.Department in project tests by datanucleus.
the class JDOQLBasicTest method testCast.
public void testCast() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
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");
bart.addSubordinate(boss);
bart.addSubordinate(boss2);
Department deptB = new Department("DeptB");
bart.addDepartment(deptB);
Qualification q1 = new Qualification("q1");
q1.setPerson(boss);
Qualification q2 = new Qualification("q2");
q2.setPerson(boss2);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(bart);
pm.makePersistent(homer);
pm.makePersistent(boss);
pm.makePersistent(boss2);
pm.makePersistent(q1);
pm.makePersistent(q2);
tx.commit();
tx.begin();
Query q = pm.newQuery(Qualification.class);
Collection c = q.filter("((Employee)person).serialNo == \"serial 3\"").imports("import org.jpox.samples.models.company.Employee").executeList();
assertEquals(1, c.size());
assertEquals("q1", ((Qualification) c.iterator().next()).getName());
q = pm.newQuery(Qualification.class);
c = q.filter("((Manager)person).serialNo == \"serial 4\"").imports("import org.jpox.samples.models.company.Employee").executeList();
assertEquals(1, c.size());
assertEquals("q2", ((Qualification) c.iterator().next()).getName());
tx.commit();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
Aggregations