openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jrba...@apache.org
Subject svn commit: r1082259 - in /openjpa/trunk: openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/ openjpa-kernel/src/main/java/...
Date Wed, 16 Mar 2011 18:48:11 GMT
Author: jrbauer
Date: Wed Mar 16 18:48:11 2011
New Revision: 1082259

URL: http://svn.apache.org/viewvc?rev=1082259&view=rev
Log:
OPENJPA-1787 Fixed VLEM hasUpdateListeners & hasPersistListeners methods and added additional
validation test cases.

Modified:
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java
    openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/validation/ValidatingLifecycleEventManager.java

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java?rev=1082259&r1=1082258&r2=1082259&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestValidationGroups.java
Wed Mar 16 18:48:11 2011
@@ -43,6 +43,12 @@ import org.apache.openjpa.persistence.te
  *     the default group.
  * 1d) Verify validation for constraints using non-default validation groups 
  *     does not occur.
+ * 1e) PrePersist does not validate with no validation group defined.
+ * 1f) PreUpdate does not validate when no validation group defined.
+ * 1g) PreUpdate only called when a pre-update validation group defined 
+ *     (ie. per-persist and pre-remove are disabled).
+ * 1h) PrePersist only called when pre-persist validation group defined
+ *     (ie. per-persist and pre-remove are disabled).
  *    
  * Verify validation occurs when specific validation groups are specified:
  * 2a) Specify a non-default group for all lifecycle events.
@@ -105,6 +111,182 @@ public class TestValidationGroups extend
         verifySpecifiedDefaultPreRemove(false);
     }
     
+    /** 
+     * 1e) PrePersist does not validate with no validation group defined.
+     */
+    public void testPersistNoValidationGroup() {
+        OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI) 
+        OpenJPAPersistence.createEntityManagerFactory(
+                "no-pre-persist-default-validation-group",
+                "org/apache/openjpa/integration/validation/persistence.xml");
+        assertNotNull(emf);
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            DefGrpEntity dge = new DefGrpEntity();
+            dge.setDgName(null);  // If default group was enabled for pre-persist, this would
cause a CVE.
+            try {
+                em.getTransaction().begin();
+                em.persist(dge);
+                em.getTransaction().commit();
+            } catch (ConstraintViolationException e) {
+                fail("A ConstraintViolationException should not have been thrown " +
+                "on pre-persist");
+            } finally {
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+            }
+        } finally {
+            closeEM(em);
+            closeEMF(emf);
+        }
+    }
+    
+    /**
+     *  1f) PreUpdate does not validate when no validation group defined.
+     */
+    public void testUpdateNoValidationGroup() {
+        OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI) 
+        OpenJPAPersistence.createEntityManagerFactory(
+                "no-pre-update-default-validation-group",
+                "org/apache/openjpa/integration/validation/persistence.xml");
+        assertNotNull(emf);
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            DefGrpEntity dge = new DefGrpEntity();
+            dge.setDgName("NotNull");
+            try {
+                em.getTransaction().begin();
+                em.persist(dge);
+                em.getTransaction().commit();
+            } catch (ConstraintViolationException e) {
+                fail("A ConstraintViolationException should not have been thrown " +
+                "on pre-persist");
+            } 
+            try {
+                em.getTransaction().begin();
+                dge.setDgName(null);
+                em.getTransaction().commit();
+            } catch (ConstraintViolationException e) {
+                fail("A ConstraintViolationException should not have been thrown " +
+                "on pre-update");
+            } finally {
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+            }
+        } finally {
+            closeEM(em);
+            closeEMF(emf);
+        }
+    }
+
+    /** 
+     * 1g) PreUpdate only called when a pre-update validation group defined 
+     *     (ie. per-persist and pre-remove are disabled).
+     */
+    public void testUpdateOnlyValidationGroup() {
+        OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI) 
+        OpenJPAPersistence.createEntityManagerFactory(
+                "no-pre-persist-default-validation-group",
+                "org/apache/openjpa/integration/validation/persistence.xml");
+        assertNotNull(emf);
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            DefGrpEntity dge = new DefGrpEntity();
+            dge.setDgName(null);  // If default group enabled for pre-persist, this would
cause a CVE.
+            try {
+                em.getTransaction().begin();
+                em.persist(dge);
+                em.getTransaction().commit();
+            } catch (ConstraintViolationException e) {
+                fail("A ConstraintViolationException should not have been thrown " +
+                "on pre-persist");
+            } 
+            try {
+                em.getTransaction().begin();
+                dge.setDgName("NotNull");
+                dge.setDgName(null);
+                em.getTransaction().commit();
+                fail("A ConstraintViolationException should have been thrown " +
+                "on pre-update");
+            } catch (ConstraintViolationException e) {
+                // expected
+            } 
+            finally {
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+            }
+        } finally {
+            closeEM(em);
+            closeEMF(emf);
+        }
+    }
+    /**
+     *  1h) PrePersist only called when pre-persist validation group defined
+     *      (ie. per-persist and pre-remove are disabled).
+     */
+    public void testPersistOnlyValidationGroup() {
+        OpenJPAEntityManagerFactorySPI emf = (OpenJPAEntityManagerFactorySPI) 
+        OpenJPAPersistence.createEntityManagerFactory(
+                "no-pre-update-default-validation-group",
+                "org/apache/openjpa/integration/validation/persistence.xml");
+        assertNotNull(emf);
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        assertNotNull(em);
+        try {
+            DefGrpEntity dge = new DefGrpEntity();
+            dge.setDgName(null);
+            try {
+                em.getTransaction().begin();
+                em.persist(dge);
+                em.getTransaction().commit();
+                fail("A ConstraintViolationException should have been thrown " +
+                "on pre-persist");
+            } catch (ConstraintViolationException e) {
+                // Expected
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+            }
+            // Fix the entity, persist with no CVE
+            try {
+                em.getTransaction().begin();
+                dge.setDgName("NotNull");
+                em.getTransaction().commit();
+            } catch (Exception e) {
+                fail("An Exception should not have been thrown " +
+                "on update");
+            }
+            // Update the entity with null value, should not case a CVE
+            try {
+                em.getTransaction().begin();
+                dge.setDgName(null);
+                em.getTransaction().commit();
+            } catch (ConstraintViolationException e) {
+                fail("A ConstraintViolationException should not have been thrown " +
+                "on pre-update");
+            } 
+            finally {
+                if (em.getTransaction().isActive()) {
+                    em.getTransaction().rollback();
+                }
+            }
+            
+        } finally {
+            closeEM(em);
+            closeEMF(emf);
+        }
+    }
+    
     /**
      * 2a) verify non-default validation group for all lifecycle events on
      * commit.  default validation group constraints should not validate

Modified: openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml?rev=1082259&r1=1082258&r2=1082259&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/resources/org/apache/openjpa/integration/validation/persistence.xml
Wed Mar 16 18:48:11 2011
@@ -100,6 +100,28 @@
         </properties>        
     </persistence-unit>
 
+    <persistence-unit name="no-pre-persist-default-validation-group">
+        <class>org.apache.openjpa.integration.validation.DefGrpEntity</class>
+        <validation-mode>CALLBACK</validation-mode>
+        <properties>
+            <property name="openjpa.jdbc.SynchronizeMappings" 
+                value="buildSchema"/>
+            <property name="javax.persistence.validation.group.pre-persist"
+                value=""/>
+        </properties>
+    </persistence-unit>
+
+    <persistence-unit name="no-pre-update-default-validation-group">
+        <class>org.apache.openjpa.integration.validation.DefGrpEntity</class>
+        <validation-mode>CALLBACK</validation-mode>
+        <properties>
+            <property name="openjpa.jdbc.SynchronizeMappings" 
+                value="buildSchema"/>
+            <property name="javax.persistence.validation.group.pre-update"
+                value=""/>
+        </properties>
+    </persistence-unit>
+
     <persistence-unit name="non-default-validation-group">
         <class>org.apache.openjpa.integration.validation.NonDefGrpEntity</class>
         <validation-mode>CALLBACK</validation-mode>

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=1082259&r1=1082258&r2=1082259&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
Wed Mar 16 18:48:11 2011
@@ -75,7 +75,7 @@ public class ValidatingLifecycleEventMan
         if (_validator == null) {            
             return super.hasUpdateListeners(source, meta);
         }
-        return _validator.validating(source, LifecycleEvent.BEFORE_PERSIST) ||
+        return _validator.validating(source, LifecycleEvent.BEFORE_UPDATE) ||
             super.hasUpdateListeners(source, meta);
     }
 
@@ -84,7 +84,7 @@ public class ValidatingLifecycleEventMan
         if (_validator == null) {            
             return super.hasPersistListeners(source, meta);
         }
-        return _validator.validating(source, LifecycleEvent.BEFORE_UPDATE) ||
+        return _validator.validating(source, LifecycleEvent.BEFORE_PERSIST) ||
             super.hasPersistListeners(source, meta);        
     }
 



Mime
View raw message