07-04-05 10:45 PM
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 th
e input String for CharIdentity.
> EXC_IllegalEventType:The event type is outside the range of valid event ty
pes.
>+EXC_ObjectIdentityStringConstruction: The instance could not be constructe
d from \
>+the parameter String "{0}". \nThe exception thrown was: "{1}". \
n\
>+Parsing the class name as "{2}" and key as "{3}".
> +EXC_ObjectIdentityStringConstructionNoD
elimiter: Missing delimiter ":".
> +EXC_ObjectIdentityStringConstructionToo
Short: Parameter is too short.
> +EXC_ObjectIdentityStringConstructionUsa
ge: The instance could not be const
ructed \
>+from the parameter String "{0}". \
>+\nThe parameter String is of the form "<className>:<keyString>".
>+EXC_CreateKeyAsObjectMustNotBeCalled: The method createKeyAsObject must no
t 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>PersistenceManage
rFactory</code> class.
> */
> public static PersistenceManagerFactory getPersistenceManagerFactory
>- (Properties props, ClassLoader cl) {
>+ (Map props, ClassLoader cl) {
> String pmfClassName = (String) props.get ("javax.jdo.PersistenceMa
nagerFactoryClass"); //NOI18N
> if (pmfClassName == null) {
> throw new JDOFatalUserException (msg.msg("EXC_NoClassNamePrope
rty")); // 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 ("getPersistenceManagerF
actory", //NOI18N
>- new Class[] {Properties.class});
>+ try {
>+ propsMethod = pmfClass.getMethod("getPersistenceManagerFac
tory", //NOI18N
>+ new Class[] {Properties.class});
>+ } catch (NoSuchMethodException nsme) {
>+ propsGetMethodException = nsme;
>+ }
>+ try {
>+ mapMethod = pmfClass.getMethod("getPersistenceManagerFacto
ry", //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 met
hod.
>+ 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_getPersis
tenceManagerFactory"), ite); //NOI18N
>+ } else throw new JDOFatalInternalException (msg.msg("EXC_getPe
rsistenceManagerFactory"), ite); //NOI18N
>+ } catch (NullPointerException e) {
>+ throw new JDOFatalInternalException (msg.msg("EXC_NullPointerE
xception", pmfClassName), e); //NOI18N
>+ } catch (ClassCastException e) {
>+ throw new JDOFatalInternalException (msg.msg("EXC_ClassCastExc
eption", pmfClassName), e); //NOI18N
> } catch (Exception e) {
> throw new JDOFatalInternalException (msg.msg("ERR_UnexpectedEx
ception"), 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", "ThisClassDo
esNotExist");
>+ try {
>+ pmf = JDOHelper.getPersistenceManagerFactory(props);
>+ fail("Bad PersistenceManagerFactoryClass should result in JDOF
atalUserException ");
>+ }
>+ 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.J
DOHelperTest$BadPMFNoSuchMethod");
>+ try {
>+ pmf = JDOHelper.getPersistenceManagerFactory(props);
>+ fail("Bad PersistenceManagerFactoryClass should result in JDOF
atalUserException ");
>+ }
>+ 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.J
DOHelperTest$BadPMFNonStaticGetPMFMethod
");
>+ try {
>+ pmf = JDOHelper.getPersistenceManagerFactory(props);
>+ fail("Bad PersistenceManagerFactoryClass should result in JDOF
atalInternalException ");
>+ }
>+ 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.J
DOHelperTest$BadPMFWrongReturnType");
>+ try {
>+ pmf = JDOHelper.getPersistenceManagerFactory(props);
>+ fail("Bad PersistenceManagerFactoryClass should result in JDOF
atalInternalException ");
>+ }
>+ 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
[ Post a follow-up to this message ]
|