db-jdo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Bouschen <mbo.t...@spree.de>
Subject Re: Please review these changes to JDOHelper
Date Mon, 04 Jul 2005 16:58:18 GMT
Hi Craig,

looks good, no remarks from my side.

Regards Michael

> Hi,
>
> I've added some more information to the JDOHelper 
> getPersistenceManagerFactory(Map props) code and added test cases.
>
>------------------------------------------------------------------------
>
>Index: src/java/javax/jdo/Bundle.properties
>===================================================================
>--- src/java/javax/jdo/Bundle.properties	(revision 202295)
>+++ src/java/javax/jdo/Bundle.properties	(working copy)
>@@ -46,3 +46,19 @@
> PersistenceManagerFactory at "{0}" from JNDI.
> EXC_StringWrongLength: There must be exactly one character in the id in the input String
for CharIdentity.
> EXC_IllegalEventType:The event type is outside the range of valid event types.
>+EXC_ObjectIdentityStringConstruction: The instance could not be constructed from \
>+the parameter String "{0}". \nThe exception thrown was: "{1}". \n\
>+Parsing the class name as "{2}" and key as "{3}".
>+EXC_ObjectIdentityStringConstructionNoDelimiter: Missing delimiter ":".
>+EXC_ObjectIdentityStringConstructionTooShort: Parameter is too short.
>+EXC_ObjectIdentityStringConstructionUsage: The instance could not be constructed \
>+from the parameter String "{0}". \
>+\nThe parameter String is of the form "<className>:<keyString>".
>+EXC_CreateKeyAsObjectMustNotBeCalled: The method createKeyAsObject must not be called
\
>+because the keyAsObject field must never be null for this class.
>+EXC_NullPointerException: The PersistenceManagerFactory class must define a static \
>+method \nPersistenceManagerFactory getPersistenceManagerFactory(Map props). \nThe class
"{0}"\n\
>+defines a non-static getPersistenceManagerFactory(Map props) method.
>+EXC_ClassCastException: The PersistenceManagerFactory class must define a static \
>+method \nPersistenceManagerFactory getPersistenceManagerFactory(Map props). \nThe class
"{0}"\n\
>+has the wrong return type for the getPersistenceManagerFactory(Map props) method.
>Index: src/java/javax/jdo/JDOHelper.java
>===================================================================
>--- src/java/javax/jdo/JDOHelper.java	(revision 202295)
>+++ src/java/javax/jdo/JDOHelper.java	(working copy)
>@@ -30,6 +30,7 @@
> import java.lang.reflect.Method;
> import java.lang.reflect.InvocationTargetException;
> 
>+import java.util.Map;
> import java.util.Properties;
> 
> import javax.jdo.spi.I18NHelper;
>@@ -249,7 +250,7 @@
>      * @see #getPersistenceManagerFactory(Properties,ClassLoader)
>      */
>     public static PersistenceManagerFactory getPersistenceManagerFactory
>-            (Properties props) {
>+            (Map props) {
>         ClassLoader cl = Thread.currentThread().getContextClassLoader();
>         return getPersistenceManagerFactory (props, cl);
>     }
>@@ -287,15 +288,49 @@
>      * @param cl a class loader to use to load the <code>PersistenceManagerFactory</code>
class.
>      */
>     public static PersistenceManagerFactory getPersistenceManagerFactory
>-            (Properties props, ClassLoader cl) {
>+            (Map props, ClassLoader cl) {
>         String pmfClassName = (String) props.get ("javax.jdo.PersistenceManagerFactoryClass");
//NOI18N
>         if (pmfClassName == null) {
>             throw new JDOFatalUserException (msg.msg("EXC_NoClassNameProperty")); //
NOI18N
>         }
>+        Method propsMethod = null;
>+        Exception propsGetMethodException = null;
>+        Method mapMethod = null;
>+        Exception mapGetMethodException = null;
>+        Method pmfMethod = null;
>         try {
>             Class pmfClass = cl.loadClass (pmfClassName);
>-            Method pmfMethod = pmfClass.getMethod ("getPersistenceManagerFactory",  //NOI18N
>-                new Class[] {Properties.class});
>+            try {
>+                propsMethod = pmfClass.getMethod("getPersistenceManagerFactory",  //NOI18N
>+                    new Class[] {Properties.class});
>+            } catch (NoSuchMethodException nsme) {
>+                propsGetMethodException = nsme;
>+            }
>+            try {
>+                mapMethod = pmfClass.getMethod("getPersistenceManagerFactory",  //NOI18N
>+                    new Class[] {Map.class});
>+            } catch (NoSuchMethodException nsme) {
>+                mapGetMethodException = nsme;
>+            }
>+            /* If the parameter is not a Properties, 
>+             * we need a mapMethod or else throw an exception.
>+             */
>+            if (!(props instanceof Properties)) {
>+                if (mapMethod != null) {
>+                    pmfMethod = mapMethod;
>+                } else {
>+                    throw mapGetMethodException;
>+                }
>+            } else { // the parameter is a Properties; use either method.
>+                if (mapMethod != null) {
>+                    pmfMethod = mapMethod;
>+                } else if (propsMethod != null) {
>+                    pmfMethod = propsMethod;
>+                } else {
>+                    throw mapGetMethodException;
>+                }
>+            }
>+            
>             return (PersistenceManagerFactory) pmfMethod.invoke (null, new Object[] {props});
>         } catch (ClassNotFoundException cnfe) {
>             throw new JDOFatalUserException (msg.msg("EXC_ClassNotFound", pmfClassName),
cnfe); //NOI18N
>@@ -307,12 +342,16 @@
>             Throwable nested = ite.getTargetException();
>             if  (nested instanceof JDOException) {
>                 throw (JDOException)nested;
>-            } else throw new JDOFatalUserException (msg.msg("EXC_getPersistenceManagerFactory"),
ite); //NOI18N
>+            } else throw new JDOFatalInternalException (msg.msg("EXC_getPersistenceManagerFactory"),
ite); //NOI18N
>+        } catch (NullPointerException e) {
>+            throw new JDOFatalInternalException (msg.msg("EXC_NullPointerException",
pmfClassName), e); //NOI18N
>+        } catch (ClassCastException e) {
>+            throw new JDOFatalInternalException (msg.msg("EXC_ClassCastException", pmfClassName),
e); //NOI18N
>         } catch (Exception e) {
>             throw new JDOFatalInternalException (msg.msg("ERR_UnexpectedException"),
e); //NOI18N
>         }
>     }
>-
>+    
>     /**
>      * Returns a {@link PersistenceManagerFactory} configured based
>      * on the properties stored in the resource at
>Index: test/java/javax/jdo/JDOHelperTest.java
>===================================================================
>--- test/java/javax/jdo/JDOHelperTest.java	(revision 202295)
>+++ test/java/javax/jdo/JDOHelperTest.java	(working copy)
>@@ -16,6 +16,7 @@
> 
> package javax.jdo;
> 
>+import java.util.Map;
> import java.util.Properties;
> 
> import javax.jdo.pc.PCPoint;
>@@ -122,9 +123,9 @@
>         // TBD test JDOHelper.isDeleted(pc) for persistent instance
>     }
> 
>-    /** */
>-    public void testGetPMF() {
>-        // test missing property javax.jdo.PersistenceManagerFactoryClass
>+    /** Test missing property javax.jdo.PersistenceManagerFactoryClass.
>+     */
>+    public void testGetPMFMissingPMFClass() {
>         PersistenceManagerFactory pmf = null;
>         try {
>             pmf = JDOHelper.getPersistenceManagerFactory(new Properties());
>@@ -134,9 +135,86 @@
>             if (verbose)
>                 println("Caught expected exception " + ex);
>         }
>+    }
> 
>-        // TBD: valid PMF class
>+    /** Test bad PMF class does not exist.
>+     */
>+    public void testBadPMFClassDoesNotExist() {
>+        PersistenceManagerFactory pmf = null;
>+        Properties props = new Properties();
>+        props.put("javax.jdo.PersistenceManagerFactoryClass", "ThisClassDoesNotExist");
>+        try {
>+            pmf = JDOHelper.getPersistenceManagerFactory(props);
>+            fail("Bad PersistenceManagerFactoryClass should result in JDOFatalUserException
");
>+        }
>+        catch (JDOFatalUserException ex) {
>+            if (verbose)
>+                println("Caught expected exception " + ex);
>+        }
>     }
> 
>+    /** Test bad PMF class no method getPersistenceManagerFactory.
>+     */
>+    public void testBadPMFNoSuchMethod() {
>+        PersistenceManagerFactory pmf = null;
>+        Properties props = new Properties();
>+        props.put("javax.jdo.PersistenceManagerFactoryClass", "javax.jdo.JDOHelperTest$BadPMFNoSuchMethod");
>+        try {
>+            pmf = JDOHelper.getPersistenceManagerFactory(props);
>+            fail("Bad PersistenceManagerFactoryClass should result in JDOFatalUserException
");
>+        }
>+        catch (JDOFatalInternalException ex) {
>+            if (verbose)
>+                println("Caught expected exception " + ex);
>+        }
>+    }
>+
>+    /** Test bad PMF class non-static getPMF method.
>+     */
>+    public void testBadPMFNonStaticGetPMFMethod() {
>+        PersistenceManagerFactory pmf = null;
>+        Properties props = new Properties();
>+        props.put("javax.jdo.PersistenceManagerFactoryClass", "javax.jdo.JDOHelperTest$BadPMFNonStaticGetPMFMethod");
>+        try {
>+            pmf = JDOHelper.getPersistenceManagerFactory(props);
>+            fail("Bad PersistenceManagerFactoryClass should result in JDOFatalInternalException
");
>+        }
>+        catch (JDOFatalInternalException ex) {
>+            if (verbose)
>+                println("Caught expected exception " + ex);
>+        }
>+    }
>+
>+    /** Test bad PMF class doesn't implement PMF.
>+     */
>+    public void testBadPMFWrongReturnType() {
>+        PersistenceManagerFactory pmf = null;
>+        Properties props = new Properties();
>+        props.put("javax.jdo.PersistenceManagerFactoryClass", "javax.jdo.JDOHelperTest$BadPMFWrongReturnType");
>+        try {
>+            pmf = JDOHelper.getPersistenceManagerFactory(props);
>+            fail("Bad PersistenceManagerFactoryClass should result in JDOFatalInternalException
");
>+        }
>+        catch (JDOFatalInternalException ex) {
>+            if (verbose)
>+                println("Caught expected exception " + ex);
>+        }
>+    }
>+
>+    private class BadPMFNoSuchMethod {
>+    }
>+
>+    private class BadPMFNonStaticGetPMFMethod {
>+        public BadPMFNonStaticGetPMFMethod 
>+                getPersistenceManagerFactory(Map props) {
>+            return new BadPMFNonStaticGetPMFMethod();
>+        }
>+    }
>+        
>+    private static class BadPMFWrongReturnType {
>+        public static BadPMFWrongReturnType 
>+                getPersistenceManagerFactory(Map props) {
>+            return new BadPMFWrongReturnType();
>+        }
>+    }
> }
>-
>  
>
>
> ------------------------------------------------------------------------
>
>
> Craig Russell
>
> Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
>
> 408 276-5638 mailto:Craig.Russell@sun.com
>
> P.S. A good JDO? O, Gasp!
>
>


-- 
Michael Bouschen		Tech@Spree Engineering GmbH
mailto:mbo.tech@spree.de	http://www.tech.spree.de/
Tel.:++49/30/235 520-33		Buelowstr. 66			
Fax.:++49/30/2175 2012		D-10783 Berlin			


Mime
View raw message