incubator-bval-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=938807&view=rev
Log:
BVAL-25 merged fixes from Carlos. Thanks!!

Added:
    incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/UnknownPropertyException.java
    incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java
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/NestedMetaProperty.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=938807&r1=938806&r2=938807&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
Wed Apr 28 07:30:23 2010
@@ -134,12 +134,16 @@ public class ClassValidator extends Bean
         }
     }
 
-    protected ValidationException unrecoverableValidationError(RuntimeException ex,
+    protected RuntimeException unrecoverableValidationError(RuntimeException ex,
                                                                Object object) {
-        if (ex instanceof ValidationException || ex instanceof IllegalArgumentException)
{
-            throw ex; // do not wrap specific ValidationExceptions (or instances from subclasses)
+        if (ex instanceof UnknownPropertyException) {
+            // Convert to IllegalArgumentException
+            return new IllegalArgumentException(ex.getMessage(), ex);
+        }
+        else if (ex instanceof ValidationException) {
+            return ex; // do not wrap specific ValidationExceptions (or instances from subclasses)
         } else {
-            throw new ValidationException("error during validation of " + object, ex);
+            return new ValidationException("error during validation of " + object, ex);
         }
     }
 

Modified: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java?rev=938807&r1=938806&r2=938807&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/NestedMetaProperty.java
Wed Apr 28 07:30:23 2010
@@ -61,7 +61,7 @@ final class NestedMetaProperty {
                 } else if (!".".equals(token)) { // it is a property name
                     MetaProperty mp = getMetaBean().getProperty(token);
                     if (mp == null) {
-                        throw new IllegalArgumentException(
+                        throw new UnknownPropertyException(
                                 "unknown property '" + token + "' in " + getMetaBean().getId());
                     }
                     if (getValue() != null) {
@@ -73,8 +73,6 @@ final class NestedMetaProperty {
             }
         } catch (ValidationException ex) {
             throw ex; // route exception
-        } catch (IllegalArgumentException ex) {
-            throw ex; // route exception
         } catch (Exception ex) { // wrap exception
             throw new ValidationException(
                     "invalid property format: " + propertyPath, ex);

Added: incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/UnknownPropertyException.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/UnknownPropertyException.java?rev=938807&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/UnknownPropertyException.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/main/java/org/apache/bval/jsr303/UnknownPropertyException.java
Wed Apr 28 07:30:23 2010
@@ -0,0 +1,49 @@
+/*
+ * 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 javax.validation.ValidationException;
+
+/**
+ * Internal exception thrown when trying to access a property that doesn't exist
+ * in a bean.
+ * 
+ * @author Carlos Vara
+ */
+public class UnknownPropertyException extends ValidationException {
+
+    private static final long serialVersionUID = 1L;
+
+    public UnknownPropertyException(String message) {
+        super(message);
+    }
+
+    public UnknownPropertyException() {
+        super();
+    }
+
+    public UnknownPropertyException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public UnknownPropertyException(Throwable cause) {
+        super(cause);
+    }
+
+}

Added: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java?rev=938807&view=auto
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java
(added)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java
Wed Apr 28 07:30:23 2010
@@ -0,0 +1,215 @@
+/*
+ * 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 junit.framework.Assert;
+import junit.framework.TestCase;
+
+import javax.validation.Validation;
+import javax.validation.ValidationException;
+import javax.validation.Validator;
+import javax.validation.ValidatorFactory;
+import javax.validation.constraints.NotNull;
+import java.util.Locale;
+
+/**
+ * Several checks to validate that the implementation of {@link Validator}
+ * throws the correct exceptions as per the spec.
+ * 
+ * @author Carlos Vara
+ */
+public class ExceptionsContractTest extends TestCase {
+
+    static ValidatorFactory factory;
+
+    static {
+        factory = Validation.buildDefaultValidatorFactory();
+        ((DefaultMessageInterpolator) factory.getMessageInterpolator()).setLocale(Locale.ENGLISH);
+    }
+
+    private Validator getValidator() {
+        return factory.getValidator();
+    }
+
+    /**
+     * Checks that the correct exception is thrown when validating a bean whose
+     * getter throws an exception.
+     */
+    public void testExceptionThrowingBean() {
+        Validator validator = getValidator();
+        try {
+            validator.validate(new ExceptionThrowingBean());
+            Assert.fail("No exception thrown when validating a bean whose getter throws a
RTE");
+        } catch (ValidationException e) {
+            // Correct
+        }
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when passing
+     * <code>null</code> as group array.
+     */
+    public void testValidateNullGroup() {
+        Validator validator = getValidator();
+        try {
+            Class<?>[] groups = null;
+            validator.validate(new String(), groups);
+            Assert.fail("No exception thrown when passing null as group array");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when passing an
+     * invalid property name.
+     */
+    public void testValidateInvalidPropertyName() {
+        Validator validator = getValidator();
+
+        // Null propertyName
+        try {
+            validator.validateProperty(new Person(), null);
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+
+        // Empty propertyName
+        try {
+            validator.validateProperty(new Person(), "");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+
+        // Invalid propertyName
+        try {
+            validator.validateProperty(new Person(), "surname");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when trying to
+     * validate a property on a null object.
+     */
+    public void testValidatePropertyOnNullBean() {
+        Validator validator = getValidator();
+        try {
+            validator.validateProperty(null, "class");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when passing
+     * <code>null</code> as group array in a
+     * {@link Validator#validateProperty(Object, String, Class...)} call.
+     */
+    public void testValidatePropertyNullGroup() {
+        Validator validator = getValidator();
+        try {
+            Class<?>[] groups = null;
+            validator.validateProperty(new Person(), "name", groups);
+            Assert.fail("No exception thrown when passing null as group array");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when calling
+     * {@link Validator#validateValue(Class, String, Object, Class...)} with a
+     * <code>null</code> class.
+     */
+    public void testValidateValueOnNullClass() {
+        Validator validator = getValidator();
+        try {
+            validator.validateValue(null, "class", Object.class);
+            Assert.fail("No exception thrown when passing null as group array");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when passing an
+     * invalid property name to
+     * {@link Validator#validateValue(Class, String, Object, Class...)}.
+     */
+    public void testValidateValueInvalidPropertyName() {
+        Validator validator = getValidator();
+
+        // Null propertyName
+        try {
+            validator.validateValue(Person.class, null, "John");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+
+        // Empty propertyName
+        try {
+            validator.validateValue(Person.class, "", "John");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+
+        // Invalid propertyName
+        try {
+            validator.validateValue(Person.class, "unexistant", "John");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    /**
+     * Checks that an {@link IllegalArgumentException} is thrown when calling
+     * {@link Validator#validateValue(Class, String, Object, Class...)} with a
+     * <code>null</code> group array.
+     */
+    public void testValidateValueNullGroup() {
+        Validator validator = getValidator();
+        try {
+            Class<?>[] groups = null;
+            validator.validateValue(Person.class, "name", "John", groups);
+            Assert.fail("No exception thrown when passing null as group array");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    public static class ExceptionThrowingBean {
+
+        @NotNull
+        public String getValue() {
+            throw new IllegalStateException();
+        }
+
+    }
+
+    public static class Person {
+        
+        @NotNull
+        public String name;
+        
+    }
+
+}



Mime
View raw message