You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by ro...@apache.org on 2010/05/10 15:50:57 UTC

svn commit: r942737 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/ConstraintValidation.java test/java/org/apache/bval/jsr303/BeanDescriptorTest.java

Author: romanstumm
Date: Mon May 10 13:50:57 2010
New Revision: 942737

URL: http://svn.apache.org/viewvc?rev=942737&view=rev
Log:
BVAL-38 merged additional fixes and test - from Carlos Varla

Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/BeanDescriptorTest.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java?rev=942737&r1=942736&r2=942737&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java (original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java Mon May 10 13:50:57 2010
@@ -85,10 +85,12 @@ public class ConstraintValidation<T exte
 
     void setGroups(Set<Class<?>> groups) {
         this.groups = groups;
+        this.attributes.put("groups", groups.toArray(new Class[groups.size()]));
     }
 
     void setPayload(Set<Class<? extends Payload>> payload) {
         this.payload = payload;
+        this.attributes.put("payload", groups.toArray(new Class[groups.size()]));
     }
 
     public boolean isReportAsSingleViolation() {

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/BeanDescriptorTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/BeanDescriptorTest.java?rev=942737&r1=942736&r2=942737&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/BeanDescriptorTest.java (original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/BeanDescriptorTest.java Mon May 10 13:50:57 2010
@@ -21,15 +21,19 @@ package org.apache.bval.jsr303;
 import junit.framework.Assert;
 import junit.framework.TestCase;
 
-import javax.validation.Validation;
-import javax.validation.Validator;
-import javax.validation.ValidatorFactory;
+import javax.validation.*;
 import javax.validation.constraints.NotNull;
 import javax.validation.metadata.BeanDescriptor;
 import javax.validation.metadata.ConstraintDescriptor;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
 import java.util.Locale;
 import java.util.Set;
 
+import static java.lang.annotation.ElementType.*;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
 /**
  * Tests the implementation of {@link BeanDescriptor} and its dependent
  * interfaces.
@@ -64,10 +68,54 @@ public class BeanDescriptorTest extends 
         Assert.assertTrue("payload attribute not present", nameDescriptor.getAttributes().containsKey("payload"));
         Assert.assertTrue("message attribute not present", nameDescriptor.getAttributes().containsKey("message"));
     }
+
+    /**
+     * Check that the groups() attribute value has the correct value when
+     * inheriting groups.
+     */
+    public void testCorrectValueForInheritedGroupsAttribute() {
+        Validator validator = getValidator();
+        
+        Set<ConstraintDescriptor<?>> passwordDescriptors = validator.getConstraintsForClass(Account.class).getConstraintsForProperty("password").getConstraintDescriptors();
+        Assert.assertEquals("Incorrect number of descriptors", 1, passwordDescriptors.size());
+        ConstraintDescriptor<?> passwordDescriptor = passwordDescriptors.iterator().next();
+        Assert.assertEquals("Incorrect number of composing constraints", 1, passwordDescriptor.getComposingConstraints().size());
+        ConstraintDescriptor<?> notNullDescriptor = passwordDescriptor.getComposingConstraints().iterator().next();
+        
+        // Check that the groups value containts Group1.class
+        Class[] notNullGroups = (Class[]) notNullDescriptor.getAttributes().get("groups");
+        boolean found = false;
+        for ( Class group : notNullGroups ) {
+            if ( group == Group1.class ) {
+                found = true;
+                break;
+            }
+        }
+        Assert.assertTrue("Group1 not present in groups attribute", found);
+    }
     
     public static class Form {
         @NotNull
         public String name;
     }
     
+    public static class Account {
+        @Password(groups={Group1.class})
+        public String password;
+    }
+    
+    @NotNull(groups={})
+    @Constraint(validatedBy = {})
+    @Documented
+    @Target({ METHOD, FIELD, TYPE })
+    @Retention(RUNTIME)
+    public static @interface Password {
+        String message() default "Invalid password";
+        Class<?>[] groups() default { };
+        Class<? extends Payload>[] payload() default {};
+    }
+    
+    public static interface Group1 {
+    }
+    
 }