use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class ExtentTest method testExtentSubclasses.
/**
* Test use of pm.getExtent and use of the subclasses flag.
*/
public void testExtentSubclasses() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee empl = new Employee(0, "Homer", "Simpson", "homer@simpsons.com", (float) 200000.0, "123");
Manager mgr = new Manager(1, "Matt", "Groening", "matt@simpsons.com", (float) 500000.0, "1");
pm.makePersistent(empl);
pm.makePersistent(mgr);
pm.flush();
// test subclasses argument == false (should contain Employee only)
Extent<Employee> extent = pm.getExtent(Employee.class, false);
java.util.Iterator<Employee> it = extent.iterator();
Employee empl2 = it.next();
assertEquals(empl.getPersonNum(), empl2.getPersonNum());
assertEquals(false, it.hasNext());
tx.commit();
// test subclasses argument == true (should contain Employee AND Manager)
tx.begin();
extent = pm.getExtent(Employee.class, true);
it = extent.iterator();
empl2 = it.next();
if (empl2 instanceof Manager) {
assertEquals(1, empl2.getPersonNum());
pm.deletePersistent(empl2);
empl2 = (Employee) it.next();
assertEquals(0, empl2.getPersonNum());
pm.deletePersistent(empl2);
} else {
assertEquals(0, empl2.getPersonNum());
pm.deletePersistent(empl2);
empl2 = (Manager) it.next();
assertEquals(1, empl2.getPersonNum());
pm.deletePersistent(empl2);
}
assertEquals(false, it.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Manager.class);
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class MultithreadedTest method processQueryAndDetach.
protected String processQueryAndDetach(boolean transaction) {
List<Employee> results = null;
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_DETACH_ON_CLOSE, "true");
pm.getFetchPlan().setGroup("all");
pm.getFetchPlan().setMaxFetchDepth(-1);
Transaction tx = pm.currentTransaction();
try {
if (transaction) {
tx.begin();
}
Query<Employee> q = pm.newQuery(Employee.class);
List<Employee> emps = q.executeList();
results = new ArrayList<Employee>(emps);
if (transaction) {
tx.commit();
}
} catch (Throwable thr) {
LOG.error("Exception query objects", thr);
return "Exception in query : " + thr.getMessage();
} finally {
if (transaction && tx.isActive()) {
tx.rollback();
}
// Detached the Employees and their loaded fields
pm.close();
}
for (Employee e : results) {
try {
LOG.debug(">> Employee: " + e.getFirstName() + " " + e.getLastName() + " bestFriend=" + e.getBestFriend());
if (e instanceof Manager) {
Set subs = ((Manager) e).getSubordinates();
if (subs == null) {
return "Manager object didnt have its subordinates detached!";
} else if (subs.size() != 100) {
return "Manager had " + subs.size() + " subordinates instead of 100";
}
} else {
Manager mgr = e.getManager();
if (mgr == null) {
return "Employee=" + e + " didnt have its manager set!";
} else {
Set<Employee> subs = mgr.getSubordinates();
if (subs == null) {
return "Employee=" + e + " didnt have its subordinates set!";
} else if (subs.size() != 100) {
return "Employee=" + e + " has Manager with " + subs.size() + " subordinates instead of 100";
}
for (Employee subE : subs) {
subE.toString();
}
}
}
} catch (Exception exc) {
LOG.error(">> Exception thrown on check of results", exc);
return "Exception thrown : " + exc.getMessage();
}
}
return null;
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class MultithreadedTest method testFind.
/**
* Test that populates the datastore, and then starts many threads retrieving objects.
*/
public void testFind() {
try {
// Persist some data
LOG.debug(">> Persisting data");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object mgrId = null;
try {
tx.begin();
Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
pm.makePersistent(mgr);
for (int i = 0; i < 100; i++) {
Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
emp.setManager(mgr);
mgr.addSubordinate(emp);
pm.makePersistent(emp);
}
tx.commit();
mgrId = JDOHelper.getObjectId(mgr);
} catch (Throwable thr) {
LOG.error("Exception persisting objects", thr);
fail("Exception persisting data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
LOG.debug(">> Persisted data");
// Verify the persistence
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query<Employee> q = pm.newQuery(Employee.class);
List<Employee> emps = q.executeList();
for (Employee e : emps) {
LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
}
LOG.debug(">> Queried Employees");
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception checking objects", thr);
fail("Exception checking data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Create the Threads
int THREAD_SIZE = 500;
final Object managerId = mgrId;
final String[] threadErrors = new String[THREAD_SIZE];
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() {
String errorMsg = processFind(managerId, true);
threadErrors[threadNo] = errorMsg;
}
});
}
// Run the Threads
LOG.debug(">> Starting threads");
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i].start();
}
for (int i = 0; i < THREAD_SIZE; i++) {
try {
threads[i].join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
}
LOG.debug(">> Completed threads");
// Process any errors and fail the test if any threads failed present
for (String error : threadErrors) {
if (error != null) {
fail(error);
}
}
} finally {
// Clean out data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLBasicTest method testBoolean.
public void testBoolean() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(homer);
tx.commit();
tx.begin();
Query q = pm.newQuery(Manager.class);
q.setFilter("1 == 1");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
q = pm.newQuery(Manager.class);
q.setFilter("1 == 0");
c = (Collection) q.execute();
assertEquals(0, c.size());
q = pm.newQuery(Manager.class);
q.setFilter("true == true");
c = (Collection) q.execute();
assertEquals(1, c.size());
q = pm.newQuery(Manager.class);
q.setFilter("true == false");
c = (Collection) q.execute();
assertEquals(0, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class JDOQLBasicTest method testNonTx.
/**
* Test query executed outside of a transaction that will cause another object
* to be fetched during the query (fetching that object must not close the
* query's connection)
*/
public void testNonTx() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "testNonTx 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 1, "testNonTx 2");
bart.setManager(homer);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(bart);
pm.makePersistent(homer);
tx.commit();
} finally {
if (tx.isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
// use a fresh PM so caches are empty
pm = pmf.getPersistenceManager();
// important for this test is that Employee.manager is in the default fetch group
Query q = pm.newQuery(Employee.class);
q.setFilter("personNum==2");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
Aggregations