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 21 import java.util.ArrayList; 22 import java.util.List; 23 24 import javax.jdo.PersistenceManager; 25 import javax.jdo.Query; 26 import javax.jdo.Transaction; 27 28 import org.apache.jdo.tck.JDO_Test; 29 import org.apache.jdo.tck.pc.mylib.PCPoint; 30 import org.apache.jdo.tck.query.QueryTest; 31 import org.apache.jdo.tck.util.BatchTestRunner; 32 33 /** 34 *<B>Title:</B> Parameter Declared with Same Name as Field of Candidate 35 Class 36 *<BR> 37 *<B>Keywords:</B> query 38 *<BR> 39 *<B>Assertion ID:</B> A14.4-2. 40 *<BR> 41 *<B>Assertion Description: </B> A field of the candidate class of a 42 *<code>Query</code> can be hidden if a parameter is declared with the same name. 43 */ 44 45 public class ParameterDeclaredWithSameNameAsFieldOfCandidateClass extends QueryTest { 46 47 /** */ 48 private static final String ASSERTION_FAILED = 49 "Assertion A14.4-2 (ParameterDeclaredWithSameNameAsFieldOfCandidateClass) failed: "; 50 51 /** 52 * The <code>main</code> is called when the class 53 * is directly executed from the command line. 54 * @param args The arguments passed to the program. 55 */ 56 public static void main(String[] args) { 57 BatchTestRunner.run(ParameterDeclaredWithSameNameAsFieldOfCandidateClass.class); 58 } 59 60 /** */ 61 public void testPositve() { 62 PersistenceManager pm = getPM(); 63 64 runTestParameterDeclaredWithSameNameAsFieldOfCandidateClass01(pm); 65 runTestParameterDeclaredWithSameNameAsFieldOfCandidateClass02(pm); 66 } 67 68 /** */ 69 void runTestParameterDeclaredWithSameNameAsFieldOfCandidateClass01( 70 PersistenceManager pm) { 71 Transaction tx = pm.currentTransaction(); 72 try { 73 tx.begin(); 74 75 Query query = pm.newQuery(); 76 query.setClass(PCPoint.class); 77 query.setCandidates(pm.getExtent(PCPoint.class, false)); 78 query.declareParameters("Integer x"); 79 query.setFilter("x == x"); 80 Object results = query.execute(new java.lang.Integer(2)); 81 82 // check query result 83 List expected = new ArrayList(); 84 Object p1 = new PCPoint(0, 0); 85 Object p2 = new PCPoint(1, 1); 86 Object p3 = new PCPoint(2, 2); 87 Object p4 = new PCPoint(3, 3); 88 Object p5 = new PCPoint(4, 4); 89 expected.add(p1); 90 expected.add(p2); 91 expected.add(p3); 92 expected.add(p4); 93 expected.add(p5); 94 expected = getFromInserted(expected); 95 printOutput(results, expected); 96 checkQueryResultWithoutOrder(ASSERTION_FAILED, "x == x", 97 results, expected); 98 if (debug) 99 logger.debug("\nTest ParameterDeclaredWithSameNameAsFieldOfCandidateClass - Passed"); 100 101 tx.commit(); 102 tx = null; 103 } 104 finally { 105 if ((tx != null) && tx.isActive()) 106 tx.rollback(); 107 } 108 } 109 110 /** */ 111 void runTestParameterDeclaredWithSameNameAsFieldOfCandidateClass02( 112 PersistenceManager pm) { 113 Transaction tx = pm.currentTransaction(); 114 try { 115 tx.begin(); 116 117 Query query = pm.newQuery(); 118 query.setClass(PCPoint.class); 119 query.setCandidates(pm.getExtent(PCPoint.class, false)); 120 query.declareParameters("Integer y"); 121 query.setFilter("y == y"); 122 Object results = query.execute(new java.lang.Integer(2)); 123 124 // check query result 125 List expected = new ArrayList(); 126 Object p1 = new PCPoint(0, 0); 127 Object p2 = new PCPoint(1, 1); 128 Object p3 = new PCPoint(2, 2); 129 Object p4 = new PCPoint(3, 3); 130 Object p5 = new PCPoint(4, 4); 131 expected.add(p1); 132 expected.add(p2); 133 expected.add(p3); 134 expected.add(p4); 135 expected.add(p5); 136 expected = getFromInserted(expected); 137 printOutput(results, expected); 138 checkQueryResultWithoutOrder(ASSERTION_FAILED, "y == y", 139 results, expected); 140 if (debug) 141 logger.debug("\nTest ParameterDeclaredWithSameNameAsFieldOfCandidateClass - Passed"); 142 143 tx.commit(); 144 tx = null; 145 } 146 finally { 147 if ((tx != null) && tx.isActive()) 148 tx.rollback(); 149 } 150 } 151 152 /** 153 * @see JDO_Test#localSetUp() 154 */ 155 protected void localSetUp() { 156 addTearDownClass(PCPoint.class); 157 loadAndPersistPCPoints(getPM()); 158 } 159 } 160