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   package org.apache.jdo.tck.api.persistencemanagerfactory;
   19   
   20   import java.lang.reflect.Method;
   21   
   22   import java.util.Iterator;
   23   import java.util.Map;
   24   import java.util.Map.Entry;
   25   import java.util.Properties;
   26   import java.util.Set;
   27   
   28   import javax.jdo.JDOFatalUserException;
   29   import javax.jdo.PersistenceManagerFactory;
   30   import javax.jdo.Transaction;
   31   
   32   import org.apache.jdo.tck.JDO_Test;
   33   import org.apache.jdo.tck.pc.mylib.PCPoint;
   34   
   35   /**
   36    * The abstract super class for all GetPMF test cases. 
   37    * @author Michael Watzek
   38    */
   39   
   40   abstract class AbstractGetPMF extends JDO_Test {
   41       
   42       /** 
   43        * Used as the PMF name in positive tests.
   44        * The variable is set to test/conf/jdori.properties.
   45        * This file contains valid JDO properties.
   46        */
   47       protected static final String validPropertiesFile = PMFProperties;
   48       
   49       /** 
   50        * Used as the PMF name in negative tests.
   51        * The variable is set to test/conf/logging.properties.
   52        * This file does not contain valid JDO properties.
   53        */
   54   /*    protected static final String invalidPropertiesFile =
   55           //we use '/' instead of File.separatorChar because
   56           //this character is passed by maven
   57           validPropertiesFile.substring(0, validPropertiesFile.lastIndexOf('/')+1) +
   58           "logging.properties";
   59   */
   60       protected static String invalidPropertiesFile;
   61       static
   62       {
   63           if (validPropertiesFile==null)
   64               System.out.println ("******************************");
   65           //we use '/' instead of File.separatorChar because
   66           //this character is passed by maven
   67           invalidPropertiesFile = validPropertiesFile.substring(0, validPropertiesFile.lastIndexOf('/')+1) +
   68           "logging.properties";
   69       }
   70   
   71       /**
   72        * Used as the PMF name in positive JNDI tests.
   73        * The variable is set to jdori.properties.
   74        * This resource contains valid JDO properties.
   75        */
   76       protected static final String jndiName =
   77           //we use '/' instead of File.separatorChar because
   78           //this character is passed by maven
   79           validPropertiesFile.substring(0, validPropertiesFile.lastIndexOf('/')+1) +
   80           "pmf.ser";
   81       
   82       /**
   83        * Removing the path prefix from argument <code>name</code>.
   84        * @return argument <code>name</code> removed by the path prefix.
   85        */
   86       protected String removePathPrefix(String name) {
   87           //we use '/' instead of File.separatorChar because
   88           //this character is passed by maven
   89           int index = name.lastIndexOf('/');
   90           if (index!=-1) {
   91               name = name.substring(index+1);
   92           }
   93           return name;
   94       }
   95   
   96       protected void makePersistent() {
   97           addTearDownClass(PCPoint.class);
   98           pm = pmf.getPersistenceManager();
   99           Transaction tx = pm.currentTransaction();
  100           tx.begin();
  101           PCPoint comp = new PCPoint(1, 2);
  102           pm.makePersistent(comp);
  103           tx.commit();
  104       }
  105   
  106       /**
  107        * Returns pmf instance for the given name.
  108        * Subclasses may use argument <code>name</code> as file name,
  109        * resource name etc.
  110        * @param name the name
  111        * @return the pmf instance
  112        */
  113       protected abstract PersistenceManagerFactory getPMF(String name);
  114   
  115       /**
  116        * This method creates a PMF calling 
  117        * method AbstractGetPMF#getPMF(name).
  118        * It passes an name of an invalid properties file as parameter.
  119        * The method expects the PMF creation to fail.
  120        * @param assertionMessage the assertion message if the test fails. 
  121        */
  122       protected void checkGetPMFWithInvalidProperties(String assertionMessage) {
  123           try {
  124               pmf = getPMF(invalidPropertiesFile);
  125               fail(assertionMessage);
  126           } catch (JDOFatalUserException e) {
  127               // expected exception
  128               if (debug)
  129                   logger.debug("caught expected exception " + e.toString());
  130           }
  131       }
  132       
  133       /**
  134        * This method creates a PMF calling 
  135        * method AbstractGetPMF#getPMF(name).
  136        * It passes an name of a valid properties file as parameter.
  137        * The method expects the PMF creation to succeed.
  138        */
  139       protected void checkGetPMFWithValidProperties() {
  140           pmf = getPMF(validPropertiesFile);
  141           verifyProperties(pmf, loadProperties(validPropertiesFile));
  142           makePersistent();
  143       }
  144       
  145       /** 
  146        * Verify that the Properties are correctly set in the PMF.
  147        */
  148       protected void verifyProperties(PersistenceManagerFactory pmf,
  149               Properties props) {
  150           Object[] noArgs = new Object[]{};
  151           String javaxjdooption = "javax.jdo.option.";
  152           Class pmfclass = pmf.getClass();
  153           Set entries = props.entrySet();
  154           StringBuffer buffer = new StringBuffer();
  155           for (Iterator it = entries.iterator(); it.hasNext();) {
  156               Entry entry = (Entry)it.next();
  157               String key = (String)entry.getKey();
  158               if (key.equals("javax.jdo.option.ConnectionPassword")) {
  159                   continue;
  160               }
  161               if (key.equals("javax.jdo.option.ConnectionUserName")) {
  162                   continue;
  163               }
  164               String expected = (String)entry.getValue();
  165               if (key.startsWith(javaxjdooption)) {
  166                   String optionName = key.substring(javaxjdooption.length());
  167                   Method getMethod = getGetMethod(pmfclass, optionName);
  168                   Object actual = getValue(getMethod, pmf, noArgs);
  169                   if (actual == null) {
  170                       buffer.append("\n");
  171                       buffer.append("Key ");
  172                       buffer.append(key);
  173                       buffer.append(" was null.");
  174                       continue;
  175                   }
  176                   String actualString = actual.toString();
  177                   if (!expected.equals(actualString)) {
  178                       buffer.append("\n");
  179                       buffer.append("Key ");
  180                       buffer.append(key);
  181                       buffer.append(" expected: \"");
  182                       buffer.append(expected);
  183                       buffer.append("\" actual: \"");
  184                       buffer.append(actual);
  185                       buffer.append("\".");
  186                   }
  187               }
  188           }
  189           if (buffer.length() != 0) {
  190               fail(buffer.toString());
  191           }
  192       }
  193       
  194       /** Get the "get" method corresponding to the option name.
  195        */
  196       Method getGetMethod(Class cls, String optionName) {
  197           try {
  198               return cls.getMethod("get" + optionName, new Class[]{});
  199           } catch (Exception ex) {
  200               fail("Unexpected exception thrown from getMethod on PMF class with option name" + 
  201                       optionName);
  202               return null;
  203           }
  204       }
  205       
  206       /** 
  207        * Get the result of executing the Method.
  208        */
  209       Object getValue(Method method, Object obj, Object[] args) {
  210           try {
  211               return method.invoke(obj, args);
  212           } catch (Exception ex) {
  213               fail("Unexpected exception executing method " + 
  214                       method.getName() + ".");
  215               return null;
  216           }
  217       }
  218   }

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