bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r943177 - in /incubator/bval/trunk: ./ bval-core/src/main/java/org/apache/bval/model/ bval-jsr303/src/main/java/org/apache/bval/jsr303/ bval-jsr303/src/test/java/org/apache/bval/jsr303/
Date Tue, 11 May 2010 17:02:54 GMT
Author: dwoods
Date: Tue May 11 17:02:54 2010
New Revision: 943177

URL: http://svn.apache.org/viewvc?rev=943177&view=rev
Log:
BVAL-39 Implicit groups should be correctly set in constraint descriptors.  Contributed by
Carlos Vara.

Modified:
    incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidation.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/BeanDescriptorTest.java
    incubator/bval/trunk/pom.xml

Modified: incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java?rev=943177&r1=943176&r2=943177&view=diff
==============================================================================
--- incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java (original)
+++ incubator/bval/trunk/bval-core/src/main/java/org/apache/bval/model/MetaProperty.java Tue
May 11 17:02:54 2010
@@ -34,6 +34,7 @@ public class MetaProperty extends Featur
 
     private Type type;
     private MetaBean metaBean;
+    private MetaBean parentMetaBean;
 
     public MetaProperty() {
     }
@@ -46,6 +47,14 @@ public class MetaProperty extends Featur
     public void setMetaBean(MetaBean metaBean) {
         this.metaBean = metaBean;
     }
+    
+    public MetaBean getParentMetaBean() {
+        return parentMetaBean;
+    }
+
+    public void setParentMetaBean(MetaBean parentMetaBean) {
+        this.parentMetaBean = parentMetaBean;
+    }    
 
     public boolean isRelationship() {
         return metaBean != null;

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java?rev=943177&r1=943176&r2=943177&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/AnnotationConstraintBuilder.java
Tue May 11 17:02:54 2010
@@ -87,31 +87,6 @@ final class AnnotationConstraintBuilder 
                 }
             });
         }
-        try {
-            /*
-           spec: Invalid constraint definitions causes are multiple but include missing or
illegal message
-           or groups elements
-            */
-            if (constraintValidation.getGroups() == null) {
-                throw new ConstraintDefinitionException(
-                      constraintValidation.getAnnotation().annotationType()
-                            .getName() + " does not contain a groups parameter.");
-            }
-            if (constraintValidation.getMessageTemplate() == null) {
-                throw new ConstraintDefinitionException(
-                      constraintValidation.getAnnotation().annotationType()
-                            .getName() + " does not contain a message parameter.");
-            }
-            if (constraintValidation.getPayload() == null) {
-                throw new ConstraintDefinitionException(
-                      constraintValidation.getAnnotation().annotationType()
-                            .getName() + " does not contain a payload parameter.");
-            }
-        } catch (ConstraintDefinitionException e) {
-            throw e;
-        } catch (Exception e) {
-            throw new IllegalArgumentException(e); // execution never reaches this point
-        }
     }
 
     private void buildGroups(Method method)

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=943177&r1=943176&r2=943177&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
Tue May 11 17:02:54 2010
@@ -236,11 +236,7 @@ public class ConstraintValidation<T exte
     }
 
     protected boolean isMemberOf(Class<?> reqGroup) {
-        /**
-         * owner: implicit grouping support:
-         * owner is reqGroup or a superclass/superinterface of reqGroup
-         */
-        return owner.isAssignableFrom(reqGroup) || groups.contains(reqGroup);
+        return groups.contains(reqGroup);
     }
 
     public Class getOwner() {

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java?rev=943177&r1=943176&r2=943177&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
Tue May 11 17:02:54 2010
@@ -190,7 +190,7 @@ public class Jsr303MetaBeanFactory imple
                 if (!factoryContext.getFactory().getAnnotationIgnores()
                       .isIgnoreAnnotations(field)) {
                     if (metaProperty == null) {
-                        metaProperty = createMetaProperty(field.getName(), field.getType());
+                        metaProperty = createMetaProperty(metabean, field.getName(), field.getType());
                         /*if (*/
                         processAnnotations(metaProperty, beanClass, field,
                               new FieldAccess(field),
@@ -218,7 +218,7 @@ public class Jsr303MetaBeanFactory imple
                         // create a property for those methods for which there is not yet
a MetaProperty
                         if (metaProperty == null) {
                             metaProperty =
-                                  createMetaProperty(propName, method.getReturnType());
+                                  createMetaProperty(metabean, propName, method.getReturnType());
                             /*if (*/
                             processAnnotations(metaProperty, beanClass, method,
                                   new MethodAccess(propName, method),
@@ -249,7 +249,7 @@ public class Jsr303MetaBeanFactory imple
                 metaProperty =
                       metabean.getProperty(meta.getAccessStrategy().getPropertyName());
                 if (metaProperty == null) {
-                    metaProperty = createMetaProperty(
+                    metaProperty = createMetaProperty(metabean,
                           meta.getAccessStrategy().getPropertyName(),
                           meta.getAccessStrategy().getJavaType());
                     metabean.putProperty(metaProperty.getName(), metaProperty);
@@ -265,18 +265,19 @@ public class Jsr303MetaBeanFactory imple
             MetaProperty metaProperty = metabean.getProperty(access.getPropertyName());
             if (metaProperty == null) {
                 metaProperty =
-                      createMetaProperty(access.getPropertyName(), access.getJavaType());
+                      createMetaProperty(metabean, access.getPropertyName(), access.getJavaType());
                 metabean.putProperty(metaProperty.getName(), metaProperty);
             }
             processValid(metaProperty, access);
         }
     }
 
-    private MetaProperty createMetaProperty(String propName, Type type) {
+    private MetaProperty createMetaProperty(MetaBean parentMetaBean, String propName, Type
type) {
         MetaProperty metaProperty;
         metaProperty = new MetaProperty();
         metaProperty.setName(propName);
         metaProperty.setType(type);
+        metaProperty.setParentMetaBean(parentMetaBean);
         return metaProperty;
     }
 
@@ -445,6 +446,25 @@ public class Jsr303MetaBeanFactory imple
         }
         final AnnotationConstraintBuilder builder = new AnnotationConstraintBuilder(
               constraintClasses, validator, annotation, owner, access);
+
+        // JSR-303 3.4.4: Add implicit groups
+        if ( prop != null && prop.getParentMetaBean() != null ) {
+            MetaBean parentMetaBean = prop.getParentMetaBean();
+            // If:
+            //  - the owner is an interface
+            //  - the class of the metabean being build is different than the owner
+            //  - and only the Default group is defined
+            // Then: add the owner interface as implicit groups
+            if ( builder.getConstraintValidation().getOwner().isInterface() &&
+                    parentMetaBean.getBeanClass() != builder.getConstraintValidation().getOwner()
&&
+                    builder.getConstraintValidation().getGroups().size() == 1 &&
+                    builder.getConstraintValidation().getGroups().contains(Default.class)
) {
+                Set<Class<?>> groups = builder.getConstraintValidation().getGroups();
+                groups.add(builder.getConstraintValidation().getOwner());
+                builder.getConstraintValidation().setGroups(groups);
+            }
+        }
+
         // If already building a constraint composition tree, ensure that:
         //  - the parent groups are inherited
         //  - the parent payload is inherited
@@ -453,6 +473,7 @@ public class Jsr303MetaBeanFactory imple
             builder.getConstraintValidation().setGroups(avb.getInheritedGroups());
             builder.getConstraintValidation().setPayload(avb.getInheritedPayload());
         }
+        
         // process composed constraints:
         // here are not other superclasses possible, because annotations do not inherit!
         if (processAnnotations(prop, owner, annotation.annotationType(), access,

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=943177&r1=943176&r2=943177&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
Tue May 11 17:02:54 2010
@@ -23,6 +23,7 @@ import junit.framework.TestCase;
 
 import javax.validation.*;
 import javax.validation.constraints.NotNull;
+import javax.validation.groups.Default;
 import javax.validation.metadata.BeanDescriptor;
 import javax.validation.metadata.ConstraintDescriptor;
 import java.lang.annotation.Documented;
@@ -83,9 +84,9 @@ public class BeanDescriptorTest extends 
         ConstraintDescriptor<?> notNullDescriptor = passwordDescriptor.getComposingConstraints().iterator().next();
         
         // Check that the groups value containts Group1.class
-        Class[] notNullGroups = (Class[]) notNullDescriptor.getAttributes().get("groups");
+        Class<?>[] notNullGroups = (Class<?>[]) notNullDescriptor.getAttributes().get("groups");
         boolean found = false;
-        for ( Class group : notNullGroups ) {
+        for ( Class<?> group : notNullGroups ) {
             if ( group == Group1.class ) {
                 found = true;
                 break;
@@ -93,6 +94,39 @@ public class BeanDescriptorTest extends 
         }
         Assert.assertTrue("Group1 not present in groups attribute", found);
     }
+
+    /**
+     * Check that the groups() attribute value contains the correct interface as
+     * implicit group when the constraint is defined in that interface instead
+     * of the queried class.
+     */
+    public void testImplicitGroupIsPresent() {
+        Validator validator = getValidator();
+        
+        Set<ConstraintDescriptor<?>> nameDescriptors = validator.getConstraintsForClass(Woman.class).getConstraintsForProperty("name").getConstraintDescriptors();
+        Assert.assertEquals("Incorrect number of descriptors", 1, nameDescriptors.size());
+        ConstraintDescriptor<?> notNullDescriptor = nameDescriptors.iterator().next();
+        
+        // Check that the groups attribute value contains the implicit group Person and the
Default group
+        Class<?>[] notNullGroups = (Class<?>[]) notNullDescriptor.getAttributes().get("groups");
+        Assert.assertEquals("Incorrect number of groups", 2, notNullGroups.length);
+        Assert.assertTrue("Default group not present", notNullGroups[0].equals(Default.class)
|| notNullGroups[1].equals(Default.class));
+        Assert.assertTrue("Implicit group not present", notNullGroups[0].equals(Person.class)
|| notNullGroups[1].equals(Person.class));
+    }
+    
+    public void testNoImplicitGroupWhenQueryingInterfaceDirectly() {
+        Validator validator = getValidator();
+        
+        Set<ConstraintDescriptor<?>> nameDescriptors = validator.getConstraintsForClass(Person.class).getConstraintsForProperty("name").getConstraintDescriptors();
+        Assert.assertEquals("Incorrect number of descriptors", 1, nameDescriptors.size());
+        ConstraintDescriptor<?> notNullDescriptor = nameDescriptors.iterator().next();
+        
+        // Check that only the default group is present
+        Class<?>[] notNullGroups = (Class<?>[]) notNullDescriptor.getAttributes().get("groups");
+        Assert.assertEquals("Incorrect number of groups", 1, notNullGroups.length);
+        Assert.assertTrue("Default group not present", notNullGroups[0].equals(Default.class));
+    }
+    
     
     public static class Form {
         @NotNull
@@ -118,4 +152,23 @@ public class BeanDescriptorTest extends 
     public static interface Group1 {
     }
     
+    
+    public static class Woman implements Person {
+        
+        private String name;
+
+        public String getName() {
+            return this.name;
+        }
+        
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+    
+    public static interface Person {
+        @NotNull
+        String getName();
+    }
+    
 }

Modified: incubator/bval/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/pom.xml?rev=943177&r1=943176&r2=943177&view=diff
==============================================================================
--- incubator/bval/trunk/pom.xml (original)
+++ incubator/bval/trunk/pom.xml Tue May 11 17:02:54 2010
@@ -168,13 +168,13 @@
             <dependency>
                 <groupId>org.apache.geronimo.specs</groupId>
                 <artifactId>geronimo-validation_1.0_spec</artifactId>
-                <version>1.0</version>
+                <version>1.1</version>
             </dependency>
             <!-- JPA2 spec required for JPA aware TraversableResolver support -->
             <dependency>
                 <groupId>org.apache.geronimo.specs</groupId>
                 <artifactId>geronimo-jpa_2.0_spec</artifactId>
-                <version>1.0</version>
+                <version>1.1</version>
             </dependency>
             <dependency>
                 <groupId>junit</groupId>



Mime
View raw message