use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLSubqueryTest method testAPISubquery.
/**
* Test a subquery using the JDOQL Query API.
*/
public void testAPISubquery() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist 2 Employees
Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.flush();
// Find the Employees earning more than the average salary
Query q = pm.newQuery(Employee.class, "salary > averageSalary");
q.declareVariables("double averageSalary");
Query averageSalaryQuery = pm.newQuery("SELECT avg(salary) FROM " + Employee.class.getName());
q.addSubquery(averageSalaryQuery, "double averageSalary", null);
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
// Don't commit the data
tx.rollback();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLSubqueryTest method testSingleStringSubqueryWithFilter.
/**
* Test a simple subquery using single-string form and a filter in the subquery.
*/
public void testSingleStringSubqueryWithFilter() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist 2 Employees
Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.flush();
// Find the Employees earning more than the average salary of people with surname "S2"
Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE salary > (SELECT avg(e.salary) FROM " + Employee.class.getName() + " e WHERE e.lastName == 'S1')");
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
// Don't commit the data
tx.rollback();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class NucleusJDOHelperTest method testObjectState.
/**
* Check the return of object state values.
*/
public void testObjectState() throws Exception {
try {
Employee emp = new Employee(1, "Donald", "Duck", "donald.duck@warnerbros.com", 123, "ABCD");
assertEquals("Expected state is incorrect", ObjectState.TRANSIENT, JDOHelper.getObjectState(emp));
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist object and it moves to "persistent-new"
Employee emp1 = (Employee) pm.makePersistent(emp);
assertEquals("Expected state is incorrect", ObjectState.PERSISTENT_NEW, JDOHelper.getObjectState(emp1));
tx.commit();
// leave transaction and it moves to "hollow / persistent-nontransactional"
assertEquals("Expected state is incorrect", ObjectState.HOLLOW_PERSISTENT_NONTRANSACTIONAL, JDOHelper.getObjectState(emp1));
tx.begin();
// Update a field and it moves to "persistent-dirty"
emp1.setAge(28);
assertEquals("Expected state is incorrect", ObjectState.PERSISTENT_DIRTY, JDOHelper.getObjectState(emp1));
tx.commit();
// leave transaction and it moves to "hollow / persistent-nontransactional"
assertEquals("Expected state is incorrect", ObjectState.HOLLOW_PERSISTENT_NONTRANSACTIONAL, JDOHelper.getObjectState(emp1));
tx.begin();
pm.makeTransient(emp1);
assertEquals("Expected state is incorrect", ObjectState.TRANSIENT, JDOHelper.getObjectState(emp1));
tx.commit();
tx.begin();
// Persist it again
emp1 = (Employee) pm.makePersistent(emp1);
assertEquals("Expected state is incorrect", ObjectState.PERSISTENT_NEW, JDOHelper.getObjectState(emp1));
// Detach it
Employee detachedEmp = (Employee) pm.detachCopy(emp1);
tx.commit();
assertEquals("Expected state is incorrect", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedEmp));
// Modify it so it moves to "detached-dirty"
detachedEmp.setFirstName("Billy");
assertEquals("Expected state is incorrect", ObjectState.DETACHED_DIRTY, JDOHelper.getObjectState(detachedEmp));
} catch (Exception e) {
} 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 PersistenceManagerTest method testNontransactionalPersist.
/**
* Test use of makePersistent with nontransactionalWrite.
*/
public void testNontransactionalPersist() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
tx.setNontransactionalWrite(true);
Object id = null;
try {
Employee emp = new Employee(101, "George", "Green", "george.green@mydomain.com", (float) 123.5, "12346");
pm.makePersistent(emp);
id = pm.getObjectId(emp);
} catch (Exception e) {
fail("Exception thrown while calling makePersistent with nontransactionalWrite " + e.getMessage());
} finally {
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
try {
tx.begin();
Employee emp = (Employee) pm.getObjectById(id);
assertNotNull("Object persisted using nontransactionalWrite is null so wasnt persisted", emp);
assertEquals("First name is incorrect", "George", emp.getFirstName());
assertEquals("Second name is incorrect", "Green", emp.getLastName());
assertEquals("Email is incorrect", "george.green@mydomain.com", emp.getEmailAddress());
tx.commit();
} catch (Exception e) {
fail("Exception thrown while querying data persisted using nontransactionalWrite " + e.getMessage());
} 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 PersistenceManagerTest method testNormalFCOCollectionFieldPersistence1.
/**
* Test that FCOs added to a Collection field are persisted when the owning PC is persisted.
*/
public void testNormalFCOCollectionFieldPersistence1() {
PersistenceManager pm = pmf.getPersistenceManager();
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
try {
pm.currentTransaction().begin();
mgr.addSubordinate(emp1);
pm.makePersistent(mgr);
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
// get a fresh PM to ensure that any results aren't coming from the cache
try {
pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
mgr = (Manager) it.next();
Collection c = mgr.getSubordinates();
assertEquals(1, c.size());
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
assertTrue(it.hasNext());
Employee emp = (Employee) it.next();
assertTrue(c.contains(emp));
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().rollback();
pm.close();
}
}
Aggregations