use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLQueryTest method testCast.
/**
* Test use of a cast operator.
*/
public void testCast() {
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 Employee with the specified serial number
LOG.info(">> Querying for cast Employee serial number");
Query q = pm.newQuery(Person.class, "((Employee)this).serialNo == \"10001\"");
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees with serial number was incorrect", 1, results.size());
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLQueryTest method testSingleStringSubquery.
/**
* Test a subquery using the JDOQL Query as single-string.
*/
public void testSingleStringSubquery() {
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("JDOQL", "SELECT FROM " + Employee.class.getName() + " WHERE salary > (SELECT avg(salary) FROM " + Employee.class.getName() + ")");
q.addExtension("datanucleus.query.evaluateInMemory", "true");
q.compile();
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());
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLQueryTest 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
LOG.info(">> Querying for Employees with salary above the average, and the average is defined by subquery");
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());
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testQueryOfInterface.
/**
* Test for the querying of an interface (persistent).
*/
public void testQueryOfInterface() {
Mouse mouse1 = new Mouse();
mouse1.setId(101);
mouse1.setManufacturer("Logitech");
mouse1.setModel("M305");
Keyboard kb1 = new Keyboard();
kb1.setId(102);
kb1.setManufacturer("Logitech");
kb1.setModel("K304");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(mouse1);
pm.makePersistent(kb1);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(ComputerPeripheral.class);
List<ComputerPeripheral> results = (List<ComputerPeripheral>) q.execute();
assertEquals("Number of results incorrect", 2, results.size());
Iterator<ComputerPeripheral> resultsIter = results.iterator();
boolean mousePresent = false;
boolean kbPresent = false;
while (resultsIter.hasNext()) {
ComputerPeripheral peri = resultsIter.next();
if (peri instanceof Mouse && peri.getId() == 101 && peri.getManufacturer().equals("Logitech") && peri.getModel().equals("M305")) {
mousePresent = true;
}
if (peri instanceof Keyboard && peri.getId() == 102 && peri.getManufacturer().equals("Logitech") && peri.getModel().equals("K304")) {
kbPresent = true;
}
}
if (!mousePresent) {
fail("Mouse not present in results");
}
if (!kbPresent) {
fail("Keyboard not present in results");
}
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Mouse.class);
clean(Keyboard.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testSingleString.
/**
* Test case to use JDOQL single-string
*/
public void testSingleString() {
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");
// Eh, what's up, doc?
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
// Meep! Meep!
Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery("SELECT UNIQUE FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES WHERE firstName == \"Bugs\"");
q.setClass(Employee.class);
Object results = q.execute();
assertTrue("UNIQUE query returned an object of an incorrect type : should have been Employee but was " + results, results instanceof Employee);
q.closeAll();
q = pm.newQuery("SELECT UNIQUE FROM " + Employee.class.getName() + " EXCLUDE SUBCLASSES WHERE firstName == \"Bugs\" import " + Employee.class.getName());
q.setClass(Employee.class);
results = q.execute();
assertTrue("UNIQUE query returned an object of an incorrect type : should have been Employee but was " + results, results instanceof Employee);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
}
}
Aggregations