use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JDOQLTest method testRetrieve.
/**
* Query returning an object with relation fields, testing the contents of the relation fields.
*/
public void testRetrieve() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery("SELECT FROM " + Person.class.getName() + " EXCLUDE SUBCLASSES" + " WHERE firstName == 'Lami'");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
Iterator it = c.iterator();
Person lami = (Person) it.next();
assertEquals("FirstName is incorrect", "Lami", lami.getFirstName());
assertNotNull("Value of field storing relation is null but shouldnt be", lami.getBestFriend());
assertEquals("FirstName of related object is incorrect", "Bugs", lami.getBestFriend().getFirstName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JDOQLTest method testFilter.
/**
* result test
*/
public void testFilter() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery("SELECT FROM " + Person.class.getName() + " EXCLUDE SUBCLASSES WHERE firstName == 'Ana'");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
Iterator it = c.iterator();
assertEquals("Ana", ((Person) it.next()).getFirstName());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Person 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.jpox.samples.models.company.Person in project tests by datanucleus.
the class PersistenceManagerTest method testDeletePersistentExceptions.
/**
* Test attempts to delete a transient object.
*/
public void testDeletePersistentExceptions() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
// test attempt to redelete transient object
try {
tx.begin();
pm.deletePersistent(p);
fail("calling deletePersistent on transient object should fail");
} catch (JDOUserException e) {
} finally {
tx.rollback();
}
// test attempt to delete a P-new-del
tx.begin();
pm.makePersistent(p);
pm.deletePersistent(p);
try {
pm.deletePersistent(p);
} catch (JDOUserException e) {
fail("calling deletePersistent on a P-del object should work");
}
tx.rollback();
// test attempt to delete a P-del
p = createNewPerson(pm, 0);
tx.begin();
pm.deletePersistent(p);
try {
pm.deletePersistent(p);
} catch (JDOUserException e) {
fail("calling deletePersistent on a P-del object should work");
}
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Person.class);
}
}
use of org.jpox.samples.models.company.Person 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();
}
}
Aggregations