Author: mbo
Date: Sat Aug 5 20:53:11 2017
New Revision: 1804214
URL: http://svn.apache.org/viewvc?rev=1804214&view=rev
Log:
JDO-761: Added named queries to SampleQueries
Modified:
db/jdo/trunk/specification/OOO/Ch14-Query.odt
db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/SampleQueries.java
db/jdo/trunk/tck/src/jdo/applicationidentity/org/apache/jdo/tck/pc/company/package.jdo
db/jdo/trunk/tck/src/jdo/datastoreidentity/org/apache/jdo/tck/pc/company/package.jdo
Modified: db/jdo/trunk/specification/OOO/Ch14-Query.odt
URL: http://svn.apache.org/viewvc/db/jdo/trunk/specification/OOO/Ch14-Query.odt?rev=1804214&r1=1804213&r2=1804214&view=diff
==============================================================================
Binary files - no diff available.
Modified: db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/SampleQueries.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/SampleQueries.java?rev=1804214&r1=1804213&r2=1804214&view=diff
==============================================================================
--- db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/SampleQueries.java (original)
+++ db/jdo/trunk/tck/src/java/org/apache/jdo/tck/query/api/SampleQueries.java Sat Aug 5 20:53:11
2017
@@ -27,13 +27,14 @@ import java.util.Map;
*<B>Assertion Description: </B>
* This test class runs the example queries from the JDO specification.
*
- * There are up to four test methods per test case:
+ * There are up to five 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
* testQueryxxc: runtime constructed JDO query using setParameters to specify the parameter
values and
* executeList/executeResultList/executeResultUnique to run the query
* testQueryxxd: single string version of the JDO query
+ * testQueryxxe: named query version of the JDO query
*/
public class SampleQueries extends QueryTest {
@@ -1079,6 +1080,37 @@ public class SampleQueries extends Query
}
/**
+ * 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 testQuery09e() {
+ 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, "construct");
+ 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);
+ 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
@@ -1400,6 +1432,37 @@ public class SampleQueries extends Query
}
}
}
+
+ /**
+ * 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 testQuery12e() {
+ 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, "group");
+ 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);
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
/**
* Selection of a Single Instance.
@@ -2030,6 +2093,31 @@ public class SampleQueries extends Query
List<String> names = q.executeResultList(String.class);
List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
+ tx.commit();
+ } finally {
+ if (tx.isActive()) {
+ tx.rollback();
+ }
+ }
+ }
+
+ /**
+ * Projection of variables.
+ *
+ * This query returns the names of all Employees of all "Research" departments.
+ */
+ public void testQuery17e() {
+ 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, "variables");
+ List<String> names = q.executeResultList(String.class);
+ List<String> expected = Arrays.asList("Michael", "Craig", "Joe");
+ checkQueryResultWithoutOrder(ASSERTION_FAILED, singleStringQuery, names, expected);
tx.commit();
} finally {
if (tx.isActive()) {
Modified: db/jdo/trunk/tck/src/jdo/applicationidentity/org/apache/jdo/tck/pc/company/package.jdo
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/jdo/applicationidentity/org/apache/jdo/tck/pc/company/package.jdo?rev=1804214&r1=1804213&r2=1804214&view=diff
==============================================================================
--- db/jdo/trunk/tck/src/jdo/applicationidentity/org/apache/jdo/tck/pc/company/package.jdo
(original)
+++ db/jdo/trunk/tck/src/jdo/applicationidentity/org/apache/jdo/tck/pc/company/package.jdo
Sat Aug 5 20:53:11 2017
@@ -62,6 +62,11 @@ has application identity.
<collection element-type="org.apache.jdo.tck.pc.company.MeetingRoom"/>
<join/>
</field>
+ <query name="variables">
+ select e.firstname
+ where name.startsWith('R&D') && employees.contains(e)
+ variables org.apache.jdo.tck.pc.company.Employee e
+ </query>
</class>
<class name="Employee"
@@ -83,7 +88,16 @@ has application identity.
<class name="FullTimeEmployee"
identity-type="application"
- persistence-capable-superclass="org.apache.jdo.tck.pc.company.Employee"/>
+ persistence-capable-superclass="org.apache.jdo.tck.pc.company.Employee">
+ <query name="construct">
+ select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary,
manager)
+ where department.name == :deptName
+ </query>
+ <query name="group">
+ select avg(salary), sum(salary), department.name
+ group by department.name having count(department.name) > 1
+ </query>
+ </class>
<class name="Insurance"
identity-type="application"
Modified: db/jdo/trunk/tck/src/jdo/datastoreidentity/org/apache/jdo/tck/pc/company/package.jdo
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck/src/jdo/datastoreidentity/org/apache/jdo/tck/pc/company/package.jdo?rev=1804214&r1=1804213&r2=1804214&view=diff
==============================================================================
--- db/jdo/trunk/tck/src/jdo/datastoreidentity/org/apache/jdo/tck/pc/company/package.jdo (original)
+++ db/jdo/trunk/tck/src/jdo/datastoreidentity/org/apache/jdo/tck/pc/company/package.jdo Sat
Aug 5 20:53:11 2017
@@ -52,6 +52,11 @@ has datastore identity.
<collection element-type="org.apache.jdo.tck.pc.company.MeetingRoom"/>
<join/>
</field>
+ <query name="variables">
+ select e.firstname
+ where name.startsWith('R&D') && employees.contains(e)
+ variables org.apache.jdo.tck.pc.company.Employee e
+ </query>
</class>
<class name="Employee" identity-type="datastore">
@@ -69,7 +74,16 @@ has datastore identity.
</field>
</class>
- <class name="FullTimeEmployee" identity-type="datastore"/>
+ <class name="FullTimeEmployee" identity-type="datastore">
+ <query name="construct">
+ select new org.apache.jdo.tck.query.api.SampleQueries$Info (firstname, salary,
manager)
+ where department.name == :deptName
+ </query>
+ <query name="group">
+ select avg(salary), sum(salary), department.name
+ group by department.name having count(department.name) > 1
+ </query>
+ </class>
<class name="Insurance" identity-type="datastore">
</class>
|