incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r943478 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/ClassValidator.java test/java/org/apache/bval/jsr303/groups/CollectionValidationTest.java test/java/org/apache/bval/jsr303/groups/GroupSequenceTest.java
Date Wed, 12 May 2010 13:16:57 GMT
Author: dwoods
Date: Wed May 12 13:16:56 2010
New Revision: 943478

URL: http://svn.apache.org/viewvc?rev=943478&view=rev
Log:
BVAL-42 Stop group sequence validation when at least 1 violation is already reported.  Contributed
by Carlos Vara.

Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/CollectionValidationTest.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/GroupSequenceTest.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java?rev=943478&r1=943477&r2=943478&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
Wed May 12 13:16:56 2010
@@ -101,12 +101,16 @@ public class ClassValidator extends Bean
     public void validateBeanNet(ValidationContext vcontext) {
         GroupValidationContext context = (GroupValidationContext) vcontext;
         List<Group> defaultGroups = expandDefaultGroup(context);
+        final ConstraintValidationListener result = (ConstraintValidationListener) vcontext.getListener();
         if (defaultGroups != null) {
             Group currentGroup = context.getCurrentGroup();
             for (Group each : defaultGroups) {
                 context.setCurrentGroup(each);
                 super.validateBeanNet(context);
-                // continue validation, even if errors already found: if (!result.isEmpty())
+                // Spec 3.4.3 - Stop validation if errors already found
+                if ( !result.isEmpty() ) {
+                    break;
+                }
             }
             context.setCurrentGroup(currentGroup); // restore  (finally{} not required)
         } else {

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/CollectionValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/CollectionValidationTest.java?rev=943478&r1=943477&r2=943478&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/CollectionValidationTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/CollectionValidationTest.java
Wed May 12 13:16:56 2010
@@ -141,15 +141,16 @@ public class CollectionValidationTest ex
         /*
         This, by the way, tests redefined default group sequence behavior
         on non-root-beans (Library.Book)!!
+        So only 1 constraint violation expected
          */
         violations = validator.validate(lib);
         assertEquals(
-              "redefined default group of Book not correctly validated from Library", 3,
+              "redefined default group of Book not correctly validated from Library", 1,
               violations.size());
         assertNotNull(TestUtils.getViolation(violations, "taggedBooks[politics].title"));
-        assertNotNull(
+        assertNull(
               TestUtils.getViolation(violations, "taggedBooks[humor].author.firstName"));
-        assertNotNull(TestUtils.getViolation(violations,
+        assertNull(TestUtils.getViolation(violations,
               "taggedBooks[science].author.addresses[0].city"));
     }
 

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/GroupSequenceTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/GroupSequenceTest.java?rev=943478&r1=943477&r2=943478&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/GroupSequenceTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/groups/GroupSequenceTest.java
Wed May 12 13:16:56 2010
@@ -28,9 +28,11 @@ import org.apache.bval.jsr303.util.TestU
 import org.apache.bval.model.MetaBean;
 
 import javax.validation.ConstraintViolation;
+import javax.validation.GroupSequence;
 import javax.validation.Validation;
 import javax.validation.Validator;
 import javax.validation.ValidatorFactory;
+import javax.validation.constraints.NotNull;
 import java.util.List;
 import java.util.Locale;
 import java.util.Set;
@@ -181,4 +183,33 @@ public class GroupSequenceTest extends T
         assertEquals(1, constraintViolations.size());
     }
 
+    
+    /**
+     * Check that when there is one constraint failure in one of the groups in
+     * a sequence, validation stops.
+     * JSR-303: 3.4.3
+     */
+    public void testValidationStopsWhenFailuresOnGroup() {
+        Validator validator = getValidator();
+        
+        // Validate Dummy with its redefined Default group
+        Set<ConstraintViolation<Dummy>> violations = validator.validate(new Dummy());
+        assertEquals("Only 1 violation expected", 1, violations.size());
+        ConstraintViolation<Dummy> violation = violations.iterator().next();
+        assertEquals("Group1 should be evaluated first", "field1", violation.getPropertyPath().toString());
+    }
+    
+    @GroupSequence({Dummy.Group1.class, Dummy.class})
+    public static class Dummy {
+        
+        @NotNull(groups=Group1.class)
+        public String field1;
+        
+        @NotNull
+        public String field2;
+        
+        interface Group1 {
+        }
+    }
+    
 }



Mime
View raw message