bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r946636 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/ main/java/org/apache/bval/jsr303/extensions/ test/java/org/apache/bval/jsr303/
Date Thu, 20 May 2010 13:58:16 GMT
Author: dwoods
Date: Thu May 20 13:58:15 2010
New Revision: 946636

URL: http://svn.apache.org/viewvc?rev=946636&view=rev
Log:
BVAL-51 Several fixes in ElementDescriptor and subinterfaces impls.  Contributed by Carlos
Vara.

Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/BeanDescriptorImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintFinderImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ElementDescriptorImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/PropertyDescriptorImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ConstructorDescriptorImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodDescriptorImpl.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ParameterDescriptorImpl.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/BeanDescriptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/BeanDescriptorImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/BeanDescriptorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/BeanDescriptorImpl.java
Thu May 20 13:58:15 2010
@@ -37,7 +37,7 @@ public class BeanDescriptorImpl extends 
 
     protected BeanDescriptorImpl(ApacheFactoryContext factoryContext, MetaBean metaBean,
                                  Validation[] validations) {
-        super(metaBean, validations);
+        super(metaBean, metaBean.getBeanClass(), validations);
         this.factoryContext = factoryContext;
     }
 
@@ -90,15 +90,10 @@ public class BeanDescriptorImpl extends 
         if (edesc == null) {
             Class<?> targetClass =
                   prop.getFeature(Features.Property.REF_BEAN_TYPE, prop.getTypeClass());
-            if (targetClass.isPrimitive()) {
-                // optimization: do not create MetaBean for primitives
-                // enhancement: do not create MetaBean for classes without any metadata?
-                edesc = new PropertyDescriptorImpl(targetClass, prop.getValidations());
-            } else {
-                edesc = new PropertyDescriptorImpl(
-                      factoryContext.getMetaBeanFinder().findForClass(targetClass),
+            edesc = new PropertyDescriptorImpl(
+                      metaBean,
+                      prop.getName(),
                       prop.getValidations());
-            }
             edesc.setCascaded((prop.getMetaBean() != null ||
                   prop.getFeature(Features.Property.REF_CASCADE) != null));
             edesc.setPropertyPath(prop.getName());

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintFinderImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintFinderImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintFinderImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintFinderImpl.java
Thu May 20 13:58:15 2010
@@ -31,6 +31,8 @@ import org.apache.bval.model.MetaBean;
 import java.lang.annotation.ElementType;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Set;
 
 /**
@@ -52,9 +54,22 @@ final class ConstraintFinderImpl impleme
               new HashSet<ConstraintValidation>(constraintDescriptors.size());
         Groups groupChain = new GroupsComputer().computeGroups(groups);
         for (Group group : groupChain.getGroups()) {
-            for (ConstraintValidation descriptor : constraintDescriptors) {
-                if (isInScope(descriptor) && isInGroup(descriptor, group)) {
-                    matchingDescriptors.add(descriptor);
+            if ( group.isDefault() ) {
+                // If group is default, check if it gets redefined
+                List<Group> expandedDefaultGroup = metaBean.getFeature(Jsr303Features.Bean.GROUP_SEQUENCE);
+                for ( Group defaultGroupMember : expandedDefaultGroup ) {
+                    for (ConstraintValidation descriptor : constraintDescriptors) {
+                        if (isInScope(descriptor) && isInGroup(descriptor, defaultGroupMember))
{
+                            matchingDescriptors.add(descriptor);
+                        }
+                    }
+                }
+            }
+            else {
+                for (ConstraintValidation descriptor : constraintDescriptors) {
+                    if (isInScope(descriptor) && isInGroup(descriptor, group)) {
+                        matchingDescriptors.add(descriptor);
+                    }
                 }
             }
         }
@@ -64,6 +79,12 @@ final class ConstraintFinderImpl impleme
     public ElementDescriptor.ConstraintFinder lookingAt(Scope scope) {
         if (scope.equals(Scope.LOCAL_ELEMENT)) {
             findInScopes.remove(Scope.HIERARCHY);
+            for (Iterator<ConstraintValidation> it = constraintDescriptors.iterator();
it.hasNext(); ) {
+                ConstraintValidation cv = it.next();
+                if ( cv.getOwner() != metaBean.getBeanClass() ) {
+                    it.remove();
+                }
+            }
         }
         return this;
     }

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ElementDescriptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ElementDescriptorImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ElementDescriptorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ElementDescriptorImpl.java
Thu May 20 13:58:15 2010
@@ -34,10 +34,10 @@ public abstract class ElementDescriptorI
     protected final Class<?> elementClass;
     private Set<ConstraintDescriptor<?>> constraintDescriptors;
 
-    protected ElementDescriptorImpl(MetaBean metaBean,
+    protected ElementDescriptorImpl(MetaBean metaBean, Class<?> elementClass, 
                                  Validation[] validations) {
         this.metaBean = metaBean;
-        this.elementClass = metaBean.getBeanClass();
+        this.elementClass = elementClass;
         createConstraintDescriptors(validations);
     }
 

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/PropertyDescriptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/PropertyDescriptorImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/PropertyDescriptorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/PropertyDescriptorImpl.java
Thu May 20 13:58:15 2010
@@ -32,8 +32,8 @@ class PropertyDescriptorImpl extends Ele
     private boolean cascaded;
     private String propertyPath;
 
-    PropertyDescriptorImpl(MetaBean metaBean, Validation[] validations) {
-        super(metaBean, validations);
+    PropertyDescriptorImpl(MetaBean metaBean, String propertyPath, Validation[] validations)
{
+        super(metaBean, metaBean.getProperty(propertyPath).getTypeClass(), validations);
     }
 
     PropertyDescriptorImpl(Class<?> elementClass, Validation[] validations) {

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ConstructorDescriptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ConstructorDescriptorImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ConstructorDescriptorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ConstructorDescriptorImpl.java
Thu May 20 13:58:15 2010
@@ -33,7 +33,7 @@ public class ConstructorDescriptorImpl e
     private boolean cascaded;
 
     protected ConstructorDescriptorImpl(MetaBean metaBean, Validation[] validations) {
-        super(metaBean, validations);
+        super(metaBean, metaBean.getBeanClass(), validations);
     }
 
     protected ConstructorDescriptorImpl(Class<?> elementClass, Validation[] validations)
{

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodDescriptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodDescriptorImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodDescriptorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodDescriptorImpl.java
Thu May 20 13:58:15 2010
@@ -33,7 +33,7 @@ public class MethodDescriptorImpl extend
     private boolean cascaded;
 
     protected MethodDescriptorImpl(MetaBean metaBean, Validation[] validations) {
-        super(metaBean, validations);
+        super(metaBean, metaBean.getClass(), validations);
     }
 
     protected MethodDescriptorImpl(Class<?> elementClass, Validation[] validations)
{

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ParameterDescriptorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ParameterDescriptorImpl.java?rev=946636&r1=946635&r2=946636&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ParameterDescriptorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/ParameterDescriptorImpl.java
Thu May 20 13:58:15 2010
@@ -30,7 +30,7 @@ public class ParameterDescriptorImpl ext
     private int index;
 
     public ParameterDescriptorImpl(MetaBean metaBean, Validation[] validations) {
-        super(metaBean, validations);
+        super(metaBean, metaBean.getClass(), validations);
     }
 
     public ParameterDescriptorImpl(Class<?> elementClass, Validation[] validations)
{

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=946636&r1=946635&r2=946636&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
Thu May 20 13:58:15 2010
@@ -26,6 +26,11 @@ import javax.validation.constraints.NotN
 import javax.validation.groups.Default;
 import javax.validation.metadata.BeanDescriptor;
 import javax.validation.metadata.ConstraintDescriptor;
+import javax.validation.metadata.ElementDescriptor;
+import javax.validation.metadata.PropertyDescriptor;
+import javax.validation.metadata.Scope;
+import javax.validation.metadata.ElementDescriptor.ConstraintFinder;
+
 import java.lang.annotation.Documented;
 import java.lang.annotation.Retention;
 import java.lang.annotation.Target;
@@ -113,7 +118,11 @@ public class BeanDescriptorTest extends 
         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));
     }
-    
+
+    /**
+     * Check that the groups() attribute value does not contain the implicit
+     * interface group when querying the interface directly.
+     */
     public void testNoImplicitGroupWhenQueryingInterfaceDirectly() {
         Validator validator = getValidator();
         
@@ -126,7 +135,35 @@ public class BeanDescriptorTest extends 
         Assert.assertEquals("Incorrect number of groups", 1, notNullGroups.length);
         Assert.assertTrue("Default group not present", notNullGroups[0].equals(Default.class));
     }
-    
+
+    /**
+     * Check that the implementations of
+     * {@link ElementDescriptor#getElementClass()} work as defined in the spec.
+     */
+    public void testElementDescriptorGetElementClass() {
+        Validator validator = getValidator();
+        
+        BeanDescriptor beanDescriptor = validator.getConstraintsForClass( Person.class );
+        Assert.assertEquals("Incorrect class returned", Person.class, beanDescriptor.getElementClass());
+        
+        PropertyDescriptor nameDescriptor = beanDescriptor.getConstraintsForProperty("name");
+        Assert.assertEquals("Incorrect class returned", String.class, nameDescriptor.getElementClass());
+    }
+
+    /**
+     * Check the correct behavior of
+     * {@link ConstraintFinder#lookingAt(javax.validation.metadata.Scope)}.
+     */
+    public void testConstraintFinderLookingAt() {
+        Validator validator = getValidator();
+        
+        PropertyDescriptor nameDescriptor = validator.getConstraintsForClass( Woman.class
).getConstraintsForProperty("name");
+        Set<ConstraintDescriptor<?>> constraints = nameDescriptor.findConstraints().lookingAt(Scope.HIERARCHY).getConstraintDescriptors();
+        Assert.assertEquals("Incorrect number of descriptors", 1, constraints.size());
+        
+        constraints = nameDescriptor.findConstraints().lookingAt(Scope.LOCAL_ELEMENT).getConstraintDescriptors();
+        Assert.assertEquals("Incorrect number of descriptors", 0, constraints.size());
+    }
     
     public static class Form {
         @NotNull



Mime
View raw message