use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class MultithreadPMTest method testMultipleDetachCopy.
public void testMultipleDetachCopy() {
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];
MultithreadDetachRunner[] runner = new MultithreadDetachRunner[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++) {
runner[i] = new MultithreadDetachRunner(pm, woody);
threads[i] = new Thread(runner[i]);
threads[i].start();
}
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i].join();
if (runner[i].getException() != null) {
LOG.error("Exception during test", runner[i].getException());
fail("Exception thrown during test : " + runner[i].getException());
}
}
} 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.Employee in project tests by datanucleus.
the class JDOQLBasicTest method testStringToUpperCase.
/**
* Test the use of the String.toUpperCase() method.
*/
public void testStringToUpperCase() {
try {
Employee woody = new Employee(1, "Woody", "Woodpecker", "Woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "Bart@simpson.com", 2, "serial 2");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
tx.commit();
tx.begin();
try {
// test 1
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("lastName.toUpperCase() == \"WOODPECKER\"");
HashSet results = new HashSet((Collection) q.execute());
assertEquals("Received incorrect number of results to String.toUpperCase()", results.size(), 1);
Employee emp = (Employee) results.iterator().next();
assertEquals("First name of retrieved object is incorrect", emp.getFirstName(), "Woody");
assertEquals("Email of retrieved object is incorrect", emp.getEmailAddress(), "Woody@woodpecker.com");
q.closeAll();
// test 2
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("\"WOODY\" == firstName.toUpperCase()");
results = new HashSet((Collection) q.execute());
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
// test 3
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.declareParameters("java.lang.String var1");
q.setFilter("var1.toUpperCase() == lastName.toUpperCase()");
results = new HashSet((Collection) q.execute("WoOdPeCkEr"));
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
} catch (JDOUserException e) {
LOG.error("Exception during test", e);
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLBasicTest method testInstanceof.
/**
* Test case to use JDOQL "instanceof"
*/
public void testInstanceof() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// 1. instanceof where the candidate class uses "new-table" (union)
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
// Eh, what's up, doc?
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
// Meep! Meep!
Manager roadrunner = new Manager(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(Employee.class, "this instanceof " + Manager.class.getName());
List results = (List) q.execute();
assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
Object obj = results.get(0);
assertTrue("Type of object returned should have been Manager but wasnt", obj instanceof Manager);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
// 2. instanceof where the candidate class using "superclass-table" (discriminator)
tx.begin();
ElectronicJournal elecJournal = new ElectronicJournal(1, "Electronics Weekly");
PrintJournal printJournal = new PrintJournal(2, "Glossy magazine");
pm.makePersistent(elecJournal);
pm.makePersistent(printJournal);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(AbstractJournal.class, "this instanceof " + PrintJournal.class.getName());
List results = (List) q.execute();
assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
Object obj = results.get(0);
assertTrue("Type of object returned should have been PrintJournal but wasnt", obj instanceof PrintJournal);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(PrintJournal.class);
clean(ElectronicJournal.class);
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLBasicTest method testAvgWithHaving.
public void testAvgWithHaving() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 10, "serial 1");
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 5, "serial 3");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 5, "serial 2");
woody.setAge(30);
bunny.setAge(30);
bart.setAge(15);
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
tx.commit();
tx.begin();
try {
LOG.info(">> QUERY WITH GROUPBY AVG");
Query q = pm.newQuery("SELECT age, avg(salary) FROM " + Employee.class.getName() + " GROUP BY age HAVING avg(salary) > 6");
List results = (List) q.execute();
// Create a nice representation of the result for error reporting
StringBuffer resultString = new StringBuffer();
for (Object object : results) {
resultString.append(Arrays.toString((Object[]) object));
}
assertEquals("Did not return the correct number of age groups: " + resultString, 1, results.size());
Object[] ageGroup = (Object[]) results.get(0);
assertEquals("Did not return the correct age group: " + resultString, 30, ageGroup[0]);
assertEquals("Did not return the correct avg salary: " + resultString, 7.5, ageGroup[1]);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLBasicTest method testStringToLowerCase.
/**
* Test the use of the String.toLowerCase() method.
*/
public void testStringToLowerCase() {
try {
Employee woody = new Employee(1, "Woody", "Woodpecker", "Woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "Bart@simpson.com", 2, "serial 2");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
tx.commit();
tx.begin();
try {
// test 1
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("lastName.toLowerCase() == \"woodpecker\"");
HashSet results = new HashSet((Collection) q.execute());
assertEquals("Received incorrect number of results to String.toUpperCase()", results.size(), 1);
Employee emp = (Employee) results.iterator().next();
assertEquals("First name of retrieved object is incorrect", emp.getFirstName(), "Woody");
assertEquals("Email of retrieved object is incorrect", emp.getEmailAddress(), "Woody@woodpecker.com");
q.closeAll();
// test 2
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("\"woody\" == firstName.toLowerCase()");
results = new HashSet((Collection) q.execute());
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
// test 3
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.declareParameters("java.lang.String var1");
q.setFilter("var1.toLowerCase() == lastName.toLowerCase()");
results = new HashSet((Collection) q.execute("WoOdPeCkEr"));
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
} catch (JDOUserException e) {
LOG.error("Exception during test", e);
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(Employee.class);
}
}
Aggregations