use of org.datanucleus.samples.instancecallback.InstanceCallbackContainer 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.InstanceCallbackContainer 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();
}
}
Aggregations