use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class MultithreadPMTest method testMultipleDetachCopyAndFetchPlanModification.
public void testMultipleDetachCopyAndFetchPlanModification() {
Properties multiProps = new Properties();
multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
PersistenceManagerFactory myPMF = getPMF(1, multiProps);
try {
int THREAD_SIZE = 1000;
Thread[] threads = new Thread[THREAD_SIZE];
Runnable[] runner = new Runnable[THREAD_SIZE];
PersistenceManager pm = myPMF.getPersistenceManager();
pm.currentTransaction().begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
woody.setManager(bart);
pm.makePersistent(woody);
pm.currentTransaction().commit();
pm.currentTransaction().begin();
try {
for (int i = 0; i < THREAD_SIZE; i++) {
if (i % 2 == 0) {
runner[i] = new MultithreadDetachRunner(pm, woody);
} else {
runner[i] = new MultithreadFetchPlanRunner(pm);
}
threads[i] = new Thread(runner[i]);
threads[i].start();
}
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i].join();
Exception e = null;
if (runner[i] instanceof MultithreadDetachRunner) {
e = ((MultithreadDetachRunner) runner[i]).getException();
} else if (runner[i] instanceof MultithreadFetchPlanRunner) {
e = ((MultithreadFetchPlanRunner) runner[i]).getException();
}
if (e != null) {
LOG.error("Exception during test", e);
fail("Exception thrown during test : " + e);
}
}
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(myPMF);
myPMF.close();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class MultithreadPMTest method testMultipleNonTransactionalRead.
/**
* Test changing the state
*/
public void testMultipleNonTransactionalRead() {
Properties multiProps = new Properties();
multiProps.setProperty(PropertyNames.PROPERTY_MULTITHREADED, "true");
PersistenceManagerFactory myPMF = getPMF(1, multiProps);
try {
int THREAD_SIZE = 1000;
Thread[] threads = new Thread[THREAD_SIZE];
PersistenceManager pm = myPMF.getPersistenceManager();
pm.currentTransaction().begin();
final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
woody.setManager(bart);
pm.makePersistent(woody);
pm.currentTransaction().commit();
pm.currentTransaction().setNontransactionalRead(true);
try {
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i] = new Thread(new Runnable() {
public void run() {
woody.getLastName();
woody.getManager().getLastName();
}
});
}
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());
}
}
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(myPMF);
myPMF.close();
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class SQLQueryTest method testSelectStarQuery.
/**
* Test of the use of SELECT * in a query.
*/
public void testSelectStarQuery() throws Exception {
try {
// Persist something to select
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Manager m1 = new Manager(1, "Barney", "Rubble", "barney.rubble@flintstones.com", 100, "123456");
pm.makePersistent(m1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Do an SQL query to find the Manager
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
String sqlText = "SELECT * FROM MANAGER";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.setClass(Manager.class);
List results = (List) query.execute();
assertTrue("\"SELECT *\" query returned null, yet should have returned some results", results != null);
assertEquals("Number of Manager objects retrieved from \"SELECT *\" query was incorrect", 1, results.size());
Manager mgr = (Manager) results.iterator().next();
// These will cause further SQL statements to retrieve the fields in the Person/Employee part of the object
assertEquals("\"SELECT *\" query returned Manager with incorrect first name", "Barney", mgr.getFirstName());
assertEquals("\"SELECT *\" query returned Manager with incorrect last name", "Rubble", mgr.getLastName());
assertEquals("\"SELECT *\" query returned Manager with incorrect email", "barney.rubble@flintstones.com", mgr.getEmailAddress());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown from \"SELECT *\" query : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class AttachDetachTest method testDetachAllOnCommitViaQuery.
/**
* Test "DetachAllOnCommit" when we retrieve objects via query and commit the txn
*/
public void testDetachAllOnCommitViaQuery() {
try {
// Persist some objects
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@warnerbros.com", 125, "123409");
Employee bugs = new Employee(2, "Bugs", "Bunny", "bugs@warnerbros.com", 200, "123410");
Account bugsAcct = new Account();
bugsAcct.setUsername("bugs");
bugsAcct.setEnabled(true);
bugs.setAccount(bugsAcct);
Manager donald = new Manager(3, "Donald", "Duck", "donald@warnerbros.com", 400, "123400");
donald.addSubordinate(woody);
donald.addSubordinate(bugs);
woody.setManager(donald);
bugs.setManager(donald);
pm.makePersistent(donald);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while persisting objects and committing transaction with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve Employees and check the detached states
pm = newPM();
pm.getFetchPlan().addGroup("groupSubordinates");
pm.getFetchPlan().addGroup("groupA");
pm.getFetchPlan().addGroup("groupC");
pm.getFetchPlan().setMaxFetchDepth(3);
((JDOPersistenceManager) pm).setDetachAllOnCommit(true);
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Employee.class);
Collection results = (Collection) q.execute();
// Detach all of Employees on commit
tx.commit();
assertThat((Collection<?>) results).as("Number of Employees retrieved is incorrect").hasSize(3);
Iterator empIter = results.iterator();
while (empIter.hasNext()) {
Employee emp = (Employee) empIter.next();
assertTrue("Employee " + StringUtils.toJVMIDString(emp) + " is not detached!", JDOHelper.isDetached(emp));
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while retrieving objects with detachAllOnCommit : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Manager in project tests by datanucleus.
the class AttachDetachTest method getDetachedManager.
/**
* Helper method for giving a detached Manager object
* @param id the manager object id
* @param fetchPlanName the fetch plan
* @return a detached Manager
*/
private Manager getDetachedManager(Object id, String fetchPlanName) {
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
pm.getFetchPlan().addGroup(fetchPlanName);
Object detached = null;
try {
tx.begin();
detached = pm.detachCopy(pm.getObjectById(id, true));
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
return (Manager) detached;
}
Aggregations