incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r939818 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java
Date Fri, 30 Apr 2010 21:14:19 GMT
Author: dwoods
Date: Fri Apr 30 21:14:18 2010
New Revision: 939818

URL: http://svn.apache.org/viewvc?rev=939818&view=rev
Log:
BVAL-31 A ConstraintDefinitionException should be thrown when the resolved validator is not
compatible with the annotation type.  Contributed by Carlos Vara.

Added:
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java
  (with props)
Modified:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java?rev=939818&r1=939817&r2=939818&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/Jsr303MetaBeanFactory.java
Fri Apr 30 21:14:18 2010
@@ -437,7 +437,13 @@ public class Jsr303MetaBeanFactory imple
             checkOneType(assignableTypes, type, owner, annotation, access);
             validator = getConstraintValidatorFactory()
                   .getInstance(validatorTypes.get(assignableTypes.get(0)));
-            validator.initialize(annotation);
+            try {
+                validator.initialize(annotation);
+            } catch (RuntimeException e) {
+                // Either a "legit" problem initializing the validator or a ClassCastException
if
+                // the validator associated annotation is not a supertype of the validated
annotation.
+                throw new ConstraintDefinitionException("Incorrect validator ["+validator.getClass().getCanonicalName()+"]
for annotation " + annotation.annotationType().getCanonicalName(), e);
+            }
         } else {
             validator = null;
         }

Added: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java?rev=939818&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java
Fri Apr 30 21:14:18 2010
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.bval.jsr303;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.util.Locale;
+
+import javax.validation.Constraint;
+import javax.validation.ConstraintDefinitionException;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.Payload;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import javax.validation.constraints.NotNull;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Checks the correct behavior of the validator resolution algorithm.
+ * 
+ * @author Carlos Vara
+ */
+public class ValidatorResolutionTest extends TestCase {
+
+    static ValidatorFactory factory;
+
+    static {
+        factory = Validation.buildDefaultValidatorFactory();
+        ((DefaultMessageInterpolator) factory.getMessageInterpolator()).setLocale(Locale.ENGLISH);
+    }
+
+    private Validator getValidator() {
+        return factory.getValidator();
+    }
+
+    /**
+     * Check that a {@link ConstraintDefinitionException} is thrown when the
+     * only available validator is associated with a different annotation type.
+     */
+    public void testInvalidValidator() {
+        Validator validator = getValidator();
+        try {
+            validator.validate(new Person());
+            fail("No exception thrown, but no valid validator available.");
+        } catch (ConstraintDefinitionException e) {
+            // correct
+        }
+    }
+    
+    
+    public static class Person {
+        @PersonName
+        public String name;
+    }
+    
+    @Constraint(validatedBy = {InvalidPersonNameValidator.class})
+    @Documented
+    @Target({ METHOD, FIELD, TYPE })
+    @Retention(RUNTIME)
+    public static @interface PersonName {
+        String message() default "Wrong person name";
+        Class<?>[] groups() default { };
+        Class<? extends Payload>[] payload() default {};
+    }
+    
+    public static class InvalidPersonNameValidator implements ConstraintValidator<NotNull,
String> {
+        public void initialize(NotNull constraintAnnotation) {
+            // Nothing
+        }
+        public boolean isValid(String value, ConstraintValidatorContext context) {
+            return true;
+        }
+    }
+    
+}

Propchange: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ValidatorResolutionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message