db-jdo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From c..@apache.org
Subject svn commit: r694815 - in /db/jdo/trunk/api2: src/java/javax/jdo/ test/java/javax/jdo/ test/schema/jdoconfig/Negative09/ test/schema/jdoconfig/Negative09/9a/ test/schema/jdoconfig/Negative09/9a/META-INF/ test/schema/jdoconfig/Negative09/9a/META-INF/serv...
Date Fri, 12 Sep 2008 21:52:11 GMT
Author: clr
Date: Fri Sep 12 14:52:10 2008
New Revision: 694815

URL: http://svn.apache.org/viewvc?rev=694815&view=rev
Log:
JDO-582 Fixed bugs in JDOHelper implementation:
If implementation returns null to getPersistenceManagerFactory, JDOHelper now throws JDOFatalInternalException.
JDOHelperTest adds a new test case for the above failure.

If implementation fails to return a valid PersistenceManagerFactory, JDOHelper now catches
all exceptions
and if no implementation can be found, throws JDOFatalException with all failed efforts.
JDOHelperConfigTest adds a new test case for the above failure.

Bundle.properties message EXC_GetPMFNoSuchMethod was incorrect.
JDOHelper used the wrong bundle message if no implementation of getPersistenceManagerFactory(Map).

Added:
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/jdoconfig.xml   (with props)
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/services/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/services/javax.jdo.PersistenceManagerFactory
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/services/
    db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/services/javax.jdo.PersistenceManagerFactory
Modified:
    db/jdo/trunk/api2/src/java/javax/jdo/Bundle.properties
    db/jdo/trunk/api2/src/java/javax/jdo/JDOHelper.java
    db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperConfigTest.java
    db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperTest.java

Modified: db/jdo/trunk/api2/src/java/javax/jdo/Bundle.properties
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/Bundle.properties?rev=694815&r1=694814&r2=694815&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/Bundle.properties (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/Bundle.properties Fri Sep 12 14:52:10 2008
@@ -21,7 +21,7 @@
 ERR_StateManagerClassCast=An element of the parameter collection is of class {0}.  \
 The parameter collection must contain only elements that are instances of java.lang.Class.
 ERR_NoMetadata=No metadata has been registered for class {0}.
-EXC_GetPMFNoSuchMethod=Method getPersistenceManagerFactory(Properties) does not exist or
is not public.
+EXC_GetPMFNoSuchMethod=Method getPersistenceManagerFactory(Map) on class {0} does not exist
or is not public.
 EXC_GetPMFNoSuchMethod2=Method getPersistenceManagerFactory(Map,Map) on class {0} does not
exist or is not public.
 EXC_GetPMFInvocationTargetException=Method getPersistenceManagerFactory(..) on class {0}
threw exception.
 EXC_GetPMFUnexpectedException=Unexpected exception caught.
@@ -43,6 +43,7 @@
 EXC_GetPMFNullPropsLoader: The class loader argument to this method cannot be null.
 EXC_GetPMFNullPMFLoader: The class loader argument to this method cannot be null.
 EXC_GetPMFNullStream: The stream argument to this method cannot be null.
+EXC_GetPMFNullPMF: The PMF {0} returned null to getPersistenceManagerFactory.
 EXC_GetPMFNoResource: The resource named "{0}" could not be found in the class loader "{1}".
 EXC_GetPMFIOExceptionRsrc: An IOException was thrown while loading the resource \
 named "{0}" into a java.util.Properties object.

Modified: db/jdo/trunk/api2/src/java/javax/jdo/JDOHelper.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/JDOHelper.java?rev=694815&r1=694814&r2=694815&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/JDOHelper.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/JDOHelper.java Fri Sep 12 14:52:10 2008
@@ -737,11 +737,13 @@
      * <BR>"javax.jdo.option.Mapping",
      * <BR>"javax.jdo.mapping.Catalog",
      * <BR>"javax.jdo.mapping.Schema",
-     * <BR>"javax.jdo.option.PersistenceUnitName".
-     * <BR>"javax.jdo.option.DetachAllOnCommit".
-     * <BR>"javax.jdo.option.CopyOnAttach".
-     * <BR>"javax.jdo.option.TransactionType".
-     * <BR>"javax.jdo.option.ServerTimeZoneID".
+     * <BR>"javax.jdo.option.PersistenceUnitName",
+     * <BR>"javax.jdo.option.DetachAllOnCommit",
+     * <BR>"javax.jdo.option.CopyOnAttach",
+     * <BR>"javax.jdo.option.ReadOnly",
+     * <BR>"javax.jdo.option.TransactionIsolationLevel",
+     * <BR>"javax.jdo.option.TransactionType",
+     * <BR>"javax.jdo.option.ServerTimeZoneID",
      * <BR>"javax.jdo.option.Name".
      * </code>
      * and properties of the form
@@ -773,6 +775,13 @@
      * <code>PersistenceManagerFactory</code>.
      * @param pmfClassLoader the class loader to use to load the
      * <code>PersistenceManagerFactory</code> class
+     * @throws JDOFatalUserException if
+     * <ul><li>the pmfClassLoader passed is invalid; or 
+     * </li><li>a valid class name cannot be obtained from
+     * either <code>props</code> or system resources 
+     * (an entry in META-INF/services/javax.jdo.PersistenceManagerFactory); or
+     * </li><li>all implementations throw an exception.
+     * </li></ul>
      * @since 2.1
      */
     protected static PersistenceManagerFactory getPersistenceManagerFactory
@@ -782,34 +791,65 @@
             throw new JDOFatalUserException (msg.msg (
                 "EXC_GetPMFNullLoader")); //NOI18N
 
+	    // first try to get the class name from the properties object.
         String pmfClassName = (String) props.get (
                 PROPERTY_PERSISTENCE_MANAGER_FACTORY_CLASS);
 
         if (!isNullOrBlank(pmfClassName)) {
+	    // a valid name was returned from the properties.
             return invokeGetPersistenceManagerFactoryOnImplementation(
                         pmfClassName, overrides, props, pmfClassLoader);
 
         } else {
-        // PMF class name null or blank -- try services
-        // for each file META-INF/services/javax.jdo.PersistenceManagerFactory
-        // try to invoke the getPersistenceManagerFactory method of the
-        // implementation class
+            /*
+             * If you have a jar file that provides the jdo implementation,
+             * a file naming the implementation goes into the file 
+             * packaged into the jar file, called
+             * META-INF/services/javax.jdo.PersistenceManagerFactory.
+             * The contents of the file is a string that is the PMF class name, 
+             * null or blank. 
+             * For each file in pmfClassLoader named
+             * META-INF/services/javax.jdo.PersistenceManagerFactory,
+             * this method will try to invoke the getPersistenceManagerFactory
+             * method of the implementation class. 
+             * Return the factory if a valid class name is extracted from 
+             * resources and the invocation returns an instance.  
+             * Otherwise add the exception thrown to 
+             * an exception list.
+             */
+            Enumeration urls = null;
             try {
-                Enumeration urls = getResources(pmfClassLoader, 
-                    SERVICE_LOOKUP_PMF_RESOURCE_NAME);
-                while (urls.hasMoreElements()) {
-                    pmfClassName = getClassNameFromURL((URL)urls.nextElement());
-                        return invokeGetPersistenceManagerFactoryOnImplementation(
-                            pmfClassName, overrides, props, pmfClassLoader);
-                }
+                urls = getResources(pmfClassLoader,
+                        SERVICE_LOOKUP_PMF_RESOURCE_NAME);
             } catch (Throwable ex) {
-                // remember exceptions from failed pmf invocations
                 exceptions.add(ex);
             }
+
+            if (urls != null){
+                while (urls.hasMoreElements()) {
+
+                    try {
+                        pmfClassName = getClassNameFromURL(
+                                (URL) urls.nextElement());
+
+                        // return the implementation that is valid.
+                        PersistenceManagerFactory pmf = 
+                            invokeGetPersistenceManagerFactoryOnImplementation(
+                                pmfClassName, overrides, props, pmfClassLoader);
+                        return pmf;
+
+                    } catch (Throwable ex) {
+
+                        // remember exceptions from failed pmf invocations
+                        exceptions.add(ex);
+
+                    }
+                }
+            }
         }
 
-        // no PMF class name and no services
-        
+        // no PMF class name in props and no services.  
+
         throw new JDOFatalUserException(msg.msg(
                 "EXC_GetPMFNoPMFClassNamePropertyOrPUNameProperty"),
                 (Throwable[])
@@ -1073,23 +1113,29 @@
             try {
                 Class implClass = forName(pmfClassName, true, cl);
                 Method m = getMethod(implClass,
-                        "getPersistenceManagerFactory",
+                        "getPersistenceManagerFactory", //NOI18N
                         new Class[]{Map.class, Map.class});
-                return (PersistenceManagerFactory) invoke(m,
+                PersistenceManagerFactory pmf = 
+                    (PersistenceManagerFactory) invoke(m,
                         null, new Object[]{overrides, properties});
+                if (pmf == null) {
+                        throw new JDOFatalInternalException(msg.msg (
+                            "EXC_GetPMFNullPMF", pmfClassName)); //NOI18N
+                    }
+                return pmf;
 
             } catch (ClassNotFoundException e) {
                 throw new JDOFatalUserException(msg.msg(
-                        "EXC_GetPMFClassNotFound", pmfClassName), e);
+                        "EXC_GetPMFClassNotFound", pmfClassName), e); //NOI18N
             } catch (NoSuchMethodException e) {
                 throw new JDOFatalInternalException(msg.msg(
-                        "EXC_GetPMFNoSuchMethod2", pmfClassName), e);
+                        "EXC_GetPMFNoSuchMethod2", pmfClassName), e); //NOI18N
             } catch (NullPointerException e) {
                 throw new JDOFatalInternalException (msg.msg(
                     "EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
             } catch (IllegalAccessException e) {
                 throw new JDOFatalUserException(msg.msg(
-                        "EXC_GetPMFIllegalAccess", pmfClassName), e);
+                        "EXC_GetPMFIllegalAccess", pmfClassName), e); //NOI18N
             } catch (ClassCastException e) {
                 throw new JDOFatalInternalException (msg.msg(
                     "EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N
@@ -1105,22 +1151,28 @@
             try {
                 Class implClass = forName(pmfClassName, true, cl);
                 Method m = getMethod(implClass,
-                        "getPersistenceManagerFactory",
+                        "getPersistenceManagerFactory", //NOI18N
                         new Class[]{Map.class});
-                return (PersistenceManagerFactory) invoke(m,
+                PersistenceManagerFactory pmf = 
+                    (PersistenceManagerFactory) invoke(m,
                         null, new Object[]{properties});
+                if (pmf == null) {
+                        throw new JDOFatalInternalException(msg.msg (
+                            "EXC_GetPMFNullPMF", pmfClassName)); //NOI18N
+                    }
+                return pmf;
             } catch (ClassNotFoundException e) {
                 throw new JDOFatalUserException(msg.msg(
-                        "EXC_GetPMFClassNotFound", pmfClassName), e);
+                        "EXC_GetPMFClassNotFound", pmfClassName), e); //NOI18N
             } catch (NoSuchMethodException e) {
                 throw new JDOFatalInternalException(msg.msg(
-                        "EXC_GetPMFNoSuchMethod2", pmfClassName), e);
+                        "EXC_GetPMFNoSuchMethod", pmfClassName), e); //NOI18N
             } catch (NullPointerException e) {
                 throw new JDOFatalInternalException (msg.msg(
                     "EXC_GetPMFNullPointerException", pmfClassName), e); //NOI18N
             } catch (IllegalAccessException e) {
                 throw new JDOFatalUserException(msg.msg(
-                        "EXC_GetPMFIllegalAccess", pmfClassName), e);
+                        "EXC_GetPMFIllegalAccess", pmfClassName), e); //NOI18N
             } catch (ClassCastException e) {
                 throw new JDOFatalInternalException (msg.msg(
                     "EXC_GetPMFClassCastException", pmfClassName), e); //NOI18N

Modified: db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperConfigTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperConfigTest.java?rev=694815&r1=694814&r2=694815&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperConfigTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperConfigTest.java Fri Sep 12 14:52:10 2008
@@ -48,6 +48,24 @@
             = initJDOConfigClasspathPrefix();
 
     protected static String initJDOConfigClasspathPrefix() {
+        return initBasedir() + "test/schema/jdoconfig";
+    }
+
+    protected static String TEST_CLASSPATH =
+            initTestClasspath();
+
+    protected static String initTestClasspath() {
+        return initBasedir() + "target/test-classes/";
+    }
+
+    protected static String API_CLASSPATH =
+            initAPIClasspath();
+
+    protected static String initAPIClasspath() {
+        return initBasedir() + "target/classes/";
+    }
+
+    protected static String initBasedir() {
         String basedir = System.getProperty("basedir");
         if (basedir != null) {
             if (!basedir.endsWith("/")) {
@@ -56,7 +74,7 @@
         } else {
             basedir = "";
         }
-        return basedir + "test/schema/jdoconfig";
+        return basedir;
     }
 
     protected static Random RANDOM = new Random(System.currentTimeMillis());
@@ -527,6 +545,7 @@
         }
     }
 
+    
     public void testNegative07_EmptyServicesFile()
             throws IOException {
         JDOConfigTestClassLoader testLoader = new JDOConfigTestClassLoader(
@@ -568,6 +587,50 @@
         assertNull(shouldBeNull);
     }
     
+    public void testNegative09_MultipleInvalidClassesInDifferentServicesFiles()
+            throws IOException {
+
+        // no class name in Negative09/jdoconfig.xml
+        // 9a and 9b include services/javax.jdo.PersistenceManagerFactory
+        // with bad implementations
+        try {
+            URLClassLoader loader = new JDOConfigTestClassLoader(
+                    JDOCONFIG_CLASSPATH_PREFIX,
+                    getClass().getClassLoader());
+            ClasspathHelper.addFile(
+                    JDOCONFIG_CLASSPATH_PREFIX + "/Negative09/9a", loader);
+            ClasspathHelper.addFile(
+                    JDOCONFIG_CLASSPATH_PREFIX + "/Negative09/9b", loader);
+            ClasspathHelper.addFile(
+                    TEST_CLASSPATH, loader);
+            ClasspathHelper.addFile(
+                    API_CLASSPATH, loader);
+
+            JDOHelper.getPersistenceManagerFactory("name.negative09", loader);
+
+            fail("JDOHelper failed to throw JDOFatalUserException");
+        }
+        catch (JDOFatalException x) {
+
+            Throwable[] nestedExceptions = x.getNestedExceptions();
+            if (nestedExceptions.length != 2) {
+                appendMessage(
+                "JDOHelper.getPersistenceManagerFactory wrong number of " +
+                "nested exceptions. Expected 2, got " + nestedExceptions.length +
+                        "\n" + x);
+            }
+            for (int i = 0; i < nestedExceptions.length; ++i) {
+                Throwable exception = nestedExceptions[i];
+                if (!(exception instanceof JDOFatalException)) {
+                    appendMessage("Nested exception " + 
+                            exception.getClass().getName() + 
+                            " is not a JDOFatalException.");
+                }
+            }
+        }
+        failOnError();
+    }
+
     private String getPMFClassNameViaServiceLookup(ClassLoader loader) {
         try {
             Enumeration urls = JDOHelper.getResources(loader, 

Modified: db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperTest.java?rev=694815&r1=694814&r2=694815&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/JDOHelperTest.java Fri Sep 12 14:52:10 2008
@@ -548,6 +548,25 @@
         }
     }
 
+    /** Test bad PMF class getPersistenceManagerFactory returns null.
+     */
+    public void testBadPMFGetPMFMethodReturnsNull() {
+        PersistenceManagerFactory pmf = null;
+        Properties props = new Properties();
+        props.put("javax.jdo.PersistenceManagerFactoryClass", 
+                "javax.jdo.JDOHelperTest$BadPMFGetPMFMethodReturnsNull");
+        try {
+            pmf = JDOHelper.getPersistenceManagerFactory(props);
+            fail("BadPMFGetPMFMethodReturnsNull.GetPersistenceManagerFactory " +
+                    "should result in JDOFatalInternalException. " +
+                    "No exception was thrown.");
+        }
+        catch (JDOFatalInternalException ex) {
+            if (verbose)
+                println("Caught expected exception " + ex);
+        }
+    }
+
     private Context getInitialContext() {
         try {
             return new InitialContext();
@@ -581,4 +600,11 @@
                     "GetPMF method throws JDOUnsupportedOptionException");
         }
     }
+
+    public static class BadPMFGetPMFMethodReturnsNull {
+        public static PersistenceManagerFactory
+                getPersistenceManagerFactory(Map props) {
+            return null;
+        }
+    }
 }

Added: db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/jdoconfig.xml
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/jdoconfig.xml?rev=694815&view=auto
==============================================================================
--- db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/jdoconfig.xml (added)
+++ db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/jdoconfig.xml Fri Sep 12
14:52:10 2008
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Do not change any values in this file without also making changes to
+    JDOHelperConfigTest.java, as that file depends on the values in this one!
+-->
+<jdoconfig
+    xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
+
+    <!-- Invalid PMFs in this file and another -->
+    <persistence-manager-factory 
+        name="name.negative09"/>
+      
+</jdoconfig>
+
+    
\ No newline at end of file

Propchange: db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/jdoconfig.xml
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/services/javax.jdo.PersistenceManagerFactory
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/services/javax.jdo.PersistenceManagerFactory?rev=694815&view=auto
==============================================================================
--- db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/services/javax.jdo.PersistenceManagerFactory
(added)
+++ db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9a/META-INF/services/javax.jdo.PersistenceManagerFactory
Fri Sep 12 14:52:10 2008
@@ -0,0 +1 @@
+javax.jdo.JDOHelperTest$BadPMFNoGetPMFMethod

Added: db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/services/javax.jdo.PersistenceManagerFactory
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/services/javax.jdo.PersistenceManagerFactory?rev=694815&view=auto
==============================================================================
--- db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/services/javax.jdo.PersistenceManagerFactory
(added)
+++ db/jdo/trunk/api2/test/schema/jdoconfig/Negative09/9b/META-INF/services/javax.jdo.PersistenceManagerFactory
Fri Sep 12 14:52:10 2008
@@ -0,0 +1 @@
+javax.jdo.JDOHelperTest$BadPMFGetPMFMethodThrowsJDOException



Mime
View raw message