use of org.datanucleus.samples.instancecallback.InstanceCallbackTester 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.datanucleus.samples.instancecallback.InstanceCallbackTester in project tests by datanucleus.
the class PersistenceManagerTest method testInstanceCallbacksInSCO.
/**
* Tests the InstanceCallback interface methods for elements in SCO fields
*/
public void testInstanceCallbacksInSCO() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
InstanceCallbackContainer owner = new InstanceCallbackContainer();
InstanceCallbackTester tester;
tester = new InstanceCallbackTester();
tester.setPersistentValue("value Set 1");
owner.addIcTesterToSet(tester);
tester = new InstanceCallbackTester();
tester.setPersistentValue("value Set 2");
owner.addIcTesterToSet(tester);
tester = new InstanceCallbackTester();
tester.setPersistentValue("value Map 1");
owner.addIcTesterToMap(tester);
tester = new InstanceCallbackTester();
tester.setPersistentValue("value Map 2");
owner.addIcTesterToMap(tester);
try {
tx.begin();
pm.makePersistent(owner);
tx.commit();
pm.close();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
// ////////////////////////////////////////
// test jdoPostLoad with evicted instances
// ////////////////////////////////////////
tx.begin();
Query q = pm.newQuery(pm.getExtent(InstanceCallbackContainer.class, false));
Collection c = (Collection) q.execute();
try {
assertEquals(1, c.size());
owner = (InstanceCallbackContainer) c.iterator().next();
Iterator it = owner.getIcTesters().iterator();
while (it.hasNext()) {
tester = ((InstanceCallbackTester) it.next());
assertNull(tester.getPersistentValue());
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
}
Iterator itMap = owner.getIcTestersByPersistentValue().values().iterator();
while (itMap.hasNext()) {
tester = ((InstanceCallbackTester) itMap.next());
assertNull(tester.getPersistentValue());
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
}
} finally {
q.closeAll();
}
tx.commit();
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
use of org.datanucleus.samples.instancecallback.InstanceCallbackTester in project tests by datanucleus.
the class PersistenceManagerTest method testInstanceCallbacks.
/**
* Tests the InstanceCallback interface methods
* @todo Add test for jdoPreClear()
*/
public void testInstanceCallbacks() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
InstanceCallbackTester tester = new InstanceCallbackTester();
tester.setPersistentValue("value");
try {
tx.begin();
pm.makePersistent(tester);
tx.commit();
Query q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
tx.begin();
Collection c = (Collection) q.execute();
// ////////////////////////////////////////
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
// test jdPreStore()
assertNull(tester.getPersistentValue());
pm.refresh(tester);
// test jdoPostLoad()
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
} finally {
q.closeAll();
}
// ////////////////////////////////////////
// test jdoPostLoad using refresh for calling the post load in a persistent-dirty instance
// ////////////////////////////////////////
q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
c = (Collection) q.execute();
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
tester.setPersistentValue("dirty pers");
tester.setTransientValue("dirty trans");
pm.refresh(tester);
assertNull(tester.getPersistentValue());
// test jdoPostLoad()
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
} finally {
q.closeAll();
}
// ////////////////////////////////////////
// test jdoPostLoad using ignoreCache = true on query
// ////////////////////////////////////////
q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
q.setIgnoreCache(true);
c = (Collection) q.execute();
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
// test jdoPostLoad()
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
} finally {
q.closeAll();
}
// ////////////////////////////////////////
// test jdoPreDelete
// ////////////////////////////////////////
q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
c = (Collection) q.execute();
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
// test jdoPreDelete()
try {
tester.setTransientValue(null);
pm.deletePersistent(tester);
fail("Delete persistent didn't fail but should have");
} catch (JDOUserCallbackException e) {
// callback throws exception with PreDeleteException as the nested exception
}
} finally {
q.closeAll();
}
tx.commit();
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
Aggregations