incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From carlosv...@apache.org
Subject svn commit: r960471 - in /incubator/bval/trunk/bval-jsr303/src: main/java/org/apache/bval/jsr303/extensions/MethodValidatorImpl.java test/java/org/apache/bval/jsr303/extensions/MethodValidatorImplTest.java
Date Mon, 05 Jul 2010 07:47:16 GMT
Author: carlosvara
Date: Mon Jul  5 07:47:16 2010
New Revision: 960471

URL: http://svn.apache.org/viewvc?rev=960471&view=rev
Log:
BVAL-72: added extra test and code to check for null descriptors

Modified:
    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/extensions/MethodValidatorImplTest.java

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=960471&r1=960470&r2=960471&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
Mon Jul  5 07:47:16 2010
@@ -24,6 +24,7 @@ import org.apache.bval.jsr303.groups.Gro
 import org.apache.bval.model.MetaBean;
 
 import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
 import javax.validation.metadata.ConstraintDescriptor;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
@@ -83,6 +84,9 @@ class MethodValidatorImpl extends ClassV
         (MethodBeanDescriptorImpl) getConstraintsForClass(clazz);
     MethodDescriptorImpl methodDescriptor =
         (MethodDescriptorImpl) beanDesc.getConstraintsForMethod(method);
+    if ( methodDescriptor == null ) {
+        throw new ValidationException("Method " + method + " doesn't belong to class " +
clazz);
+    }
     return validateParameters(methodDescriptor.getMetaBean(),
         methodDescriptor.getParameterDescriptors(), parameters, groupArray);
   }
@@ -95,6 +99,9 @@ class MethodValidatorImpl extends ClassV
         (MethodBeanDescriptorImpl) getConstraintsForClass(clazz);
     MethodDescriptorImpl methodDescriptor =
         (MethodDescriptorImpl) beanDesc.getConstraintsForMethod(method);
+    if ( methodDescriptor == null ) {
+        throw new ValidationException("Method " + method + " doesn't belong to class " +
clazz);
+    }
     ParameterDescriptorImpl paramDesc = (ParameterDescriptorImpl) methodDescriptor
         .getParameterDescriptors().get(parameterIndex);
     return validateParameter(paramDesc, parameter, groupArray);
@@ -108,6 +115,9 @@ class MethodValidatorImpl extends ClassV
         (MethodBeanDescriptorImpl) getConstraintsForClass(clazz);
     ConstructorDescriptorImpl constructorDescriptor =
         (ConstructorDescriptorImpl) beanDesc.getConstraintsForConstructor(constructor);
+    if ( constructorDescriptor == null ) {
+        throw new ValidationException("Constructor " + constructor + " doesn't belong to
class " + clazz);
+    }
     return validateParameters(constructorDescriptor.getMetaBean(),
         constructorDescriptor.getParameterDescriptors(), parameters, groupArray);
   }
@@ -119,9 +129,12 @@ class MethodValidatorImpl extends ClassV
                                                            Class<?>... groupArray)
{
     MethodBeanDescriptorImpl beanDesc =
         (MethodBeanDescriptorImpl) getConstraintsForClass(clazz);
-    ConstructorDescriptorImpl methodDescriptor =
+    ConstructorDescriptorImpl constructorDescriptor =
         (ConstructorDescriptorImpl) beanDesc.getConstraintsForConstructor(constructor);
-    ParameterDescriptorImpl paramDesc = (ParameterDescriptorImpl) methodDescriptor
+    if ( constructorDescriptor == null ) {
+        throw new ValidationException("Constructor " + constructor + " doesn't belong to
class " + clazz);
+    }
+    ParameterDescriptorImpl paramDesc = (ParameterDescriptorImpl) constructorDescriptor
         .getParameterDescriptors().get(parameterIndex);
     return validateParameter(paramDesc, parameter, groupArray);
   }
@@ -137,6 +150,9 @@ class MethodValidatorImpl extends ClassV
         (MethodBeanDescriptorImpl) getConstraintsForClass(clazz);
     MethodDescriptorImpl methodDescriptor =
         (MethodDescriptorImpl) beanDesc.getConstraintsForMethod(method);
+    if ( methodDescriptor == null ) {
+        throw new ValidationException("Method " + method + " doesn't belong to class " +
clazz);
+    }
     final GroupValidationContext<ConstraintValidationListener<Object>> context
=
         createContext(methodDescriptor.getMetaBean(), returnedValue, null, groupArray);
     validateReturnedValueInContext(context, methodDescriptor);

Modified: incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/extensions/MethodValidatorImplTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/extensions/MethodValidatorImplTest.java?rev=960471&r1=960470&r2=960471&view=diff
==============================================================================
--- incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/extensions/MethodValidatorImplTest.java
(original)
+++ incubator/bval/trunk/bval-jsr303/src/test/java/org/apache/bval/jsr303/extensions/MethodValidatorImplTest.java
Mon Jul  5 07:47:16 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.bval.jsr303.extensions;
 
+import junit.framework.Assert;
 import junit.framework.Test;
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
@@ -23,7 +24,10 @@ import org.apache.bval.jsr303.ApacheVali
 import org.apache.bval.jsr303.ClassValidator;
 import org.apache.bval.jsr303.extensions.ExampleMethodService.Person;
 
+import javax.validation.ValidationException;
 import javax.validation.Validator;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.util.Set;
@@ -215,6 +219,61 @@ public class MethodValidatorImplTest ext
         results = mv.validateParameters(service.getClass(), personOp2, new Object[]{p});
         assertTrue("No violations expected", results.isEmpty());
     }
+    
+    
+    /**
+     * Validate a method defined in an interface using the following combinations:
+     * <ul>
+     *  <li>impl.class + impl.method</li>
+     *  <li>interface.class + interface.method</li>
+     *  <li>impl.class + interface.method</li>
+     *  <li>interface.class + impl.method</li>
+     * </ul>
+     */
+    public void testValidateImplementedMethod() throws NoSuchMethodException {
+        UserMethodsImpl um = new UserMethodsImpl();
+        MethodValidator mv = getValidator().unwrap(MethodValidator.class);
+        
+        Method classMethod = um.getClass().getMethod("findUser", new Class[]{String.class,
String.class, Integer.class});
+        Method ifaceMethod = UserMethods.class.getMethod("findUser", new Class[]{String.class,
String.class, Integer.class});
+        
+        Set<?> results;
+        
+        // Validate from class (should create violations)
+        results = mv.validateParameters(um.getClass(), classMethod, new Object[]{"", "valid",
null });
+        assertEquals("Invalid number of violations", 2, results.size());
+        
+        // Validate from interface
+        results = mv.validateParameters(UserMethods.class, ifaceMethod, new Object[]{"",
"valid", null });
+        assertEquals("Invalid number of violations", 0, results.size());
+        
+        // Invalid combinations
+        try {
+            results = mv.validateParameters(UserMethods.class, classMethod, new Object[]{"",
"valid", null });
+            Assert.fail("Exception not thrown when validating interface.class + impl.method");
+        } catch (ValidationException e) {
+            // Expected
+        }
+        try {
+            results = mv.validateParameters(um.getClass(), ifaceMethod, new Object[]{"",
"valid", null });
+            Assert.fail("Exception not thrown when validating impl.class + interface.method");
+        } catch (ValidationException e) {
+            // Expected
+        }
+        
+    }
+    
+    public static interface UserMethods {
+        void findUser(String param1, String param2, Integer param3);
+    }
+    
+    public static class UserMethodsImpl implements UserMethods {
+        @Override
+        public void findUser( @Size( min=1 ) String param1, @NotNull String param2, @NotNull
Integer param3) {
+            return;
+        }        
+    }
+    
 
     private Validator getValidator() {
         return ApacheValidatorFactory.getDefault().getValidator();



Mime
View raw message