You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-dev@db.apache.org by mb...@apache.org on 2019/02/27 20:24:17 UTC
[db-jdo] 01/01: JDO-652: SampleQueries: use try-with-resource when
creating query instances and added JDOQLTypedQuery test cases
This is an automated email from the ASF dual-hosted git repository.
mbo pushed a commit to branch JDO-652
in repository https://gitbox.apache.org/repos/asf/db-jdo.git
commit 72f3b1928915103987dcf829d56fd0d2b7ef4568
Author: Michael Bouschen <mb...@apache.org>
AuthorDate: Wed Feb 27 21:23:56 2019 +0100
JDO-652: SampleQueries: use try-with-resource when creating query instances and added JDOQLTypedQuery test cases
---
.../apache/jdo/tck/query/api/SampleQueries.java | 2229 ++++++++++++--------
1 file changed, 1395 insertions(+), 834 deletions(-)
diff --git a/tck/src/main/java/org/apache/jdo/tck/query/api/SampleQueries.java b/tck/src/main/java/org/apache/jdo/tck/query/api/SampleQueries.java
index eb58f4b..acb2b77 100644
--- a/tck/src/main/java/org/apache/jdo/tck/query/api/SampleQueries.java
+++ b/tck/src/main/java/org/apache/jdo/tck/query/api/SampleQueries.java
@@ -6,13 +6,22 @@ import org.apache.jdo.tck.pc.company.Department;
import org.apache.jdo.tck.pc.company.Employee;
import org.apache.jdo.tck.pc.company.FullTimeEmployee;
import org.apache.jdo.tck.pc.company.Person;
+import org.apache.jdo.tck.pc.company.QDepartment;
+import org.apache.jdo.tck.pc.company.QEmployee;
+import org.apache.jdo.tck.pc.company.QFullTimeEmployee;
import org.apache.jdo.tck.query.QueryTest;
import org.apache.jdo.tck.util.BatchTestRunner;
import org.apache.jdo.tck.util.EqualityHelper;
+import javax.jdo.JDOQLTypedQuery;
+import javax.jdo.JDOQLTypedSubquery;
import javax.jdo.Query;
import javax.jdo.Transaction;
+import javax.jdo.query.CollectionExpression;
+import javax.jdo.query.NumericExpression;
+import javax.jdo.query.StringExpression;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -27,7 +36,7 @@ import java.util.Map;
*<B>Assertion Description: </B>
* This test class runs the example queries from the JDO specification.
*
- * There are up to five test methods per test case:
+ * There are up to six test methods per test case:
* testQueryxxa: runtime constructed JDO query using execute to run the query
* testQueryxxb: runtime constructed JDO query using setNamedParameters to specify the parameter values and
* executeList/executeResultList/executeResultUnique to run the query
@@ -35,6 +44,7 @@ import java.util.Map;
* executeList/executeResultList/executeResultUnique to run the query
* testQueryxxd: single string version of the JDO query
* testQueryxxe: named query version of the JDO query
+ * testQueryxxf: JDOQLTypedQuery version
*/
public class SampleQueries extends QueryTest {
@@ -54,6 +64,80 @@ public class SampleQueries extends QueryTest {
BatchTestRunner.run(SampleQueries.class);
}
+ private static final String SINGLE_STRING_QUERY_01 =
+ "select from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > 30000";
+
+ private static final String SINGLE_STRING_QUERY_02 =
+ "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
+ "where salary > 30000 order by salary ascending";
+
+ private static final String SINGLE_STRING_QUERY_03 =
+ "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
+ "where salary > :sal && firstname.startsWith(:begin)";
+
+ private static final String SINGLE_STRING_QUERY_04 =
+ "select from org.apache.jdo.tck.pc.company.Employee where department.name == :dep";
+
+ private static final String SINGLE_STRING_QUERY_05 =
+ "select from org.apache.jdo.tck.pc.company.Department " +
+ "where employees.contains(emp) && emp.weeklyhours > :hours variables Employee emp";
+
+ private static final String SINGLE_STRING_QUERY_06 =
+ "select from org.apache.jdo.tck.pc.company.Department where :depts.contains(name)";
+
+ private static final String SINGLE_STRING_QUERY_07 =
+ "select firstname from org.apache.jdo.tck.pc.company.Employee where department.name == :deptName";
+
+ private static final String SINGLE_STRING_QUERY_08 =
+ "select firstname, salary, manager as reportsTo into org.apache.jdo.tck.query.api.SampleQueries$Info " +
+ "from org.apache.jdo.tck.pc.company.FullTimeEmployee where department.name == :deptName";
+
+ private static final String SINGLE_STRING_QUERY_09 =
+ "select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary, manager) " +
+ "from org.apache.jdo.tck.pc.company.FullTimeEmployee where department.name == :deptName";
+
+ private static final String SINGLE_STRING_QUERY_10 =
+ "select avg(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
+ "where department.name == :deptName";
+
+ private static final String SINGLE_STRING_QUERY_11 =
+ "select avg(salary), sum(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
+ "where department.name == :deptName";
+
+ private static final String SINGLE_STRING_QUERY_12 =
+ "select avg(salary), sum(salary), department.name " +
+ "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
+ "group by department.name having count(department.name) > 1";
+
+ private static final String SINGLE_STRING_QUERY_13 =
+ "select unique this from org.apache.jdo.tck.pc.company.Employee where firstname == :empName";
+
+ private static final String SINGLE_STRING_QUERY_14 =
+ "select unique salary from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
+ "where firstname == :empName";
+
+ private static final String SINGLE_STRING_QUERY_15 =
+ "select into org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper " +
+ "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
+
+ private static final String SINGLE_STRING_QUERY_16 =
+ "select into org.apache.jdo.tck.query.api.SampleQueries$EmpInfo " +
+ "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
+
+ private static final String SINGLE_STRING_QUERY_17 =
+ "select e.firstname from org.apache.jdo.tck.pc.company.Department " +
+ "where name.startsWith('R&D') && employees.contains(e) " +
+ "variables org.apache.jdo.tck.pc.company.Employee e";
+
+ private static final String SINGLE_STRING_QUERY_18 =
+ "select firstname from org.apache.jdo.tck.pc.company.Employee " +
+ "where this.weeklyhours > (select avg(e.weeklyhours) from org.apache.jdo.tck.pc.company.Employee e)";
+
+ private static final String SINGLE_STRING_QUERY_19 =
+ "select firstname from org.apache.jdo.tck.pc.company.Employee " +
+ "where this.weeklyhours > " +
+ " (select AVG(e.weeklyhours) from this.department.employees e where e.manager == this.manager)";
+
/**
* Basic query.
*
@@ -68,13 +152,14 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > 30000";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000");
- List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute();
List<FullTimeEmployee> expected =
- getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp5"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ getTransientCompanyModelInstancesAsList(new String[]{"emp1", "emp2", "emp5"});
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000")) {
+ List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_01, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -97,13 +182,14 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > 30000";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000");
- List<FullTimeEmployee> emps = q.executeList();
List<FullTimeEmployee> expected =
getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp5"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000")) {
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_01, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -126,13 +212,46 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > 30000";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- List<FullTimeEmployee> emps = q.executeList();
List<FullTimeEmployee> expected =
getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp5"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_01)) {
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_01, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Basic query.
+ *
+ * This query selects all Employee instances from the candidate collection where
+ * the salary is greater than the constant 30000.
+ * Note that the float value for salary is unwrapped for the comparison with the
+ * literal int value, which is promoted to float using numeric promotion.
+ * If the value for the salary field in a candidate instance isnull, then it cannot
+ * be unwrapped for the comparison, and the candidate instance is rejected.
+ */
+ public void testQuery01f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<FullTimeEmployee> expected =
+ getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp5"});
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.filter(cand.salary.gt(30000.));
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_01, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -151,15 +270,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > 30000 order by salary ascending";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000");
- q.setOrdering ("salary ascending");
- List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute();
List<FullTimeEmployee> expected =
getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp5", "emp2"});
- checkQueryResultWithOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000")) {
+ q.setOrdering ("salary ascending");
+ List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute();
+ checkQueryResultWithOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_02, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -178,15 +297,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > 30000 order by salary ascending";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000");
- q.setOrdering ("salary ascending");
- List<FullTimeEmployee> emps = q.executeList();
List<FullTimeEmployee> expected =
getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp5", "emp2"});
- checkQueryResultWithOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > 30000")) {
+ q.setOrdering ("salary ascending");
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_02, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -205,14 +324,43 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > 30000 order by salary ascending";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- List<FullTimeEmployee> emps = q.executeList();
List<FullTimeEmployee> expected =
getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp5", "emp2"});
- checkQueryResultWithOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_02)) {
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_02, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Basic query with ordering.
+ *
+ * This query selects all Employee instances from the candidate collection where the salary
+ * is greater than the constant 30000, and returns a Collection ordered based on employee salary.
+ */
+ public void testQuery02f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<FullTimeEmployee> expected =
+ getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp5", "emp2"});
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.filter(cand.salary.gt(30000.));
+ q.orderBy(cand.salary.asc());
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_02, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -234,15 +382,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > :sal && firstname.startsWith(:begin)";
- Query<FullTimeEmployee> q =
- pm.newQuery(FullTimeEmployee.class,"salary > sal && firstname.startsWith(begin)");
- q.declareParameters("Double sal, String begin");
- List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute(30000., "M");
List<FullTimeEmployee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class,"salary > sal && firstname.startsWith(begin)")) {
+ q.declareParameters("Double sal, String begin");
+ List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute(30000., "M");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_03, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -264,19 +412,19 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > :sal && firstname.startsWith(:begin)";
- Query<FullTimeEmployee> q =
- pm.newQuery(FullTimeEmployee.class,"salary > sal && firstname.startsWith(begin)");
- q.declareParameters("Double sal, String begin");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("sal", 30000.);
- paramValues.put("begin", "M");
- q.setNamedParameters(paramValues);
- List<FullTimeEmployee> emps = q.executeList();
List<FullTimeEmployee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class,"salary > sal && firstname.startsWith(begin)")) {
+ q.declareParameters("Double sal, String begin");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("sal", 30000.);
+ paramValues.put("begin", "M");
+ q.setNamedParameters(paramValues);
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_03, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -298,16 +446,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > :sal && firstname.startsWith(:begin)";
- Query<FullTimeEmployee> q =
- pm.newQuery(FullTimeEmployee.class,"salary > sal && firstname.startsWith(begin)");
- q.declareParameters("Double sal, String begin");
- q.setParameters(30000., "M");
- List<FullTimeEmployee> emps = q.executeList();
List<FullTimeEmployee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class,"salary > sal && firstname.startsWith(begin)")) {
+ q.declareParameters("Double sal, String begin");
+ q.setParameters(30000., "M");
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_03, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -329,13 +477,49 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where salary > :sal && firstname.startsWith(:begin)";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute(30000., "M");
List<FullTimeEmployee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_03)) {
+ List<FullTimeEmployee> emps = (List<FullTimeEmployee>)q.execute(30000., "M");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_03, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Parameter passing.
+ *
+ * This query selects all Employee instances from the candidate collection where the salary
+ * is greater than the value passed as a parameter and the name starts with the value passed
+ * as a second parameter.
+ * If the value for the salary field in a candidate instance is null, then it cannot be
+ * unwrapped for the comparison, and the candidate instance is rejected.
+ */
+ public void testQuery03f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<FullTimeEmployee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1"});
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ NumericExpression<Double> sal = q.numericParameter("sal", Double.class);
+ StringExpression begin = q.stringParameter("begin");
+ q.filter(cand.salary.gt(sal).and(cand.firstname.startsWith(begin)));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("sal", 30000.);
+ paramValues.put("begin", "M");
+ q.setParameters(paramValues);
+ List<FullTimeEmployee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_03, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -357,13 +541,14 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Employee where department.name == :dep";
- Query<Employee> q = pm.newQuery(Employee.class, "department.name == dep");
- q.declareParameters("String dep");
- List<Employee> emps = (List<Employee>)q.execute ("R&D");
List<Employee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class, "department.name == dep")) {
+ q.declareParameters("String dep");
+ List<Employee> emps = (List<Employee>)q.execute ("R&D");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_04, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -385,16 +570,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Employee where department.name == :dep";
- Query<Employee> q = pm.newQuery (Employee.class, "department.name == dep");
- q.declareParameters ("String dep");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("dep", "R&D");
- q.setNamedParameters(paramValues);
- List<Employee> emps = q.executeList();
List<Employee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<Employee> q = pm.newQuery (Employee.class, "department.name == dep")) {
+ q.declareParameters ("String dep");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("dep", "R&D");
+ q.setNamedParameters(paramValues);
+ List<Employee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_04, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -416,14 +602,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Employee where department.name == :dep";
- Query<Employee> q = pm.newQuery (Employee.class, "department.name == dep");
- q.declareParameters ("String dep");
- q.setParameters("R&D");
- List<Employee> emps = q.executeList();
List<Employee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<Employee> q = pm.newQuery (Employee.class, "department.name == dep")) {
+ q.declareParameters ("String dep");
+ q.setParameters("R&D");
+ List<Employee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_04, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -445,15 +632,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Employee where department.name == :dep";
- Query<Employee> q = pm.newQuery (singleStringQuery);
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("dep", "R&D");
- q.setNamedParameters(paramValues);
- List<Employee> emps = q.executeList();
List<Employee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emps, expected);
+ try (Query<Employee> q = pm.newQuery (SINGLE_STRING_QUERY_04)) {
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("dep", "R&D");
+ q.setNamedParameters(paramValues);
+ List<Employee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_04, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -461,7 +649,41 @@ public class SampleQueries extends QueryTest {
}
}
}
-
+
+ /**
+ * Navigation through single-valued field.
+ *
+ * This query selects all Employee instances from the candidate collection where the value
+ * of the name field in the Department instance associated with the Employee instance
+ * is equal to the value passed as a parameter.
+ * If the value for the dept field in a candidate instance is null, then it cannot be
+ * navigated for the comparison, and the candidate instance is rejected.
+ */
+ public void testQuery04f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<Employee> expected = getTransientCompanyModelInstancesAsList(new String[] {"emp1", "emp2", "emp3"});
+ try (JDOQLTypedQuery<Employee> q = pm.newJDOQLTypedQuery(Employee.class)) {
+ QEmployee cand = QEmployee.candidate();
+ StringExpression dep = q.stringParameter("dep");
+ q.filter(cand.department.name.eq(dep));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("dep", "R&D");
+ q.setParameters(paramValues);
+ List<Employee> emps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_04, emps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
/**
* Navigation through multi-valued field.
*
@@ -473,17 +695,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department " +
- "where employees.contains(emp) && emp.weeklyhours > :hours " +
- "variables Employee emp";
- String filter = "employees.contains (emp) && emp.weeklyhours > hours";
- Query<Department> q = pm.newQuery(Department.class, filter);
- q.declareVariables("Employee emp");
- q.declareParameters("double hours");
- List<Department> deps = (List<Department>)q.execute (30.);
List expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, deps, expected);
+ try (Query<Department> q =
+ pm.newQuery(Department.class, "employees.contains (emp) && emp.weeklyhours > hours")) {
+ q.declareVariables("Employee emp");
+ q.declareParameters("double hours");
+ List<Department> deps = (List<Department>)q.execute (30.);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_05, deps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -503,20 +724,19 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department " +
- "where employees.contains(emp) && emp.weeklyhours > :hours " +
- "variables Employee emp";
- String filter = "employees.contains (emp) && emp.weeklyhours > hours";
- Query<Department> q = pm.newQuery(Department.class, filter);
- q.declareVariables("Employee emp");
- q.declareParameters("double hours");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("hours", 30.);
- q.setNamedParameters(paramValues);
- List<Department> deps = q.executeList();
- List<Department> expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, deps, expected);
+ List expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
+ try (Query<Department> q =
+ pm.newQuery(Department.class, "employees.contains (emp) && emp.weeklyhours > hours")) {
+ q.declareVariables("Employee emp");
+ q.declareParameters("double hours");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("hours", 30.);
+ q.setNamedParameters(paramValues);
+ List<Department> deps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_05, deps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -536,18 +756,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department " +
- "where employees.contains(emp) && emp.weeklyhours > :hours " +
- "variables Employee emp";
- String filter = "employees.contains (emp) && emp.weeklyhours > hours";
- Query<Department> q = pm.newQuery(Department.class, filter);
- q.declareVariables("Employee emp");
- q.declareParameters("double hours");
- q.setParameters(30.);
- List<Department> deps = q.executeList();
- List<Department> expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, deps, expected);
+ List expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
+ try (Query<Department> q =
+ pm.newQuery(Department.class, "employees.contains (emp) && emp.weeklyhours > hours")) {
+ q.declareVariables("Employee emp");
+ q.declareParameters("double hours");
+ q.setParameters(30.);
+ List<Department> deps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_05, deps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -567,14 +786,46 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department " +
- "where employees.contains(emp) && emp.weeklyhours > :hours " +
- "variables Employee emp";
- Query<Department> q = pm.newQuery(singleStringQuery);
- List<Department> deps = (List<Department>)q.execute (30.);
List expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, deps, expected);
+ try (Query<Department> q = pm.newQuery(SINGLE_STRING_QUERY_05)) {
+ List<Department> deps = (List<Department>)q.execute (30.);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_05, deps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Navigation through multi-valued field.
+ *
+ * This query selects all Department instances from the candidate collection where
+ * the collection of Employee instances contains at least one Employee instance
+ * having a salary greater than the value passed as a parameter.
+ */
+ public void testQuery05f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List expected = getTransientCompanyModelInstancesAsList(new String[] {"dept1"});
+ try (JDOQLTypedQuery<Department> q = pm.newJDOQLTypedQuery(Department.class)) {
+ QDepartment cand = QDepartment.candidate();
+ QEmployee emp = QEmployee.variable("emp");
+ NumericExpression<Double> hours = q.numericParameter("hours", double.class);
+ q.filter(cand.employees.contains(emp).and(emp.weeklyhours.gt(hours)));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("hours", 30.);
+ q.setParameters(paramValues);
+ List<Department> deps = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_05, deps, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -593,16 +844,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department where :depts.contains(name)";
- String filter = "depts.contains(name)";
- Query<Department> q = pm.newQuery(Department.class, filter);
- q.declareParameters("java.util.Collection depts");
- List<String> deptNames = Arrays.asList("R&D", "Sales", "Marketing");
- List<Department> result = (List<Department>)q.execute(deptNames);
List<Department> expected =
getTransientCompanyModelInstancesAsList(new String[] {"dept1", "dept2", "dept3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, result, expected);
+ try (Query<Department> q = pm.newQuery(Department.class, "depts.contains(name)")) {
+ q.declareParameters("java.util.Collection depts");
+ List<String> deptNames = Arrays.asList("R&D", "Sales", "Marketing");
+ List<Department> result = (List<Department>)q.execute(deptNames);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_06, result, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -621,18 +872,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department where :depts.contains(name)";
- String filter = "depts.contains(name)";
- Query<Department> q = pm.newQuery(Department.class, filter);
- q.declareParameters("java.util.Collection depts");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("depts", Arrays.asList("R&D", "Sales", "Marketing"));
- q.setNamedParameters(paramValues);
- List<Department> result = q.executeList();
List<Department> expected =
getTransientCompanyModelInstancesAsList(new String[] {"dept1", "dept2", "dept3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, result, expected);
+ try (Query<Department> q = pm.newQuery(Department.class, "depts.contains(name)")) {
+ q.declareParameters("java.util.Collection depts");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("depts", Arrays.asList("R&D", "Sales", "Marketing"));
+ q.setNamedParameters(paramValues);
+ List<Department> result = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_06, result, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -651,16 +902,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department where :depts.contains(name)";
- String filter = "depts.contains(name)";
- Query<Department> q = pm.newQuery(Department.class, filter);
- q.declareParameters("java.util.Collection depts");
- q.setParameters(Arrays.asList("R&D", "Sales", "Marketing"));
- List<Department> result = q.executeList();
List<Department> expected =
getTransientCompanyModelInstancesAsList(new String[] {"dept1", "dept2", "dept3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, result, expected);
+ try (Query<Department> q = pm.newQuery(Department.class, "depts.contains(name)")) {
+ q.declareParameters("java.util.Collection depts");
+ q.setParameters(Arrays.asList("R&D", "Sales", "Marketing"));
+ List<Department> result = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_06, result, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -679,16 +930,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select from org.apache.jdo.tck.pc.company.Department where :depts.contains(name)";
- Query<Department> q = pm.newQuery(singleStringQuery);
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("depts", Arrays.asList("R&D", "Sales", "Marketing"));
- q.setNamedParameters(paramValues);
- List<Department> result = q.executeList();
List<Department> expected =
getTransientCompanyModelInstancesAsList(new String[] {"dept1", "dept2", "dept3"});
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, result, expected);
+ try (Query<Department> q = pm.newQuery(SINGLE_STRING_QUERY_06)) {
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("depts", Arrays.asList("R&D", "Sales", "Marketing"));
+ q.setNamedParameters(paramValues);
+ List<Department> result = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_06, result, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -698,6 +950,39 @@ public class SampleQueries extends QueryTest {
}
/**
+ * Membership in a collection.
+ *
+ * This query selects all Department instances where the name field is contained in
+ * a parameter collection, which in this example consists of three department names.
+ */
+ public void testQuery06f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<Department> expected =
+ getTransientCompanyModelInstancesAsList(new String[] {"dept1", "dept2", "dept3"});
+ try (JDOQLTypedQuery<Department> q = pm.newJDOQLTypedQuery(Department.class)) {
+ QDepartment cand = QDepartment.candidate();
+ CollectionExpression<Collection<String>, String> depts =
+ q.collectionParameter("depts", String.class);
+ q.filter(depts.contains(cand.name));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("depts", Arrays.asList("R&D", "Sales", "Marketing"));
+ q.setParameters(paramValues);
+ List<Department> result = q.executeList();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_06, result, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
* Projection of a Single Field.
*
* This query selects names of all Employees who work in the parameter department.
@@ -706,14 +991,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee where department.name == :deptName";
- Query<Employee> q = pm.newQuery(Employee.class, "department.name == deptName");
- q.setResult("firstname");
- q.declareParameters("String deptName");
- List<String> names = (List<String>)q.execute("R&D");
List<String> expected = Arrays.asList("Joe", "Craig", "Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class, "department.name == deptName")) {
+ q.setResult("firstname");
+ q.declareParameters("String deptName");
+ List<String> names = (List<String>)q.execute("R&D");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_07, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -731,17 +1017,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee where department.name == :deptName";
- Query<Employee> q = pm.newQuery(Employee.class, "department.name == deptName");
- q.setResult("firstname");
- q.declareParameters("String deptName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("deptName", "R&D");
- q.setNamedParameters(paramValues);
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Joe", "Craig", "Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class, "department.name == deptName")) {
+ q.setResult("firstname");
+ q.declareParameters("String deptName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setNamedParameters(paramValues);
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_07, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -759,15 +1046,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee where department.name == :deptName";
- Query<Employee> q = pm.newQuery(Employee.class, "department.name == deptName");
- q.setResult("firstname");
- q.declareParameters("String deptName");
- q.setParameters("R&D");
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Joe", "Craig", "Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class, "department.name == deptName")) {
+ q.setResult("firstname");
+ q.declareParameters("String deptName");
+ q.setParameters("R&D");
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_07, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -785,15 +1073,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee where department.name == :deptName";
- Query<Employee> q = pm.newQuery(singleStringQuery);
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("deptName", "R&D");
- q.setNamedParameters(paramValues);
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Joe", "Craig", "Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(SINGLE_STRING_QUERY_07)) {
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setNamedParameters(paramValues);
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_07, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -803,6 +1092,39 @@ public class SampleQueries extends QueryTest {
}
/**
+ * Projection of a Single Field.
+ *
+ * This query selects names of all Employees who work in the parameter department.
+ */
+ public void testQuery07f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<String> expected = Arrays.asList("Joe", "Craig", "Michael");
+ try (JDOQLTypedQuery<Employee> q = pm.newJDOQLTypedQuery(Employee.class)) {
+ QEmployee cand = QEmployee.candidate();
+ StringExpression deptName = q.stringParameter("deptName");
+ q.filter(cand.department.name.eq(deptName));
+ q.result(false, cand.firstname);
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setParameters(paramValues);
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_07, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+
+
+ /**
* Projection of Multiple Fields and Expressions.
*
* This query selects names, salaries, and bosses of Employees who work in the parameter department.
@@ -811,25 +1133,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname, salary, manager as reportsTo into org.apache.jdo.tck.query.api.SampleQueries$Info " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("firstname, salary, manager as reportsTo");
- q.setResultClass(Info.class);
- q.declareParameters("String deptName");
- List<Info> infos = (List<Info>) q.execute("R&D");
-
- Info info1 = new Info();
- info1.firstname = "Michael";
- info1.salary = 40000.;
- info1.reportsTo = (Employee)getTransientCompanyModelInstance("emp2");
- Info info2 = new Info();
- info2.firstname = "Craig";
- info2.salary = 50000.;
- info2.reportsTo = null;
- List expected = Arrays.asList(info1, info2);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List expected = testQuery08Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("firstname, salary, manager as reportsTo");
+ q.setResultClass(Info.class);
+ q.declareParameters("String deptName");
+ List<Info> infos = (List<Info>) q.execute("R&D");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_08, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -847,28 +1160,20 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname, salary, manager as reportsTo into org.apache.jdo.tck.query.api.SampleQueries$Info " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("firstname, salary, manager as reportsTo");
- q.setResultClass(Info.class);
- q.declareParameters("String deptName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("deptName", "R&D");
- q.setNamedParameters(paramValues);
- List<Info> infos = q.executeResultList(Info.class);
-
- Info info1 = new Info();
- info1.firstname = "Michael";
- info1.salary = 40000.;
- info1.reportsTo = (Employee)getTransientCompanyModelInstance("emp2");
- Info info2 = new Info();
- info2.firstname = "Craig";
- info2.salary = 50000.;
- info2.reportsTo = null;
- List expected = Arrays.asList(info1, info2);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<Info> expected = testQuery08Helper();
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("firstname, salary, manager as reportsTo");
+ q.setResultClass(Info.class);
+ q.declareParameters("String deptName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setNamedParameters(paramValues);
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_08, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -886,26 +1191,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname, salary, manager as reportsTo into org.apache.jdo.tck.query.api.SampleQueries$Info " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("firstname, salary, manager as reportsTo");
- q.setResultClass(Info.class);
- q.declareParameters("String deptName");
- q.setParameters("R&D");
- List<Info> infos = q.executeResultList(Info.class);
-
- Info info1 = new Info();
- info1.firstname = "Michael";
- info1.salary = 40000.;
- info1.reportsTo = (Employee)getTransientCompanyModelInstance("emp2");
- Info info2 = new Info();
- info2.firstname = "Craig";
- info2.salary = 50000.;
- info2.reportsTo = null;
- List expected = Arrays.asList(info1, info2);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<Info> expected = testQuery08Helper();
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("firstname, salary, manager as reportsTo");
+ q.setResultClass(Info.class);
+ q.declareParameters("String deptName");
+ q.setParameters("R&D");
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_08, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -923,23 +1220,45 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname, salary, manager as reportsTo into org.apache.jdo.tck.query.api.SampleQueries$Info " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- q.setParameters("R&D");
- List<Info> infos = q.executeResultList(Info.class);
+ List<Info> expected = testQuery08Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_08)) {
+ q.setParameters("R&D");
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_08, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
- Info info1 = new Info();
- info1.firstname = "Michael";
- info1.salary = 40000.;
- info1.reportsTo = (Employee)getTransientCompanyModelInstance("emp2");
- Info info2 = new Info();
- info2.firstname = "Craig";
- info2.salary = 50000.;
- info2.reportsTo = null;
- List expected = Arrays.asList(info1, info2);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ /**
+ * Projection of Multiple Fields and Expressions.
+ *
+ * This query selects names, salaries, and bosses of Employees who work in the parameter department.
+ */
+ public void testQuery08f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<Info> expected = testQuery08Helper();
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(false, cand.firstname, cand.salary, cand.manager.as("reportsTo"));
+ StringExpression deptName = q.stringParameter("deptName");
+ q.filter(cand.department.name.eq(deptName));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setParameters(paramValues);
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_08, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -958,20 +1277,19 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary, manager) " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("new org.apache.jdo.tck.query.api.SampleQueries$Info(firstname, salary, manager)");
- q.declareParameters("String deptName");
- List<Info> infos = (List<Info>)q.execute("R&D");
-
List<Info> expected = Arrays.asList(
new Info("Michael", 40000., (Employee)getTransientCompanyModelInstance("emp2")),
new Info("Craig", 50000., null)
);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("new org.apache.jdo.tck.query.api.SampleQueries$Info(firstname, salary, manager)");
+ q.declareParameters("String deptName");
+ List<Info> infos = (List<Info>)q.execute("R&D");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_09, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -990,23 +1308,22 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary, manager) " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("new org.apache.jdo.tck.query.api.SampleQueries$Info(firstname, salary, manager)");
- q.declareParameters("String deptName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("deptName", "R&D");
- q.setNamedParameters(paramValues);
- List<Info> infos = q.executeResultList(Info.class);
-
List<Info> expected = Arrays.asList(
new Info("Michael", 40000., (Employee)getTransientCompanyModelInstance("emp2")),
new Info("Craig", 50000., null)
);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("new org.apache.jdo.tck.query.api.SampleQueries$Info(firstname, salary, manager)");
+ q.declareParameters("String deptName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setNamedParameters(paramValues);
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_09, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1025,21 +1342,20 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary, manager) " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("new org.apache.jdo.tck.query.api.SampleQueries$Info(firstname, salary, manager)");
- q.declareParameters("String deptName");
- q.setParameters("R&D");
- List<Info> infos = q.executeResultList(Info.class);
-
List<Info> expected = Arrays.asList(
new Info("Michael", 40000., (Employee)getTransientCompanyModelInstance("emp2")),
new Info("Craig", 50000., null)
);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("new org.apache.jdo.tck.query.api.SampleQueries$Info(firstname, salary, manager)");
+ q.declareParameters("String deptName");
+ q.setParameters("R&D");
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_09, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1058,19 +1374,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary, manager) " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- q.setParameters("R&D");
- List<Info> infos = q.executeResultList(Info.class);
-
List<Info> expected = Arrays.asList(
new Info("Michael", 40000., (Employee)getTransientCompanyModelInstance("emp2")),
new Info("Craig", 50000., null)
);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_09)) {
+ q.setParameters("R&D");
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_09, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1089,19 +1403,52 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary, manager) " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newNamedQuery(FullTimeEmployee.class, "constructor");
- q.setParameters("R&D");
- List<Info> infos = q.executeResultList(Info.class);
+ List<Info> expected = Arrays.asList(
+ new Info("Michael", 40000., (Employee)getTransientCompanyModelInstance("emp2")),
+ new Info("Craig", 50000., null)
+ );
+ try (Query<FullTimeEmployee> q = pm.newNamedQuery(FullTimeEmployee.class, "constructor")) {
+ q.setParameters("R&D");
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_09, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+ /**
+ * Projection of Multiple Fields and Expressions into a Constructed instance.
+ *
+ * This query selects names, salaries, and bosses of Employees who work in the parameter department,
+ * and uses the constructor for the result class.
+ */
+ public void testQuery09f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
List<Info> expected = Arrays.asList(
new Info("Michael", 40000., (Employee)getTransientCompanyModelInstance("emp2")),
new Info("Craig", 50000., null)
);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(false, cand.firstname, cand.salary, cand.manager);
+ StringExpression deptName = q.stringParameter("deptName");
+ q.filter(cand.department.name.eq(deptName));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setParameters(paramValues);
+ List<Info> infos = q.executeResultList(Info.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_09, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1120,16 +1467,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("avg(salary)");
- q.declareParameters("String deptName");
- Double avgSalary = (Double) q.execute("R&D");
-
Double expected = 45000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSalary, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("avg(salary)");
+ q.declareParameters("String deptName");
+ Double avgSalary = (Double) q.execute("R&D");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_10, avgSalary, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1148,19 +1495,19 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("avg(salary)");
- q.declareParameters("String deptName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("deptName", "R&D");
- q.setNamedParameters(paramValues);
- Double avgSalary = q.executeResultUnique(Double.class);
-
Double expected = 45000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSalary, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("avg(salary)");
+ q.declareParameters("String deptName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setNamedParameters(paramValues);
+ Double avgSalary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_10, avgSalary, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1179,17 +1526,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("avg(salary)");
- q.declareParameters("String deptName");
- q.setParameters("R&D");
- Double avgSalary = q.executeResultUnique(Double.class);
-
Double expected = 45000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSalary, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("avg(salary)");
+ q.declareParameters("String deptName");
+ q.setParameters("R&D");
+ Double avgSalary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_10, avgSalary, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1208,15 +1555,46 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- q.setParameters("R&D");
- Double avgSalary = q.executeResultUnique(Double.class);
+ Double expected = 45000.;
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_10)) {
+ q.setParameters("R&D");
+ Double avgSalary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_10, avgSalary, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+ /**
+ * Aggregation of a single Field.
+ *
+ * This query averages the salaries of Employees who work in the parameter department
+ * and returns a single value.
+ */
+ public void testQuery10f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
Double expected = 45000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSalary, expected);
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(false, cand.salary.avg());
+ StringExpression deptName = q.stringParameter("deptName");
+ q.filter(cand.department.name.eq(deptName));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setParameters(paramValues);
+ Double avgSalary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_10, avgSalary, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1234,16 +1612,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("avg(salary), sum(salary)");
- q.declareParameters("String deptName");
- Object[] avgSum = (Object[]) q.execute("R&D");
-
Double[] expected = new Double[] {45000., 90000.};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSum, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("avg(salary), sum(salary)");
+ q.declareParameters("String deptName");
+ Object[] avgSum = (Object[]) q.execute("R&D");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_11, avgSum, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1261,19 +1639,19 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("avg(salary), sum(salary)");
- q.declareParameters("String deptName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("deptName", "R&D");
- q.setNamedParameters(paramValues);
- Object[] avgSum = q.executeResultUnique(Object[].class);
-
Double[] expected = new Double[] {45000., 90000.};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSum, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("avg(salary), sum(salary)");
+ q.declareParameters("String deptName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setNamedParameters(paramValues);
+ Object[] avgSum = q.executeResultUnique(Object[].class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_11, avgSum, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1291,17 +1669,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "department.name == deptName");
- q.setResult("avg(salary), sum(salary)");
- q.declareParameters("String deptName");
- q.setParameters("R&D");
- Object[] avgSum = q.executeResultUnique(Object[].class);
-
Double[] expected = new Double[] {45000., 90000.};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSum, expected);
+ try (Query<FullTimeEmployee> q =
+ pm.newQuery(FullTimeEmployee.class, "department.name == deptName")) {
+ q.setResult("avg(salary), sum(salary)");
+ q.declareParameters("String deptName");
+ q.setParameters("R&D");
+ Object[] avgSum = q.executeResultUnique(Object[].class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_11, avgSum, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1319,15 +1697,45 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary) from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where department.name == :deptName";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- q.setParameters("R&D");
- Object[] avgSum = q.executeResultUnique(Object[].class);
+ Double[] expected = new Double[] {45000., 90000.};
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_11)) {
+ q.setParameters("R&D");
+ Object[] avgSum = q.executeResultUnique(Object[].class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_11, avgSum, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+ /**
+ * Aggregation of Multiple Fields and Expressions.
+ *
+ * This query averages and sums the salaries of Employees who work in the parameter department.
+ */
+ public void testQuery11f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
Double[] expected = new Double[] {45000., 90000.};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, avgSum, expected);
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(false, cand.salary.avg(), cand.salary.sum());
+ StringExpression deptName = q.stringParameter("deptName");
+ q.filter(cand.department.name.eq(deptName));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("deptName", "R&D");
+ q.setParameters(paramValues);
+ Object[] avgSum = q.executeResultUnique(Object[].class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_11, avgSum, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1346,21 +1754,20 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary), department.name " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "group by department.name having count(department.name)";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class);
- q.setResult("avg(salary), sum(salary), department.name");
- q.setGrouping("department.name having count(department.name) > 1");
- List<Object[]> results = (List<Object[]>)q.execute();
- if (results.size() != 1) {
- fail(ASSERTION_FAILED,
- "Query result has size " + results.size() + ", expected query result of size 1");
- }
- Object[] row = results.get(0);
Object[] expectedRow = new Object[]{45000., 90000., "R&D"};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, row, expectedRow);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class)) {
+ q.setResult("avg(salary), sum(salary), department.name");
+ q.setGrouping("department.name having count(department.name) > 1");
+ List<Object[]> results = (List<Object[]>)q.execute();
+ if (results.size() != 1) {
+ fail(ASSERTION_FAILED,
+ "Query result has size " + results.size() + ", expected query result of size 1");
+ }
+ Object[] row = results.get(0);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_12, row, expectedRow);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1379,21 +1786,20 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary), department.name " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "group by department.name having count(department.name)";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class);
- q.setResult("avg(salary), sum(salary), department.name");
- q.setGrouping("department.name having count(department.name) > 1");
- List<Object[]> results = q.executeResultList(Object[].class);
- if (results.size() != 1) {
- fail(ASSERTION_FAILED,
- "Query result has size " + results.size() + ", expected query result of size 1");
- }
- Object[] row = results.get(0);
Object[] expectedRow = new Object[]{45000., 90000., "R&D"};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, row, expectedRow);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class)) {
+ q.setResult("avg(salary), sum(salary), department.name");
+ q.setGrouping("department.name having count(department.name) > 1");
+ List<Object[]> results = q.executeResultList(Object[].class);
+ if (results.size() != 1) {
+ fail(ASSERTION_FAILED,
+ "Query result has size " + results.size() + ", expected query result of size 1");
+ }
+ Object[] row = results.get(0);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_12, row, expectedRow);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1412,19 +1818,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary), department.name " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "group by department.name having count(department.name) > 1";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- List<Object[]> results = q.executeResultList(Object[].class);
- if (results.size() != 1) {
- fail(ASSERTION_FAILED,
- "Query result has size " + results.size() + ", expected query result of size 1");
- }
- Object[] row = results.get(0);
Object[] expectedRow = new Object[]{45000., 90000., "R&D"};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, row, expectedRow);
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_12)) {
+ List<Object[]> results = q.executeResultList(Object[].class);
+ if (results.size() != 1) {
+ fail(ASSERTION_FAILED,
+ "Query result has size " + results.size() + ", expected query result of size 1");
+ }
+ Object[] row = results.get(0);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_12, row, expectedRow);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1443,19 +1848,52 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select avg(salary), sum(salary), department.name " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "group by department.name having count(department.name) > 1";
- Query<FullTimeEmployee> q = pm.newNamedQuery(FullTimeEmployee.class, "grouping");
- List<Object[]> results = q.executeResultList(Object[].class);
- if (results.size() != 1) {
- fail(ASSERTION_FAILED,
- "Query result has size " + results.size() + ", expected query result of size 1");
+ Object[] expectedRow = new Object[]{45000., 90000., "R&D"};
+ try (Query<FullTimeEmployee> q = pm.newNamedQuery(FullTimeEmployee.class, "grouping")) {
+ List<Object[]> results = q.executeResultList(Object[].class);
+ if (results.size() != 1) {
+ fail(ASSERTION_FAILED,
+ "Query result has size " + results.size() + ", expected query result of size 1");
+ }
+ Object[] row = results.get(0);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_12, row, expectedRow);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
}
- Object[] row = results.get(0);
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Aggregation of Multiple fields with Grouping.
+ *
+ * This query averages and sums the salaries of Employees who work in all departments having
+ * more than one employee and aggregates by department name.
+ */
+ public void testQuery12f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
Object[] expectedRow = new Object[]{45000., 90000., "R&D"};
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, row, expectedRow);
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(false, cand.salary.avg(), cand.salary.sum(), cand.department.name);
+ q.groupBy(cand.department.name);
+ q.having(cand.department.name.count().gt(1L));
+ List<Object[]> results = q.executeResultList(Object[].class);
+ if (results.size() != 1) {
+ fail(ASSERTION_FAILED,
+ "Query result has size " + results.size() + ", expected query result of size 1");
+ }
+ Object[] row = results.get(0);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_12, row, expectedRow);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1473,15 +1911,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique this from org.apache.jdo.tck.pc.company.Employee " +
- "where firstname == :empName";
- Query<Employee> q = pm.newQuery(Employee.class, "firstname == empName");
- q.setUnique(true);
- q.declareParameters ("String empName");
- Employee emp = (Employee)q.execute("Michael");
Employee expectedEmp = (Employee)getTransientCompanyModelInstance("emp1");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emp, expectedEmp);
+ try (Query<Employee> q = pm.newQuery(Employee.class, "firstname == empName")) {
+ q.setUnique(true);
+ q.declareParameters ("String empName");
+ Employee emp = (Employee)q.execute("Michael");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_13, emp, expectedEmp);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1499,18 +1937,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique this from org.apache.jdo.tck.pc.company.Employee " +
- "where firstname == :empName";
- Query<Employee> q = pm.newQuery (Employee.class, "firstname == empName");
- q.setUnique(true);
- q.declareParameters ("String empName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("empName", "Michael");
- q.setNamedParameters(paramValues);
- Employee emp = q.executeUnique();
Employee expectedEmp = (Employee)getTransientCompanyModelInstance("emp1");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emp, expectedEmp);
+ try (Query<Employee> q = pm.newQuery (Employee.class, "firstname == empName")) {
+ q.setUnique(true);
+ q.declareParameters ("String empName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("empName", "Michael");
+ q.setNamedParameters(paramValues);
+ Employee emp = q.executeUnique();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_13, emp, expectedEmp);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1528,16 +1966,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique this from org.apache.jdo.tck.pc.company.Employee " +
- "where firstname == :empName";
- Query<Employee> q = pm.newQuery (Employee.class, "firstname == empName");
- q.setUnique(true);
- q.declareParameters ("String empName");
- q.setParameters("Michael");
- Employee emp = q.executeUnique();
Employee expectedEmp = (Employee)getTransientCompanyModelInstance("emp1");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emp, expectedEmp);
+ try (Query<Employee> q = pm.newQuery (Employee.class, "firstname == empName")) {
+ q.setUnique(true);
+ q.declareParameters ("String empName");
+ q.setParameters("Michael");
+ Employee emp = q.executeUnique();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_13, emp, expectedEmp);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1555,14 +1993,44 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique this from org.apache.jdo.tck.pc.company.Employee " +
- "where firstname == :empName";
- Query<Employee> q = pm.newQuery (singleStringQuery);
- q.setParameters("Michael");
- Employee emp = q.executeResultUnique(Employee.class);
Employee expectedEmp = (Employee)getTransientCompanyModelInstance("emp1");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, emp, expectedEmp);
+ try (Query<Employee> q = pm.newQuery (SINGLE_STRING_QUERY_13)) {
+ q.setParameters("Michael");
+ Employee emp = q.executeResultUnique(Employee.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_13, emp, expectedEmp);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Selection of a Single Instance.
+ *
+ * This query returns a single instance of Employee.
+ */
+ public void testQuery13f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ Employee expectedEmp = (Employee)getTransientCompanyModelInstance("emp1");
+ try (JDOQLTypedQuery<Employee> q = pm.newJDOQLTypedQuery(Employee.class)) {
+ QEmployee cand = QEmployee.candidate();
+ StringExpression empName = q.stringParameter("empName");
+ q.filter(cand.firstname.eq(empName));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("empName", "Michael");
+ q.setParameters(paramValues);
+ Employee emp = q.executeUnique();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_13, emp, expectedEmp);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1580,17 +2048,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique salary from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where firstname == :empName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "firstname == empName");
- q.setResult("salary");
- q.setResultClass(Double.class);
- q.setUnique(true);
- q.declareParameters("String empName");
- Double salary = (Double) q.execute ("Michael");
Double expectedSalary = 40000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, salary, expectedSalary);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "firstname == empName")) {
+ q.setResult("salary");
+ q.setResultClass(Double.class);
+ q.setUnique(true);
+ q.declareParameters("String empName");
+ Double salary = (Double) q.execute ("Michael");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_14, salary, expectedSalary);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1608,19 +2076,19 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique salary from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where firstname == :empName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "firstname == empName");
- q.setResult("salary");
- q.setResultClass(Double.class);
- q.declareParameters("String empName");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("empName", "Michael");
- q.setNamedParameters(paramValues);
- Double salary = q.executeResultUnique(Double.class);
Double expectedSalary = 40000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, salary, expectedSalary);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "firstname == empName")) {
+ q.setResult("salary");
+ q.setResultClass(Double.class);
+ q.declareParameters("String empName");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("empName", "Michael");
+ q.setNamedParameters(paramValues);
+ Double salary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_14, salary, expectedSalary);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1638,17 +2106,17 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique salary from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where firstname == :empName";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "firstname == empName");
- q.setResult("salary");
- q.setResultClass(Double.class);
- q.declareParameters("String empName");
- q.setParameters("Michael");
- Double salary = q.executeResultUnique(Double.class);
Double expectedSalary = 40000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, salary, expectedSalary);
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "firstname == empName")) {
+ q.setResult("salary");
+ q.setResultClass(Double.class);
+ q.declareParameters("String empName");
+ q.setParameters("Michael");
+ Double salary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_14, salary, expectedSalary);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1666,14 +2134,45 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select unique salary from org.apache.jdo.tck.pc.company.FullTimeEmployee " +
- "where firstname == :empName";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- q.setParameters("Michael");
- Double salary = q.executeResultUnique(Double.class);
Double expectedSalary = 40000.;
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, salary, expectedSalary);
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_14)) {
+ q.setParameters("Michael");
+ Double salary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_14, salary, expectedSalary);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Selection of a Single Field.
+ *
+ * This query returns a single field of a single Employee.
+ */
+ public void testQuery14f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ Double expectedSalary = 40000.;
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(false, cand.salary);
+ StringExpression empName = q.stringParameter("empName");
+ q.filter(cand.firstname.eq(empName));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("empName", "Michael");
+ q.setParameters(paramValues);
+ Double salary = q.executeResultUnique(Double.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_14, salary, expectedSalary);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1693,29 +2192,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
- q.setResultClass(EmpWrapper.class);
-
- q.declareParameters("Double sal");
- List<EmpWrapper> infos = (List<EmpWrapper>)q.execute(30000.);
-
- EmpWrapper wrapper1 = new EmpWrapper();
- wrapper1.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp1");
- EmpWrapper wrapper2 = new EmpWrapper();
- wrapper2.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp2");
- EmpWrapper wrapper3 = new EmpWrapper();
- wrapper3.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp5");
- List<EmpWrapper> expected = Arrays.asList(wrapper1, wrapper2, wrapper3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<EmpWrapper> expected = testQuery15Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal")) {
+ q.setResultClass(EmpWrapper.class);
+ q.declareParameters("Double sal");
+ List<EmpWrapper> infos = (List<EmpWrapper>)q.execute(30000.);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_15, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1735,32 +2220,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
- q.setResultClass(EmpWrapper.class);
-
- q.declareParameters ("Double sal");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("sal", 30000.);
- q.setNamedParameters(paramValues);
- List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
-
- EmpWrapper wrapper1 = new EmpWrapper();
- wrapper1.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp1");
- EmpWrapper wrapper2 = new EmpWrapper();
- wrapper2.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp2");
- EmpWrapper wrapper3 = new EmpWrapper();
- wrapper3.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp5");
- List<EmpWrapper> expected = Arrays.asList(wrapper1, wrapper2, wrapper3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<EmpWrapper> expected = testQuery15Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal")) {
+ q.setResultClass(EmpWrapper.class);
+ q.declareParameters ("Double sal");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("sal", 30000.);
+ q.setNamedParameters(paramValues);
+ List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_15, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1780,30 +2251,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
- q.setResultClass(EmpWrapper.class);
-
- q.declareParameters ("Double sal");
- q.setParameters(30000.);
- List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
-
- EmpWrapper wrapper1 = new EmpWrapper();
- wrapper1.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp1");
- EmpWrapper wrapper2 = new EmpWrapper();
- wrapper2.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp2");
- EmpWrapper wrapper3 = new EmpWrapper();
- wrapper3.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp5");
- List<EmpWrapper> expected = Arrays.asList(wrapper1, wrapper2, wrapper3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<EmpWrapper> expected = testQuery15Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal")) {
+ q.setResultClass(EmpWrapper.class);
+ q.declareParameters ("Double sal");
+ q.setParameters(30000.);
+ List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_15, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1823,28 +2280,47 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpWrapper"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
-
- q.setParameters(30000.);
- List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
+ List<EmpWrapper> expected = testQuery15Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_15)) {
+ q.setParameters(30000.);
+ List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_15, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
- EmpWrapper wrapper1 = new EmpWrapper();
- wrapper1.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp1");
- EmpWrapper wrapper2 = new EmpWrapper();
- wrapper2.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp2");
- EmpWrapper wrapper3 = new EmpWrapper();
- wrapper3.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp5");
- List<EmpWrapper> expected = Arrays.asList(wrapper1, wrapper2, wrapper3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ /**
+ * Projection of "this" to User-defined Result Class with Matching Field.
+ *
+ * This query selects instances of Employee who make more than the parameter salary and
+ * stores the result in a user-defined class. Since the default is "distinct this as FullTimeEmployee",
+ * the field must be named FullTimeEmployee and be of type FullTimeEmployee.
+ */
+ public void testQuery15f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<EmpWrapper> expected = testQuery15Helper();
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(true, cand.as("FullTimeEmployee"));
+ NumericExpression<Double> sal = q.numericParameter("sal", Double.class);
+ q.filter(cand.salary.gt(sal));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("sal", 30000.);
+ q.setParameters(paramValues);
+ List<EmpWrapper> infos = q.executeResultList(EmpWrapper.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_15, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1863,29 +2339,15 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpInfo " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpInfo"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
- q.setResultClass(EmpInfo.class);
-
- q.declareParameters("Double sal");
- List<EmpInfo> infos = (List<EmpInfo>)q.execute(30000.);
-
- EmpInfo info1 = new EmpInfo();
- info1.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp1"));
- EmpInfo info2 = new EmpInfo();
- info2.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp2"));
- EmpInfo info3 = new EmpInfo();
- info3.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp5"));
- List<EmpInfo> expected = Arrays.asList(info1, info2, info3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<EmpInfo> expected = testQuery16Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal")) {
+ q.setResultClass(EmpInfo.class);
+ q.declareParameters("Double sal");
+ List<EmpInfo> infos = (List<EmpInfo>)q.execute(30000.);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_16, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1904,32 +2366,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpInfo " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpInfo"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
- q.setResultClass(EmpInfo.class);
-
- q.declareParameters("Double sal");
- Map<String, Object> paramValues = new HashMap<>();
- paramValues.put("sal", 30000.);
- q.setNamedParameters(paramValues);
- List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
-
- EmpInfo info1 = new EmpInfo();
- info1.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp1"));
- EmpInfo info2 = new EmpInfo();
- info2.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp2"));
- EmpInfo info3 = new EmpInfo();
- info3.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp5"));
- List<EmpInfo> expected = Arrays.asList(info1, info2, info3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<EmpInfo> expected = testQuery16Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal")) {
+ q.setResultClass(EmpInfo.class);
+ q.declareParameters("Double sal");
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("sal", 30000.);
+ q.setNamedParameters(paramValues);
+ List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_16, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1948,30 +2396,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpInfo " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal");
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpInfo"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
- q.setResultClass(EmpInfo.class);
-
- q.declareParameters("Double sal");
- q.setParameters(30000.);
- List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
-
- EmpInfo info1 = new EmpInfo();
- info1.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp1"));
- EmpInfo info2 = new EmpInfo();
- info2.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp2"));
- EmpInfo info3 = new EmpInfo();
- info3.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp5"));
- List<EmpInfo> expected = Arrays.asList(info1, info2, info3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ List<EmpInfo> expected = testQuery16Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(FullTimeEmployee.class, "salary > sal")) {
+ q.setResultClass(EmpInfo.class);
+ q.declareParameters("Double sal");
+ q.setParameters(30000.);
+ List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_16, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -1990,28 +2424,46 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select into org.apache.jdo.tck.query.api.SampleQueries$EmpInfo " +
- "from org.apache.jdo.tck.pc.company.FullTimeEmployee where salary > :sal";
- Query<FullTimeEmployee> q = pm.newQuery(singleStringQuery);
- // ToDo: the following line should no be necessary
- // org.datanucleus.exceptions.NucleusUserException:
- // Query needs to return objects of type "org.apache.jdo.tck.query.api.SampleQueries$EmpInfo"
- // but it was impossible to set the field "birthdate" type "java.util.Date". The field should
- // have either a public set/put method, or be public.
- //q.setResult("distinct this as FullTimeEmployee");
-
- q.setParameters(30000.);
- List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
+ List<EmpInfo> expected = testQuery16Helper();
+ try (Query<FullTimeEmployee> q = pm.newQuery(SINGLE_STRING_QUERY_16)) {
+ q.setParameters(30000.);
+ List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_16, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
- EmpInfo info1 = new EmpInfo();
- info1.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp1"));
- EmpInfo info2 = new EmpInfo();
- info2.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp2"));
- EmpInfo info3 = new EmpInfo();
- info3.setFullTimeEmployee((FullTimeEmployee)getTransientCompanyModelInstance("emp5"));
- List<EmpInfo> expected = Arrays.asList(info1, info2, info3);
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, infos, expected);
+ /**
+ * Projection of "this" to User-defined Result Class with Matching Method
+ *
+ * This query selects instances of FullTimeEmployee who make more than the parameter salary and
+ * stores the result in a user-defined class.
+ */
+ public void testQuery16f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<EmpInfo> expected = testQuery16Helper();
+ try (JDOQLTypedQuery<FullTimeEmployee> q = pm.newJDOQLTypedQuery(FullTimeEmployee.class)) {
+ QFullTimeEmployee cand = QFullTimeEmployee.candidate();
+ q.result(true, cand);
+ NumericExpression<Double> sal = q.numericParameter("sal", Double.class);
+ q.filter(cand.salary.gt(sal));
+ Map<String, Object> paramValues = new HashMap<>();
+ paramValues.put("sal", 30000.);
+ q.setParameters(paramValues);
+ List<EmpInfo> infos = q.executeResultList(EmpInfo.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_16, infos, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2029,17 +2481,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select e.firstname from org.apache.jdo.tck.pc.company.Department " +
- "where name.startsWith('R&D') && employees.contains(e) " +
- "variables org.apache.jdo.tck.pc.company.Employee e";
- Query<Department> q = pm.newQuery(Department.class);
- q.declareVariables("org.apache.jdo.tck.pc.company.Employee e");
- q.setFilter("name.startsWith('R&D') && employees.contains(e)");
- q.setResult("e.firstname");
- List<String> names = (List<String>) q.execute();
List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Department> q = pm.newQuery(Department.class)) {
+ q.declareVariables("org.apache.jdo.tck.pc.company.Employee e");
+ q.setFilter("name.startsWith('R&D') && employees.contains(e)");
+ q.setResult("e.firstname");
+ List<String> names = (List<String>) q.execute();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_17, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2057,17 +2508,16 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select e.firstname from org.apache.jdo.tck.pc.company.Department " +
- "where name.startsWith('R&D') && employees.contains(e) " +
- "variables org.apache.jdo.tck.pc.company.Employee e";
- Query<Department> q = pm.newQuery(Department.class);
- q.declareVariables("org.apache.jdo.tck.pc.company.Employee e");
- q.setFilter("name.startsWith('R&D') && employees.contains(e)");
- q.setResult("e.firstname");
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Department> q = pm.newQuery(Department.class)) {
+ q.declareVariables("org.apache.jdo.tck.pc.company.Employee e");
+ q.setFilter("name.startsWith('R&D') && employees.contains(e)");
+ q.setResult("e.firstname");
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_17, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2085,14 +2535,13 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select e.firstname from org.apache.jdo.tck.pc.company.Department " +
- "where name.startsWith('R&D') && employees.contains(e) " +
- "variables org.apache.jdo.tck.pc.company.Employee e";
- Query<Department> q = pm.newQuery(singleStringQuery);
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Department> q = pm.newQuery(SINGLE_STRING_QUERY_17)) {
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_17, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2110,14 +2559,13 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select e.firstname from org.apache.jdo.tck.pc.company.Department " +
- "where name.startsWith('R&D') && employees.contains(e) " +
- "variables org.apache.jdo.tck.pc.company.Employee e";
- Query<Department> q = pm.newNamedQuery(Department.class, "projectingVariables");
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Department> q = pm.newNamedQuery(Department.class, "projectingVariables")) {
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_17, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2125,7 +2573,35 @@ public class SampleQueries extends QueryTest {
}
}
}
-
+
+ /**
+ * Projection of variables.
+ *
+ * This query returns the names of all Employees of all "Research" departments.
+ */
+ public void testQuery17f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
+ try (JDOQLTypedQuery<Department> q = pm.newJDOQLTypedQuery(Department.class)) {
+ QDepartment cand = QDepartment.candidate();
+ QEmployee e = QEmployee.variable("e");
+ q.filter(cand.name.startsWith("R&D").and(cand.employees.contains(e)));
+ q.result(false, e.firstname);
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_17, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
/**
* Non-correlated subquery
*
@@ -2135,19 +2611,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee " +
- "where this.weeklyhours > " +
- " (select avg(e.weeklyhours) from org.apache.jdo.tck.pc.company.Employee e)";
- Query<Employee> subq = pm.newQuery(Employee.class);
- subq.setResult("avg(weeklyhours)");
- Query<Employee> q = pm.newQuery(Employee.class);
- q.setFilter("this.weeklyhours > average_hours");
- q.setResult("this.firstname");
- q.addSubquery(subq, "double average_hours", null);
- List<String> names = (List<String>)q.execute();
List<String> expected = Arrays.asList("Michael", "Craig");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class)) {
+ Query<Employee> subq = pm.newQuery(Employee.class);
+ subq.setResult("avg(weeklyhours)");
+ q.setFilter("this.weeklyhours > average_hours");
+ q.setResult("this.firstname");
+ q.addSubquery(subq, "double average_hours", null);
+ List<String> names = (List<String>)q.execute();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_18, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2165,19 +2640,18 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee " +
- "where this.weeklyhours > " +
- " (select avg(e.weeklyhours) from org.apache.jdo.tck.pc.company.Employee e)";
- Query<Employee> subq = pm.newQuery(Employee.class);
- subq.setResult("avg(weeklyhours)");
- Query<Employee> q = pm.newQuery(Employee.class);
- q.setFilter("this.weeklyhours > average_hours");
- q.setResult("this.firstname");
- q.addSubquery(subq, "double average_hours", null);
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael", "Craig");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class)) {
+ Query<Employee> subq = pm.newQuery(Employee.class);
+ subq.setResult("avg(weeklyhours)");
+ q.setFilter("this.weeklyhours > average_hours");
+ q.setResult("this.firstname");
+ q.addSubquery(subq, "double average_hours", null);
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_18, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2195,14 +2669,42 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee " +
- "where this.weeklyhours > " +
- " (select avg(e.weeklyhours) from org.apache.jdo.tck.pc.company.Employee e)";
- Query<Employee> q = pm.newQuery(singleStringQuery);
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael", "Craig");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(SINGLE_STRING_QUERY_18)) {
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_18, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Non-correlated subquery
+ *
+ * This query returns names of employees who work more than the average of all employees.
+ */
+ public void testQuery18f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<String> expected = Arrays.asList("Michael", "Craig");
+ try (JDOQLTypedQuery<Employee> q = pm.newJDOQLTypedQuery(Employee.class)) {
+ QEmployee cand = QEmployee.candidate();
+ q.result(false, cand.firstname);
+ JDOQLTypedSubquery<Employee> subquery = q.subquery("e");
+ QEmployee candsub = QEmployee.candidate("e");
+ q.filter(cand.weeklyhours.gt(subquery.selectUnique(candsub.weeklyhours.avg())));
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_18, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2223,22 +2725,20 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee " +
- "where this.weeklyhours > " +
- " (select AVG(e.weeklyhours) from this.department.employees e " +
- " where e.manager == this.manager)";
- Query<Employee> subq = pm.newQuery(Employee.class);
- subq.setFilter("this.manager == :manager");
- subq.setResult("avg(weeklyhours)");
- Query<Employee> q = pm.newQuery(Employee.class);
- q.setFilter("this.weeklyhours > average_hours");
- q.setResult("firstname");
- q.addSubquery(subq, "double average_hours","this.department.employees",
- "this.manager");
- List<String> names = (List<String>)q.execute();
List<String> expected = Arrays.asList("Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class)) {
+ Query<Employee> subq = pm.newQuery(Employee.class);
+ subq.setFilter("this.manager == :manager");
+ subq.setResult("avg(weeklyhours)");
+ q.setFilter("this.weeklyhours > average_hours");
+ q.setResult("firstname");
+ q.addSubquery(subq, "double average_hours","this.department.employees",
+ "this.manager");
+ List<String> names = (List<String>)q.execute();
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_19, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2259,22 +2759,20 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee " +
- "where this.weeklyhours > " +
- " (select AVG(e.weeklyhours) from this.department.employees e " +
- " where e.manager == this.manager)";
- Query<Employee> subq = pm.newQuery(Employee.class);
- subq.setFilter("this.manager == :manager");
- subq.setResult("avg(weeklyhours)");
- Query<Employee> q = pm.newQuery(Employee.class);
- q.setFilter("this.weeklyhours > average_hours");
- q.setResult("firstname");
- q.addSubquery(subq, "double average_hours","this.department.employees",
- "this.manager");
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(Employee.class)) {
+ Query<Employee> subq = pm.newQuery(Employee.class);
+ subq.setFilter("this.manager == :manager");
+ subq.setResult("avg(weeklyhours)");
+ q.setFilter("this.weeklyhours > average_hours");
+ q.setResult("firstname");
+ q.addSubquery(subq, "double average_hours","this.department.employees",
+ "this.manager");
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_19, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2295,15 +2793,46 @@ public class SampleQueries extends QueryTest {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
- String singleStringQuery =
- "select firstname from org.apache.jdo.tck.pc.company.Employee " +
- "where this.weeklyhours > " +
- " (select AVG(e.weeklyhours) from this.department.employees e " +
- " where e.manager == this.manager)";
- Query<Employee> q = pm.newQuery(singleStringQuery);
- List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael");
- checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ try (Query<Employee> q = pm.newQuery(SINGLE_STRING_QUERY_19)) {
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_19, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Correlated subquery.
+ *
+ * This query returns names of employees who work more than the average of employees
+ * in the same department having the same manager. The candidate collection of the
+ * subquery is the collection of employees in the department of the candidate employee
+ * and the parameter passed to the subquery is the manager of the candidate employee.
+ */
+ public void testQuery19f() {
+ Transaction tx = pm.currentTransaction();
+ try {
+ tx.begin();
+ List<String> expected = Arrays.asList("Michael");
+ try (JDOQLTypedQuery<Employee> q = pm.newJDOQLTypedQuery(Employee.class)) {
+ QEmployee cand = QEmployee.candidate();
+ q.result(false, cand.firstname);
+ JDOQLTypedSubquery<Employee> subquery = q.subquery("e");
+ QEmployee candsub = QEmployee.candidate("e");
+ subquery.filter(candsub.manager.eq(cand.manager));
+ q.filter(cand.weeklyhours.gt(subquery.selectUnique(candsub.weeklyhours.avg())));
+ List<String> names = q.executeResultList(String.class);
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, SINGLE_STRING_QUERY_19, names, expected);
+ } catch (Exception ex) {
+ fail(ASSERTION_FAILED, ex.getLocalizedMessage());
+ }
tx.commit();
} finally {
if (tx.isActive()) {
@@ -2345,6 +2874,38 @@ public class SampleQueries extends QueryTest {
}
}
+ private List<Info> testQuery08Helper() {
+ Info info1 = new Info();
+ info1.firstname = "Michael";
+ info1.salary = 40000.;
+ info1.reportsTo = (Employee)getTransientCompanyModelInstance("emp2");
+ Info info2 = new Info();
+ info2.firstname = "Craig";
+ info2.salary = 50000.;
+ info2.reportsTo = null;
+ return Arrays.asList(info1, info2);
+ }
+
+ private List<EmpWrapper> testQuery15Helper() {
+ EmpWrapper wrapper1 = new EmpWrapper();
+ wrapper1.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp1");
+ EmpWrapper wrapper2 = new EmpWrapper();
+ wrapper2.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp2");
+ EmpWrapper wrapper3 = new EmpWrapper();
+ wrapper3.FullTimeEmployee = (FullTimeEmployee)getTransientCompanyModelInstance("emp5");
+ return Arrays.asList(wrapper1, wrapper2, wrapper3);
+ }
+
+ private List<EmpInfo> testQuery16Helper() {
+ EmpInfo info1 = new EmpInfo();
+ info1.setFullTimeEmployee((FullTimeEmployee) getTransientCompanyModelInstance("emp1"));
+ EmpInfo info2 = new EmpInfo();
+ info2.setFullTimeEmployee((FullTimeEmployee) getTransientCompanyModelInstance("emp2"));
+ EmpInfo info3 = new EmpInfo();
+ info3.setFullTimeEmployee((FullTimeEmployee) getTransientCompanyModelInstance("emp5"));
+ return Arrays.asList(info1, info2, info3);
+ }
+
public static class Info {
public String firstname;
public Double salary;