use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerProxyTest method testProxyInMultiThreads.
/**
* Test for use of PM proxy using multiple threads.
* In this test we create a proxy in sub threads only, so don't re-use the underlying PM of the main thread.
*/
public void testProxyInMultiThreads() throws Exception {
try {
// Persist some objects in main thread (without proxy)
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Manager m1 = new Manager(101, "Daffy", "Duck", "daffy.duck@warnerbros.com", 105.45f, "123407");
pm.makePersistent(m1);
Manager m2 = new Manager(102, "Donald", "Duck", "donald.duck@warnerbros.com", 105.46f, "123408");
pm.makePersistent(m2);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Start multiple threads to persist the object
int THREAD_SIZE = 5;
Thread[] threads = new Thread[THREAD_SIZE];
for (int i = 0; i < THREAD_SIZE; i++) {
final int threadNo = i;
threads[i] = new Thread(new Runnable() {
public void run() {
PersistenceManager pmthread = pmf.getPersistenceManagerProxy();
try {
pmthread.currentTransaction().begin();
Manager m2 = new Manager(110 + threadNo, "Donald", "Duck", "donald.duck@warnerbros.com", 105.46f, "123408");
pmthread.makePersistent(m2);
pmthread.currentTransaction().commit();
} catch (Exception e) {
NucleusLogger.GENERAL.error("Exception while persisting object in thread " + threadNo, e);
fail("Exception thrown while accessing object in thread " + threadNo + " : " + e.getMessage());
} finally {
if (pmthread.currentTransaction().isActive()) {
pmthread.currentTransaction().rollback();
}
pmthread.close();
}
}
});
}
// Start the threads
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i].start();
}
// Wait for the end of the threads
for (int i = 0; i < THREAD_SIZE; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
// Get a new PM and check
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
// Check that our objects are persisted
tx.begin();
Query<Manager> q = pm.newQuery(Manager.class);
Collection<Manager> results = q.executeList();
assertEquals("Number of persisted objects is incorrect", 7, results.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Manager.class);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerProxyTest method testProxyResourceLocal.
/**
* Test for use of PM proxy using resource-local transactions.
*/
public void testProxyResourceLocal() throws Exception {
try {
// Create a PM proxy
PersistenceManager pm = pmf.getPersistenceManagerProxy();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Manager m1 = new Manager(101, "Daffy", "Duck", "daffy.duck@warnerbros.com", 105.45f, "123407");
pm.makePersistent(m1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
// Close the proxy, which will close the underlying PM but not the proxy
pm.close();
}
// Try to access something on the proxy to check if it is closed
try {
tx = pm.currentTransaction();
// The proxy should have created a new delegate
} catch (Exception e) {
fail("Access to the PM methods after close should have worked, but failed " + e.getMessage());
}
try {
tx.begin();
Manager m2 = new Manager(102, "Donald", "Duck", "donald.duck@warnerbros.com", 105.46f, "123408");
pm.makePersistent(m2);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
// Close the proxy, which will close the underlying PM but not the proxy
pm.close();
}
// PM closed so just re-get the txn
tx = pm.currentTransaction();
try {
// Check that our objects are persisted
tx.begin();
Query<Manager> q = pm.newQuery(Manager.class);
Collection<Manager> results = q.executeList();
assertEquals("Number of persisted objects is incorrect", 2, results.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
// Close the proxy, which will close the underlying PM but not the proxy
pm.close();
}
} finally {
// Clean out our data
clean(Manager.class);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability2.
/**
* Test that when an element with N-1 relation with FK collection is persisted, the owning PC is persisted also.
* TODO Move to reachability tests.
*/
public void testFKCollectionFieldPersistenceByReachability2() {
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(d);
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));
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 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();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class PersistenceManagerTest method queryManager.
private Manager queryManager(long managerNum, PersistenceManager pm) {
Transaction tx = pm.currentTransaction();
try {
tx.setRetainValues(true);
tx.begin();
Extent<Manager> clnManager = pm.getExtent(Manager.class, true);
Query<Manager> q = pm.newQuery(clnManager);
try {
Collection<Manager> managers = q.executeList();
Iterator<Manager> i = managers.iterator();
while (i.hasNext()) {
Manager p = i.next();
if (p.getPersonNum() == managerNum) {
return p;
}
}
} finally {
q.closeAll();
}
} finally {
tx.commit();
}
return null;
}
Aggregations