incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1103858 - in /incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src: main/java/org/apache/bval/jsr303/dynamic/NestedValidator.java test/java/org/apache/bval/jsr303/dynamic/NestedValidatorTest.java
Date Mon, 16 May 2011 19:48:20 GMT
Author: mbenson
Date: Mon May 16 19:48:20 2011
New Revision: 1103858

URL: http://svn.apache.org/viewvc?rev=1103858&view=rev
Log:
support ElementDescriptor APIs in NestedValidator

Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/jsr303/dynamic/NestedValidator.java
    incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/jsr303/dynamic/NestedValidatorTest.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/jsr303/dynamic/NestedValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/jsr303/dynamic/NestedValidator.java?rev=1103858&r1=1103857&r2=1103858&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/jsr303/dynamic/NestedValidator.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/main/java/org/apache/bval/jsr303/dynamic/NestedValidator.java
Mon May 16 19:48:20 2011
@@ -20,9 +20,12 @@ import java.lang.annotation.ElementType;
 import java.lang.reflect.Type;
 
 import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
+import javax.validation.metadata.BeanDescriptor;
 
 import org.apache.bval.DynamicMetaBean;
 import org.apache.bval.jsr303.GroupValidationContext;
+import org.apache.bval.jsr303.util.PathImpl;
 import org.apache.bval.jsr303.util.PathNavigation;
 import org.apache.bval.jsr303.util.ValidationContextTraversal;
 import org.apache.bval.model.MetaBean;
@@ -65,6 +68,24 @@ public class NestedValidator extends Dyn
     /**
      * {@inheritDoc}
      */
+    @Override
+    public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
+        if (clazz == null) {
+            throw new IllegalArgumentException("Class cannot be null");
+        }
+        try {
+            DynamicValidationState.CURRENT.set(new DynamicValidationStateBean(null, rootBean,
PathImpl.create(path)));
+            return createBeanDescriptor(factoryContext().readOnly().getMetaBean(clazz));
+        } catch (RuntimeException ex) {
+            throw new ValidationException("error retrieving constraints for " + clazz, ex);
+        } finally {
+            DynamicValidationState.CURRENT.remove();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
     protected GroupValidationContext createContext(MetaBean metaBean, final Object object,
Class objectClass,
         Class... groups) {
         if (StringUtils.isEmpty(path)) {

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/jsr303/dynamic/NestedValidatorTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/jsr303/dynamic/NestedValidatorTest.java?rev=1103858&r1=1103857&r2=1103858&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/jsr303/dynamic/NestedValidatorTest.java
(original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303-dynamic/provider/src/test/java/org/apache/bval/jsr303/dynamic/NestedValidatorTest.java
Mon May 16 19:48:20 2011
@@ -19,6 +19,7 @@ package org.apache.bval.jsr303.dynamic;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.beans.PropertyDescriptor;
 import java.util.ArrayList;
 import java.util.Locale;
 import java.util.Set;
@@ -28,6 +29,7 @@ import javax.validation.Validation;
 import javax.validation.Validator;
 import javax.validation.constraints.NotNull;
 import javax.validation.constraints.Pattern;
+import javax.validation.metadata.BeanDescriptor;
 
 import org.apache.bval.jsr303.ApacheValidationProvider;
 import org.apache.bval.jsr303.ApacheValidatorConfiguration;
@@ -143,4 +145,20 @@ public class NestedValidatorTest {
         errors = new NestedValidator(validatorContext, Author.class, "addresses[1]").validate(address1,
Book.All.class);
         assertEquals(1, errors.size());
     }
+
+    @Test
+    public void testDescriptor() {
+        validatorContext.constrain(Author.class, "addresses[].addressline2",
+            StubConstraint.build().withGroups(First.class).new CustomConfig<Pattern>()
{
+                protected void customConfigure(Pattern stub) {
+                    when(stub.regexp()).thenReturn("[0..9]+.*");
+                }
+            }.build());
+
+        assertEquals(1, validator.getConstraintsForClass(Address.class).getConstraintsForProperty("addressline2")
+            .getConstraintDescriptors().size());
+        assertEquals(2,
+            new NestedValidator(validatorContext, Author.class, "addresses[]").getConstraintsForClass(Address.class)
+                .getConstraintsForProperty("addressline2").getConstraintDescriptors().size());
+    }
 }



Mime
View raw message