incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romanst...@apache.org
Subject svn commit: r938971 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/ test/java/org/apache/bval/jsr303/ test/java/org/apache/bval/jsr303/example/
Date Wed, 28 Apr 2010 12:35:23 GMT
Author: romanstumm
Date: Wed Apr 28 12:35:23 2010
New Revision: 938971

URL: http://svn.apache.org/viewvc?rev=938971&view=rev
Log:
BVAL-28 Property path for nodes inside iterables that do not implement List should not contain
the index
- merged, reviewed and adapted the fixes from Carlos Varla

Added:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidatorIdentity.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/example/Continent.java
Modified:
    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/GroupValidationContext.java
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContextImpl.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.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=938971&r1=938970&r2=938971&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
Wed Apr 28 12:35:23 2010
@@ -114,7 +114,7 @@ public class ConstraintValidation<T exte
         if (!isMemberOf(context.getCurrentGroup().getGroup())) {
             return; // do not validate in the current group
         }
-        if (validator != null && !context.collectValidated(context.getPropertyPath(),
validator))
+        if (validator != null && !context.collectValidated(validator))
             return; // already done
 
         if (context.getMetaProperty() != null && !isCascadeEnabled(context)) {

Added: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidatorIdentity.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidatorIdentity.java?rev=938971&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidatorIdentity.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidatorIdentity.java
Wed Apr 28 12:35:23 2010
@@ -0,0 +1,91 @@
+package org.apache.bval.jsr303;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.Path;
+
+/**
+ * Class that stores the needed properties to ensure that a validation is not
+ * checked more than once.
+ * <p>
+ * These properties are:
+ * <ul>
+ * <li>The ref of the bean to which the validation would be applied.</li>
+ * <li>The path of the property.</li>
+ * <li>The ref of the {@link ConstraintValidator}.</li>
+ * </ul>
+ * 
+ * @author Carlos Vara
+ */
+final class ConstraintValidatorIdentity {
+
+    private final Object bean;
+    private final Path path;
+    private final ConstraintValidator<?, ?> constraintValidator;
+
+    public ConstraintValidatorIdentity(Object bean, Path path, ConstraintValidator<?,
?> constraintValidator) {
+        this.bean = bean;
+        this.path = path;
+        this.constraintValidator = constraintValidator;
+    }
+
+    public Object getBean() {
+        return bean;
+    }
+
+    public Path getPath() {
+        return path;
+    }
+
+    public ConstraintValidator<?, ?> getConstraintValidator() {
+        return constraintValidator;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null) {
+            return false;
+        }
+
+        if (!(obj instanceof ConstraintValidatorIdentity)) {
+            return false;
+        }
+
+        ConstraintValidatorIdentity other = (ConstraintValidatorIdentity) obj;
+
+        // Bean ref must be the same
+        if (this.bean != other.bean) {
+            return false;
+        }
+
+        // ConstraintValidator ref must be the same
+        if (this.constraintValidator != other.constraintValidator) {
+            return false;
+        }
+
+        // Path must be equals
+        if (!this.path.equals(other.path)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result
+                + ((this.bean == null) ? 0 : this.bean.hashCode());
+        result = prime * result
+                + ((this.path == null) ? 0 : this.path.hashCode());
+        result = prime * result
+                + ((this.constraintValidator == null) ? 0 : this.constraintValidator.hashCode());
+        return result;
+    }
+
+}

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContext.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContext.java?rev=938971&r1=938970&r2=938971&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContext.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContext.java
Wed Apr 28 12:35:23 2010
@@ -56,6 +56,6 @@ public interface GroupValidationContext<
 
     TraversableResolver getTraversableResolver();
 
-    boolean collectValidated(Object bean, ConstraintValidator constraint);
+    boolean collectValidated(ConstraintValidator constraint);
 
 }

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContextImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContextImpl.java?rev=938971&r1=938970&r2=938971&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContextImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/GroupValidationContextImpl.java
Wed Apr 28 12:35:23 2010
@@ -32,7 +32,10 @@ import javax.validation.ConstraintValida
 import javax.validation.MessageInterpolator;
 import javax.validation.TraversableResolver;
 import javax.validation.metadata.ConstraintDescriptor;
-import java.util.*;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
 
 /**
  * Description: instance per validation process, not thread-safe<br/>
@@ -57,8 +60,8 @@ final class GroupValidationContextImpl<T
    * contains the validation constraints that have already been processed during
    * this validation routine (as part of a previous group match)
    */
-  private HashMap<PathImpl, IdentityHashMap<ConstraintValidator, Object>> validatedConstraints
=
-      new HashMap<PathImpl, IdentityHashMap<ConstraintValidator, Object>>();
+  private HashSet<ConstraintValidatorIdentity> validatedConstraints =
+      new HashSet<ConstraintValidatorIdentity>();
   private ConstraintValidation constraintValidation;
   private final TraversableResolver traversableResolver;
 
@@ -73,7 +76,7 @@ final class GroupValidationContextImpl<T
   }
 
   @Override
-  public void setCurrentIndex(int index) {
+  public void setCurrentIndex(Integer index) {
     path.getLeafNode().setIndex(index);
   }
 
@@ -94,55 +97,49 @@ final class GroupValidationContextImpl<T
     super.moveUp(bean, metaBean); // call super!
   }
 
-    /**
-     * add the object in the current group to the collection of validated
-     * objects to keep track of them to avoid endless loops during validation.
-     * 
-     * @return true when the object was not already validated in this context
-     */
-    @Override
-    public boolean collectValidated() {
-
-        Map<Group, Set<PathImpl>> groupMap = (Map<Group, Set<PathImpl>>)
validatedObjects
-                .get(getBean());
-        if (groupMap == null) {
-            groupMap = new HashMap<Group, Set<PathImpl>>();
-            validatedObjects.put(getBean(), groupMap);
-        }
-        Set<PathImpl> validatedPathsForGroup = groupMap.get(getCurrentGroup());
-        if (validatedPathsForGroup == null) {
-            validatedPathsForGroup = new HashSet<PathImpl>();
-            groupMap.put(getCurrentGroup(), validatedPathsForGroup);
-        }
-
-        // If any of the paths is a subpath of the current path, there is a
-        // circular dependency, so return false
-        for (PathImpl validatedPath : validatedPathsForGroup) {
-            if (path.isSubPathOf(validatedPath)) {
-                return false;
-            }
-        }
-
-        // Else, add the currentPath to the set of validatedPaths
-        validatedPathsForGroup.add(PathImpl.copy(path));
-        return true;
+  /**
+   * add the object in the current group to the collection of validated
+   * objects to keep track of them to avoid endless loops during validation.
+   *
+   * @return true when the object was not already validated in this context
+   */
+  @Override
+  public boolean collectValidated() {
+
+    Map<Group, Set<PathImpl>> groupMap = (Map<Group, Set<PathImpl>>)
validatedObjects
+        .get(getBean());
+    if (groupMap == null) {
+      groupMap = new HashMap<Group, Set<PathImpl>>();
+      validatedObjects.put(getBean(), groupMap);
+    }
+    Set<PathImpl> validatedPathsForGroup = groupMap.get(getCurrentGroup());
+    if (validatedPathsForGroup == null) {
+      validatedPathsForGroup = new HashSet<PathImpl>();
+      groupMap.put(getCurrentGroup(), validatedPathsForGroup);
     }
-  
-    /**
-     * @return true when the constraint for the object in this path was not
-     *         already validated in this context
-     */
-    public boolean collectValidated(Object path, ConstraintValidator constraint) {
-        IdentityHashMap<ConstraintValidator, Object> constraints = this.validatedConstraints
-                .get(path);
-        if (constraints == null) {
-            constraints = new IdentityHashMap<ConstraintValidator, Object>();
-            this.validatedConstraints.put((PathImpl) path, constraints);
-        }
 
-        return (constraints.put(constraint, Boolean.TRUE) == null);
+    // If any of the paths is a subpath of the current path, there is a
+    // circular dependency, so return false
+    for (PathImpl validatedPath : validatedPathsForGroup) {
+      if (path.isSubPathOf(validatedPath)) {
+        return false;
+      }
     }
 
+    // Else, add the currentPath to the set of validatedPaths
+    validatedPathsForGroup.add(PathImpl.copy(path));
+    return true;
+  }
+
+  /**
+   * @return true when the constraint for the object in this path was not
+   *         already validated in this context
+   */
+  public boolean collectValidated(ConstraintValidator constraint) {
+    ConstraintValidatorIdentity cvi = new ConstraintValidatorIdentity(getBean(), getPropertyPath(),
constraint);
+    return this.validatedConstraints.add(cvi);
+  }
+
   public void resetValidatedConstraints() {
     validatedConstraints.clear();
   }

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java?rev=938971&r1=938970&r2=938971&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidationTest.java
Wed Apr 28 12:35:23 2010
@@ -189,6 +189,24 @@ public class ValidationTest extends Test
         assertPropertyPath("addresses[1].country", constraints);
         assertPropertyPath("addresses[2].country", constraints);
     }
+    
+    /**
+     * Check correct path reporting when validating a set of beans.
+     */
+    public void testPropertyPathOnSet() {
+        Continent c = new Continent();
+        c.name = "c1";
+        Country country = new Country();
+        country.setISO2Code("xx");
+        country.setISO3Code("xxx");
+        country.setName(null);
+        c.countries.add(country);
+        
+        Set<ConstraintViolation<Continent>> constraints = getValidator().validate(c);
+        Assert.assertEquals("Incorrect number of violations detected", 1, constraints.size());
+        assertPropertyPath("countries[].name", constraints);
+        
+    }
 
     private <T> void assertPropertyPath(String propertyPath,
                                         Set<ConstraintViolation<T>> constraints)
{

Added: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/example/Continent.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/example/Continent.java?rev=938971&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/example/Continent.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/example/Continent.java
Wed Apr 28 12:35:23 2010
@@ -0,0 +1,21 @@
+package org.apache.bval.jsr303.example;
+
+import javax.validation.Valid;
+import javax.validation.constraints.NotNull;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * A continent has a name and a set of {@link Country}s.
+ * 
+ * @author Carlos Vara
+ */
+public class Continent {
+
+    @NotNull
+    public String name;
+    
+    @Valid
+    public Set<Country> countries = new HashSet<Country>();
+    
+}



Mime
View raw message