Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.api.persistencemanagerfactory » [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   
   19   package org.apache.jdo.tck.api.persistencemanagerfactory;
   20   
   21   import java.util.Arrays;
   22   import java.util.Collection;
   23   import java.util.Iterator;
   24   import java.util.Properties;
   25   
   26   import javax.jdo.JDOException;
   27   import javax.jdo.JDOFatalInternalException;
   28   import javax.jdo.JDOUserException;
   29   import javax.jdo.PersistenceManagerFactory;
   30   
   31   import org.apache.jdo.tck.JDO_Test;
   32   import org.apache.jdo.tck.util.BatchTestRunner;
   33   
   34   /**
   35    *<B>Title:</B> After GetPersistenceManager No Set Methods Succeed
   36    *<BR>
   37    *<B>Keywords:</B> 
   38    *<BR>
   39    *<B>Assertion ID:</B> A11.3-3.
   40    *<BR>
   41    *<B>Assertion Description: </B>
   42    * After the first use of
   43    * <code>PersistenceManagerFactory.getPersistenceManager()</code>, 
   44    * none of the <code>set</code> methods will succeed.
   45    */
   46   
   47   public class AfterGetPersistenceManagerNoSetMethodsSucceed extends JDO_Test {
   48   
   49       private String username;
   50       private String password;
   51   
   52       private Class[] stringParameters = null;
   53       private Class[] booleanParameters = null;
   54       private Object[] stringParameter = null;
   55       private Object[] booleanParameter = null;
   56       private SetProperty[] setMethods = null;
   57       private GetProperty[] getMethods = null;
   58   
   59       /** */
   60       private static final String ASSERTION_FAILED = 
   61           "Assertion A11.3-3 (AfterGetPersistenceManagerNoSetMethodsSucceed) failed: ";
   62       
   63       /**
   64        * The <code>main</code> is called when the class
   65        * is directly executed from the command line.
   66        * @param args The arguments passed to the program.
   67        */
   68       public static void main(String[] args) {
   69           BatchTestRunner.run(AfterGetPersistenceManagerNoSetMethodsSucceed.class);
   70       }
   71   
   72       /** */
   73       public AfterGetPersistenceManagerNoSetMethodsSucceed() {
   74           super();
   75           initVariables();
   76       }
   77   
   78       /** */
   79       public void initVariables() {
   80           stringParameters = new Class[]{String.class};
   81           booleanParameters = new Class[]{boolean.class};
   82           stringParameter = new Object[]{"Nobody knows the trouble"};
   83           booleanParameter = new Object[]{Boolean.FALSE};
   84   
   85           setMethods = new SetProperty[] {
   86               new SetProperty("setConnectionDriverName", stringParameters, stringParameter),
   87               new SetProperty("setConnectionFactoryName", stringParameters, stringParameter),
   88               new SetProperty("setConnectionFactory2Name", stringParameters, stringParameter),
   89               new SetProperty("setConnectionURL", stringParameters, stringParameter),
   90               new SetProperty("setConnectionUserName", stringParameters, stringParameter),
   91               new SetProperty("setConnectionPassword", stringParameters, stringParameter),
   92               new SetProperty("setIgnoreCache", booleanParameters, booleanParameter),
   93               new SetProperty("setMultithreaded", booleanParameters, booleanParameter),
   94               new SetProperty("setNontransactionalRead", booleanParameters, booleanParameter),
   95               new SetProperty("setNontransactionalWrite", booleanParameters, booleanParameter),
   96               new SetProperty("setOptimistic", booleanParameters, booleanParameter),
   97               new SetProperty("setRestoreValues", booleanParameters, booleanParameter),
   98               new SetProperty("setRetainValues", booleanParameters, booleanParameter)
   99           };
  100   
  101           getMethods = new GetProperty[] {
  102               new GetProperty("getConnectionDriverName"),
  103               new GetProperty("getConnectionFactoryName"),
  104               new GetProperty("getConnectionFactory2Name"),
  105               new GetProperty("getConnectionURL"),
  106               new GetProperty("getConnectionUserName"),
  107               new GetProperty("getIgnoreCache"),
  108               new GetProperty("getMultithreaded"),
  109               new GetProperty("getNontransactionalRead"),
  110               new GetProperty("getNontransactionalWrite"),
  111               new GetProperty("getOptimistic"),
  112               new GetProperty("getRestoreValues"),
  113               new GetProperty("getRetainValues")
  114           };
  115       }
  116   
  117       /** */
  118       public void testGetPersistenceManagerWithNoParametes() {
  119           runTest(false);
  120       }
  121   
  122       /** */
  123       public void testGetPersistenceManagerWithParameters() {
  124           Properties props = loadProperties(PMFProperties);
  125           username = props.getProperty(CONNECTION_USERNAME_PROP);  
  126           password = props.getProperty(CONNECTION_PASSWORD_PROP);  
  127           runTest(true);
  128       }
  129   
  130       /** */
  131       public void runTest(boolean bUserAndPasswd) {       
  132           pmf = getPMF();
  133           if (!bUserAndPasswd)
  134               pm = getPM();
  135           else
  136               pm = getPMF().getPersistenceManager(username,password);
  137   
  138           // each set method should throw an exception
  139           Collection setCollection = Arrays.asList(setMethods);
  140           for (Iterator it = setCollection.iterator(); it.hasNext();) {
  141               SetProperty sp = (SetProperty)it.next();
  142               String where = sp.getMethodName();
  143               try {
  144                   sp.execute(pmf);
  145                   fail(ASSERTION_FAILED,
  146                        "pmf method " + where + 
  147                        " should throw JDOUserException when called after getPersistenceManager");
  148               } catch (JDOUserException ex) {
  149                   if (debug)
  150                       logger.debug("Caught expected exception " + ex.toString() + " from " + where);
  151               }
  152           }
  153           // each get method should succeed
  154           Collection getCollection = Arrays.asList(getMethods);
  155           for (Iterator it = getCollection.iterator(); it.hasNext();) {
  156               GetProperty gp = (GetProperty)it.next();
  157               String where = gp.getMethodName();
  158               try {
  159                   gp.execute(pmf);
  160               } catch (JDOUserException ex) {
  161                   fail(ASSERTION_FAILED,
  162                        "Caught unexpected exception " + ex.toString() + " from " + where);
  163               }
  164           }
  165       }
  166      
  167       /** */
  168       static class SetProperty {
  169           
  170           java.lang.reflect.Method method;
  171           String methodName;
  172           Class[] parameters;
  173           Object[] parameter;
  174          
  175           SetProperty(String methodName, Class[] parameters, Object[] parameter) {
  176               this.methodName = methodName;
  177               this.parameters = parameters;
  178               this.parameter = parameter;
  179               try {
  180                   method = PersistenceManagerFactory.class.getMethod(methodName, parameters);
  181               } catch (NoSuchMethodException ex) {
  182                   throw new JDOFatalInternalException("Method not defined: " + methodName);
  183               }
  184           }
  185           void execute(PersistenceManagerFactory pmf) {
  186               try {
  187                   method.invoke(pmf, parameter);
  188               } catch (IllegalAccessException ex) {
  189                   throw new JDOFatalInternalException("IllegalAccessException", ex);
  190               } catch (java.lang.reflect.InvocationTargetException ex) {
  191                   throw (JDOException)ex.getTargetException();
  192               }
  193           }
  194          
  195           String getMethodName() {
  196               return methodName;
  197           }
  198       }
  199      
  200       /** */
  201       static class GetProperty {
  202          
  203           java.lang.reflect.Method method;
  204           String methodName;
  205          
  206           GetProperty(String methodName) {
  207               this.methodName = methodName;
  208               try {
  209                   method = PersistenceManagerFactory.class.getMethod(methodName,
  210                           (Class[])null);
  211               } catch (NoSuchMethodException ex) {
  212                   throw new JDOFatalInternalException(
  213                       "Method not defined: " + methodName);
  214               }
  215           }
  216           void execute(PersistenceManagerFactory pmf) {
  217               try {
  218                   method.invoke(pmf, (Object[])null);
  219               } catch (IllegalAccessException ex) {
  220                   throw new JDOFatalInternalException("IllegalAccessException", ex);
  221               } catch (java.lang.reflect.InvocationTargetException ex) {
  222                   throw (JDOException)ex.getTargetException();
  223               }
  224           }
  225          
  226           String getMethodName() {
  227               return methodName;
  228           }
  229       }
  230   }

Save This Page
Home » jdo2-tck-2.3-ea-src » org.apache.jdo.tck.api.persistencemanagerfactory » [javadoc | source]