[hibernate-commits] Hibernate SVN: r15901 - in validator/trunk/hibernate-validator/src: main/java/org/hibernate/validation/engine and 4 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Thu Feb 5 06:24:24 EST 2009
Author: hardy.ferentschik
Date: 2009-02-05 06:24:23 -0500 (Thu, 05 Feb 2009)
New Revision: 15901
Added:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForArray.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForCollection.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForMap.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForString.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Coordinate.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java
Removed:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidator.java
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MaxValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MinValidatorForNumber.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BuiltinConstraints.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintCompositionTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java
Log:
BVAL-90 added some more tests and made the code work with arrays
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MaxValidatorForNumber.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MaxValidatorForNumber.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MaxValidatorForNumber.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -48,12 +48,9 @@
else if ( value instanceof BigInteger ) {
return ( ( BigInteger ) value ).compareTo( BigInteger.valueOf( maxValue ) ) != 1;
}
- else if ( value instanceof Number ) {
+ else {
double doubleValue = value.doubleValue();
return doubleValue <= maxValue;
}
- else {
- return false;
- }
}
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MinValidatorForNumber.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MinValidatorForNumber.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/MinValidatorForNumber.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -48,12 +48,10 @@
else if ( value instanceof BigInteger ) {
return ( ( BigInteger ) value ).compareTo( BigInteger.valueOf( minValue ) ) != -1;
}
- else if ( value instanceof Number ) {
+ else {
double doubleValue = value.doubleValue();
return doubleValue >= minValue;
}
- else {
- return false;
- }
+
}
}
Deleted: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidator.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidator.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidator.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -1,68 +0,0 @@
-// $Id$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, and individual contributors
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* Licensed 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.hibernate.validation.constraints;
-
-import java.lang.reflect.Array;
-import java.util.Collection;
-import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
-import javax.validation.constraints.Size;
-
-/**
- * Check that a string's length is between min and max.
- *
- * @author Emmanuel Bernard
- * @author Gavin King
- */
-//FIXME split into several type-safe validators
-public class SizeValidator implements ConstraintValidator<Size, Object> {
- private int min;
- private int max;
-
- public void initialize(Size parameters) {
- min = parameters.min();
- max = parameters.max();
- }
-
- /**
- * {@inheritDoc}
- */
- public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
- if ( value == null ) {
- return true;
- }
-
- int size;
- if ( value instanceof String ) {
- String string = ( String ) value;
- size = string.length();
-
- }
- else if ( value instanceof Collection ) {
- Collection collection = ( Collection ) value;
- size = collection.size();
- }
- else if ( value instanceof Array ) {
- size = Array.getLength( value );
- }
- else {
- throw new IllegalArgumentException( "Expected String type." );
- }
- return size >= min && size <= max;
- }
-}
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForArray.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForArray.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForArray.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -0,0 +1,56 @@
+// $Id: SizeValidatorForString.java 15853 2009-02-02 15:42:10Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints;
+
+import java.lang.reflect.Array;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.Size;
+
+/**
+ * Check that a string's length is between min and max.
+ *
+ * @author Hardy Ferentschik
+ */
+public class SizeValidatorForArray implements ConstraintValidator<Size, Object[]> {
+ private int min;
+ private int max;
+
+ public void initialize(Size parameters) {
+ min = parameters.min();
+ max = parameters.max();
+ }
+
+ /**
+ * Checks the number of entries in an array.
+ *
+ * @param array The array to validate.
+ * @param constraintValidatorContext context in which the constraint is evaluated.
+ *
+ * @return Returns <code>true</code> if the array is <code>null</code> or the number of entries in
+ * <code>array</code> is between the specified <code>min</code> and <code>max</code> values (inclusive),
+ * <code>false</code> otherwise.
+ */
+ public boolean isValid(Object[] array, ConstraintValidatorContext constraintValidatorContext) {
+ if ( array == null ) {
+ return true;
+ }
+ int length = Array.getLength( array );
+ return length >= min && length <= max;
+ }
+}
\ No newline at end of file
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForCollection.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForCollection.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForCollection.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -0,0 +1,56 @@
+// $Id: SizeValidatorForString.java 15853 2009-02-02 15:42:10Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints;
+
+import java.util.Collection;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.Size;
+
+/**
+ * Check that a string's length is between min and max.
+ *
+ * @author Hardy Ferentschik
+ */
+public class SizeValidatorForCollection implements ConstraintValidator<Size, Collection> {
+ private int min;
+ private int max;
+
+ public void initialize(Size parameters) {
+ min = parameters.min();
+ max = parameters.max();
+ }
+
+ /**
+ * Checks the number of entries in a map.
+ *
+ * @param collection The collection to validate.
+ * @param constraintValidatorContext context in which the constraint is evaluated.
+ *
+ * @return Returns <code>true</code> if the collection is <code>null</code> or the number of entries in
+ * <code>collection</code> is between the specified <code>min</code> and <code>max</code> values (inclusive),
+ * <code>false</code> otherwise.
+ */
+ public boolean isValid(Collection collection, ConstraintValidatorContext constraintValidatorContext) {
+ if ( collection == null ) {
+ return true;
+ }
+ int length = collection.size();
+ return length >= min && length <= max;
+ }
+}
\ No newline at end of file
Added: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForMap.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForMap.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForMap.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -0,0 +1,56 @@
+// $Id: SizeValidatorForString.java 15853 2009-02-02 15:42:10Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints;
+
+import java.util.Map;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.Size;
+
+/**
+ * Check that a string's length is between min and max.
+ *
+ * @author Hardy Ferentschik
+ */
+public class SizeValidatorForMap implements ConstraintValidator<Size, Map> {
+ private int min;
+ private int max;
+
+ public void initialize(Size parameters) {
+ min = parameters.min();
+ max = parameters.max();
+ }
+
+ /**
+ * Checks the number of entries in a map.
+ *
+ * @param map The map to validate.
+ * @param constraintValidatorContext context in which the constraint is evaluated.
+ *
+ * @return Returns <code>true</code> if the map is <code>null</code> or the number of entries in <code>map</code>
+ * is between the specified <code>min</code> and <code>max</code> values (inclusive),
+ * <code>false</code> otherwise.
+ */
+ public boolean isValid(Map map, ConstraintValidatorContext constraintValidatorContext) {
+ if ( map == null ) {
+ return true;
+ }
+ int size = map.size();
+ return size >= min && size <= max;
+ }
+}
\ No newline at end of file
Copied: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForString.java (from rev 15883, validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidator.java)
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForString.java (rev 0)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForString.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -0,0 +1,56 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.constraints;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+import javax.validation.constraints.Size;
+
+/**
+ * Check that a string's length is between min and max.
+ *
+ * @author Emmanuel Bernard
+ * @author Gavin King
+ * @author Hardy Ferentschik
+ */
+public class SizeValidatorForString implements ConstraintValidator<Size, String> {
+ private int min;
+ private int max;
+
+ public void initialize(Size parameters) {
+ min = parameters.min();
+ max = parameters.max();
+ }
+
+ /**
+ * Checks the length of the specified string.
+ *
+ * @param s The string to validate.
+ * @param constraintValidatorContext context in which the constraint is evaluated.
+ *
+ * @return Returns <code>true</code> if the string is <code>null</code> or the length of <code>s</code> between the specified
+ * <code>min</code> and <code>max</code> values (inclusive), <code>false</code> otherwise.
+ */
+ public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
+ if ( s == null ) {
+ return true;
+ }
+ int length = s.length();
+ return length >= min && length <= max;
+ }
+}
Property changes on: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/constraints/SizeValidatorForString.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BuiltinConstraints.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BuiltinConstraints.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/BuiltinConstraints.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -45,7 +45,10 @@
import org.hibernate.validation.constraints.NullValidator;
import org.hibernate.validation.constraints.PastValidatorForCalendar;
import org.hibernate.validation.constraints.PastValidatorForDate;
-import org.hibernate.validation.constraints.SizeValidator;
+import org.hibernate.validation.constraints.SizeValidatorForString;
+import org.hibernate.validation.constraints.SizeValidatorForCollection;
+import org.hibernate.validation.constraints.SizeValidatorForArray;
+import org.hibernate.validation.constraints.SizeValidatorForMap;
/**
* @author Hardy Ferentschik
@@ -94,7 +97,10 @@
builtinConstraints.put( Future.class, constraintList );
constraintList = new ArrayList<Class<? extends ConstraintValidator<?, ?>>>();
- constraintList.add( SizeValidator.class );
+ constraintList.add( SizeValidatorForString.class );
+ constraintList.add( SizeValidatorForCollection.class );
+ constraintList.add( SizeValidatorForArray.class );
+ constraintList.add( SizeValidatorForMap.class );
builtinConstraints.put( Size.class, constraintList );
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -18,6 +18,7 @@
package org.hibernate.validation.engine;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -162,17 +163,13 @@
* @return The class of a matching validator.
*/
private Class findMatchingValidatorClass(Object value) {
- Class valueClass = value.getClass();
+ Class valueClass = determineValueClass( value );
+
Map<Class<?>, Class<? extends ConstraintValidator<? extends Annotation, ?>>> validatorsTypes = ValidatorTypeHelper
.getValidatorsTypes( descriptor.getConstraintValidatorClasses() );
- List<Class> assignableClasses = new ArrayList<Class>();
- for ( Class clazz : validatorsTypes.keySet() ) {
- if ( clazz.isAssignableFrom( valueClass ) ) {
- assignableClasses.add( clazz );
- }
- }
+ List<Class> assignableClasses = findAssingableClasses( valueClass, validatorsTypes );
resolveAssignableClasses( assignableClasses );
if ( assignableClasses.size() != 1 ) {
@@ -191,6 +188,24 @@
return validatorsTypes.get( assignableClasses.get( 0 ) );
}
+ private List<Class> findAssingableClasses(Class valueClass, Map<Class<?>, Class<? extends ConstraintValidator<? extends Annotation, ?>>> validatorsTypes) {
+ List<Class> assignableClasses = new ArrayList<Class>();
+ for ( Class clazz : validatorsTypes.keySet() ) {
+ if ( clazz.isAssignableFrom( valueClass ) ) {
+ assignableClasses.add( clazz );
+ }
+ }
+ return assignableClasses;
+ }
+
+ private Class determineValueClass(Object value) {
+ Class valueClass = value.getClass();
+ if ( valueClass.isArray() ) {
+ valueClass = Array.class;
+ }
+ return valueClass;
+ }
+
/**
* Tries to reduce all assignable classes down to a single class.
*
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ReflectionHelper.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -152,9 +152,15 @@
}
}
}
- catch ( Exception e ) {
+ catch ( NoSuchMethodException nsme ) {
// ignore
}
+ catch ( IllegalAccessException iae ) {
+ // ignore
+ }
+ catch ( InvocationTargetException ite ) {
+ // ignore
+ }
return isMultiValueConstraint;
}
@@ -181,9 +187,15 @@
}
}
}
- catch ( Exception e ) {
+ catch ( NoSuchMethodException nsme ) {
// ignore
}
+ catch ( IllegalAccessException iae ) {
+ // ignore
+ }
+ catch ( InvocationTargetException ite ) {
+ // ignore
+ }
return annotationList;
}
Modified: validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java
===================================================================
--- validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -18,6 +18,8 @@
package org.hibernate.validation.util;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
+import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
@@ -59,36 +61,23 @@
}
private static Class<?> extractType(Class<? extends ConstraintValidator<?, ?>> validator) {
-
Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
Type constraintValidatorType = resolveTypes( resolvedTypes, validator );
//we now have all bind from a type to its resolution at one level
-
- //FIXME throw assertion exception if constraintValidatorType == null
Type validatorType = ( ( ParameterizedType ) constraintValidatorType ).getActualTypeArguments()[VALIDATOR_TYPE_INDEX];
- while ( resolvedTypes.containsKey( validatorType ) ) {
- validatorType = resolvedTypes.get( validatorType );
+ if ( validatorType == null ) {
+ throw new ValidationException( "Null is an invalid type for a constraint validator." );
}
- //FIXME raise an exception if validatorType is not a class
- return ( Class<?> ) validatorType;
- }
+ else if ( validatorType instanceof GenericArrayType ) {
+ validatorType = Array.class;
+ }
- //TEst method, remove
- public static Type extractTypeLoose(Class<? extends ConstraintValidator<?, ?>> validator) {
-
- Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
- Type constraintValidatorType = resolveTypes( resolvedTypes, validator );
-
- //we now have all bind from a type to its resolution at one level
-
- //FIXME throw assertion exception if constraintValidatorType == null
- Type validatorType = ( ( ParameterizedType ) constraintValidatorType ).getActualTypeArguments()[VALIDATOR_TYPE_INDEX];
while ( resolvedTypes.containsKey( validatorType ) ) {
validatorType = resolvedTypes.get( validatorType );
}
//FIXME raise an exception if validatorType is not a class
- return validatorType;
+ return ( Class<?> ) validatorType;
}
private static Type resolveTypes(Map<Type, Type> resolvedTypes, Type type) {
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintCompositionTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintCompositionTest.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/ConstraintCompositionTest.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -24,10 +24,6 @@
import static org.junit.Assert.fail;
import org.junit.Test;
-import org.hibernate.validation.constraints.NotNullValidator;
-import org.hibernate.validation.constraints.PatternValidator;
-import org.hibernate.validation.constraints.SizeValidator;
-import org.hibernate.validation.constraints.composition.GermanZipcodeConstraintValidator;
import org.hibernate.validation.eg.FrenchAddress;
import org.hibernate.validation.eg.GermanAddress;
import static org.hibernate.validation.util.TestUtil.assertConstraintViolation;
@@ -54,7 +50,6 @@
assertConstraintViolation(
constraintViolations.iterator().next(),
"may not be null",
- NotNullValidator.class,
FrenchAddress.class,
null,
"zipCode"
@@ -69,7 +64,6 @@
assertConstraintViolation(
violation,
"A french zip code has a length of 5",
- SizeValidator.class,
FrenchAddress.class,
"abc",
"zipCode"
@@ -79,7 +73,6 @@
assertConstraintViolation(
violation,
"must match \"d*\"",
- PatternValidator.class,
FrenchAddress.class,
"abc",
"zipCode"
@@ -89,7 +82,6 @@
assertConstraintViolation(
violation,
"must match \".....\"",
- PatternValidator.class,
FrenchAddress.class,
"abc",
"zipCode"
@@ -109,7 +101,6 @@
assertConstraintViolation(
violation,
"A french zip code has a length of 5",
- SizeValidator.class,
FrenchAddress.class,
"123",
"zipCode"
@@ -119,7 +110,6 @@
assertConstraintViolation(
violation,
"must match \".....\"",
- PatternValidator.class,
FrenchAddress.class,
"123",
"zipCode"
@@ -148,7 +138,6 @@
assertConstraintViolation(
constraintViolations.iterator().next(),
"Falsche Postnummer.",
- GermanZipcodeConstraintValidator.class,
GermanAddress.class,
null,
"zipCode"
@@ -170,7 +159,6 @@
assertConstraintViolation(
constraintViolations.iterator().next(),
"Falsche Postnummer.",
- GermanZipcodeConstraintValidator.class,
GermanAddress.class,
"abc",
"zipCode"
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Coordinate.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Coordinate.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Coordinate.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -0,0 +1,32 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.engine.validatorresolution;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Coordinate {
+
+ long longitude;
+ long latidute;
+
+ public Coordinate(long longitude, long latidute) {
+ this.longitude = longitude;
+ this.latidute = latidute;
+ }
+}
Added: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java (rev 0)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -0,0 +1,83 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, and individual contributors
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* Licensed 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.hibernate.validation.engine.validatorresolution;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import javax.validation.constraints.Size;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class Suburb {
+ public enum Facility {
+ SHOPPING_MALL, BUS_TERMINAL
+ }
+
+ @Size(min = 5, max = 10)
+ private String name;
+
+ @Size(min = 2, max = 2)
+ private Map<Facility, Boolean> facilities;
+
+ @Size(min = 2)
+ private Set<String> streetNames;
+
+ @Size(min = 4, max = 1000)
+ private Coordinate[] boundingBox;
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public Map<Facility, Boolean> getFacilities() {
+ return facilities;
+ }
+
+ public void addFacility(Facility f, Boolean exist) {
+ if ( facilities == null ) {
+ facilities = new HashMap<Facility, Boolean>();
+ }
+ facilities.put( f, exist );
+ }
+
+ public Set<String> getStreetNames() {
+ return streetNames;
+ }
+
+ public void addStreetName(String streetName) {
+ if ( streetNames == null ) {
+ streetNames = new HashSet<String>();
+ }
+ streetNames.add( streetName );
+ }
+
+ public Coordinate[] getBoundingBox() {
+ return boundingBox;
+ }
+
+ public void setBoundingBox(Coordinate[] boundingBox) {
+ this.boundingBox = boundingBox;
+ }
+}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -27,6 +27,7 @@
import org.junit.Test;
import org.hibernate.validation.eg.MultipleMinMax;
+import static org.hibernate.validation.util.TestUtil.assertConstraintViolation;
import static org.hibernate.validation.util.TestUtil.assertNumberOfViolations;
import static org.hibernate.validation.util.TestUtil.getValidator;
@@ -59,4 +60,80 @@
assertTrue( e.getMessage().startsWith( "There are multiple validators" ) );
}
}
+
+ @Test
+ public void testMultipleSizeValidators() {
+ Validator validator = getValidator();
+
+ Suburb suburb = new Suburb();
+
+ // all values are null and should pass
+ Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ suburb.setName( "" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(), "size must be between 5 and 10", Suburb.class, "", "name"
+ );
+
+ suburb.setName( "Hoegsbo" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ suburb.addFacility( Suburb.Facility.SHOPPING_MALL, false );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "size must be between 2 and 2",
+ Suburb.class,
+ suburb.getFacilities(),
+ "facilities"
+ );
+
+ suburb.addFacility( Suburb.Facility.BUS_TERMINAL, true );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ suburb.addStreetName( "Sikelsgatan" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "size must be between 2 and 2147483647",
+ Suburb.class,
+ suburb.getStreetNames(),
+ "streetNames"
+ );
+
+ suburb.addStreetName( "Marklandsgatan" );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+
+ Coordinate[] boundingBox = new Coordinate[3];
+ boundingBox[0] = new Coordinate( 0l, 0l );
+ boundingBox[1] = new Coordinate( 0l, 1l );
+ boundingBox[2] = new Coordinate( 1l, 0l );
+ suburb.setBoundingBox( boundingBox );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 1 );
+ assertConstraintViolation(
+ constraintViolations.iterator().next(),
+ "size must be between 4 and 1000",
+ Suburb.class,
+ suburb.getBoundingBox(),
+ "boundingBox"
+ );
+
+ boundingBox = new Coordinate[4];
+ boundingBox[0] = new Coordinate( 0l, 0l );
+ boundingBox[1] = new Coordinate( 0l, 1l );
+ boundingBox[2] = new Coordinate( 1l, 0l );
+ boundingBox[3] = new Coordinate( 1l, 1l );
+ suburb.setBoundingBox( boundingBox );
+ constraintViolations = validator.validate( suburb );
+ assertNumberOfViolations( constraintViolations, 0 );
+ }
}
\ No newline at end of file
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -48,48 +48,39 @@
return hibernateValidator;
}
- public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class constraintType, Class rootBean, Object invalidValue, String propertyPath, Class leafBean) {
+ public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class rootBean, Object invalidValue, String propertyPath, Class leafBean) {
assertEquals(
"Wrong leaf bean type",
leafBean,
violation.getLeafBean().getClass()
);
- assertConstraintViolation( violation, errorMessage, constraintType, rootBean, invalidValue, propertyPath );
+ assertConstraintViolation( violation, errorMessage, rootBean, invalidValue, propertyPath );
}
- public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class constraintType, Class rootBean, Object invalidValue, String propertyPath) {
+ public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class rootBean, Object invalidValue, String propertyPath) {
assertEquals(
"Wrong propertyPath",
propertyPath,
violation.getPropertyPath()
);
- assertConstraintViolation( violation, errorMessage, constraintType, rootBean, invalidValue );
+ assertConstraintViolation( violation, errorMessage, rootBean, invalidValue );
}
- public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class constraintType, Class rootBean, Object invalidValue) {
+ public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class rootBean, Object invalidValue) {
assertEquals(
"Wrong invalid value",
invalidValue,
violation.getInvalidValue()
);
- assertConstraintViolation( violation, errorMessage, constraintType, rootBean );
+ assertConstraintViolation( violation, errorMessage, rootBean );
}
- public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class constraintType, Class rootBean) {
+ public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class rootBean) {
assertEquals(
"Wrong root bean type",
rootBean,
violation.getRootBean().getClass()
);
- assertConstraintViolation( violation, errorMessage, constraintType );
- }
-
- public static void assertConstraintViolation(ConstraintViolation violation, String errorMessage, Class constraintType) {
- assertEquals(
- "Wrong constraint error Type",
- constraintType,
- violation.getConstraintDescriptor().getConstraintValidatorClasses().get(0)
- );
assertConstraintViolation( violation, errorMessage );
}
Modified: validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java
===================================================================
--- validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java 2009-02-05 08:27:17 UTC (rev 15900)
+++ validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java 2009-02-05 11:24:23 UTC (rev 15901)
@@ -18,14 +18,10 @@
package org.hibernate.validation.util;
import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import java.util.Set;
import javax.validation.ConstraintValidator;
-import javax.validation.ConstraintValidatorContext;
-import javax.validation.constraints.NotNull;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
@@ -46,32 +42,5 @@
Map<Class<?>, Class<? extends ConstraintValidator<? extends Annotation, ?>>> validatorsTypes = ValidatorTypeHelper
.getValidatorsTypes( validators );
assertEquals( FrenchZipcodeConstraintValidator.class, validatorsTypes.get( String.class ) );
-
- Type type = ValidatorTypeHelper
- .extractTypeLoose( TestValidator.class );
-
-
- Type type2 = ValidatorTypeHelper
- .extractTypeLoose( TestValidator2.class );
- assertEquals( type, type2 );
-
}
-
- public static class TestValidator implements ConstraintValidator<NotNull, Set<String>> {
- public void initialize(NotNull constraintAnnotation) {
- }
-
- public boolean isValid(Set<String> object, ConstraintValidatorContext constraintValidatorContext) {
- return false;
- }
- }
-
- public static class TestValidator2 implements ConstraintValidator<NotNull, Set<String>> {
- public void initialize(NotNull constraintAnnotation) {
- }
-
- public boolean isValid(Set<String> object, ConstraintValidatorContext constraintValidatorContext) {
- return true;
- }
- }
}
\ No newline at end of file
More information about the hibernate-commits
mailing list