db-jdo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m..@apache.org
Subject svn commit: r699883 - in /db/jdo/trunk: tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/ tck2-legacy/src/java/org/apache/jdo/tck/query/result/ tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/ tck2/src/java/org/apache/jdo/tck/quer...
Date Sun, 28 Sep 2008 19:18:59 GMT
Author: mbo
Date: Sun Sep 28 12:18:58 2008
New Revision: 699883

URL: http://svn.apache.org/viewvc?rev=699883&view=rev
Log:
JDO-580: Add TCK query test using methods in the query result

Added:
    db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
  (with props)
    db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
  (with props)
    db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
  (with props)
    db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java   (with
props)
Modified:
    db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
    db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java

Added: db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java?rev=699883&view=auto
==============================================================================
--- db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
(added)
+++ db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
Sun Sep 28 12:18:58 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+ 
+package org.apache.jdo.tck.query.jdoql.subqueries;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+
+import org.apache.jdo.tck.pc.company.CompanyModelReader;
+import org.apache.jdo.tck.pc.company.Department;
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> Test using an aggregate on a size expression
+ *<BR>
+ *<B>Keywords:</B> query aggregate size
+ *<BR>
+ *<B>Assertion ID:</B>
+ *<BR>
+ *<B>Assertion Description: </B> 
+ */
+public class AggregateOnSize extends SubqueriesTest {
+
+    /** */
+    private static final String ASSERTION_FAILED = 
+        "Assertion (AggregateOnSize) failed: ";
+    
+    /**
+     * The <code>main</code> is called when the class
+     * is directly executed from the command line.
+     * @param args The arguments passed to the program.
+     */
+    public static void main(String[] args) {
+        BatchTestRunner.run(AggregateOnSize.class);
+    }
+
+    /** */
+    public void testMaxAndSizeInSubquery() throws Exception {
+        PersistenceManager pm = getPM();
+
+        List expectedResult = getTransientCompanyModelInstancesAsList(
+            new String[]{"dept1"});
+
+        // Select departments with the maximum number of employees
+        String singleStringJDOQL = 
+            "SELECT FROM " + Department.class.getName() + " WHERE this.employees.size() ==
" + 
+            "(SELECT MAX(d.employees.size()) FROM " + Department.class.getName() + " d)";
+
+        // API query
+        Query sub = pm.newQuery(Department.class);
+        sub.setResult("MAX(this.employees.size())");
+        Query apiQuery = pm.newQuery(Department.class);
+        apiQuery.setFilter("this.employees.size() == number");
+        apiQuery.addSubquery(sub, "long number", null); 
+        executeJDOQuery(ASSERTION_FAILED, apiQuery, singleStringJDOQL, 
+                        false, null, expectedResult, true);
+
+        // API query against memory model
+        List allDepartments = getAllPersistentInstances(pm, Department.class);
+        apiQuery.setCandidates(allDepartments);
+        executeJDOQuery(ASSERTION_FAILED, apiQuery, singleStringJDOQL, 
+                        false, null, expectedResult, true);
+
+        // single String JDOQL
+        Query singleStringQuery = pm.newQuery(singleStringJDOQL);
+        executeJDOQuery(ASSERTION_FAILED, singleStringQuery, singleStringJDOQL, 
+                        false, null, expectedResult, true);
+    }
+
+    /**
+     * @see JDO_Test#localSetUp()
+     */
+    protected void localSetUp() {
+        addTearDownClass(CompanyModelReader.getTearDownClasses());
+        loadAndPersistCompanyModel(getPM());
+    }
+
+}

Propchange: db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java?rev=699883&r1=699882&r2=699883&view=diff
==============================================================================
--- db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
(original)
+++ db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
Sun Sep 28 12:18:58 2008
@@ -48,16 +48,29 @@
      * @return a List including all persistent Employee instances
      */
     protected List getAllEmployees(PersistenceManager pm) {
+        return getAllPersistentInstances(pm, Employee.class);
+    }
+
+    /** 
+     * Helper method retuning all persistent instances of the specified class.
+     * Note, this methods executes a JDO query in a new transaction.
+     * @param pm the PersistenceManager
+     * @param pcClass the persistent capable class 
+     * @return a List including all persistent instances of the specified class.
+     */
+    protected List getAllPersistentInstances(PersistenceManager pm, 
+                                             Class pcClass) {
         Transaction tx = pm.currentTransaction();
         try {
             tx.begin();
-            List allEmployees = (List)pm.newQuery(Employee.class).execute();
+            List all = (List)pm.newQuery(pcClass).execute();
             tx.commit();
-            return allEmployees;
+            return all;
         } finally { 
             if ((tx != null) && tx.isActive()) {
                 tx.rollback();
             }
         }
     }
+
 }

Added: db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java?rev=699883&view=auto
==============================================================================
--- db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
(added)
+++ db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
Sun Sep 28 12:18:58 2008
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+ 
+package org.apache.jdo.tck.query.jdoql.result;
+
+import java.math.BigInteger;
+import java.util.Arrays;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+
+import org.apache.jdo.tck.pc.company.CompanyModelReader;
+import org.apache.jdo.tck.pc.company.Department;
+import org.apache.jdo.tck.pc.company.Employee;
+import org.apache.jdo.tck.query.QueryElementHolder;
+import org.apache.jdo.tck.query.QueryTest;
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> Test using methods in the result expression
+ *<BR>
+ *<B>Keywords:</B> query result
+ *<BR>
+ *<B>Assertion ID:</B>
+ *<BR>
+ *<B>Assertion Description: </B> 
+ */
+public class MethodsInResult extends QueryTest {
+
+    /** */
+    private static final String ASSERTION_FAILED = 
+        "Assertion (MethodsInResult) failed: ";
+    
+    /** 
+     * The array of valid queries which may be executed as 
+     * single string queries and as API queries.
+     */
+    private static final QueryElementHolder[] VALID_QUERIES = {
+        // collection.size()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.employees.size()",
+        /*INTO*/        null, 
+        /*FROM*/        Department.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.name ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+        
+        // map.size()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.phoneNumbers.size()",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // MAX(collection.size())
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "MAX(this.employees.size())",
+        /*INTO*/        null, 
+        /*FROM*/        Department.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    null,
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // map.get()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.phoneNumbers.get('home')",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // String.substring()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.firstname.substring(1,4)",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // String.indexOf()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.firstname.indexOf('First')",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null)    
+    };
+
+    /** 
+     * The expected results of valid queries.
+     */
+    private Object[] expectedResult = {
+        // collection.size()
+        Arrays.asList(new Object[] { new Long(3), new Long(2) }),
+        // map.size()
+        Arrays.asList(new Object[] { new Long(2), new Long(2), new Long(2), new Long(2),
new Long(2) }),
+        // MAX(collection.size())
+        new Long(3),
+        // map.get()
+        Arrays.asList(new Object[] { "1111", "2222", "3333", "3343", "3363" }),
+        // String.substring()
+        Arrays.asList(new Object[] { "mp1", "mp2", "mp3", "mp4", "mp5" }),
+        // String.indexOf()
+        //Arrays.asList(new Object[] { new BigInteger("4"), new BigInteger("4"), new BigInteger("4"),
new BigInteger("4"), new BigInteger("4") }),
+        Arrays.asList(new Object[] { new Integer(4), new Integer(4), new Integer(4), new
Integer(4), new Integer(4) }),
+    };
+    
+    /**
+     * The <code>main</code> is called when the class
+     * is directly executed from the command line.
+     * @param args The arguments passed to the program.
+     */
+    public static void main(String[] args) {
+        BatchTestRunner.run(MethodsInResult.class);
+    }
+
+    public void testCollectionSizeInResult() throws Exception {
+        int index = 0;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testMapSizeInResult() throws Exception {
+        int index = 1;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testMaxAndSizeInResult() throws Exception {
+        int index = 2;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testMapGetInResult() throws Exception {
+        int index = 3;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testSubstringInResult() throws Exception {
+        int index = 4;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testIndexOfInResult() throws Exception {
+        int index = 5;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    /**
+     * @see JDO_Test#localSetUp()
+     */
+    protected void localSetUp() {
+        addTearDownClass(CompanyModelReader.getTearDownClasses());
+        loadAndPersistCompanyModel(getPM());
+    }
+
+}

Propchange: db/jdo/trunk/tck2-legacy/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java?rev=699883&view=auto
==============================================================================
--- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
(added)
+++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
Sun Sep 28 12:18:58 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+ 
+package org.apache.jdo.tck.query.jdoql.subqueries;
+
+import java.util.Arrays;
+import java.util.List;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+
+import org.apache.jdo.tck.pc.company.CompanyModelReader;
+import org.apache.jdo.tck.pc.company.Department;
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> Test using an aggregate on a size expression
+ *<BR>
+ *<B>Keywords:</B> query aggregate size
+ *<BR>
+ *<B>Assertion ID:</B>
+ *<BR>
+ *<B>Assertion Description: </B> 
+ */
+public class AggregateOnSize extends SubqueriesTest {
+
+    /** */
+    private static final String ASSERTION_FAILED = 
+        "Assertion (AggregateOnSize) failed: ";
+    
+    /**
+     * The <code>main</code> is called when the class
+     * is directly executed from the command line.
+     * @param args The arguments passed to the program.
+     */
+    public static void main(String[] args) {
+        BatchTestRunner.run(AggregateOnSize.class);
+    }
+
+    /** */
+    public void testMaxAndSizeInSubquery() throws Exception {
+        PersistenceManager pm = getPM();
+
+        List expectedResult = getTransientCompanyModelInstancesAsList(
+            new String[]{"dept1"});
+
+        // Select departments with the maximum number of employees
+        String singleStringJDOQL = 
+            "SELECT FROM " + Department.class.getName() + " WHERE this.employees.size() ==
" + 
+            "(SELECT MAX(d.employees.size()) FROM " + Department.class.getName() + " d)";
+
+        // API query
+        Query sub = pm.newQuery(Department.class);
+        sub.setResult("MAX(this.employees.size())");
+        Query apiQuery = pm.newQuery(Department.class);
+        apiQuery.setFilter("this.employees.size() == number");
+        apiQuery.addSubquery(sub, "long number", null); 
+        executeJDOQuery(ASSERTION_FAILED, apiQuery, singleStringJDOQL, 
+                        false, null, expectedResult, true);
+
+        // API query against memory model
+        List allDepartments = getAllPersistentInstances(pm, Department.class);
+        apiQuery.setCandidates(allDepartments);
+        executeJDOQuery(ASSERTION_FAILED, apiQuery, singleStringJDOQL, 
+                        false, null, expectedResult, true);
+
+        // single String JDOQL
+        Query singleStringQuery = pm.newQuery(singleStringJDOQL);
+        executeJDOQuery(ASSERTION_FAILED, singleStringQuery, singleStringJDOQL, 
+                        false, null, expectedResult, true);
+    }
+
+    /**
+     * @see JDO_Test#localSetUp()
+     */
+    protected void localSetUp() {
+        addTearDownClass(CompanyModelReader.getTearDownClasses());
+        loadAndPersistCompanyModel(getPM());
+    }
+
+}

Propchange: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/AggregateOnSize.java
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java?rev=699883&r1=699882&r2=699883&view=diff
==============================================================================
--- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
(original)
+++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/jdoql/subqueries/SubqueriesTest.java
Sun Sep 28 12:18:58 2008
@@ -48,16 +48,29 @@
      * @return a List including all persistent Employee instances
      */
     protected List getAllEmployees(PersistenceManager pm) {
+        return getAllPersistentInstances(pm, Employee.class);
+    }
+
+    /** 
+     * Helper method retuning all persistent instances of the specified class.
+     * Note, this methods executes a JDO query in a new transaction.
+     * @param pm the PersistenceManager
+     * @param pcClass the persistent capable class 
+     * @return a List including all persistent instances of the specified class.
+     */
+    protected List getAllPersistentInstances(PersistenceManager pm, 
+                                             Class pcClass) {
         Transaction tx = pm.currentTransaction();
         try {
             tx.begin();
-            List allEmployees = (List)pm.newQuery(Employee.class).execute();
+            List all = (List)pm.newQuery(pcClass).execute();
             tx.commit();
-            return allEmployees;
+            return all;
         } finally { 
             if ((tx != null) && tx.isActive()) {
                 tx.rollback();
             }
         }
     }
+
 }

Added: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java?rev=699883&view=auto
==============================================================================
--- db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java (added)
+++ db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java Sun Sep
28 12:18:58 2008
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software 
+ * distributed under the License is distributed on an "AS IS" BASIS, 
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
+ * See the License for the specific language governing permissions and 
+ * limitations under the License.
+ */
+ 
+package org.apache.jdo.tck.query.jdoql.result;
+
+import java.math.BigInteger;
+import java.util.Arrays;
+
+import javax.jdo.PersistenceManager;
+import javax.jdo.Query;
+
+import org.apache.jdo.tck.pc.company.CompanyModelReader;
+import org.apache.jdo.tck.pc.company.Department;
+import org.apache.jdo.tck.pc.company.Employee;
+import org.apache.jdo.tck.query.QueryElementHolder;
+import org.apache.jdo.tck.query.QueryTest;
+import org.apache.jdo.tck.util.BatchTestRunner;
+
+/**
+ *<B>Title:</B> Test using methods in the result expression
+ *<BR>
+ *<B>Keywords:</B> query result
+ *<BR>
+ *<B>Assertion ID:</B>
+ *<BR>
+ *<B>Assertion Description: </B> 
+ */
+public class MethodsInResult extends QueryTest {
+
+    /** */
+    private static final String ASSERTION_FAILED = 
+        "Assertion (MethodsInResult) failed: ";
+    
+    /** 
+     * The array of valid queries which may be executed as 
+     * single string queries and as API queries.
+     */
+    private static final QueryElementHolder[] VALID_QUERIES = {
+        // collection.size()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.employees.size()",
+        /*INTO*/        null, 
+        /*FROM*/        Department.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.name ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+        
+        // map.size()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.phoneNumbers.size()",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // MAX(collection.size())
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "MAX(this.employees.size())",
+        /*INTO*/        null, 
+        /*FROM*/        Department.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    null,
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // map.get()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.phoneNumbers.get('home')",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // String.substring()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.firstname.substring(1,4)",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null),
+
+        // String.indexOf()
+        new QueryElementHolder(
+        /*UNIQUE*/      null,
+        /*RESULT*/      "this.firstname.indexOf('First')",
+        /*INTO*/        null, 
+        /*FROM*/        Employee.class,
+        /*EXCLUDE*/     null,
+        /*WHERE*/       null,
+        /*VARIABLES*/   null,
+        /*PARAMETERS*/  null,
+        /*IMPORTS*/     null,
+        /*GROUP BY*/    null,
+        /*ORDER BY*/    "this.lastname ascending, this.firstname ascending",
+        /*FROM*/        null,
+        /*TO*/          null)    
+    };
+
+    /** 
+     * The expected results of valid queries.
+     */
+    private Object[] expectedResult = {
+        // collection.size()
+        Arrays.asList(new Object[] { new Long(3), new Long(2) }),
+        // map.size()
+        Arrays.asList(new Object[] { new Long(2), new Long(2), new Long(2), new Long(2),
new Long(2) }),
+        // MAX(collection.size())
+        new Long(3),
+        // map.get()
+        Arrays.asList(new Object[] { "1111", "2222", "3333", "3343", "3363" }),
+        // String.substring()
+        Arrays.asList(new Object[] { "mp1", "mp2", "mp3", "mp4", "mp5" }),
+        // String.indexOf()
+        //Arrays.asList(new Object[] { new BigInteger("4"), new BigInteger("4"), new BigInteger("4"),
new BigInteger("4"), new BigInteger("4") }),
+        Arrays.asList(new Object[] { new Integer(4), new Integer(4), new Integer(4), new
Integer(4), new Integer(4) }),
+    };
+    
+    /**
+     * The <code>main</code> is called when the class
+     * is directly executed from the command line.
+     * @param args The arguments passed to the program.
+     */
+    public static void main(String[] args) {
+        BatchTestRunner.run(MethodsInResult.class);
+    }
+
+    public void testCollectionSizeInResult() throws Exception {
+        int index = 0;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testMapSizeInResult() throws Exception {
+        int index = 1;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testMaxAndSizeInResult() throws Exception {
+        int index = 2;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testMapGetInResult() throws Exception {
+        int index = 3;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testSubstringInResult() throws Exception {
+        int index = 4;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    public void testIndexOfInResult() throws Exception {
+        int index = 5;
+        executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+        executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
+                expectedResult[index]);
+    }
+
+    /**
+     * @see JDO_Test#localSetUp()
+     */
+    protected void localSetUp() {
+        addTearDownClass(CompanyModelReader.getTearDownClasses());
+        loadAndPersistCompanyModel(getPM());
+    }
+
+}

Propchange: db/jdo/trunk/tck2/src/java/org/apache/jdo/tck/query/result/MethodsInResult.java
------------------------------------------------------------------------------
    svn:eol-style = LF



Mime
View raw message