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/05 22:53:03 UTC

svn commit: r1031833 - in /incubator/bval/sandbox/lang3-work/bval-jsr303/src: main/java/org/apache/bval/jsr303/ test/java/org/apache/bval/jsr303/

Author: mbenson
Date: Fri Nov  5 21:53:03 2010
New Revision: 1031833

URL: http://svn.apache.org/viewvc?rev=1031833&view=rev
Log:
throw IllegalArgumentException when an incompatible value is passed to Validator.validateValue()

Added:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java   (with props)
Modified:
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
    incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java?rev=1031833&r1=1031832&r2=1031833&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/ClassValidator.java Fri Nov  5 21:53:03 2010
@@ -45,6 +45,7 @@ import org.apache.bval.util.AccessStrate
 import org.apache.bval.util.ValidationHelper;
 import org.apache.commons.lang3.ClassUtils;
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.reflect.TypeUtils;
 
 // TODO: centralize treatMapsLikeBeans
 
@@ -55,6 +56,8 @@ import org.apache.commons.lang3.ObjectUt
  * <p>
  * API class
  * 
+ * @version $Rev$ $Date$
+ * 
  * @author Roman Stumm
  * @author Carlos Vara
  */
@@ -503,7 +506,7 @@ public class ClassValidator implements C
      * @return a {@link RuntimeException} of the appropriate type
      */
     protected static RuntimeException unrecoverableValidationError(RuntimeException ex, Object object) {
-        if (ex instanceof UnknownPropertyException) {
+        if (ex instanceof UnknownPropertyException || ex instanceof IncompatiblePropertyValueException) {
             // Convert to IllegalArgumentException
             return new IllegalArgumentException(ex.getMessage(), ex);
         } else if (ex instanceof ValidationException) {
@@ -708,6 +711,10 @@ public class ClassValidator implements C
             MetaProperty prop = context.getMetaProperty();
             boolean fixed = false;
             if (value != VALIDATE_PROPERTY) {
+                if (!TypeUtils.isAssignable(value == null ? null : value.getClass(), contextTraversal.getType())) {
+                    throw new IncompatiblePropertyValueException(String.format(
+                        "%3$s is not a valid value for property %2$s of type %1$s", beanType, propertyName, value));
+                }
                 if (prop == null) {
                     context.setBean(value);
                 } else {

Added: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java?rev=1031833&view=auto
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java (added)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java Fri Nov  5 21:53:03 2010
@@ -0,0 +1,69 @@
+/*
+ * 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 validate a value for a property for which it is not assignment-compatible.
+ * 
+ * @version $Rev$ $Date$
+ * 
+ * @author Matt Benson
+ */
+public class IncompatiblePropertyValueException extends ValidationException {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Create a new {@link IncompatiblePropertyValueException} instance.
+     * 
+     * @param message
+     */
+    public IncompatiblePropertyValueException(String message) {
+        super(message);
+    }
+
+    /**
+     * Create a new IncompatiblePropertyValueException instance.
+     */
+    public IncompatiblePropertyValueException() {
+        super();
+    }
+
+    /**
+     * Create a new IncompatiblePropertyValueException instance.
+     * 
+     * @param message
+     * @param cause
+     */
+    public IncompatiblePropertyValueException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    /**
+     * Create a new IncompatiblePropertyValueException instance.
+     * 
+     * @param cause
+     */
+    public IncompatiblePropertyValueException(Throwable cause) {
+        super(cause);
+    }
+
+}

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/sandbox/lang3-work/bval-jsr303/src/main/java/org/apache/bval/jsr303/IncompatiblePropertyValueException.java
------------------------------------------------------------------------------
    svn:keywords = Revision Date

Modified: incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java?rev=1031833&r1=1031832&r2=1031833&view=diff
==============================================================================
--- incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java (original)
+++ incubator/bval/sandbox/lang3-work/bval-jsr303/src/test/java/org/apache/bval/jsr303/ExceptionsContractTest.java Fri Nov  5 21:53:03 2010
@@ -207,6 +207,27 @@ public class ExceptionsContractTest exte
     }
 
     /**
+     * Enforces the "not a valid object property" part of the {@link IllegalArgumentException}
+     * declaration on {@link Validator#validateValue(Class, String, Object, Class...)}
+     */
+    public void testValidateIncompatibleValue() {
+        try {
+            Class<?>[] groups = null;
+            validator.validateValue(Person.class, "name", 666, groups);
+            Assert.fail("No exception thrown when passing Integer for string value");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+        try {
+            Class<?>[] groups = null;
+            validator.validateValue(Person.class, "age", null, groups);
+            Assert.fail("No exception thrown when passing null for primitive value");
+        } catch (IllegalArgumentException e) {
+            // Correct
+        }
+    }
+
+    /**
      * Checks that an {@link IllegalArgumentException} is thrown when calling
      * {@link BeanDescriptor#getConstraintsForProperty(String)} with an invalid
      * property name.
@@ -243,6 +264,7 @@ public class ExceptionsContractTest exte
         @NotNull
         public String name;
 
+        public int age;
     }
 
 }