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 javax.jdo.JDOUserException; 21 import javax.jdo.PersistenceManager; 22 import javax.jdo.Query; 23 24 import org.apache.jdo.tck.JDO_Test; 25 import org.apache.jdo.tck.pc.company.CompanyModelReader; 26 import org.apache.jdo.tck.pc.company.Department; 27 import org.apache.jdo.tck.pc.company.Employee; 28 import org.apache.jdo.tck.query.QueryTest; 29 import org.apache.jdo.tck.util.BatchTestRunner; 30 31 /** 32 *<B>Title:</B> Element Returned in Query Result 33 *<BR> 34 *<B>Keywords:</B> query 35 *<BR> 36 *<B>Assertion ID:</B> A14.6.1-8. 37 *<BR> 38 *<B>Assertion Description: </B> 39 * If a persistent instance associated with another PersistenceManager is 40 * passed as a parameter, JDOUserException is thrown during execute(). 41 */ 42 43 public class ParameterBoundToDifferentPM extends QueryTest { 44 45 /** */ 46 private static final String ASSERTION_FAILED = 47 "Assertion A14.6.1-8 (ParameterBoundToDifferentPM) failed: "; 48 49 /** 50 * The <code>main</code> is called when the class 51 * is directly executed from the command line. 52 * @param args The arguments passed to the program. 53 */ 54 public static void main(String[] args) { 55 BatchTestRunner.run(ParameterBoundToDifferentPM.class); 56 } 57 58 /** */ 59 public void testNegative() { 60 // get parameter dept1 61 getPM().currentTransaction().begin(); 62 Department dept1 = (Department) getPersistentCompanyModelInstance("dept1"); 63 getPM().currentTransaction().commit(); 64 65 // pass parameter dept1 to query of different pm 66 PersistenceManager pm2 = pmf.getPersistenceManager(); 67 pm2.currentTransaction().begin(); 68 try { 69 Query q = pm2.newQuery(Employee.class, "department == d"); 70 q.declareParameters("Department d"); 71 try { 72 q.execute(dept1); 73 } finally { 74 q.closeAll(); 75 } 76 fail(ASSERTION_FAILED, 77 "Query.execute should throw a JDOUserException if a query " + 78 "parameter is bound to a different PersistenceManager"); 79 } 80 catch (JDOUserException ex) { 81 // expected exception 82 if (debug) logger.debug("caught expected exception " + ex); 83 } 84 finally { 85 if ((pm2 != null) && !pm2.isClosed()) { 86 if (pm2.currentTransaction().isActive()) { 87 pm2.currentTransaction().rollback(); 88 } 89 pm2.close(); 90 } 91 } 92 } 93 94 /** 95 * @see JDO_Test#localSetUp() 96 */ 97 protected void localSetUp() { 98 addTearDownClass(CompanyModelReader.getTearDownClasses()); 99 loadAndPersistCompanyModel(getPM()); 100 } 101 }