You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bval.apache.org by mb...@apache.org on 2010/11/04 22:18:51 UTC

svn commit: r1031254 - in /incubator/bval/sandbox/lang3-work/bval-jsr303d/src: main/java/org/apache/bval/constraints/dynamic/ test/java/org/apache/bval/constraints/dynamic/

Author: mbenson
Date: Thu Nov  4 21:18:50 2010
New Revision: 1031254

URL: http://svn.apache.org/viewvc?rev=1031254&view=rev
Log:
it doesn't make sense to merge validation groups

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractConstraintAppender.java
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractGenericConstraintAppender.java
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractValuesConstraintAppender.java
    incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractConstraintAppender.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractConstraintAppender.java?rev=1031254&r1=1031253&r2=1031254&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractConstraintAppender.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractConstraintAppender.java Thu Nov  4 21:18:50 2010
@@ -80,7 +80,8 @@ public abstract class AbstractConstraint
      * @return Class[]
      */
     protected Class<?>[] getGroups(Annotation constraint) {
-        return (Class<?>[]) ConstraintAnnotationAttributes.GROUPS.getValue(constraint);
+        Class<?>[] result = ConstraintAnnotationAttributes.GROUPS.getValue(constraint);
+        return result.length == 0 ? GroupsComputer.getDefaultGroupArray() : result;
     }
 
     /**

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractGenericConstraintAppender.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractGenericConstraintAppender.java?rev=1031254&r1=1031253&r2=1031254&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractGenericConstraintAppender.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractGenericConstraintAppender.java Thu Nov  4 21:18:50 2010
@@ -17,12 +17,13 @@
 package org.apache.bval.constraints.dynamic;
 
 import java.lang.annotation.Annotation;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 
 import org.apache.bval.jsr303.ConstraintAnnotationAttributes;
 import org.apache.bval.jsr303.dynamic.ConstraintAppender;
-import org.apache.bval.jsr303.groups.Groups;
 
 /**
  * Convenience superclass for a {@link ConstraintAppender} that deals exclusively with a single constraint annotation
@@ -85,10 +86,11 @@ public abstract class AbstractGenericCon
             // non-constraint; don't know how to handle:
             return false;
         }
-        Groups groups = GROUPS_COMPUTER.computeGroups(getGroups(constraint));
+        HashSet<Class<?>> groups = new HashSet<Class<?>>();
+        Collections.addAll(groups, getGroups(constraint));
         for (Annotation a : collection) {
             if (constraintType.isInstance(a)) {
-                if (Collections.disjoint(groups.getGroups(), GROUPS_COMPUTER.computeGroups(getGroups(a)).getGroups())) {
+                if (Collections.disjoint(groups, Arrays.asList(getGroups(a)))) {
                     continue;
                 }
                 return false;

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractValuesConstraintAppender.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractValuesConstraintAppender.java?rev=1031254&r1=1031253&r2=1031254&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractValuesConstraintAppender.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/main/java/org/apache/bval/constraints/dynamic/AbstractValuesConstraintAppender.java Thu Nov  4 21:18:50 2010
@@ -32,12 +32,12 @@ import java.util.Set;
 
 import javax.validation.ConstraintDefinitionException;
 import javax.validation.Payload;
+import javax.validation.groups.Default;
 
 import org.apache.bval.constraints.dynamic.Values.Strategy;
 import org.apache.bval.constraints.dynamic.Values.Rule;
-import org.apache.bval.jsr303.groups.Group;
-import org.apache.bval.jsr303.groups.Groups;
 import org.apache.bval.jsr303.groups.GroupsComputer;
+import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.proxy2.stub.AnnotationFactory;
 import org.apache.commons.proxy2.stub.StubConfigurer;
@@ -57,8 +57,6 @@ import org.apache.commons.proxy2.stub.St
 public abstract class AbstractValuesConstraintAppender<A extends Annotation, M extends Annotation, V> extends
     AbstractGenericMultivaluedConstraintAppender<A, M> {
 
-    private static final GroupsComputer GROUPS_COMPUTER = new GroupsComputer();
-
     /**
      * {@link AnnotationFactory} instance for annotation generation.
      */
@@ -109,8 +107,8 @@ public abstract class AbstractValuesCons
      * Values holder.
      */
     private class ValuesMerger {
-        Map<Group, CumulativeValues> includedValuesByGroup = new HashMap<Group, CumulativeValues>();
-        Map<Group, CumulativeValues> excludedValuesByGroup = new HashMap<Group, CumulativeValues>();
+        Map<Class<?>, CumulativeValues> includedValuesByGroup = new HashMap<Class<?>, CumulativeValues>();
+        Map<Class<?>, CumulativeValues> excludedValuesByGroup = new HashMap<Class<?>, CumulativeValues>();
 
         /**
          * Merge the specified constraint.
@@ -134,11 +132,14 @@ public abstract class AbstractValuesCons
             // depending on the existing inclusions/exclusions:
 
             Rule rule = getRule(constraint);
-            Groups chain = GROUPS_COMPUTER.computeGroups(getGroups(constraint));
             Strategy strategy = getStrategy(constraint);
 
             List<V> constraintValue = Arrays.asList(getConstraintValue(constraint));
-            for (Group group : chain.getGroups()) {
+            Class<?>[] groups = getGroups(constraint);
+            if (groups.length == 0) {
+                groups = new Class[] { Default.class };
+            }
+            for (Class<?> group : getGroups(constraint)) {
                 CumulativeValues includes = null;
                 CumulativeValues excludes = null;
                 if (strategy == Strategy.REPLACE) {
@@ -202,7 +203,6 @@ public abstract class AbstractValuesCons
                 }
             }
         }
-
     }
 
     /**
@@ -275,7 +275,7 @@ public abstract class AbstractValuesCons
      * @return <code>true</code> while more processing to be done.
      */
     private boolean process(ValuesMerger valuesMerger, Collection<A> constraints) {
-        Map<Group, CumulativeValues> valuesMap = valuesMerger.includedValuesByGroup;
+        Map<Class<?>, CumulativeValues> valuesMap = valuesMerger.includedValuesByGroup;
         Rule rule = Rule.INCLUDE;
         if (valuesMap.isEmpty()) {
             valuesMap = valuesMerger.excludedValuesByGroup;
@@ -284,24 +284,24 @@ public abstract class AbstractValuesCons
             }
             rule = Rule.EXCLUDE;
         }
-        Group group = findFirstRootGroup(valuesMap.keySet());
+        Class<?> group = findFirstRootGroup(valuesMap.keySet());
         if (group == null) {
             throw new ConstraintDefinitionException(String.format("Can't find root group among %s", valuesMap.keySet()));
         }
         CumulativeValues values = valuesMap.remove(group);
         HashSet<Class<?>> groups = new HashSet<Class<?>>();
-        groups.add(group.getGroup());
+        groups.add(group);
         Set<Class<? extends Payload>> payloads = new LinkedHashSet<Class<? extends Payload>>(values.payload);
         Set<String> messages = new LinkedHashSet<String>(values.message);
         if (!values.values.isEmpty() || values.strategy == Strategy.REPLACE) {
             // merge groups with same values and strategy:
-            for (Iterator<Map.Entry<Group, CumulativeValues>> iter = valuesMap.entrySet().iterator(); iter.hasNext();) {
-                Map.Entry<Group, CumulativeValues> e = iter.next();
+            for (Iterator<Map.Entry<Class<?>, CumulativeValues>> iter = valuesMap.entrySet().iterator(); iter.hasNext();) {
+                Map.Entry<Class<?>, CumulativeValues> e = iter.next();
                 if (e.getValue().values.equals(values.values) && e.getValue().strategy == values.strategy) {
                     iter.remove();
                     // swallow groups that extend our base group:
-                    if (!group.getGroup().isAssignableFrom(e.getKey().getGroup())) {
-                        groups.add(e.getKey().getGroup());
+                    if (!group.isAssignableFrom(e.getKey())) {
+                        groups.add(e.getKey());
                     }
                     payloads.addAll(e.getValue().payload);
                     messages.addAll(e.getValue().message);
@@ -327,14 +327,14 @@ public abstract class AbstractValuesCons
      * @param groups
      * @return Group
      */
-    private Group findFirstRootGroup(Set<Group> groups) {
-        for (Group group : groups) {
+    private Class<?> findFirstRootGroup(Set<Class<?>> groups) {
+        for (Class<?> group : groups) {
             boolean ok = true;
-            for (Group other : groups) {
+            for (Class<?> other : groups) {
                 if (other == group) {
                     continue;
                 }
-                if (other.getGroup().isAssignableFrom(group.getGroup())) {
+                if (other.isAssignableFrom(group)) {
                     ok = false;
                     break;
                 }
@@ -446,8 +446,11 @@ public abstract class AbstractValuesCons
     @SuppressWarnings("unchecked")
     @Override
     protected Class<?>[] getGroups(Annotation constraint) {
-        return constraintType.isInstance(constraint) ? getTypedConstraintGroups((A) constraint) : super
-            .getGroups(constraint);
+        if (constraintType.isInstance(constraint)) {
+            Class<?>[] result = getTypedConstraintGroups((A) constraint);
+            return ArrayUtils.isEmpty(result) ? GroupsComputer.getDefaultGroupArray() : result;
+        }
+        return super.getGroups(constraint);
     }
 
     /**

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java?rev=1031254&r1=1031253&r2=1031254&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303d/src/test/java/org/apache/bval/constraints/dynamic/ValuesConstraintValidationTest.java Thu Nov  4 21:18:50 2010
@@ -492,59 +492,6 @@ public class ValuesConstraintValidationT
                 .getConstraintDescriptors().size());
     }
 
-    @Test
-    public void testValuesConstraintAccumulationGroupingInheritance() {
-        validatorContext.constrain(TestBean.class, "fooBarOrBaz",
-            annotationFactory.create(new StubConfigurer<Values>() {
-
-                @Override
-                protected void configure(Values stub) {
-                    when(stub.groups()).thenReturn(Y.class).when(stub.value()).thenReturn("foo", "bar");
-                }
-            }));
-        assertEquals(2, validator.getConstraintsForClass(TestBean.class).getConstraintsForProperty("fooBarOrBaz")
-            .getConstraintDescriptors().size());
-        validatorContext.constrain(TestBean.class, "fooBarOrBaz",
-            annotationFactory.create(new StubConfigurer<Values>() {
-
-                @Override
-                protected void configure(Values stub) {
-                    when(stub.groups()).thenReturn(Z.class).when(stub.value()).thenReturn("foo", "bar", "baz");
-                }
-            }));
-        assertEquals(1, validator.getConstraintsForClass(TestBean.class).getConstraintsForProperty("fooBarOrBaz")
-            .getConstraintDescriptors().size());
-    }
-
-    @Test
-    public void testLabeledValuesConstraintAccumulationGroupingInheritance() {
-        validatorContext.constrain(TestBean.class, "annotationOrConstructor",
-            annotationFactory.create(new StubConfigurer<Values.Labeled>() {
-
-                @Override
-                protected void configure(Labeled stub) {
-                    when(stub.groups()).thenReturn(Y.class).when(stub.value())
-                        .thenReturn(item(ElementType.ANNOTATION_TYPE, "@"));
-                }
-            }));
-        assertEquals(2,
-            validator.getConstraintsForClass(TestBean.class).getConstraintsForProperty("annotationOrConstructor")
-                .getConstraintDescriptors().size());
-        validatorContext.constrain(TestBean.class, "annotationOrConstructor",
-            annotationFactory.create(new StubConfigurer<Values.Labeled>() {
-
-                @Override
-                protected void configure(Labeled stub) {
-                    when(stub.groups()).thenReturn(Z.class).when(stub.value())
-                        .thenReturn(item(ElementType.ANNOTATION_TYPE, "@"), item(ElementType.CONSTRUCTOR, "new"));
-                }
-            }));
-        assertEquals(1,
-            validator.getConstraintsForClass(TestBean.class).getConstraintsForProperty("annotationOrConstructor")
-                .getConstraintDescriptors().size());
-
-    }
-
     public interface X {
     }