You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by mb...@apache.org on 2005/05/22 20:40:21 UTC
svn commit: r171355 [27/31] - in /incubator/jdo/trunk/fostore20: ./ src/
src/conf/ src/java/ src/java/org/ src/java/org/apache/
src/java/org/apache/jdo/ src/java/org/apache/jdo/impl/
src/java/org/apache/jdo/impl/fostore/ test/ test/conf/ test/fsuid2/
test/fsuid2/org/ test/fsuid2/org/apache/ test/fsuid2/org/apache/jdo/
test/fsuid2/org/apache/jdo/pc/ test/java/ test/java/org/
test/java/org/apache/ test/java/org/apache/jdo/
test/java/org/apache/jdo/impl/ test/java/org/apache/jdo/impl/fostore/
test/java/org/apache/jdo/pc/ test/java/org/apache/jdo/pc/appid/
test/java/org/apache/jdo/pc/empdept/
test/java/org/apache/jdo/pc/serializable/
test/java/org/apache/jdo/pc/xempdept/ test/java/org/apache/jdo/test/
test/java/org/apache/jdo/test/query/ test/java/org/apache/jdo/test/util/
test/jdo/ test/jdo/org/ test/jdo/org/apache/ test/jdo/org/apache/jdo/
test/jdo/org/apache/jdo/pc/ test/jdo/org/apache/jdo/pc/appid/
test/jdo/org/apache/jdo/pc/empdept/ test/jdo/org/apache/jdo/pc/serializable/
test/jdo/org/apache/jdo/pc/xempdept/
Added: incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ParameterTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ParameterTest.java?rev=171355&view=auto
==============================================================================
--- incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ParameterTest.java (added)
+++ incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ParameterTest.java Sun May 22 11:40:13 2005
@@ -0,0 +1,1059 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/*
+ * ParameterTest.java
+ *
+ * Created on April 10, 2000
+ */
+
+package org.apache.jdo.test.query;
+
+import java.util.*;
+import java.io.PrintStream;
+
+import javax.jdo.*;
+
+import org.apache.jdo.pc.xempdept.Department;
+import org.apache.jdo.pc.xempdept.Employee;
+import org.apache.jdo.pc.xempdept.Insurance;
+import org.apache.jdo.pc.xempdept.PrimitiveTypes;
+
+/**
+ *
+ * @author Michael Bouschen
+ */
+public class ParameterTest
+ extends PositiveTest
+{
+ public ParameterTest(PersistenceManagerFactory pmf, PrintStream log)
+ {
+ super(pmf, log);
+ }
+
+ protected boolean isTestMethodName(String methodName)
+ {
+ return methodName.startsWith("parameter");
+ }
+
+ // ==========================================================================
+ // Test methods
+ // ==========================================================================
+
+ /**
+ * Testcase: parameter in relational operation
+ * field == parameter
+ */
+ public void parameter001()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long id");
+ query.setFilter("empid == id");
+ setEmployeeCandidates(query);
+ Object result = query.execute(new Long(1));
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: parameter in relational operation
+ * field == parameter (type String)
+ */
+ public void parameter002()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("String lastname");
+ query.setFilter("this.lastname == lastname");
+ setEmployeeCandidates(query);
+ Object result = query.execute("lastSalesFour");
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: parameter in relational operation
+ * field == parameter (type java.lang.String)
+ */
+ public void parameter003()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("java.lang.String lastname");
+ query.setFilter("lastname == this.lastname");
+ setEmployeeCandidates(query);
+ Object result = query.execute("lastSalesFour");
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: executeWithArray
+ */
+ public void parameter004()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long l");
+ query.setFilter("empid == l");
+ setEmployeeCandidates(query);
+ Object[] actualParams = {new Long(1)};
+ Object result = query.executeWithArray(actualParams);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: executeWithMap
+ */
+ public void parameter005()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long l");
+ query.setFilter("empid == l");
+ setEmployeeCandidates(query);
+ Map actualParams = new java.util.HashMap();
+ actualParams.put("l", new Long(1));
+ Object result = query.executeWithMap(actualParams);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: date comparison
+ */
+ public void parameter006()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import java.util.Date");
+ query.declareParameters("Date d");
+ query.setFilter("hiredate == d");
+ setEmployeeCandidates(query);
+ Object result = query.execute(new GregorianCalendar(2000, 1, 2).getTime());
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: date comparison
+ */
+ public void parameter007()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import java.util.Date");
+ query.declareParameters("Date d");
+ query.setFilter("hiredate < d");
+ setEmployeeCandidates(query);
+ Object result = query.execute(new GregorianCalendar(2000, 1, 2).getTime());
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 11;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 12;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 13;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: multiple parameters
+ */
+ public void parameter008()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("String name, long id");
+ query.setFilter("firstname == name && empid == id");
+ setEmployeeCandidates(query);
+ Object result = query.execute("firstEngOne", new Long(1));
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: parameter value is null
+ */
+ public void parameter009()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("String name");
+ query.setFilter("firstname == name");
+ setEmployeeCandidates(query);
+ Object result = query.execute(null);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 100;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: parameter field access
+ */
+ public void parameter010()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("Department d");
+ query.setFilter("department.deptid == d.deptid");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * relship == param
+ * navigating reference side of 1:n relationship
+ */
+ public void parameter011()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("Department d");
+ query.setFilter("department == d");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * param == relship
+ * navigating reference side of 1:n relationship
+ */
+ public void parameter012()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("Department d");
+ query.setFilter("d == department");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * relship != param
+ * navigating reference side of 1:n relationship
+ */
+ public void parameter013()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("Department d");
+ query.setFilter("department != d");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 5;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 6;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 7;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 8;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 9;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 11;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 12;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 13;
+ expected.add(key);
+ // MBO: include employees having no department.
+ // Problem with SQL queries, outer joins?
+ key = new Employee.Oid();
+ key.empid = 100;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * param != relship
+ * navigating reference side of 1:n relationship
+ */
+ public void parameter014()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("Department d");
+ query.setFilter("d != department");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 5;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 6;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 7;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 8;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 9;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 11;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 12;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 13;
+ expected.add(key);
+ // MBO: include employees having no department.
+ // Problem with SQL queries, outer joins?
+ key = new Employee.Oid();
+ key.empid = 100;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * relship == param
+ * navigating 1:1 relationship
+ */
+ public void parameter015()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Employee e = getEmployeeById(2L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Insurance.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Employee");
+ query.declareParameters("Employee e");
+ query.setFilter("employee == e");
+ setInsuranceCandidates(query);
+ Object result = query.execute(e);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Insurance.Oid key = new Insurance.Oid();
+ key.insid = 2;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * param == relship
+ * navigating 1:1 relationship
+ */
+
+ public void parameter016()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Employee e = getEmployeeById(2L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Insurance.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Employee");
+ query.declareParameters("Employee e");
+ query.setFilter("e == employee");
+ setInsuranceCandidates(query);
+ Object result = query.execute(e);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Insurance.Oid key = new Insurance.Oid();
+ key.insid = 2;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * relship != param
+ * navigating 1:1 relationship
+ */
+ public void parameter017()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Employee e = getEmployeeById(2L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Insurance.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Employee");
+ query.declareParameters("Employee e");
+ query.setFilter("employee != e");
+ setInsuranceCandidates(query);
+ Object result = query.execute(e);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Insurance.Oid key = new Insurance.Oid();
+ key.insid = 1;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 3;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 4;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 5;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 6;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 7;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 8;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 9;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 11;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 12;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 13;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * param != relship
+ * navigating 1:1 relationship
+ */
+ public void parameter018()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+ Employee e = getEmployeeById(2L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Insurance.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Employee");
+ query.declareParameters("Employee e");
+ query.setFilter("e != employee");
+ setInsuranceCandidates(query);
+ Object result = query.execute(e);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Insurance.Oid key = new Insurance.Oid();
+ key.insid = 1;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 3;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 4;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 5;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 6;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 7;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 8;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 9;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 11;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 12;
+ expected.add(key);
+ key = new Insurance.Oid();
+ key.insid = 13;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * relship == param
+ * navigating navigating reference side of 1:n self referencing relationship
+ */
+ public void parameter019()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Employee e = getEmployeeById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("Employee mgr");
+ query.setFilter("manager == mgr");
+ setEmployeeCandidates(query);
+ Object result = query.execute(e);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: object comparison
+ * relship != param
+ * navigating navigating reference side of 1:n self referencing relationship
+ */
+ public void parameter020()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Employee e = getEmployeeById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("Employee mgr");
+ query.setFilter("manager != mgr");
+ setEmployeeCandidates(query);
+ Object result = query.execute(e);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ // MBO: include employees having no manager.
+ // Problem with SQL queries, outer joins?
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 5;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 6;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 7;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 8;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 9;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 11;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 12;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 13;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 100;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: declareImports including two imports
+ * checks bug report 4421917
+ */
+ public void parameter021()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+ Insurance i = getInsuranceById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department; import org.apache.jdo.pc.xempdept.Insurance;");
+ query.declareParameters("Department d, Insurance i");
+ query.setFilter("department == d & insurance == i");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d, i);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: Boolean parameter compared with boolean field
+ * checks bug report 4527554
+ */
+ public void parameter022()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.declareParameters("Boolean value");
+ query.setFilter("booleanNotNull == value");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute(Boolean.TRUE);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ key = new PrimitiveTypes.Oid();
+ key.id = 3;
+ expected.add(key);
+ key = new PrimitiveTypes.Oid();
+ key.id = 5;
+ expected.add(key);
+ key = new PrimitiveTypes.Oid();
+ key.id = 11;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: boolean parameter compared with Boolean field
+ * checks bug report 4527554
+ */
+ public void parameter023()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.declareParameters("boolean value");
+ query.setFilter("booleanNull == value");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute(Boolean.TRUE);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ key = new PrimitiveTypes.Oid();
+ key.id = 3;
+ expected.add(key);
+ key = new PrimitiveTypes.Oid();
+ key.id = 5;
+ expected.add(key);
+ key = new PrimitiveTypes.Oid();
+ key.id = 11;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: BigDecimal parameter compared with BigDecimal field having different scale.
+ * checks bug report 4531789
+ */
+ public void parameter024()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.declareParameters("java.math.BigDecimal value");
+ query.setFilter("bigDecimal == value");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute(new java.math.BigDecimal("1.00000"));
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+}
Added: incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/PositiveTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/PositiveTest.java?rev=171355&view=auto
==============================================================================
--- incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/PositiveTest.java (added)
+++ incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/PositiveTest.java Sun May 22 11:40:13 2005
@@ -0,0 +1,485 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/*
+ * PositiveTest.java
+ *
+ * Created on March 31, 2000
+ */
+
+package org.apache.jdo.test.query;
+
+import java.io.PrintStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+import javax.jdo.Extent;
+import javax.jdo.PersistenceManager;
+import javax.jdo.PersistenceManagerFactory;
+import javax.jdo.Query;
+import javax.jdo.Transaction;
+
+import org.apache.jdo.impl.fostore.FOStorePMF;
+import org.apache.jdo.impl.jdoql.tree.Tree;
+import org.apache.jdo.jdoql.tree.QueryTree;
+
+import org.apache.jdo.pc.xempdept.Company;
+import org.apache.jdo.pc.xempdept.Department;
+import org.apache.jdo.pc.xempdept.Employee;
+import org.apache.jdo.pc.xempdept.FullTimeEmployee;
+import org.apache.jdo.pc.xempdept.Insurance;
+import org.apache.jdo.pc.xempdept.PartTimeEmployee;
+import org.apache.jdo.pc.xempdept.PrimitiveTypes;
+import org.apache.jdo.pc.xempdept.Project;
+
+/**
+ *
+ * @author Michael Bouschen
+ */
+public abstract class PositiveTest
+ implements QueryTest
+{
+ /** The persistence manager factory. */
+ protected PersistenceManagerFactory pmf;
+
+ /** The persistence manager. */
+ protected PersistenceManager pm;
+
+ /** The log stream. */
+ protected PrintStream log;
+
+ /** Company candidates */
+ protected Collection companyCandidates;
+
+ /** Department candidates */
+ protected Collection departmentCandidates;
+
+ /** Employee candidates */
+ protected Collection employeeCandidates;
+
+ /** FullTimeEmployee candidates */
+ protected Collection fullTimeEmployeeCandidates;
+
+ /** PartTimeEmployee candidates */
+ protected Collection partTimeEmployeeCandidates;
+
+ /** Insurance candidates */
+ protected Collection insuranceCandidates;
+
+ /** Project candidates */
+ protected Collection projectCandidates;
+
+ /** PrimitiveTypes candidates */
+ protected Collection primitiveTypesCandidates;
+
+ /** Company extent */
+ protected Extent companyExtent;
+
+ /** Department extent */
+ protected Extent departmentExtent;
+
+ /** Employee extent */
+ protected Extent employeeExtent;
+
+ /** FullTimeEmployee extent */
+ protected Extent fullTimeEmployeeExtent;
+
+ /** PartTimeEmployee extent */
+ protected Extent partTimeEmployeeExtent;
+
+ /** Insurance extent */
+ protected Extent insuranceExtent;
+
+ /** Project extent */
+ protected Extent projectExtent;
+
+ /** PrimitiveTypes extent */
+ protected Extent primitiveTypesExtent;
+
+ /**
+ *
+ */
+ public PositiveTest(PersistenceManagerFactory pmf, PrintStream log)
+ {
+ this.pmf = pmf;
+ this.pm = pmf.getPersistenceManager();
+ this.log = log;
+ }
+
+ /**
+ *
+ */
+ public void initCandidates(Collection companyCandidates,
+ Collection departmentCandidates,
+ Collection employeeCandidates,
+ Collection fullTimeEmployeeCandidates,
+ Collection partTimeEmployeeCandidates,
+ Collection insuranceCandidates,
+ Collection projectCandidates,
+ Collection primitiveTypesCandidates)
+ {
+ this.companyCandidates = companyCandidates;
+ if (companyCandidates == null)
+ companyExtent = pm.getExtent(Company.class, true);
+ this.departmentCandidates = departmentCandidates;
+ if (departmentCandidates == null)
+ departmentExtent = pm.getExtent(Department.class, true);
+ this.employeeCandidates = employeeCandidates;
+ if (employeeCandidates == null)
+ employeeExtent = pm.getExtent(Employee.class, true);
+ this.fullTimeEmployeeCandidates = fullTimeEmployeeCandidates;
+ if (fullTimeEmployeeCandidates == null)
+ fullTimeEmployeeExtent = pm.getExtent(FullTimeEmployee.class, true);
+ this.partTimeEmployeeCandidates = partTimeEmployeeCandidates;
+ if (partTimeEmployeeCandidates == null)
+ partTimeEmployeeExtent = pm.getExtent(PartTimeEmployee.class, true);
+ this.insuranceCandidates = insuranceCandidates;
+ if (insuranceCandidates == null)
+ insuranceExtent = pm.getExtent(Insurance.class, true);
+ this.projectCandidates = projectCandidates;
+ if (projectCandidates == null)
+ projectExtent = pm.getExtent(Project.class, true);
+ this.primitiveTypesCandidates = primitiveTypesCandidates;
+ if (primitiveTypesCandidates == null)
+ primitiveTypesExtent = pm.getExtent(PrimitiveTypes.class, true);
+ }
+
+ /**
+ *
+ */
+ public Department getDepartmentById(long deptid)
+ {
+ Iterator i = (departmentCandidates == null) ?
+ departmentExtent.iterator() : departmentCandidates.iterator();
+ while (i.hasNext()) {
+ Department next = (Department)i.next();
+ if (deptid == next.getDeptid())
+ return next;
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public Employee getEmployeeById(long empid)
+ {
+ Iterator i = (employeeCandidates == null) ?
+ employeeExtent.iterator() : employeeCandidates.iterator();
+ while (i.hasNext()) {
+ Employee next = (Employee)i.next();
+ if (empid == next.getEmpid())
+ return next;
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public FullTimeEmployee getFullTimeEmployeeById(long empid)
+ {
+ Iterator i = (fullTimeEmployeeCandidates == null) ?
+ fullTimeEmployeeExtent.iterator() : fullTimeEmployeeCandidates.iterator();
+ while (i.hasNext()) {
+ FullTimeEmployee next = (FullTimeEmployee)i.next();
+ if (empid == next.getEmpid())
+ return next;
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public PartTimeEmployee getPartTimeEmployeeById(long empid)
+ {
+ Iterator i = (partTimeEmployeeCandidates == null) ?
+ partTimeEmployeeExtent.iterator() : partTimeEmployeeCandidates.iterator();
+ while (i.hasNext()) {
+ PartTimeEmployee next = (PartTimeEmployee)i.next();
+ if (empid == next.getEmpid())
+ return next;
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public Insurance getInsuranceById(long insid)
+ {
+ Iterator i = (insuranceCandidates == null) ?
+ insuranceExtent.iterator() : insuranceCandidates.iterator();
+ while (i.hasNext()) {
+ Insurance next = (Insurance)i.next();
+ if (insid == next.getInsid())
+ return next;
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public Project getProjectById(long projid)
+ {
+ Iterator i = (projectCandidates == null) ?
+ projectExtent.iterator() : projectCandidates.iterator();
+ while (i.hasNext()) {
+ Project next = (Project)i.next();
+ if (projid == next.getProjid())
+ return next;
+ }
+ return null;
+ }
+
+ /**
+ *
+ */
+ public PrimitiveTypes getPrimitiveTypesById(long id)
+ {
+ Iterator i = (primitiveTypesCandidates == null) ?
+ primitiveTypesExtent.iterator() : primitiveTypesCandidates.iterator();
+ while (i.hasNext()) {
+ PrimitiveTypes next = (PrimitiveTypes)i.next();
+ if (id == next.getId())
+ return next;
+ }
+ return null;
+ }
+
+ /** */
+ protected void setDepartmentCandidates(Query query)
+ {
+ if (departmentCandidates == null)
+ query.setCandidates(departmentExtent);
+ else
+ query.setCandidates(departmentCandidates);
+ }
+
+ /** */
+ protected void setEmployeeCandidates(Query query)
+ {
+ if (employeeCandidates == null)
+ query.setCandidates(employeeExtent);
+ else
+ query.setCandidates(employeeCandidates);
+ }
+
+ /** */
+ protected void setFullTimeEmployeeCandidates(Query query)
+ {
+ if (fullTimeEmployeeCandidates == null)
+ query.setCandidates(fullTimeEmployeeExtent);
+ else
+ query.setCandidates(fullTimeEmployeeCandidates);
+ }
+
+ /** */
+ protected void setPartTimeEmployeeCandidates(Query query)
+ {
+ if (partTimeEmployeeCandidates == null)
+ query.setCandidates(partTimeEmployeeExtent);
+ else
+ query.setCandidates(partTimeEmployeeCandidates);
+ }
+
+ /** */
+ protected void setInsuranceCandidates(Query query)
+ {
+ if (employeeCandidates == null)
+ query.setCandidates(insuranceExtent);
+ else
+ query.setCandidates(insuranceCandidates);
+ }
+
+ /** */
+ protected void setProjectCandidates(Query query)
+ {
+ if (employeeCandidates == null)
+ query.setCandidates(projectExtent);
+ else
+ query.setCandidates(projectCandidates);
+ }
+
+ /** */
+ protected void setPrimitiveTypesCandidates(Query query)
+ {
+ if (employeeCandidates == null)
+ query.setCandidates(primitiveTypesExtent);
+ else
+ query.setCandidates(primitiveTypesCandidates);
+ }
+
+ /**
+ * Create a new query tree node
+ */
+ protected QueryTree newQueryTree()
+ {
+ if (pmf instanceof FOStorePMF)
+ return ((FOStorePMF)pmf).newQueryTree();
+ else
+ return new Tree();
+ }
+
+ /**
+ * Runs all test method provided by this class.
+ */
+ public boolean runAll()
+ {
+ boolean ok = true;
+ Method[] methods = this.getClass().getDeclaredMethods();
+ for (int i = 0; i < methods.length; i++)
+ {
+ Method m = methods[i];
+ if (isTestMethod(m))
+ if (!runTest(m))
+ ok = false;
+ }
+ return ok;
+ }
+
+ /**
+ * Runs all test case specified in tests
+ */
+ public boolean runTests(Vector tests)
+ {
+ boolean ok = true;
+ for (Iterator i = tests.iterator(); i.hasNext();)
+ {
+ String methodName = (String)i.next();
+ try
+ {
+ if (isTestMethodName(methodName))
+ {
+ Method m = this.getClass().getMethod(methodName, new Class[0]);
+ if (!runTest(m))
+ ok = false;
+ }
+ }
+ catch (NoSuchMethodException ex)
+ {
+ // this test class does not support the test method => skip
+ }
+ }
+ return ok;
+ }
+
+ /**
+ * Runs test method spcified as java.lang.reflect.Method argument.
+ * Method must be a method taking no arguments.
+ */
+ protected boolean runTest(Method method)
+ {
+ String methodName = method.getName();
+ try
+ {
+ method.invoke(this, new Object[0]);
+ log.println(methodName + ": OK ");
+ }
+ catch (InvocationTargetException ex)
+ {
+ Throwable target = ex.getTargetException();
+ log.println(methodName + ": " + target.getMessage());
+ log.println(" stack trace:");
+ target.printStackTrace(log);
+ return false;
+ }
+ catch (Exception ex)
+ {
+ log.println(methodName + ": problems invoking test method " + methodName + " " + ex);
+ ex.printStackTrace(log);
+ return false;
+ }
+ finally
+ {
+ // close transaction, if it is left open
+ Transaction tx = pm.currentTransaction();
+ if (tx.isActive())
+ tx.rollback();
+ }
+ return true;
+ }
+
+ /**
+ * Checks the specified method being a test method
+ */
+ protected boolean isTestMethod(Method method)
+ {
+ return (isTestMethodName(method.getName()) &&
+ method.getReturnType().equals(void.class) &&
+ method.getParameterTypes().length == 0);
+ }
+
+ /**
+ * Checks the specified argument being the name of a test method
+ */
+ protected abstract boolean isTestMethodName(String methodName);
+
+ /**
+ * Compares the query result with the expectd result
+ */
+ protected void checkQueryResult(Object result, Collection expected)
+ throws Exception
+ {
+ if (result == null)
+ {
+ throw new Exception("Query returns null");
+ }
+ if (!(result instanceof Collection))
+ {
+ throw new Exception("Query result is not a collection: " + result.getClass().getName());
+ }
+ if (!result.equals(expected))
+ {
+ String lf = System.getProperty("line.separator");
+ throw new Exception("Wrong query result: " + lf +
+ "query returns: " + result + lf +
+ "expected result: " + expected);
+ }
+ }
+
+ /**
+ *
+ */
+ protected List getResultOids(Object result)
+ throws Exception
+ {
+ if (result == null) {
+ throw new Exception("Query returns null");
+ }
+ else if (!(result instanceof Collection)) {
+ throw new Exception("Query result is not a collection: " + result.getClass().getName());
+ }
+ else {
+ List oids = new ArrayList();
+ for (Iterator i = ((Collection)result).iterator(); i.hasNext(); ) {
+ Object next = i.next();
+ if (next instanceof org.apache.jdo.pc.xempdept.Identifiable)
+ oids.add(((org.apache.jdo.pc.xempdept.Identifiable)next).getOid());
+ else
+ throw new Exception("Unknown instance in query result: " + next.getClass().getName());
+ }
+ return oids;
+ }
+ }
+}
Added: incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryApiTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryApiTest.java?rev=171355&view=auto
==============================================================================
--- incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryApiTest.java (added)
+++ incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryApiTest.java Sun May 22 11:40:13 2005
@@ -0,0 +1,531 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/* QueryApiTest.java
+ *
+ * These are positive tests written to test the basic functionalities of
+ * the following API of Query module
+ *
+ * Testing the following Query Factory methods of PersistenceManager Interface
+ * Query newQuery()
+ * Query newQuery(Object query)
+ * Query newQuery (Class cls)
+ * Query newQuery (Class cls, Collection cln)
+ * Query newQuery (Class cls, String filter)
+ * Query newQuery (Class cls, Collection cln, String filter)
+ *
+ * Collection getExtent (Class PersistenceCapableClass, boolean subclasses)
+ *
+ * Testing the following methods of Query Interface
+ *
+ * PersistenceManaget getPersistenceManager()
+ * void setClass (Class cls)
+ * void setCollection (Collection cln) OR SetCandidates(Collection cln)
+ * void setFilter (String filter)
+ * void declareImports (String imports)
+ * void declareVariables (String variables)
+ * void declareParameters (String parameters)
+ * void setOrdering (String ordering)
+ * void setIgnoreCache()
+ * boolean getIgnoreCache()
+ * void compile()
+ * void execute()
+ * void execute(Object p1)
+ * void execute(Object p1, Object p2)
+ * void execute(Object p1, Object p2, Object p3)
+ * Object executeWithArray(Object[] a)
+ * Object executeWithMap( Map param)
+ *
+ */
+
+package org.apache.jdo.test.query;
+
+import java.util.*;
+import java.io.PrintStream;
+
+import javax.jdo.*;
+
+import org.apache.jdo.pc.xempdept.Employee;
+
+/**
+ *
+ * @author Shrikant Wagh
+ * @author Michael Bouschen
+ */
+
+public class QueryApiTest extends PositiveTest
+{
+ public QueryApiTest(PersistenceManagerFactory pmf, PrintStream log)
+ {
+ super(pmf, log);
+ }
+
+ protected boolean isTestMethodName(String methodName)
+ {
+ return methodName.startsWith("queryApi");
+ }
+
+ // Tests the following methods :
+ // PersistenceManager::newQuery()
+ // Query::setClass(Class class)
+ // Query::setCandidate(Collection cln);
+ // Query::setFilter(String filter);
+ // Query::execute()
+ // Query::getExtent(Class PersistenceCapableClass, boolean subclass)
+
+
+ public void queryApi001() throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.setFilter("empid == 1");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests PersistenceManager::newQuery(Class cls) method
+
+ public void queryApi002() throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery(Employee.class);
+ query.setFilter("empid == 1");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests PersistenceManager::Query(Class cls, Collection cln) method
+
+ public void queryApi003() throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = (employeeCandidates == null) ?
+ pm.newQuery(pm.getExtent(Employee.class, true)) :
+ pm.newQuery(Employee.class, employeeCandidates);
+ query.setFilter("empid == 1");
+
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests PersistenceManager::newQuery(Class cls, String filter) method
+
+ public void queryApi004() throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery(Employee.class, "empid == 1");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+
+
+ // Tests PersistenceManager::newQuery(Extent cln, String filter) method
+
+ public void queryApi005() throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = (employeeCandidates == null) ?
+ pm.newQuery(pm.getExtent(Employee.class, true), "empid == 1") :
+ pm.newQuery(Employee.class, employeeCandidates, "empid == 1");
+
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests PersistenceManager::newQuery(Object query) method
+
+ public void queryApi006() throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery(Employee.class);
+ query.setFilter("empid == 1");
+ setEmployeeCandidates(query);
+
+ Query nwQuery = pm.newQuery(query);
+ setEmployeeCandidates(nwQuery);
+
+ Object result = nwQuery.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::declareParameters(String param)
+ // void Query::execute(Object obj)
+
+ public void queryApi007()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long id");
+ query.setFilter("empid == id");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute(new Long(1));
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::execute(Object obj1, Object obj2)
+
+ public void queryApi008()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long id, String lname");
+ query.setFilter("(empid == id) && (this.lastname == lname)");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute(new Long(1), "lastEngOne");
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::execute(Object obj1, Object obj2, Object obj3)
+
+ public void queryApi009()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long id, String lname, String fname");
+ query.setFilter("(empid == id) && (this.lastname == lname) && (this.firstname == fname)");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute(new Long(1), "lastEngOne", "firstEngOne");
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::executeWithArray(Object[] obj) method
+
+ public void queryApi010()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long l");
+ query.setFilter("empid == l");
+ setEmployeeCandidates(query);
+ Object[] actualParams = {new Long(1)};
+
+ Object result = query.executeWithArray(actualParams);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::executeWithArray(Object[] obj) method with multiple parameters
+
+ public void queryApi011()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long l, String lname");
+ query.setFilter("(empid == l) && (this.lastname == lname)");
+ setEmployeeCandidates(query);
+ Object[] actualParams = {new Long(1), "lastEngOne"};
+
+ Object result = query.executeWithArray(actualParams);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::executeWithMap(Map param) method
+
+ public void queryApi012()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long l");
+ query.setFilter("empid == l");
+ setEmployeeCandidates(query);
+
+ Map actualParams = new java.util.HashMap();
+ actualParams.put("l", new Long(1));
+
+ Object result = query.executeWithMap(actualParams);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests void Query::executeWithMap(Map param) method with multiple params
+
+ public void queryApi013()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long l, String lname");
+ query.setFilter("(empid == l) && (this.lastname == lname)");
+ setEmployeeCandidates(query);
+
+ Map actualParams = new java.util.HashMap();
+ actualParams.put("l", new Long(1));
+ actualParams.put("lname", "lastEngOne");
+
+ Object result = query.executeWithMap(actualParams);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests Query::setOrdering(String ordering) method with order by ascending
+
+ public void queryApi014()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.setFilter("empid > 2 & empid < 9");
+ query.setOrdering("lastname ascending");
+ setEmployeeCandidates(query);
+
+ Object result = query.execute();
+ List oids = getResultOids(result);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 8;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 7;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 6;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 5;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ // Tests Query::setOrdering(String ordering) method with order by descending
+
+ public void queryApi015()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.setFilter("empid > 2 & empid < 9");
+ query.setOrdering("lastname descending");
+ setEmployeeCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 5;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 6;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 7;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 8;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+
+}
Added: incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryErrorTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryErrorTest.java?rev=171355&view=auto
==============================================================================
--- incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryErrorTest.java (added)
+++ incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryErrorTest.java Sun May 22 11:40:13 2005
@@ -0,0 +1,204 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/*
+ * QueryErrorTest.java
+ *
+ * Created on April 6, 2000
+ */
+
+package org.apache.jdo.test.query;
+
+import java.util.*;
+import java.io.*;
+
+import javax.jdo.*;
+
+import org.apache.jdo.jdoql.JDOQueryException;
+import org.apache.jdo.jdoql.tree.Expression;
+import org.apache.jdo.jdoql.tree.QueryTree;
+import org.apache.jdo.pc.xempdept.Employee;
+
+/**
+ *
+ * @author Michael Bouschen
+ */
+public class QueryErrorTest
+ extends NegativeTest
+{
+ public QueryErrorTest(PersistenceManagerFactory pmf, PrintStream log)
+ {
+ super(pmf, log);
+ }
+
+ protected boolean isTestMethodName(String methodName)
+ {
+ return methodName.startsWith("queryError");
+ }
+
+ // ==========================================================================
+ // Test methods
+ // ==========================================================================
+
+ /**
+ * Testcase: missing candidate class
+ */
+ public void queryError001()
+ throws Exception
+ {
+ String expectedMsg = msg.msg("EXC_MissingCandidateClass"); //NOI18N
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ try
+ {
+ Query query = pm.newQuery();
+ query.setFilter("empid == 1");
+ Object result = query.execute();
+ checkMissingException(JDOQueryException.class, expectedMsg);
+ }
+ catch (JDOException ex)
+ {
+ checkJDOException(ex, JDOQueryException.class, expectedMsg);
+ }
+
+ tx.rollback();
+ }
+
+ /**
+ * Testcase: missing candidate class in query tree
+ */
+ public void queryError002()
+ throws Exception
+ {
+ String expectedMsg = msg.msg("EXC_MissingCandidateClass"); //NOI18N
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ try
+ {
+ // create query tree
+ QueryTree qt = newQueryTree();
+ Expression field = qt.newIdentifier("empid");
+ Expression constant = qt.newConstant(new Integer(1));
+ Expression equals = qt.newEquals(field, constant);
+ qt.setFilter(equals);
+
+ Query query = pm.newQuery(qt);
+ Object result = query.execute();
+ checkMissingException(JDOQueryException.class, expectedMsg);
+ }
+ catch (JDOException ex)
+ {
+ checkJDOException(ex, JDOQueryException.class, expectedMsg);
+ }
+
+ tx.rollback();
+ }
+
+ /**
+ * Testcase: directly use deserialized query w/o binding to a new PersistenceManager
+ */
+ public void queryError003()
+ throws Exception
+ {
+ String expectedMsg = msg.msg("EXC_UnboundQuery"); //NOI18N
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.setFilter("empid == 3");
+
+ // serialize query object
+ ByteArrayOutputStream bout = new ByteArrayOutputStream ();
+ ObjectOutputStream oout = new ObjectOutputStream (bout);
+ oout.writeObject (query);
+ oout.flush ();
+ byte[] bytes = bout.toByteArray();
+ oout.close ();
+
+ // deserialize query object
+ ByteArrayInputStream bin = new ByteArrayInputStream (bytes);
+ ObjectInputStream oin = new ObjectInputStream (bin);
+ query = (Query)oin.readObject ();
+ oin.close ();
+
+ try
+ {
+ Object result = query.execute();
+ checkMissingException(JDOQueryException.class, expectedMsg);
+ }
+ catch (JDOException ex)
+ {
+ checkJDOException(ex, JDOQueryException.class, expectedMsg);
+ }
+
+ tx.rollback();
+ }
+
+ /**
+ * Testcase: compiled query is null
+ */
+ public void queryError004()
+ throws Exception
+ {
+ String expectedMsg = msg.msg("EXC_NullQueryInstance"); //NOI18N
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ try
+ {
+ Query compiled = null;
+ Query query = pm.newQuery(compiled);
+ checkMissingException(JDOQueryException.class, expectedMsg);
+ }
+ catch (JDOException ex)
+ {
+ checkJDOException(ex, JDOQueryException.class, expectedMsg);
+ }
+
+ tx.rollback();
+ }
+
+ /**
+ * Testcase: invalid compiled query (compiled query must have the class QueryImpl)
+ */
+ public void queryError005()
+ throws Exception
+ {
+ String expectedMsg =
+ msg.msg("EXC_InvalidCompiledQuery", "java.lang.Integer"); //NOI18N
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ try
+ {
+ Object compiled = new Integer(1);
+ Query query = pm.newQuery(compiled);
+ checkMissingException(JDOQueryException.class, expectedMsg);
+ }
+ catch (JDOException ex)
+ {
+ checkJDOException(ex, JDOQueryException.class, expectedMsg);
+ }
+
+ tx.rollback();
+ }
+
+}
Added: incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryTest.java?rev=171355&view=auto
==============================================================================
--- incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryTest.java (added)
+++ incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/QueryTest.java Sun May 22 11:40:13 2005
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/*
+ * QueryTest.java
+ *
+ * Created on April 11, 2000
+ */
+
+package org.apache.jdo.test.query;
+
+import java.util.Vector;
+
+/**
+ *
+ * @author Michael Bouschen
+ */
+public interface QueryTest
+{
+ /**
+ * Runs all test method provided by this class.
+ */
+ public boolean runAll();
+
+ /**
+ * Runs test method with name methodName.
+ * MethodName must describe a method taking no arguments.
+ */
+ public boolean runTests(Vector tests);
+}
Added: incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ScopingTest.java
URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ScopingTest.java?rev=171355&view=auto
==============================================================================
--- incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ScopingTest.java (added)
+++ incubator/jdo/trunk/fostore20/test/java/org/apache/jdo/test/query/ScopingTest.java Sun May 22 11:40:13 2005
@@ -0,0 +1,564 @@
+/*
+ * Copyright 2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+/*
+ * ScopingTest.java
+ *
+ * Created on April 12, 2001
+ */
+
+package org.apache.jdo.test.query;
+
+import java.util.*;
+import java.io.PrintStream;
+
+import javax.jdo.*;
+
+import org.apache.jdo.pc.xempdept.Department;
+import org.apache.jdo.pc.xempdept.Employee;
+import org.apache.jdo.pc.xempdept.PrimitiveTypes;
+
+/**
+ *
+ * @author Michael Bouschen
+ */
+public class ScopingTest
+ extends PositiveTest
+{
+ public ScopingTest(PersistenceManagerFactory pmf, PrintStream log)
+ {
+ super(pmf, log);
+ }
+
+ protected boolean isTestMethodName(String methodName)
+ {
+ return methodName.startsWith("scoping");
+ }
+
+ // ==========================================================================
+ // Test methods
+ // ==========================================================================
+
+ /**
+ * Testcase: use of unqualified field name
+ */
+ public void scoping001()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.setFilter("id == 1");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: use of this qualifier
+ */
+ public void scoping002()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.setFilter("this.id == 1");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: parameter with same name as field
+ */
+ public void scoping003()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("String empid");
+ query.setFilter("empid == firstname");
+ setEmployeeCandidates(query);
+ Object result = query.execute("firstEngTwo");
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 12;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: parameter with same name as field, using parameter and field
+ */
+ public void scoping004()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("String department");
+ query.setFilter("this.department.name == department");
+ setEmployeeCandidates(query);
+ Object result = query.execute("Engineering");
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 11;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 12;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: variable with same name as field
+ */
+ public void scoping005()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareVariables("Employee manager");
+ query.setFilter("team.contains(manager) & manager.empid == 5");
+ setEmployeeCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: variable with same name as field, using variable and field
+ */
+ public void scoping006()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareVariables("Employee team");
+ query.setFilter("this.team.contains(team) & team.empid == 2");
+ setEmployeeCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: using a field having the same name as the candidate class
+ */
+ public void scoping007()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.setFilter("PrimitiveTypes == 1");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a type name having the same name as a field in declareParameters
+ */
+ public void scoping008()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ PrimitiveTypes param = getPrimitiveTypesById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.declareParameters("PrimitiveTypes p");
+ query.setFilter("this == p");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute(param);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a type name having the same name as a field in declareParameters
+ */
+ public void scoping009()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ PrimitiveTypes param = getPrimitiveTypesById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(PrimitiveTypes.class);
+ query.declareParameters("PrimitiveTypes PrimitiveTypes");
+ query.setFilter("this == PrimitiveTypes");
+ setPrimitiveTypesCandidates(query);
+ Object result = query.execute(param);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ PrimitiveTypes.Oid key = new PrimitiveTypes.Oid();
+ key.id = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a parameter having the same name as the candidate class
+ */
+ public void scoping010()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("long Employee");
+ query.setFilter("empid == Employee");
+ setEmployeeCandidates(query);
+ Object result = query.execute(new Long(2));
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a parameter having the same name as the candidate class
+ */
+ public void scoping011()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Employee manager = getEmployeeById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareParameters("Employee Employee");
+ query.setFilter("manager == Employee");
+ setEmployeeCandidates(query);
+ Object result = query.execute(manager);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a variable having the same name as the candidate class
+ */
+ public void scoping012()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareVariables("Employee Employee");
+ query.setFilter("team.contains(Employee) & Employee.empid == 3");
+ setEmployeeCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a parameter having the same name as an imported type
+ */
+ public void scoping013()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("long Department");
+ query.setFilter("department.deptid == Department");
+ setEmployeeCandidates(query);
+ Object result = query.execute(new Long(2));
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 5;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a parameter having the same name as an imported type
+ */
+ public void scoping014()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ Department d = getDepartmentById(1L);
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Department");
+ query.declareParameters("Department Department");
+ query.setFilter("Department == department");
+ setEmployeeCandidates(query);
+ Object result = query.execute(d);
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 2;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 3;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+ /**
+ * Testcase: namespaces: using a variable having the same name as an imported type
+ */
+ public void scoping015()
+ throws Exception
+ {
+ Transaction tx = pm.currentTransaction();
+ tx.begin();
+
+ // create and execute query
+
+ Query query = pm.newQuery();
+ query.setClass(Employee.class);
+ query.declareImports("import org.apache.jdo.pc.xempdept.Project");
+ query.declareVariables("Project Project");
+ query.setFilter("projects.contains(Project) & Project.projid == 3");
+ setEmployeeCandidates(query);
+ Object result = query.execute();
+ List oids = getResultOids(result);
+ Collections.sort(oids);
+
+ // check query result
+
+ Collection expected = new ArrayList();
+ Employee.Oid key = new Employee.Oid();
+ key.empid = 1;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 4;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 6;
+ expected.add(key);
+ key = new Employee.Oid();
+ key.empid = 7;
+ expected.add(key);
+ checkQueryResult(oids, expected);
+
+ tx.commit();
+ }
+
+}