use of org.datanucleus.samples.lifecyclelistener.BasicListener in project tests by datanucleus.
the class PersistenceManagerProxyTest method testLifecycleListenerRegisteredInPMFforAllClasses.
/**
* Test of lifecycle listener registered for all classes
*/
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
BasicListener listener = new BasicListener(true);
PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
pmf.addInstanceLifecycleListener(listener, null);
freezePMF(pmf);
try {
PersistenceManager pm = pmf.getPersistenceManagerProxy();
Transaction tx = pm.currentTransaction();
int i = 0;
try {
tx.begin();
// Persist an object and check the events
Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
pm.makePersistent(person);
// Persist related objects and check the events
// Manager has a 1-N (FK) with Department
Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
Department dept1 = new Department("Invasions");
Department dept2 = new Department("Propaganda");
Department dept3 = new Department("Lies");
manager.addDepartment(dept1);
manager.addDepartment(dept2);
manager.addDepartment(dept3);
dept1.setManager(manager);
dept2.setManager(manager);
dept3.setManager(manager);
pm.makePersistent(manager);
pm.flush();
Integer[] events = listener.getRegisteredEventsAsArray();
assertEquals("Wrong number of lifecycle events", 15, events.length);
if (tx.getOptimistic()) {
// Person
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
int numPreStore = 0;
int numPostStore = 0;
int numEventsToProcess = i + 10;
for (int j = i; j < numEventsToProcess; j++) {
if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
numPreStore++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
numPostStore++;
}
i++;
}
// 1 for each object
assertEquals("Number of PreStore events is wrong", 5, numPreStore);
// 1 for each object
assertEquals("Number of PostStore events is wrong", 5, numPostStore);
} else {
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
}
tx.rollback();
listener.getRegisteredEvents().clear();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm.currentTransaction();
try {
tx2.begin();
// Persist an object and check the events
pm.makePersistent(person);
pm.flush();
events = listener.getRegisteredEventsAsArray();
i = 0;
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
tx2.rollback();
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
} catch (Exception e) {
LOG.error("Exception while running lifecycle listener simple object test", e);
fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
listener.getRegisteredEvents().clear();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
pmf.close();
}
}
use of org.datanucleus.samples.lifecyclelistener.BasicListener in project tests by datanucleus.
the class PersistenceManagerTest method testLifecycleListenerRegisteredInPMFforAllClasses.
/**
* Test of lifecycle listener registered for all classes
*/
public void testLifecycleListenerRegisteredInPMFforAllClasses() {
BasicListener listener = new BasicListener(true);
PersistenceManagerFactory pmf = getConfigurablePMF(1, null);
pmf.addInstanceLifecycleListener(listener, null);
freezePMF(pmf);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
int i = 0;
try {
tx.begin();
// Persist an object and check the events
Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
pm.makePersistent(person);
// Persist related objects and check the events
// Manager has a 1-N (FK) with Department
Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
Department dept1 = new Department("Invasions");
Department dept2 = new Department("Propaganda");
Department dept3 = new Department("Lies");
manager.addDepartment(dept1);
manager.addDepartment(dept2);
manager.addDepartment(dept3);
dept1.setManager(manager);
dept2.setManager(manager);
dept3.setManager(manager);
pm.makePersistent(manager);
pm.flush();
Integer[] events = listener.getRegisteredEventsAsArray();
assertEquals("Wrong number of lifecycle events", 15, events.length);
if (tx.getOptimistic()) {
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Expecting 5 PreStore and 5 PostStore; 1 for each of the objects
int numPreStore = 0;
int numPostStore = 0;
int numOther = 0;
for (int j = i; j < 15; j++) {
if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
numPreStore++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
numPostStore++;
} else {
numOther++;
}
i++;
}
assertEquals("Number of PreStore events was incorrect", 5, numPreStore);
assertEquals("Number of PostStore events was incorrect", 5, numPostStore);
assertEquals("Number of other events was incorrect", 0, numOther);
} else {
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
}
tx.rollback();
listener.getRegisteredEvents().clear();
PersistenceManager pm2 = pmf.getPersistenceManager();
Transaction tx2 = pm.currentTransaction();
try {
tx2.begin();
// Persist an object and check the events
pm.makePersistent(person);
pm.flush();
events = listener.getRegisteredEventsAsArray();
i = 0;
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
tx2.rollback();
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
listener.getRegisteredEvents().clear();
pmf.close();
}
}
use of org.datanucleus.samples.lifecyclelistener.BasicListener in project tests by datanucleus.
the class PersistenceManagerTest method testLifecycleListenerForSimpleObjects.
/**
* Test of basic lifecycle listener behaviour, listeneing to the changes in the lifecycle
* of a simple object (with no relationships). The object is persisted, then updated, then detached
* then updated (whilst detached), then attached, and finally deleted. This exercises all listener
* event types.
*/
public void testLifecycleListenerForSimpleObjects() {
BasicListener listener = new BasicListener(true);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
int i = 0;
try {
pm.addInstanceLifecycleListener(listener, new Class[] { Person.class });
Object person_id;
tx.begin();
// Persist an object and check the events
Person person = new Person(12345, "Fred", "Smith", "Fred.Smith@jpox.org");
pm.makePersistent(person);
pm.flush();
Integer[] events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Save the object id
person_id = pm.getObjectId(person);
// Clean the cache and retrieve the object from datastore
tx.begin();
pm.evictAll();
// Update a field on the object and check the events
person = (Person) pm.getObjectById(person_id);
person.setEmailAddress("Fred.Smith@aol.com");
pm.flush();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
tx.begin();
pm.evictAll();
// Retrieve the object and detach it
person = (Person) pm.getObjectById(person_id);
Person detachedPerson = (Person) pm.detachCopy(person);
pm.flush();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DETACH, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_DETACH, events[i++].intValue());
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
if (tx.getOptimistic()) {
// Nothing here. TODO Why are there no preClear/postClear for optimistic yet there are for pessimistic?
} else {
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
}
// Update the detached object
detachedPerson.setLastName("Green");
tx.begin();
// Attach the detached object
pm.makePersistent(detachedPerson);
pm.flush();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_PRE_ATTACH, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_ATTACH, events[i++].intValue());
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
// Make sure the get goes to the DB
pmf.getDataStoreCache().evictAll();
// Make sure the get goes to the DB
pm.evictAll();
tx.begin();
// Delete the object and check the events
person = (Person) pm.getObjectById(person_id);
pm.deletePersistent(person);
pm.flush();
events = listener.getRegisteredEventsAsArray();
if (tx.getOptimistic()) {
// TODO Why 2 preDelete here?
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DELETE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DELETE, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_DELETE, events[i++].intValue());
} else {
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DELETE, events[i++].intValue());
if (vendorID == null) {
// Not present for RDBMS
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
}
assertEquals(LifecycleListenerSpecification.EVENT_POST_DELETE, events[i++].intValue());
}
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while running lifecycle listener simple object test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
listener.getRegisteredEvents().clear();
}
}
use of org.datanucleus.samples.lifecyclelistener.BasicListener in project tests by datanucleus.
the class PersistenceManagerTest method testLifecycleListenerForCollections.
/**
* Test of basic lifecycle listener behaviour, listeneing to the changes in the lifecycle
* of an object with a collection of other objects.
*/
public void testLifecycleListenerForCollections() {
BasicListener listener = new BasicListener(true);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
int i = 0;
try {
pm.addInstanceLifecycleListener(listener, new Class[] { Manager.class, Department.class });
Object managerId;
Object dept2Id;
tx.begin();
// Persist related objects and check the events
// Manager has a 1-N (FK) with Department
Manager manager = new Manager(12346, "George", "Bush", "george.bush@thewhitehouse.com", 2000000, "ABC-DEF");
Department dept1 = new Department("Invasions");
Department dept2 = new Department("Propaganda");
Department dept3 = new Department("Lies");
manager.addDepartment(dept1);
manager.addDepartment(dept2);
manager.addDepartment(dept3);
dept1.setManager(manager);
dept2.setManager(manager);
dept3.setManager(manager);
pm.makePersistent(manager);
pm.flush();
Integer[] events = listener.getRegisteredEventsAsArray();
if (tx.getOptimistic()) {
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
int numPreStore = 0;
int numPostStore = 0;
int numEventsToProcess = i + 8;
for (int j = i; j < numEventsToProcess; j++) {
if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_STORE) {
numPreStore++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_STORE) {
numPostStore++;
}
i++;
}
assertEquals("Number of PreStore events is wrong", 4, numPreStore);
assertEquals("Number of PostStore events is wrong", 4, numPostStore);
} else {
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CREATE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
}
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
// Evict anything in the L2 cache so we know we are going to the
// datastore, and hence get predictable callback ordering
pmf.getDataStoreCache().evictAll();
// Save the object ids
managerId = pm.getObjectId(manager);
dept2Id = pm.getObjectId(dept2);
// Make sure the get goes to the DB
pmf.getDataStoreCache().evictAll();
// Make sure the get goes to the DB
pm.evictAll();
tx.begin();
dept2 = (Department) pm.getObjectById(dept2Id);
manager = (Manager) pm.getObjectById(managerId);
// Remove manager of dept2 and check the events
dept2.setManager(null);
manager.removeDepartment(dept2);
pm.flush();
events = listener.getRegisteredEventsAsArray();
if (tx.getOptimistic()) {
int numPostLoad = 0;
int numPreDirty = 0;
int numPostDirty = 0;
int numEventsToProcess = i + 8;
for (int j = i; j < numEventsToProcess; j++) {
if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_LOAD) {
numPostLoad++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_PRE_DIRTY) {
numPreDirty++;
} else if (events[j].intValue() == LifecycleListenerSpecification.EVENT_POST_DIRTY) {
numPostDirty++;
}
i++;
}
assertEquals("Number of PostLoad is wrong", 4, numPostLoad);
// 1 for Department2 and 1 for Manager
assertEquals("Number of PreDirty is wrong", 2, numPreDirty);
// 1 for Department2 and 1 for Manager
assertEquals("Number of PostDirty is wrong", 2, numPostDirty);
} else {
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
if (vendorID == null) {
// Not needed on RDBMS for some reason
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
}
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_DIRTY, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_DIRTY, events[i++].intValue());
}
// Commit the changes and check the events
tx.commit();
events = listener.getRegisteredEventsAsArray();
if (tx.getOptimistic()) {
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
} else {
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
if (vendorID != null) {
// RDBMS loads here
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
assertEquals(LifecycleListenerSpecification.EVENT_POST_LOAD, events[i++].intValue());
}
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_STORE, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Manager
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 2
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
if (vendorID == null) {
// Clear the other 2 departments
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 1
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_PRE_CLEAR, events[i++].intValue());
// Department 3
assertEquals(LifecycleListenerSpecification.EVENT_POST_CLEAR, events[i++].intValue());
}
}
assertTrue("Total number of lifecycle events received was incorrect : should have been " + i + " but was " + events.length, events.length == i);
// TODO Add attach/detach of the Manager and its Departments.
} catch (Exception e) {
LOG.error("Exception thrown in test", e);
fail("Exception thrown while running lifecycle listener collection test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
listener.getRegisteredEvents().clear();
}
}
Aggregations