use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SQLQueryTest method testWithCandidateClassWithResultClass.
/**
* Test of SQL queries with a candidate class AND a result class.
*/
public void testWithCandidateClassWithResultClass() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some basic data to query, and query using a ResultClass that is constructor based
tx = pm.currentTransaction();
tx.begin();
Person pers1 = new Person(234568, "Homer", "Simpson", "homer.simpson@fox.com");
pers1.setAge(45);
Person pers2 = new Person(234578, "Marge", "Simpson", "marge.simpson@fox.com");
pers1.setAge(42);
pm.makePersistent(pers1);
pm.makePersistent(pers2);
tx.commit();
tx = pm.currentTransaction();
tx.begin();
try {
Query amountQuery = pm.newNamedQuery(Person.class, "PersonDetails");
List results = (List) amountQuery.execute();
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
Object obj = resultsIter.next();
assertEquals("ResultClass of query is incorrect.", PersonalDetails.class.getName(), obj.getClass().getName());
}
amountQuery.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
// Create some inherited data to query and query using a ResultClass without constructor
tx = pm.currentTransaction();
tx.begin();
Developer p3 = new Developer(13, "James", "Java", "james@java.com", (float) 15.00, "1234569", new Integer(3), "jdo");
pm.makePersistent(p3);
tx.commit();
tx = pm.currentTransaction();
tx.begin();
Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkillForResult");
List results = (List) inhQuery.execute("jdo");
Iterator iter = results.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been DeveloperRC", obj.getClass().getName().equals(DeveloperRC.class.getName()));
DeveloperRC p = (DeveloperRC) obj;
assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
}
tx.commit();
// Create some inherited data to query and query using INNER JOIN and users class
tx = pm.currentTransaction();
tx.begin();
Developer p4 = new Developer(100, "Mike", "Microsoft", "mike@microsoft.com", 10, "1234570", new Integer(3), ".net");
p4.setGlobalNum("GUID-p4");
pm.makePersistent(p4);
tx.commit();
tx = pm.currentTransaction();
tx.begin();
Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResult");
results = (List) inhQuery2.execute(".net");
iter = results.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been DeveloperRC", obj.getClass().getName().equals(DeveloperRC.class.getName()));
DeveloperRC p = (DeveloperRC) obj;
assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", p.getSKILL().equals(".net"));
assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", p.getGlobalNum().equals("GUID-p4"));
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, p.personNum);
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, (int) p.salary);
}
tx.commit();
// Create some inherited data to query and query using HashMap.
tx = pm.currentTransaction();
tx.begin();
Developer p5 = new Developer();
p5.setFirstName("John");
p5.setSKILL("uml");
p5.setGlobalNum("GUID-p5");
p5.setSalary(10);
p5.setPersonNum(100);
p5.setSerialNo("" + new Random(System.currentTimeMillis()).nextInt());
pm.makePersistent(p5);
tx.commit();
// ResultClass = java.util.HashMap
tx = pm.currentTransaction();
tx.begin();
Query inhQuery3 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultHashMap");
results = (List) inhQuery3.execute("uml");
iter = results.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been HashMap", obj.getClass().getName().equals(HashMap.class.getName()));
HashMap p = (HashMap) obj;
assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "SKILL").equals("uml"));
assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "GLOBALNUM").equals("GUID-p5"));
Number salary = (Number) getValueForKeyInMapCaseInsensitive(p, "salary");
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, salary.intValue());
Object personNumObj = getValueForKeyInMapCaseInsensitive(p, "PERSONNUM");
if (personNumObj instanceof Long) {
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((Long) personNumObj).intValue());
} else if (personNumObj instanceof BigDecimal) {
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((BigDecimal) personNumObj).intValue());
} else {
fail("Test doest support keys in map of type " + personNumObj.getClass());
}
}
tx.commit();
// ResultClass = java.util.Map
tx = pm.currentTransaction();
tx.begin();
Query inhQuery4 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultMap");
results = (List) inhQuery4.execute("uml");
iter = results.iterator();
while (iter.hasNext()) {
Object obj = iter.next();
assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been HashMap", obj.getClass().getName().equals(HashMap.class.getName()));
HashMap p = (HashMap) obj;
assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "SKILL").equals("uml"));
assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "GLOBALNUM").equals("GUID-p5"));
Number salary = (Number) getValueForKeyInMapCaseInsensitive(p, "salary");
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, salary.intValue());
Object personNumObj = getValueForKeyInMapCaseInsensitive(p, "PERSONNUM");
if (personNumObj instanceof Long) {
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((Long) personNumObj).intValue());
} else if (personNumObj instanceof BigDecimal) {
assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((BigDecimal) personNumObj).intValue());
} else {
fail("Test doest support keys in map of type " + personNumObj.getClass());
}
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while querying with candidate class and result class : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Developer.class);
clean(Person.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class SQLQueryTest method testQueryWithTimeout.
/**
* Test of a query with a timeout.
*/
public void testQueryWithTimeout() throws Exception {
// Try a query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// TODO Change this to a query that will take a LONG time and check for it
String sqlText = "SELECT count(*) FROM PERSON";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.addExtension("org.jpox.query.timeout", "1");
query.execute();
tx.commit();
} catch (JDODataStoreException dse) {
fail("JDODataStoreException thrown when using query timeout : " + dse.getCause().getMessage());
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
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);
}
}
Aggregations