Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.query.jdoql.parameters » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    * 
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    * 
   11    * Unless required by applicable law or agreed to in writing, software 
   12    * distributed under the License is distributed on an "AS IS" BASIS, 
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
   14    * See the License for the specific language governing permissions and 
   15    * limitations under the License.
   16    */
   17   
   18   package org.apache.jdo.tck.query.jdoql.parameters;
   19   
   20   import java.util.Arrays;
   21   import java.util.List;
   22   
   23   import org.apache.jdo.tck.JDO_Test;
   24   import org.apache.jdo.tck.pc.company.CompanyModelReader;
   25   import org.apache.jdo.tck.pc.company.Employee;
   26   import org.apache.jdo.tck.pc.company.Person;
   27   import org.apache.jdo.tck.query.QueryElementHolder;
   28   import org.apache.jdo.tck.query.QueryTest;
   29   import org.apache.jdo.tck.util.BatchTestRunner;
   30   
   31   /**
   32    *<B>Title:</B> Implicit parameters.
   33    *<BR>
   34    *<B>Keywords:</B> query
   35    *<BR>
   36    *<B>Assertion ID:</B> A14.6.3-3.
   37    *<BR>
   38    *<B>Assertion Description: </B>
   39    * Parameters implicitly declared (in the result, filter, grouping, ordering, 
   40    * or range) are identified by prepending a ":" to the parameter 
   41    * everywhere it appears. All parameter types can be determined 
   42    * by one of the following techniques:
   43    */
   44   public class ImplicitParameters extends QueryTest {
   45   
   46       /** */
   47       private static final String ASSERTION_FAILED = 
   48           "Assertion A14.6.3-3 (ImplicitParameters) failed: ";
   49       
   50       /** 
   51        * The array of valid queries which may be executed as 
   52        * single string queries and as API queries.
   53        */
   54       private static final QueryElementHolder[] VALID_QUERIES = {
   55           new QueryElementHolder(
   56           /*UNIQUE*/      null,
   57           /*RESULT*/      "this, :param", 
   58           /*INTO*/        null, 
   59           /*FROM*/        Person.class,
   60           /*EXCLUDE*/     null,
   61           /*WHERE*/       null,
   62           /*VARIABLES*/   null,
   63           /*PARAMETERS*/  null,
   64           /*IMPORTS*/     null,
   65           /*GROUP BY*/    null,
   66           /*ORDER BY*/    null,
   67           /*FROM*/        null,
   68           /*TO*/          null),
   69           new QueryElementHolder(
   70           /*UNIQUE*/      null,
   71           /*RESULT*/      null, 
   72           /*INTO*/        null, 
   73           /*FROM*/        Person.class,
   74           /*EXCLUDE*/     null,
   75           /*WHERE*/       "firstname == :param",
   76           /*VARIABLES*/   null,
   77           /*PARAMETERS*/  null,
   78           /*IMPORTS*/     null,
   79           /*GROUP BY*/    null,
   80           /*ORDER BY*/    null,
   81           /*FROM*/        null,
   82           /*TO*/          null),
   83           new QueryElementHolder(
   84           /*UNIQUE*/      null,
   85           /*RESULT*/      "department.name", 
   86           /*INTO*/        null, 
   87           /*FROM*/        Employee.class,
   88           /*EXCLUDE*/     null,
   89           /*WHERE*/       null,
   90           /*VARIABLES*/   null,
   91           /*PARAMETERS*/  null,
   92           /*IMPORTS*/     null,
   93           /*GROUP BY*/    "department.name HAVING COUNT(this) >= :minValue",
   94           /*ORDER BY*/    null,
   95           /*FROM*/        null,
   96           /*TO*/          null),
   97           new QueryElementHolder(
   98           /*UNIQUE*/      null,
   99           /*RESULT*/      null, 
  100           /*INTO*/        null, 
  101           /*FROM*/        Person.class,
  102           /*EXCLUDE*/     null,
  103           /*WHERE*/       null,
  104           /*VARIABLES*/   null,
  105           /*PARAMETERS*/  null,
  106           /*IMPORTS*/     null,
  107           /*GROUP BY*/    null,
  108           /*ORDER BY*/    null,
  109           /*FROM*/        ":zero",
  110           /*TO*/          ":five")
  111       };
  112       
  113       private static String parameter = "parameterInResult";
  114       
  115       /** 
  116        * The expected results of valid queries.
  117        */
  118       private Object[] expectedResult = {
  119           getExpectedResultOfFirstQuery(
  120                   getTransientCompanyModelInstancesAsList(new String[] {
  121                   "emp1", "emp2", "emp3", "emp4", "emp5"})),
  122           getTransientCompanyModelInstancesAsList(new String[]{"emp1"}),
  123           /* Note: "Development" is not a bean name! */
  124           Arrays.asList(new Object[]{"Development"}),
  125           getTransientCompanyModelInstancesAsList(new String[] {
  126                   "emp1", "emp2", "emp3", "emp4", "emp5"})
  127       };
  128               
  129       /**
  130        * The <code>main</code> is called when the class
  131        * is directly executed from the command line.
  132        * @param args The arguments passed to the program.
  133        */
  134       public static void main(String[] args) {
  135           BatchTestRunner.run(ImplicitParameters.class);
  136       }
  137       
  138       /** */
  139       public void testResult() {
  140           int index = 0;
  141           executeQuery(index, new Object[] {parameter});
  142       }
  143       
  144       /** */
  145       public void testFilter() {
  146           int index = 1;
  147           executeQuery(index, new Object[] {"emp1First"});
  148       }
  149       
  150       /** */
  151       public void testGrouping() {
  152           int index = 2;
  153           executeQuery(index, new Object[] {new Long(3)});
  154       }
  155       
  156       /** */
  157       public void testRange() {
  158           int index = 3;
  159           executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
  160                   new Object[] {new Long(0), new Long(5)}, expectedResult[index]);
  161       }
  162       
  163       /**
  164        * @see JDO_Test#localSetUp()
  165        */
  166       protected void localSetUp() {
  167           addTearDownClass(CompanyModelReader.getTearDownClasses());
  168           loadAndPersistCompanyModel(getPM());
  169       }
  170   
  171       /** */
  172       private void executeQuery(int index, Object[] parameters) {
  173           executeAPIQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
  174                   parameters, expectedResult[index]);
  175           executeSingleStringQuery(ASSERTION_FAILED, VALID_QUERIES[index], 
  176                   parameters, expectedResult[index]);
  177       }
  178       
  179       private List getExpectedResultOfFirstQuery(List instances) {
  180           Object[] expectedResult = new Object[instances.size()];
  181           for (int i = 0; i < expectedResult.length; i++) {
  182               expectedResult[i] = new Object[] {instances.get(i), parameter};
  183           }
  184           return Arrays.asList(expectedResult);
  185       }
  186   }

Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.query.jdoql.parameters » [javadoc | source]