incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carlosv...@apache.org
Subject svn commit: r962867 - 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 Sat, 10 Jul 2010 16:12:56 GMT
Author: carlosvara
Date: Sat Jul 10 16:12:55 2010
New Revision: 962867

URL: http://svn.apache.org/viewvc?rev=962867&view=rev
Log:
BVAL-78: merged patch by Matt Benson

Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.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/ConstraintValidationListener.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/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java?rev=962867&r1=962866&r2=962867&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
Sat Jul 10 16:12:55 2010
@@ -126,7 +126,7 @@ public class ClassValidator extends Abst
       Class<T> objectClass = (Class<T>) object.getClass();
       MetaBean objectMetaBean = factoryContext.getMetaBeanFinder().findForClass(objectClass);
       
-      final GroupValidationContext<ConstraintValidationListener<T>> context =
+      final GroupValidationContext<T> context =
           createContext(objectMetaBean, object, objectClass, groups);
       final ConstraintValidationListener<T> result = context.getListener();
       final Groups sequence = context.getGroups();
@@ -193,7 +193,7 @@ public class ClassValidator extends Abst
       Class<T> objectClass = (Class<T>) object.getClass();
       MetaBean objectMetaBean = factoryContext.getMetaBeanFinder().findForClass(objectClass);
 
-      GroupValidationContext<ConstraintValidationListener<T>> context =
+      GroupValidationContext<T> context =
           createContext(objectMetaBean, object, objectClass, groups);
       ConstraintValidationListener<T> result = context.getListener();
       NestedMetaProperty nestedProp = getNestedProperty(objectMetaBean, object, propertyName);
@@ -272,7 +272,7 @@ public class ClassValidator extends Abst
 
     try {
       MetaBean metaBean = factoryContext.getMetaBeanFinder().findForClass(beanType);
-      GroupValidationContext<ConstraintValidationListener<T>> context =
+      GroupValidationContext<T> context =
           createContext(metaBean, null, beanType, groups);
       ConstraintValidationListener<T> result = context.getListener();
       context.setMetaProperty(
@@ -619,11 +619,11 @@ public class ClassValidator extends Abst
    * @param groups
    * @return {@link GroupValidationContext} instance
    */
-  protected <T> GroupValidationContext<ConstraintValidationListener<T>>
createContext(
+  protected <T> GroupValidationContext<T> createContext(
       MetaBean metaBean, T object, Class<T> objectClass, Class<?>[] groups) {
     ConstraintValidationListener<T> listener = new ConstraintValidationListener<T>(object,
objectClass);
-    GroupValidationContextImpl<ConstraintValidationListener<T>> context =
-        new GroupValidationContextImpl<ConstraintValidationListener<T>>(listener,
+    GroupValidationContextImpl<T> context =
+        new GroupValidationContextImpl<T>(listener,
             this.factoryContext.getMessageInterpolator(),
             this.factoryContext.getTraversableResolver(), metaBean);
     context.setBean(object, metaBean);

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=962867&r1=962866&r2=962867&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
Sat Jul 10 16:12:55 2010
@@ -19,7 +19,6 @@
 package org.apache.bval.jsr303;
 
 
-import org.apache.bval.BeanValidationContext;
 import org.apache.bval.jsr303.util.NodeImpl;
 import org.apache.bval.jsr303.util.PathImpl;
 import org.apache.bval.model.Validation;
@@ -133,14 +132,14 @@ public class ConstraintValidation<T exte
    * {@inheritDoc}
    */
   public <L extends ValidationListener> void validate(ValidationContext<L> context)
{
-    validate((GroupValidationContext<L>) context);
+    validate((GroupValidationContext<?>) context);
   }
 
   /**
    * Validate a {@link GroupValidationContext}.
    * @param context root
    */
-  public <L extends ValidationListener> void validate(GroupValidationContext<L>
context) {
+  public void validate(GroupValidationContext<?> context) {
     context.setConstraintValidation(this);
     /**
      * execute unless the given validation constraint has already been processed
@@ -161,25 +160,23 @@ public class ConstraintValidation<T exte
 
     // process composed constraints
     if (isReportAsSingleViolation()) {
-      BeanValidationContext gctx = (BeanValidationContext) context;
-      ConstraintValidationListener oldListener =
-          ((ConstraintValidationListener) gctx.getListener());
-      ConstraintValidationListener listener =
-          new ConstraintValidationListener(oldListener.getRootBean(), oldListener.getRootBeanType());
-      gctx.setListener(listener);
+      ConstraintValidationListener<?> listener = context.getListener();
+      listener.beginReportAsSingle();
+
+      boolean failed = false;
       try {
-        for (ConstraintValidation composed : getComposingValidations()) {
-          composed.validate(context);
-        }
+          // stop validating when already failed and ReportAsSingleInvalidConstraint = true
?
+          for (Iterator<ConstraintValidation<?>> composed = getComposingValidations().iterator();
!failed && composed.hasNext();) {
+              composed.next().validate(context);
+              failed = listener.hasViolations();
+          }
       } finally {
-        gctx.setListener(oldListener);
+          listener.endReportAsSingle();
+          // Restore current constraint validation
+          context.setConstraintValidation(this);
       }
 
-      // Restore current constraint validation
-      context.setConstraintValidation(this);
-
-      // stop validating when already failed and ReportAsSingleInvalidConstraint = true ?
-      if (!listener.getConstraintViolations().isEmpty()) {
+      if (failed) {
         // TODO RSt - how should the composed constraint error report look like?
         ConstraintValidatorContextImpl jsrContext =
             new ConstraintValidatorContextImpl(context, this);

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidationListener.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidationListener.java?rev=962867&r1=962866&r2=962867&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidationListener.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/ConstraintValidationListener.java
Sat Jul 10 16:12:55 2010
@@ -28,6 +28,7 @@ import javax.validation.metadata.Constra
 import java.lang.annotation.ElementType;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * Description: JSR-303 {@link ValidationListener} implementation; provides {@link ConstraintViolation}s.<br/>
@@ -36,6 +37,10 @@ public final class ConstraintValidationL
     private final Set<ConstraintViolation<T>> constraintViolations = new HashSet<ConstraintViolation<T>>();
     private final T rootBean;
     private final Class<T> rootBeanType;
+    // TODO: Currently there is no need for atomicity here as all the validation process
+    //       is single-threaded and it's unlikely to change in the near future.
+    private final AtomicInteger compositeDepth = new AtomicInteger(0);
+    private boolean hasCompositeError;
 
     /**
      * Create a new ConstraintValidationListener instance.
@@ -67,6 +72,10 @@ public final class ConstraintValidationL
 
     private void addError(String messageTemplate, Path propPath,
                           ValidationContext<?> context) {
+        if (compositeDepth.get() > 0) {
+            hasCompositeError |= true;
+            return;
+        }
         final Object value;
 
         final ConstraintDescriptor<?> descriptor;
@@ -138,4 +147,29 @@ public final class ConstraintValidationL
         return constraintViolations.size();
     }
 
+    /**
+     * Learn whether there are violations available.
+     * If in report-as-single-violation mode, the result is scoped accordingly.
+     * Note that this means you must check before exiting report-as-single-violation mode
+     * @return boolean
+     */
+    public boolean hasViolations() {
+        return compositeDepth.get() == 0 ? !constraintViolations.isEmpty() : hasCompositeError;

+    }
+
+    /**
+     * Signify the beginning of a report-as-single-violation composite validation.
+     * @return <code>true</code> as this call caused the listener to enter report-as-single-violation
mode
+     */
+    public boolean beginReportAsSingle() {
+        return compositeDepth.incrementAndGet() == 1;
+    }
+
+    /**
+     * Signify the end of a report-as-single-violation composite validation.
+     * @return <code>true</code> as this call caused the listener to exit report-as-single-violation
mode
+     */
+    public boolean endReportAsSingle() {
+        return compositeDepth.decrementAndGet() == 0;
+    }
 }

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=962867&r1=962866&r2=962867&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
Sat Jul 10 16:12:55 2010
@@ -22,7 +22,6 @@ import org.apache.bval.jsr303.groups.Gro
 import org.apache.bval.jsr303.util.PathImpl;
 import org.apache.bval.model.MetaBean;
 import org.apache.bval.model.ValidationContext;
-import org.apache.bval.model.ValidationListener;
 
 import javax.validation.ConstraintValidator;
 import javax.validation.MessageInterpolator;
@@ -31,8 +30,8 @@ import javax.validation.TraversableResol
 /**
  * Description: JSR-303 {@link ValidationContext} extension. <br/>
  */
-public interface GroupValidationContext<T extends ValidationListener>
-      extends ValidationContext<T> {
+public interface GroupValidationContext<T>
+      extends ValidationContext<ConstraintValidationListener<T>> {
     
     /**
      * Get the groups of this {@link GroupValidationContext}.

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=962867&r1=962866&r2=962867&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
Sat Jul 10 16:12:55 2010
@@ -25,7 +25,6 @@ import org.apache.bval.jsr303.util.NodeI
 import org.apache.bval.jsr303.util.PathImpl;
 import org.apache.bval.model.MetaBean;
 import org.apache.bval.model.MetaProperty;
-import org.apache.bval.model.ValidationListener;
 import org.apache.bval.util.AccessStrategy;
 
 import javax.validation.ConstraintValidator;
@@ -39,8 +38,8 @@ import java.util.Set;
 /**
  * Description: instance per validation process, not thread-safe<br/>
  */
-final class GroupValidationContextImpl<T extends ValidationListener>
-    extends BeanValidationContext<T>
+final class GroupValidationContextImpl<T>
+    extends BeanValidationContext<ConstraintValidationListener<T>>
     implements GroupValidationContext<T>, MessageInterpolator.Context {
 
   private final MessageInterpolator messageResolver;
@@ -75,7 +74,7 @@ final class GroupValidationContextImpl<T
    * @param traversableResolver
    * @param rootMetaBean
    */
-  public GroupValidationContextImpl(T listener, MessageInterpolator aMessageResolver,
+  public GroupValidationContextImpl(ConstraintValidationListener<T> listener, MessageInterpolator
aMessageResolver,
                                     TraversableResolver traversableResolver,
                                     MetaBean rootMetaBean) {
     // inherited variable 'validatedObjects' is of type: HashMap<GraphBeanIdentity, Set<PathImpl>>
in this class 

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java?rev=962867&r1=962866&r2=962867&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java
Sat Jul 10 16:12:55 2010
@@ -173,7 +173,7 @@ class MethodValidatorImpl extends ClassV
     if ( methodDescriptor == null ) {
         throw new ValidationException("Method " + method + " doesn't belong to class " +
clazz);
     }
-    final GroupValidationContext<ConstraintValidationListener<Object>> context
=
+    final GroupValidationContext<Object> context =
         createContext(methodDescriptor.getMetaBean(), returnedValue, null, groupArray);
     validateReturnedValueInContext(context, methodDescriptor);
     ConstraintValidationListener<T> result = (ConstraintValidationListener<T>)
context.getListener();
@@ -210,7 +210,7 @@ class MethodValidatorImpl extends ClassV
   private <T> Set<ConstraintViolation<T>> validateParameter(
       ParameterDescriptorImpl paramDesc, Object parameter, Class<?>... groupArray)
{
     try {
-      final GroupValidationContext<ConstraintValidationListener<Object>> context
=
+      final GroupValidationContext<Object> context =
           createContext(paramDesc.getMetaBean(), parameter, null, groupArray);
       final ConstraintValidationListener<T> result = (ConstraintValidationListener<T>)
context.getListener();
       validateParameterInContext(context, paramDesc);
@@ -224,7 +224,7 @@ class MethodValidatorImpl extends ClassV
    * validate constraints hosted on parameters of a method
    */
   private <T> void validateParameterInContext(
-      GroupValidationContext<ConstraintValidationListener<T>> context,
+      GroupValidationContext<T> context,
       ParameterDescriptorImpl paramDesc) {
 
     final Groups groups = context.getGroups();
@@ -276,7 +276,7 @@ class MethodValidatorImpl extends ClassV
    * validate constraints hosted on parameters of a method
    */
   private <T> void validateReturnedValueInContext(
-      GroupValidationContext<ConstraintValidationListener<T>> context,
+      GroupValidationContext<T> context,
       MethodDescriptorImpl methodDescriptor) {
 
     final Groups groups = context.getGroups();

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java?rev=962867&r1=962866&r2=962867&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ConstraintValidatorContextTest.java
Sat Jul 10 16:12:55 2010
@@ -103,7 +103,7 @@ public class ConstraintValidatorContextT
     }
 
     // TODO: mock
-    public static class DummyContext<T extends ValidationListener> implements
+    public static class DummyContext<T> implements
             GroupValidationContext<T> {
 
         public boolean collectValidated(ConstraintValidator constraint) {
@@ -166,7 +166,7 @@ public class ConstraintValidatorContextT
             throw new IllegalStateException("Unexpected call");
         }
 
-        public T getListener() {
+        public ConstraintValidationListener<T> getListener() {
             throw new IllegalStateException("Unexpected call");
         }
 



Mime
View raw message