You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2009/06/16 22:11:45 UTC

svn commit: r785366 - in /openjpa/trunk: openjpa-integration/ openjpa-integration/validation/ openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ openjpa-kernel/src/main/java/org/apache/openjpa/conf/ openjpa-kernel/s...

Author: dwoods
Date: Tue Jun 16 20:11:45 2009
New Revision: 785366

URL: http://svn.apache.org/viewvc?rev=785366&view=rev
Log:
OPENJPA-1111  Validation mode of callback should cause a PersistenceException when no provider is available.  Also some config rework as suggested by Pinaki in reviewing OPENJPA-1114.  This is Part 1, as the thrown PersistenceException is not marked as Fatal and contains a null description, but does have the stack.

Added:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/ValidationUtils.java   (with props)
Modified:
    openjpa/trunk/openjpa-integration/pom.xml
    openjpa/trunk/openjpa-integration/validation/pom.xml
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/validation/ValidatingLifecycleEventManager.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
    openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties

Modified: openjpa/trunk/openjpa-integration/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/pom.xml?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/pom.xml (original)
+++ openjpa/trunk/openjpa-integration/pom.xml Tue Jun 16 20:11:45 2009
@@ -34,6 +34,7 @@
     <modules>
         <module>examples</module>
         <module>tck</module>
+        <module>validation</module>
         <!-- <module>osgi-itests</module> -->
     </modules>
 </project>

Modified: openjpa/trunk/openjpa-integration/validation/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/pom.xml?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/pom.xml (original)
+++ openjpa/trunk/openjpa-integration/validation/pom.xml Tue Jun 16 20:11:45 2009
@@ -63,11 +63,17 @@
             </activation>
             <dependencies>
                 <!-- See http://code.google.com/p/agimatec-validation/wiki/GettingStarted -->
+                <!-- Validation API RI, which we'll test with the RI
                 <dependency>
                     <groupId>javax.validation</groupId>
                     <artifactId>validation-api</artifactId>
                     <version>1.0.Beta4</version>
-                    <!-- <version>1.0.CR2</version> -->
+                    <scope>test</scope>
+                </dependency>
+                -->
+                <dependency>
+                    <groupId>org.apache.geronimo.specs</groupId>
+                    <artifactId>geronimo-validation_1.0_spec</artifactId>
                     <scope>test</scope>
                 </dependency>
                 <dependency>
@@ -91,7 +97,8 @@
             </dependencies>
             <repositories>
                 <repository>
-                    <!-- have to use a commiters home dir until released -->
+                    <!-- have to use a commiters home dir for the Agimatec
+                         artifacts until released in a public repo -->
                     <id>dwoods.people.apache.org</id>
                     <name>dwoods Staging Repo</name>
                     <url>http://people.apache.org/~dwoods/staging-repo</url>

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java (original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidatingLEM.java Tue Jun 16 20:11:45 2009
@@ -20,6 +20,8 @@
 import javax.persistence.EntityManager;
 import javax.persistence.Query;
 import javax.persistence.ValidationMode;
+import javax.validation.Validation;
+import javax.validation.ValidatorFactory;
 
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.event.LifecycleEventManager;
@@ -36,7 +38,7 @@
  * focusing on the following Validation Provider scenarios:
  *   1) Mode of NONE will create a LifecycleEventManager
  *   2) Mode of AUTO will create a ValidatingLifecycleEventManager
- *   3) Mode of CALLBACK will create a ValidatingLifecycleEventManager
+ *   3) Map mode of CALLBACK will create a ValidatingLifecycleEventManager
  *   4) Verify a passed in ValidatorFactory is used
  * 
  * @version $Rev$ $Date$
@@ -72,35 +74,25 @@
      */
     public void testValidatingLEM1() {
         getLog().trace("testValidatingLEM1() - NONE");
-        OpenJPAEntityManagerFactory emf = null;
-
         // create our EMF
-        emf = OpenJPAPersistence.createEntityManagerFactory(
-            "simple-none-mode",
-            "org/apache/openjpa/integration/validation/persistence.xml");
+        OpenJPAEntityManagerFactory emf = OpenJPAPersistence
+            .createEntityManagerFactory(
+                "simple-none-mode",
+                "org/apache/openjpa/integration/validation/persistence.xml");
         assertNotNull(emf);
         // create EM
         OpenJPAEntityManager em = emf.createEntityManager();
         assertNotNull(em);
         try {
-            Query q = em.createNamedQuery("FindAll");
-            @SuppressWarnings("unchecked")
-            List results = q.getResultList();
-
             // verify created LifecycleEventManager type
             OpenJPAConfiguration conf = em.getConfiguration();
             assertNotNull(conf);
             assertTrue("ValidationMode",
                 conf.getValidationMode().equalsIgnoreCase("NONE"));
-//            Class<?> lem = conf.getLifecycleEventManagerInstance().getClass();
-            LifecycleEventManager lem = conf.getLifecycleEventManagerInstance();
+            Class<?> lem = conf.getLifecycleEventManagerInstance().getClass();
             assertNotNull(lem);
-            System.out.println("**** LEM=" + lem.toString());
-//            assertFalse("Expected a LifecycleEventManager instance", 
-//                ValidatingLifecycleEventManager.class.isAssignableFrom(lem));
-            ValidatingLifecycleEventManager vlem =
-                (ValidatingLifecycleEventManager)lem;
-            System.out.println("**** VLEM=" + vlem.toString());
+            assertFalse("Expected a LifecycleEventManager instance", 
+                ValidatingLifecycleEventManager.class.isAssignableFrom(lem));
         } catch (Exception e) {
             fail("Unexpected testValidatingLEM1() exception = " + e);
         } finally {
@@ -112,99 +104,124 @@
 
     /**
      * Scenario being tested:
-     *   2) An invalid ValidationFactory with a mode of NONE will not
-     *      cause an exception
+     *   2) Mode of AUTO will create a ValidatingLifecycleEventManager
      */
-    public void XXXtestValidatingLEM2() {
-        getLog().trace("testValidatingLEM2() - ignored invalid factory");
-        OpenJPAEntityManagerFactory emf = null;
-
-        // create the Map to test overrides
-        //   Just use current class object, as we have no provider to test with
-        Map<String,Object> props = new HashMap<String,Object>();
-        props.put("javax.persistence.validation.factory",
-            this.getClass());
-
+    public void testValidatingLEM2() {
+        getLog().trace("testValidatingLEM2() - AUTO");
         // create our EMF
-        emf = OpenJPAPersistence.createEntityManagerFactory(
-            "simple-none-mode",
-            "org/apache/openjpa/integration/validation/persistence.xml",
-            props);
+        OpenJPAEntityManagerFactory emf = OpenJPAPersistence
+            .createEntityManagerFactory(
+                "simple-auto-mode",
+                "org/apache/openjpa/integration/validation/persistence.xml");
         assertNotNull(emf);
-        // verify same "validation factory" object is returned
-        OpenJPAConfiguration conf = emf.getConfiguration();
-        assertNotNull(conf);
-        assertEquals("ValidationFactory", 
-            this.getClass(),
-            conf.getValidationFactoryInstance());
+        // create EM
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify created LifecycleEventManager type
+            OpenJPAConfiguration conf = em.getConfiguration();
+            assertNotNull(conf);
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("AUTO"));
+            Class<?> lem = conf.getLifecycleEventManagerInstance().getClass();
+            assertNotNull(lem);
+            assertTrue("Expected a ValidatingLifecycleEventManager instance", 
+                ValidatingLifecycleEventManager.class.isAssignableFrom(lem));
+        } catch (Exception e) {
+            fail("Unexpected testValidatingLEM2() exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
+        }
     }
 
     /**
      * Scenario being tested:
-     *   3) An invalid ValidationFactory with a mode of AUTO will not
-     *      cause an exception
+     *   3) Map mode of CALLBACK will create a ValidatingLifecycleEventManager
      */
-    public void XXXtestValidatingLEM3() {
-        getLog().trace("testValidatingLEM3() - optional invalid factory");
-        OpenJPAEntityManagerFactory emf = null;
-
+    public void testValidatingLEM3() {
+        getLog().trace("testValidatingLEM3() - CALLBACK");
         // create the Map to test overrides
         //   Just use current class object, as we have no provider to test with
         Map<String,Object> props = new HashMap<String,Object>();
-        props.put("javax.persistence.validation.factory",
-            this.getClass());
-
-        // create our EMF
-        emf = OpenJPAPersistence.createEntityManagerFactory(
-            "simple-auto-mode",
-            "org/apache/openjpa/integration/validation/persistence.xml",
-            props);
+        props.put("javax.persistence.validation.mode",
+            String.valueOf(ValidationMode.CALLBACK));
+        // create our EMF w/ props
+        OpenJPAEntityManagerFactory emf = OpenJPAPersistence
+            .createEntityManagerFactory(
+                "simple-none-mode",
+                "org/apache/openjpa/integration/validation/persistence.xml",
+                props);
         assertNotNull(emf);
-        // verify same "validation factory" object is returned
-        OpenJPAConfiguration conf = emf.getConfiguration();
-        assertNotNull(conf);
-        assertEquals("ValidationFactory", 
-            this.getClass(),
-            conf.getValidationFactoryInstance());
+        // create EM
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify created LifecycleEventManager type
+            OpenJPAConfiguration conf = em.getConfiguration();
+            assertNotNull(conf);
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("CALLBACK"));
+            Class<?> lem = conf.getLifecycleEventManagerInstance().getClass();
+            assertNotNull(lem);
+            assertTrue("Expected a ValidatingLifecycleEventManager instance", 
+                ValidatingLifecycleEventManager.class.isAssignableFrom(lem));
+        } catch (Exception e) {
+            fail("Unexpected testValidatingLEM3() exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
+        }
     }
 
     /**
      * Scenario being tested:
-     *   4) An invalid ValidationFactory with a mode of CALLBACK will
-     *      cause an exception
+     *   4) Verify a passed in ValidatorFactory is used
      */
-    //@AllowFailure(message="This will fail until OPENJPA-1111 is resolved.")
-    public void XXXtestValidatingLEM4() {
-        getLog().trace("testValidatingLEM4() - required invalid factory");
-        OpenJPAEntityManagerFactory emf = null;
-
+    public void testValidatingLEM4() {
+        getLog().trace("testValidatingLEM4() - provided ValidatorFactory");
+        // create a default factory to pass in
+        ValidatorFactory factory = null;
+        try {
+            factory = Validation.buildDefaultValidatorFactory();
+        } catch (javax.validation.ValidationException e) {
+            // no validation providers found
+        }
+        assertNotNull(factory);
         // create the Map to test overrides
-        //   Just use current class object, as we have no provider to test with
         Map<String,Object> props = new HashMap<String,Object>();
-        props.put("javax.persistence.validation.factory",
-            this.getClass());
-        props.put("javax.persistence.validation.mode",
-            String.valueOf(ValidationMode.CALLBACK));
-
-        try {
-            // create our EMF
-            emf = OpenJPAPersistence.createEntityManagerFactory(
-                "simple-callback-mode",
+        props.put("javax.persistence.validation.factory", factory);
+        // create our EMF w/ props
+        OpenJPAEntityManagerFactory emf = OpenJPAPersistence
+            .createEntityManagerFactory(
+                "simple-auto-mode",
                 "org/apache/openjpa/integration/validation/persistence.xml",
                 props);
-            assertNotNull(emf);
-            // verify validation mode
-            OpenJPAConfiguration conf = emf.getConfiguration();
+        assertNotNull(emf);
+        // create EM
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            // verify expected validation config items
+            OpenJPAConfiguration conf = em.getConfiguration();
             assertNotNull(conf);
-            assertEquals("Validation mode", 
-                String.valueOf(ValidationMode.CALLBACK),
-                conf.getValidationMode());
-            fail("Expected an exception when ValidationMode=CALLBACK and " +
-                "an invalid ValidatorFactory is provided.");
+            assertTrue("ValidationMode",
+                conf.getValidationMode().equalsIgnoreCase("AUTO"));
+            Class<?> lem = conf.getLifecycleEventManagerInstance().getClass();
+            assertNotNull(lem);
+            assertTrue("Expected a ValidatingLifecycleEventManager instance", 
+                ValidatingLifecycleEventManager.class.isAssignableFrom(lem));
+            // verify factory matches
+            assertEquals("Expected same ValidatorFactory instance",
+                factory, conf.getValidationFactoryInstance());
         } catch (Exception e) {
-            // expected
-            getLog().trace("testValidatingLEM4() - caught expected " +
-                "exception", e);
+            fail("Unexpected testValidatingLEM4() exception = " + e);
+        } finally {
+            if ((em != null) && em.isOpen()) {
+                em.close();
+            }
         }
     }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfiguration.java Tue Jun 16 20:11:45 2009
@@ -1687,17 +1687,40 @@
      */
     public void setValidationFactory(Object factory);
     
-   /**
-     * Gets the lifecycle event manager instance
+    /**
+     * The Validator provided by the container or created by the runtime.
+     * Defaults to <code>null</code>.
+     *
+     * @since 2.0.0
+     */
+    public Object getValidatorInstance();
+
+    /**
+     * Set the container or application provided Validator instance.
+     * If not set, defaults to <code>null</code>.
+     *
+     * @since 2.0.0
+     */
+    public void setValidatorInstance(Object val);
+    
+    /**
+     * Gets the lifecycle event manager instance classname.
+     * 
+     * @since 2.0.0
+     */
+    public String getLifecycleEventManager();
+
+    /**
+     * Gets the lifecycle event manager instance.
      * 
      * @since 2.0.0
      */
     public LifecycleEventManager getLifecycleEventManagerInstance();
 
     /**
-     * Sets the lifecycle event manager instance.
+     * Sets the lifecycle event manager instance classname.
      * 
      * @since 2.0.0
      */
-    public void setLifecycleEventManager(LifecycleEventManager eventMgr);
+    public void setLifecycleEventManager(String eventMgr);    
 }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/conf/OpenJPAConfigurationImpl.java Tue Jun 16 20:11:45 2009
@@ -67,6 +67,7 @@
 import org.apache.openjpa.util.ImplHelper;
 import org.apache.openjpa.util.ProxyManager;
 import org.apache.openjpa.util.StoreFacadeTypeRegistry;
+import org.apache.openjpa.validation.ValidatingLifecycleEventManager;
 
 /**
  * Implementation of the {@link OpenJPAConfiguration} interface.
@@ -156,6 +157,7 @@
     public ObjectValue specification;
     public StringValue validationMode;
     public ObjectValue validationFactory;
+    public ObjectValue validator;
     public ObjectValue lifecycleEventManager;
 
     
@@ -569,7 +571,18 @@
             "getValidationFactoryInstance");
         validationFactory.setDynamic(true);
 
-        lifecycleEventManager = addObject("LifecycleEventManager");
+        validator = addObject("Validator");
+        validator.setInstantiatingGetter("getValidatorInstance");
+        validator.setDynamic(true);
+
+        lifecycleEventManager = addPlugin("LifecycleEventManager", true);
+        aliases = new String[] {
+            "default", LifecycleEventManager.class.getName(),
+            "validating", ValidatingLifecycleEventManager.class.getName(),
+        };
+        lifecycleEventManager.setAliases(aliases);
+        lifecycleEventManager.setDefault(aliases[0]);
+        lifecycleEventManager.setString(aliases[0]);
         lifecycleEventManager.setInstantiatingGetter(
                 "getLifecycleEventManagerInstance");
 
@@ -1609,16 +1622,31 @@
     public void setValidationFactory(Object factory) {
         validationFactory.set(factory);                            
     }
-    
+
+    public Object getValidatorInstance() {
+        return validator.get();
+    }
+
+    public void setValidatorInstance(Object val) {
+        validator.set(val);                            
+    }
+
+    public String getLifecycleEventManager() {
+        return lifecycleEventManager.getString();
+    }
+
     public LifecycleEventManager getLifecycleEventManagerInstance() {
-        if (lifecycleEventManager.get() == null) {
-            lifecycleEventManager.set(new LifecycleEventManager());
+        LifecycleEventManager lem = (LifecycleEventManager)
+            lifecycleEventManager.get();
+        if (lem == null) {
+            lem = (LifecycleEventManager)lifecycleEventManager
+                .instantiate(LifecycleEventManager.class, this);
         }
-        return (LifecycleEventManager)lifecycleEventManager.get();
+        return lem;
     }
 
-    public void setLifecycleEventManager(LifecycleEventManager eventMgr) {
-        lifecycleEventManager.set(eventMgr);
+    public void setLifecycleEventManager(String lem) {
+        lifecycleEventManager.setString(lem);
     }
 
     public boolean getDynamicEnhancementAgent() {

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/validation/ValidatingLifecycleEventManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/validation/ValidatingLifecycleEventManager.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/validation/ValidatingLifecycleEventManager.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/validation/ValidatingLifecycleEventManager.java Tue Jun 16 20:11:45 2009
@@ -18,8 +18,11 @@
  */
 package org.apache.openjpa.validation;
 
+import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.event.LifecycleEvent;
 import org.apache.openjpa.event.LifecycleEventManager;
+import org.apache.openjpa.lib.conf.Configurable;
+import org.apache.openjpa.lib.conf.Configuration;
 import org.apache.openjpa.meta.ClassMetaData;
 
 /**
@@ -29,8 +32,10 @@
  * 
  */
 @SuppressWarnings("serial")
-public class ValidatingLifecycleEventManager extends LifecycleEventManager {
+public class ValidatingLifecycleEventManager extends LifecycleEventManager
+    implements Configurable {
 
+    private OpenJPAConfiguration _conf = null;
     private Validator _validator = null;
 
     /**
@@ -38,11 +43,33 @@
      * no validation will occur.
      * @param validator
      */
-    public ValidatingLifecycleEventManager(Validator validator) {
+    public ValidatingLifecycleEventManager() {
         super();
-        _validator = validator;        
     }
-        
+
+    /* (non-Javadoc)
+     * @see org.apache.openjpa.lib.conf.Configurable#endConfiguration()
+     */
+    public void endConfiguration() {
+        _validator = (Validator)_conf.getValidatorInstance();
+    }
+   
+    /* (non-Javadoc)
+     * @see org.apache.openjpa.lib.conf.Configurable#setConfiguration(
+     *      org.apache.openjpa.lib.conf.Configuration)
+     */
+    public void setConfiguration(Configuration conf) {
+        if (conf instanceof OpenJPAConfiguration) {
+            _conf = (OpenJPAConfiguration)conf;
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.openjpa.lib.conf.Configurable#startConfiguration()
+     */
+    public void startConfiguration() {
+    }
+
     @Override
     public boolean hasUpdateListeners(Object source, ClassMetaData meta) {
         if (_validator == null) {            

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationFactory.java Tue Jun 16 20:11:45 2009
@@ -22,6 +22,7 @@
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.PersistenceException;
 import org.apache.openjpa.persistence.query.SimpleEntity;
 import org.apache.openjpa.persistence.test.AllowFailure;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
@@ -125,9 +126,8 @@
     /**
      * Scenario being tested:
      *   4) An invalid ValidationFactory with a mode of CALLBACK will
-     *      cause an exception
+     *      cause a fatal exception
      */
-    @AllowFailure(message="This will fail until OPENJPA-1111 is resolved.")
     public void testValidationFactory4() {
         getLog().trace("testValidationFactory4() - required invalid factory");
         OpenJPAEntityManagerFactory emf = null;
@@ -155,10 +155,10 @@
                 conf.getValidationMode());
             fail("Expected an exception when ValidationMode=CALLBACK and " +
                 "an invalid ValidatorFactory is provided.");
-        } catch (Exception e) {
+        } catch (PersistenceException e) {
             // expected
             getLog().trace("testValidationFactory4() - caught expected " +
-                "exception", e);
+                "PersistenceException", e);
         }
     }
 

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/validation/TestValidationMode.java Tue Jun 16 20:11:45 2009
@@ -22,6 +22,7 @@
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
 import org.apache.openjpa.persistence.OpenJPAPersistence;
+import org.apache.openjpa.persistence.PersistenceException;
 import org.apache.openjpa.persistence.query.SimpleEntity;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
@@ -112,23 +113,31 @@
     /**
      * Scenario being tested:
      *   4) Validation mode of CALLBACK in persistence.xml overrides default
+     *   and causes an exception when neither the Validation APIs nor
+     *   a provider are available
      */
     public void testValidationMode4() {
         getLog().trace("testValidationMode4() - persistence.xml overrides " +
             "Default");
         OpenJPAEntityManagerFactory emf = null;
 
-        // create our EMF
-        emf = OpenJPAPersistence.createEntityManagerFactory(
-            "simple-callback-mode",
-            "org/apache/openjpa/persistence/validation/persistence.xml");
-        assertNotNull(emf);
-        // verify validation mode
-        OpenJPAConfiguration conf = emf.getConfiguration();
-        assertNotNull(conf);
-        assertEquals("Validation mode", 
-            String.valueOf(ValidationMode.CALLBACK),
-            conf.getValidationMode());
+        try {
+            // create our EMF
+            emf = OpenJPAPersistence.createEntityManagerFactory(
+                "simple-callback-mode",
+                "org/apache/openjpa/persistence/validation/persistence.xml");
+            assertNotNull(emf);
+            // verify validation mode
+            OpenJPAConfiguration conf = emf.getConfiguration();
+            assertNotNull(conf);
+            assertEquals("Validation mode", 
+                String.valueOf(ValidationMode.CALLBACK),
+                conf.getValidationMode());
+        } catch (PersistenceException e) {
+            // expected when no Validation APIs or provider are available
+            getLog().trace("testValidationMode4() - caught expected " +
+                "exception", e);
+        }
     }
 
     /**

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProductDerivation.java Tue Jun 16 20:11:45 2009
@@ -180,12 +180,7 @@
             Compatibility compatibility = conf.getCompatibilityInstance();
             compatibility.setFlushBeforeDetach(true);
             compatibility.setCopyOnDetach(true);
-        } else {
-            // see if we can/need to create a ValidatingLifecycleEventManager
-            // Note: BrokerImpl uses the instantiating getter, which will create
-            //   a normal LifecycleEventManager if a VLEM wasn't created.
-            conf.setLifecycleEventManager(createVLEM(conf));
-        }
+        } 
         return true;
     }
 
@@ -542,82 +537,6 @@
     }
     
     /**
-     * Try creating a ValidatingLifecycleEventManager based on the configured
-     * validation mode. This allows the Bean Validation Spec API to be an
-     * optional runtime dependency.
-     * @param conf
-     * @return ValidatingLifecycleEventManager if appropriate
-     * @throws ValidationException - a WrappedException for fatal errors
-     */
-    /**
-     * @param conf
-     * @throws ValidationException
-     */
-    private LifecycleEventManager createVLEM(OpenJPAConfigurationImpl conf) 
-        throws ValidationException {
-        LifecycleEventManager lem = null;
-        // only try creating a ValidatingLifecycleEventManager if needed
-        if (!String.valueOf(ValidationMode.NONE).equalsIgnoreCase(
-            conf.getValidationMode())) {
-            // we'll use this in the exception handlers
-            boolean bValRequired = String.valueOf(ValidationMode.CALLBACK)
-                .equalsIgnoreCase(conf.getValidationMode());
-            // a little trace message
-            conf.getConfigurationLog().trace("Will try to create a " +
-                "ValidatingLifecycleEventManager based on the ValidationMode=" +
-                conf.getValidationMode());
-            // see if the javax.validation spec api is available
-            try {
-                @SuppressWarnings("unused")
-                Class<?> c = Class.forName(
-                    "javax.validation.ValidationException");
-            } catch (ClassNotFoundException e) {
-                if (bValRequired) {
-                    // fatal error - ValidationMode requires a validator
-                    conf.getConfigurationLog().error(
-                        _loc.get("vlem-creation-error"), e);
-                    // rethrow as a WrappedException
-                    throw new ValidationException(new RuntimeException(e));
-                } else {
-                    // no optional validation provider, so just trace output
-                    conf.getConfigurationLog().trace(
-                        _loc.get("vlem-creation-warn", "No available Bean " +
-                        "Validation APIs"));
-                    return lem;
-                }
-            }
-            // we have the javax.validation APIs
-            try {
-                // try loading a validation provider
-                Validator val = new ValidatorImpl(
-                    conf.getValidationFactoryInstance(),
-                    conf.getValidationMode());
-                // we have a Validator, so try to create a VLEM
-                lem = new ValidatingLifecycleEventManager(val);
-            } catch (RuntimeException e) {
-                if (bValRequired) {
-                    // fatal error - ValidationMode requires a validator
-                    conf.getConfigurationLog().error(
-                        _loc.get("vlem-creation-error"), e);
-                    // rethrow as a WrappedException
-                    throw new ValidationException(e);
-                } else {
-                    // unexpected, but validation is optional,
-                    // so just log it as a warning
-                    conf.getConfigurationLog().warn(
-                        _loc.get("vlem-creation-warn", e.getMessage()));
-                }
-            }
-        } else {
-            // a little trace message
-            conf.getConfigurationLog().trace("Not creating a " +
-                "ValidatingLifecycleEventManager based on the ValidationMode=" +
-                conf.getValidationMode());
-        }
-        return lem;
-    }
-
-    /**
      * Custom configuration provider.   
      */
     public static class ConfigurationProviderImpl

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PersistenceProviderImpl.java Tue Jun 16 20:11:45 2009
@@ -42,7 +42,9 @@
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.meta.MetaDataModes;
 import org.apache.openjpa.meta.MetaDataRepository;
+import org.apache.openjpa.persistence.validation.ValidationUtils;
 import org.apache.openjpa.util.ClassResolver;
+import org.apache.openjpa.validation.ValidationException;
 
 
 /**
@@ -89,6 +91,8 @@
                 ;//return null;
             }
             loadAgent(_log, conf);
+            // Create appropriate LifecycleEventManager
+            loadValidator(_log, conf);
             return JPAFacadeHelper.toEntityManagerFactory(factory);
         } catch (Exception e) {
             throw PersistenceExceptions.toPersistenceException(e);
@@ -163,6 +167,12 @@
                         _loc.get("transformer-registration-error", pui));
                 }
             }
+            
+            // Create appropriate LifecycleEventManager
+            OpenJPAConfiguration conf = factory.getConfiguration();
+            _log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);
+            loadValidator(_log, conf);
+
             return JPAFacadeHelper.toEntityManagerFactory(factory);
         } catch (Exception e) {
             throw PersistenceExceptions.toPersistenceException(e);
@@ -235,6 +245,7 @@
         throw new UnsupportedOperationException(
         "JPA 2.0 - Method not yet implemented");
     }
+    
     /**
      * This private worker method will attempt load the PCEnhancerAgent.
      */
@@ -246,4 +257,19 @@
             }
         }
     }
+    
+    /**
+     * This private worker method will attempt to setup the proper
+     * LifecycleEventManager type based on if the javax.validation APIs are
+     * available and a ValidatorImpl is required by the configuration.
+     * @param log
+     * @param conf
+     * @throws if validation setup failed and was required by the config
+     */
+    private void loadValidator(Log log, OpenJPAConfiguration conf) {
+        if ((ValidationUtils.setupValidation(conf) == true) &&
+                _log.isInfoEnabled()) {
+            _log.info(_loc.get("vlem-creation-info"));
+        }
+    }
 }

Added: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/ValidationUtils.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/ValidationUtils.java?rev=785366&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/ValidationUtils.java (added)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/ValidationUtils.java Tue Jun 16 20:11:45 2009
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.validation;
+
+import javax.persistence.ValidationMode;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.lib.log.Log;
+import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.validation.ValidationException;
+
+
+/**
+ * Validation helper routines and wrappers to remove runtime dependencies
+ * on the Bean Valdiation APIs or a implementation.
+ * 
+ * Note:  This class should have no direct dependency on the javax.validation
+ *        packages, which should only occur in the ValidatorImpl class.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ValidationUtils {
+
+    private static final Localizer _loc = Localizer.forPackage(
+        ValidationUtils.class);
+
+    /**
+     * Setup Validation support by determining if the javax.validation APIs
+     * are available and then create a Validator if required by the
+     * provided configuration.
+     * @param conf
+     * @return true if a Validator was created, false otherwise.
+     * @throws If a Validator was required but could not be created.
+     */
+    public static boolean setupValidation(OpenJPAConfiguration conf) {
+        Log log = conf.getConfigurationLog();
+        boolean brc = false;
+        
+        // only try creating a Validator for JPA2 and if not mode==NONE
+        if (conf.getSpecificationInstance().getVersion() < 2) {
+            if (log.isTraceEnabled()) {
+                log.trace("Not creating a ValidatorImpl because " +
+                    "this app is using the JPA 1.0 Spec");
+            }
+        }
+        else if (!(String.valueOf(ValidationMode.NONE)
+                .equalsIgnoreCase(conf.getValidationMode()))) {
+            // we'll use this in the exception handlers
+            boolean bValRequired = String.valueOf(ValidationMode.CALLBACK)
+                .equalsIgnoreCase(conf.getValidationMode());
+            try {
+                // see if the javax.validation spec api is available
+                if (log.isTraceEnabled()) {
+                    log.trace("Trying to load javax.validation APIs " +
+                        "based on the ValidationMode="
+                        + conf.getValidationMode());
+                }
+                @SuppressWarnings("unused")
+                Class<?> c = Class.forName(
+                    "javax.validation.ValidationException");
+            } catch (ClassNotFoundException e) {
+                if (bValRequired) {
+                    // fatal error - ValidationMode requires a validator
+                    log.error(_loc.get("vlem-creation-error"), e);
+                    // rethrow as a WrappedException
+                    throw new ValidationException(new RuntimeException(e));
+                } else {
+                    // no optional validation provider, so just trace output
+                    if (log.isTraceEnabled()) {
+                        log.trace(_loc.get("vlem-creation-warn",
+                            "No available javax.validation APIs"));
+                    }
+                    return brc;
+                }
+            }
+            // we have the javax.validation APIs
+            try {
+                // try loading a validation provider
+                ValidatorImpl validator = new ValidatorImpl(
+                    conf.getValidationFactoryInstance(),
+                    conf.getValidationMode());
+                // set the Validator into the config
+                conf.setValidatorInstance(validator);
+                // update the LifecycleEventManager plugin to use it
+                conf.setLifecycleEventManager("validating");
+                // all done, so return good rc if anyone cares
+                brc = true;
+            } catch (RuntimeException e) {
+                if (bValRequired) {
+                    // fatal error - ValidationMode requires a validator
+                    log.error(_loc.get("vlem-creation-error"), e);
+                    // rethrow as a WrappedException
+                    throw new ValidationException(e);
+                } else {
+                    // unexpected, but validation is optional,
+                    // so just log it as a warning
+                    log.warn(_loc.get("vlem-creation-warn", e.getMessage()));
+                    return brc;
+                }
+            }
+        } else {
+            if (log.isTraceEnabled()) {
+                log.trace("Not creating a ValidatorImpl because " +
+                    "ValidationMode=" + conf.getValidationMode());
+            }
+        }
+        return brc;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/ValidationUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties?rev=785366&r1=785365&r2=785366&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties (original)
+++ openjpa/trunk/openjpa-persistence/src/main/resources/org/apache/openjpa/persistence/localizer.properties Tue Jun 16 20:11:45 2009
@@ -200,4 +200,5 @@
     Reason returned: "{0}"
 vlem-creation-error: A fatal error occurred while trying to \
     create the required validation provider.
+vlem-creation-info: OpenJPA dynamically loaded a validation provider.