Author: hardy.ferentschik
Date: 2009-03-12 14:11:00 -0400 (Thu, 12 Mar 2009)
New Revision: 16146
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java
Modified:
validator/trunk/hibernate-validator/pom.xml
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.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/validatorresolution/Suburb.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/ValidatorTypeTest.java
Log:
HV-90 - Added jtype 0.1 as dependency and use the maven shade plugin to include the class
files directly into the jar file
Modified: validator/trunk/hibernate-validator/pom.xml
===================================================================
--- validator/trunk/hibernate-validator/pom.xml 2009-03-11 13:55:02 UTC (rev 16145)
+++ validator/trunk/hibernate-validator/pom.xml 2009-03-12 18:11:00 UTC (rev 16146)
@@ -28,6 +28,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
+ <dependency>
+ <groupId>com.googlecode.jtype</groupId>
+ <artifactId>jtype</artifactId>
+ <version>0.1.0</version>
+ </dependency>
<dependency>
<groupId>org.slf4j</groupId>
@@ -67,6 +72,28 @@
</resources>
<plugins>
<plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <artifactSet>
+ <includes>
+
<include>com.googlecode.jtype:jtype</include>
+ </includes>
+ </artifactSet>
+ <transformers>
+ <transformer
implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/>
+ </transformers>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
<groupId>org.jboss.maven.plugins</groupId>
<artifactId>maven-jdocbook-plugin</artifactId>
<version>2.1.0</version>
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-03-11
13:55:02 UTC (rev 16145)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConstraintTree.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -19,6 +19,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -29,6 +30,7 @@
import javax.validation.UnexpectedTypeException;
import javax.validation.ValidationException;
+import com.googlecode.jtype.TypeUtils;
import org.slf4j.Logger;
import org.hibernate.validation.util.LoggerFactory;
@@ -36,7 +38,7 @@
/**
* Due to constraint conposition a single constraint annotation can lead to a whole
constraint tree beeing validated.
- * This class encapsulates such a tree for a single constraint annotation.
+ * This class encapsulates such a tree.
*
* @author Hardy Ferentschik
*/
@@ -88,16 +90,27 @@
return descriptor;
}
- public <T, V> void validateConstraints(V value, ExecutionContext<T>
executionContext, List<ConstraintViolationImpl<T>> constraintViolations) {
+ /**
+ * Validates the specified value.
+ *
+ * @param value The value to validate
+ * @param type The type of the value determined from the type the annotation was placed
on.
+ * @param executionContext The current execution context.
+ * @param constraintViolations List of constraint violation into which to accumulate all
constraint violation as we traverse
+ * this <code>ConstraintTree </code>.
+ * @param <T> Type of the root bean for the current validation.
+ * @param <V> Type of the value to be validated.
+ */
+ public <T, V> void validateConstraints(V value, Type type,
ExecutionContext<T> executionContext, List<ConstraintViolationImpl<T>>
constraintViolations) {
for ( ConstraintTree<?> tree : getChildren() ) {
- tree.validateConstraints( value, executionContext, constraintViolations );
+ tree.validateConstraints( value, type, executionContext, constraintViolations );
}
if ( log.isTraceEnabled() ) {
log.trace( "Validating value {} against constraint defined by {}", value,
descriptor );
}
ConstraintValidator<A, V> validator = getInitalizedValidator(
- value, executionContext.getConstraintValidatorFactory()
+ value, type, executionContext.getConstraintValidatorFactory()
);
executionContext.setCurrentConstraintDescriptor( descriptor );
if ( !validator.isValid( value, executionContext ) ) {
@@ -119,58 +132,65 @@
/**
* @param value The value to be validated.
+ * @param type The type of the value to be validated (the type of the member/class the
constraint was placed on).
* @param constraintFactory constraint factory used to instantiate the constraint
validator.
*
* @return A initalized constraint validator matching the type of the value to be
validated.
*/
- private <V> ConstraintValidator<A, V> getInitalizedValidator(Object value,
ConstraintValidatorFactory constraintFactory) {
- Class<? extends ConstraintValidator<?, ?>> validatorClass;
- //FIXME This sounds really bad, why value can be null. Why are we deciding of the
validator based on the value?
- if ( value == null ) {
- validatorClass = descriptor.getConstraintValidatorClasses().get( 0 );
- }
- else {
- validatorClass = findMatchingValidatorClass( value );
- }
+ private <V> ConstraintValidator<A, V> getInitalizedValidator(V value, Type
type, ConstraintValidatorFactory constraintFactory) {
+ Class<? extends ConstraintValidator<?, ?>> validatorClass =
findMatchingValidatorClass( value, type );
+
@SuppressWarnings("unchecked")
- ConstraintValidator<?, ?> constraintValidator =
- constraintFactory.getInstance( validatorClass );
+ ConstraintValidator<A, V> constraintValidator = ( ConstraintValidator<A, V>
) constraintFactory.getInstance(
+ validatorClass
+ );
initializeConstraint( descriptor, constraintValidator );
- return ( ConstraintValidator<A, V> ) constraintValidator;
+ return constraintValidator;
}
/**
* Runs the validator resolution algorithm.
*
- * @param value the object to validate
+ * @param value The value to be validated.
+ * @param type The type of the value to be validated (the type of the member/class the
constraint was placed on).
*
* @return The class of a matching validator.
*/
- private Class<? extends ConstraintValidator<?, ?>>
findMatchingValidatorClass(Object value) {
+ private Class<? extends ConstraintValidator<?, ?>>
findMatchingValidatorClass(Object value, Type type) {
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes
=
+ ValidatorTypeHelper.getValidatorsTypes( descriptor.getConstraintValidatorClasses()
);
- Class valueClass = determineValueClass( value );
+ List<Type> suitableTypes = new ArrayList<Type>();
+ findSuitableValidatorTypes( type, validatorsTypes, suitableTypes );
+ if ( value != null ) {
+ findSuitableValidatorTypes( determineValueClass( value ), validatorsTypes,
suitableTypes );
+ }
- Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>>
validatorsTypes =
- ValidatorTypeHelper.getValidatorsTypes( ( List<Class<? extends
ConstraintValidator<A, ?>>> ) descriptor.getConstraintValidatorClasses() );
- List<Class> assignableClasses = findAssignableClasses( valueClass,
validatorsTypes );
+ resolveAssignableTypes( suitableTypes );
+ verifyResolveWasUnique( type, suitableTypes );
- resolveAssignableClasses( assignableClasses );
- verifyResolveWasUnique( valueClass, assignableClasses );
+ return validatorsTypes.get( suitableTypes.get( 0 ) );
+ }
- return validatorsTypes.get( assignableClasses.get( 0 ) );
+ private Class determineValueClass(Object value) {
+ Class valueClass = value.getClass();
+ if ( valueClass.isArray() ) {
+ valueClass = Array.class;
+ }
+ return valueClass;
}
- private void verifyResolveWasUnique(Class valueClass, List<Class>
assignableClasses) {
+ private void verifyResolveWasUnique(Type valueClass, List<Type> assignableClasses)
{
if ( assignableClasses.size() == 0 ) {
- throw new UnexpectedTypeException( "No validator could be found for type: "
+ valueClass.getName() );
+ throw new UnexpectedTypeException( "No validator could be found for type: "
+ valueClass );
}
else if ( assignableClasses.size() > 1 ) {
StringBuilder builder = new StringBuilder();
builder.append( "There are multiple validators which could validate the type
" );
builder.append( valueClass );
builder.append( ". The validator classes are: " );
- for ( Class clazz : assignableClasses ) {
- builder.append( clazz.getName() );
+ for ( Type clazz : assignableClasses ) {
+ builder.append( clazz );
builder.append( ", " );
}
builder.delete( builder.length() - 2, builder.length() );
@@ -178,54 +198,44 @@
}
}
- private List<Class> findAssignableClasses(Class valueClass, Map<Class<?>,
Class<? extends ConstraintValidator<?, ?>>> validatorsTypes) {
- List<Class> assignableClasses = new ArrayList<Class>();
- for ( Class clazz : validatorsTypes.keySet() ) {
- if ( clazz.isAssignableFrom( valueClass ) ) {
- assignableClasses.add( clazz );
+ private void findSuitableValidatorTypes(Type type, Map<Type, Class<? extends
ConstraintValidator<?, ?>>> validatorsTypes, List<Type> suitableTypes)
{
+ for ( Type validatorType : validatorsTypes.keySet() ) {
+ if ( TypeUtils.isAssignable( validatorType, type ) && !suitableTypes.contains(
validatorType ) ) {
+ suitableTypes.add( validatorType );
}
}
- 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.
*
- * @param assignableClasses The set of all classes which are assignable to the class of
the value to be validated and
+ * @param assignableTypes The set of all classes which are assignable to the class of
the value to be validated and
* which are handled by at least one of the validators for the specified constraint.
*/
- private void resolveAssignableClasses(List<Class> assignableClasses) {
- if ( assignableClasses.size() == 0 || assignableClasses.size() == 1 ) {
+ private void resolveAssignableTypes(List<Type> assignableTypes) {
+ if ( assignableTypes.size() == 0 || assignableTypes.size() == 1 ) {
return;
}
- List<Class> classesToRemove = new ArrayList<Class>();
+ List<Type> typesToRemove = new ArrayList<Type>();
do {
- classesToRemove.clear();
- Class clazz = assignableClasses.get( 0 );
- for ( int i = 1; i < assignableClasses.size(); i++ ) {
- if ( clazz.isAssignableFrom( assignableClasses.get( i ) ) ) {
- classesToRemove.add( clazz );
+ typesToRemove.clear();
+ Type type = assignableTypes.get( 0 );
+ for ( int i = 1; i < assignableTypes.size(); i++ ) {
+ if ( TypeUtils.isAssignable( type, assignableTypes.get( i ) ) ) {
+ typesToRemove.add( type );
}
- else if ( assignableClasses.get( i ).isAssignableFrom( clazz ) ) {
- classesToRemove.add( assignableClasses.get( i ) );
+ else if ( TypeUtils.isAssignable( assignableTypes.get( i ), type ) ) {
+ typesToRemove.add( assignableTypes.get( i ) );
}
}
- assignableClasses.removeAll( classesToRemove );
- } while ( classesToRemove.size() > 0 );
+ assignableTypes.removeAll( typesToRemove );
+ } while ( typesToRemove.size() > 0 );
}
- private void initializeConstraint
+ private <V> void initializeConstraint
(ConstraintDescriptor<A>
- descriptor, ConstraintValidator
+ descriptor, ConstraintValidator<A, V>
constraintValidator) {
try {
constraintValidator.initialize( descriptor.getAnnotation() );
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java 2009-03-11
13:55:02 UTC (rev 16145)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/MetaConstraint.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -19,14 +19,16 @@
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
+import java.lang.reflect.Array;
import java.lang.reflect.Field;
+import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintDescriptor;
-import javax.validation.ValidationException;
import org.hibernate.validation.util.ReflectionHelper;
@@ -44,24 +46,16 @@
private final ConstraintTree<A> constraintTree;
/**
- * The type (class) the constraint was defined on. <code>null</code> if the
constraint was specified on method or
- * field level.
+ * The type the constraint was defined on.
*/
private final Type type;
/**
- * The method the constraint was defined on. <code>null</code> if the
constraint was specified on class or
- * field level.
+ * The member the constraint was defined on.
*/
- private final Method method;
+ private final Member member;
/**
- * The field the constraint was defined on. <code>null</code> if the
constraint was specified on class or
- * method level.
- */
- private final Field field;
-
- /**
* The JavaBeans name for this constraint.
*/
private final String propertyName;
@@ -77,32 +71,28 @@
*/
private final Class<T> beanClass;
- public MetaConstraint(Type t, ConstraintDescriptor<A> constraintDescriptor) {
- this( t, null, null, ElementType.TYPE, ( Class<T> ) t.getClass(),
constraintDescriptor, "" );
+ public MetaConstraint(Type type, ConstraintDescriptor<A> constraintDescriptor) {
+ this.type = type;
+ this.elementType = ElementType.TYPE;
+ this.member = null;
+ this.propertyName = "";
+ this.beanClass = ( Class<T> ) type.getClass();
+ constraintTree = new ConstraintTree<A>( constraintDescriptor );
}
- public MetaConstraint(Method m, Class<T> beanClass, ConstraintDescriptor<A>
constraintDescriptor) {
- this(
- null,
- m,
- null,
- ElementType.METHOD,
- beanClass,
- constraintDescriptor,
- ReflectionHelper.getPropertyName( m )
- );
- }
-
- public MetaConstraint(Field f, Class<T> beanClass, ConstraintDescriptor<A>
constraintDescriptor) {
- this( null, null, f, ElementType.FIELD, beanClass, constraintDescriptor, f.getName()
);
- }
-
- private MetaConstraint(Type t, Method m, Field f, ElementType elementType,
Class<T> beanClass, ConstraintDescriptor<A> constraintDescriptor, String
property) {
- this.type = t;
- this.method = m;
- this.field = f;
- this.elementType = elementType;
- this.propertyName = property;
+ public MetaConstraint(Member member, Class<T> beanClass,
ConstraintDescriptor<A> constraintDescriptor) {
+ if ( member instanceof Method ) {
+ this.elementType = ElementType.METHOD;
+ }
+ else if ( member instanceof Field ) {
+ this.elementType = ElementType.FIELD;
+ }
+ else {
+ throw new IllegalArgumentException( "Non allowed member type: " + member );
+ }
+ this.type = null;
+ this.member = member;
+ this.propertyName = ReflectionHelper.getPropertyName( member );
this.beanClass = beanClass;
constraintTree = new ConstraintTree<A>( constraintDescriptor );
}
@@ -117,31 +107,6 @@
}
/**
- * @param o the object from which to retrieve the value.
- *
- * @return Returns the value for this constraint from the specified object. Depending on
the type either the value itself
- * is returned of method or field access is used to access the value.
- */
- public Object getValue(Object o) {
- switch ( elementType ) {
- case TYPE: {
- return o;
- }
- case METHOD: {
- return ReflectionHelper.getValue( method, o );
- }
- case FIELD: {
- return ReflectionHelper.getValue( field, o );
- }
- default: {
- throw new ValidationException(
- "Invalid state of MetaConstraint. Parameter elementType has unexpected value -
" + elementType
- );
- }
- }
- }
-
- /**
* @return Returns <code>true</code> in case the constraint is defined on a
collection, <code>false</code>
* otherwise.
*/
@@ -163,18 +128,6 @@
return constraintTree.getDescriptor();
}
- public Method getMethod() {
- return method;
- }
-
- public Field getField() {
- return field;
- }
-
- public Type getType() {
- return type;
- }
-
public Class<T> getBeanClass() {
return beanClass;
}
@@ -195,7 +148,7 @@
final Object leafBeanInstance = executionContext.peekCurrentBean();
Object value = getValue( leafBeanInstance );
List<ConstraintViolationImpl<T>> constraintViolations = new
ArrayList<ConstraintViolationImpl<T>>();
- constraintTree.validateConstraints( value, executionContext, constraintViolations );
+ constraintTree.validateConstraints( value, typeOfAnnoatedElement(), executionContext,
constraintViolations );
if ( constraintViolations.size() > 0 ) {
executionContext.addConstraintFailures( constraintViolations );
return false;
@@ -205,7 +158,7 @@
public <T> boolean validateConstraint(Object value, ExecutionContext<T>
executionContext) {
List<ConstraintViolationImpl<T>> constraintViolations = new
ArrayList<ConstraintViolationImpl<T>>();
- constraintTree.validateConstraints( value, executionContext, constraintViolations );
+ constraintTree.validateConstraints( value, typeOfAnnoatedElement(), executionContext,
constraintViolations );
if ( constraintViolations.size() > 0 ) {
executionContext.addConstraintFailures( constraintViolations );
return false;
@@ -220,20 +173,30 @@
t = type;
break;
}
- case METHOD: {
- t = ReflectionHelper.typeOf( method );
- break;
+ default: {
+ t = ReflectionHelper.typeOf( member );
+ if ( t instanceof Class && ((Class) t).isArray()) {
+ t = Array.class;
+ }
}
- case FIELD: {
- t = ReflectionHelper.typeOf( field );
- break;
+ }
+ return t;
+ }
+
+ /**
+ * @param o the object from which to retrieve the value.
+ *
+ * @return Returns the value for this constraint from the specified object. Depending on
the type either the value itself
+ * is returned of method or field access is used to access the value.
+ */
+ private Object getValue(Object o) {
+ switch ( elementType ) {
+ case TYPE: {
+ return o;
}
default: {
- throw new ValidationException(
- "Invalid state of MetaConstraint. Parameter elementType has unexpected value -
" + elementType
- );
+ return ReflectionHelper.getValue( member, o );
}
}
- return t;
}
}
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-03-11
13:55:02 UTC (rev 16145)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/util/ValidatorTypeHelper.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -45,14 +45,14 @@
* @return Return a Map<Class, Class<? extends
ConstraintValidator>> where the map
* key is the type the validator accepts and value the validator class itself.
*/
- public static <T extends Annotation> Map<Class<?>, Class<? extends
ConstraintValidator<?, ?>>> getValidatorsTypes(
+ public static <T extends Annotation> Map<Type, Class<? extends
ConstraintValidator<?, ?>>> getValidatorsTypes(
List<Class<? extends ConstraintValidator<T, ?>>> validators) {
if ( validators == null || validators.size() == 0 ) {
throw new ValidationException( "No ConstraintValidators associated to
@Constraint" );
}
else {
- Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>>
validatorsTypes =
- new HashMap<Class<?>, Class<? extends ConstraintValidator<?,
?>>>();
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>>
validatorsTypes =
+ new HashMap<Type, Class<? extends ConstraintValidator<?, ?>>>();
for ( Class<? extends ConstraintValidator<?, ?>> validator : validators )
{
validatorsTypes.put( extractType( validator ), validator );
}
@@ -60,7 +60,7 @@
}
}
- private static Class<?> extractType(Class<? extends ConstraintValidator<?,
?>> validator) {
+ private static Type extractType(Class<? extends ConstraintValidator<?, ?>>
validator) {
Map<Type, Type> resolvedTypes = new HashMap<Type, Type>();
Type constraintValidatorType = resolveTypes( resolvedTypes, validator );
@@ -77,7 +77,7 @@
validatorType = resolvedTypes.get( validatorType );
}
//FIXME raise an exception if validatorType is not a class
- return ( Class<?> ) validatorType;
+ return validatorType;
}
private static Type resolveTypes(Map<Type, Type> resolvedTypes, Type type) {
Copied:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java
(from rev 16121,
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Ambigious.java)
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -0,0 +1,42 @@
+// $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.lang.annotation.Documented;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+
+/**
+ * A test constraint which can lead to a error when trying to reslove the validator.
+ *
+ * @author Hardy Ferentschik
+ */
+@Constraint(validatedBy = { PostCodeListValidatorForString.class,
PostCodeListValidatorForNumber.class })
+@Documented
+@Target({ METHOD, FIELD, TYPE })
+@Retention(RUNTIME)
+public @interface PostCodeList {
+ public abstract String message() default "foobar";
+
+ public abstract Class<?>[] groups() default { };
+}
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeList.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:mergeinfo
+
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForNumber.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -0,0 +1,34 @@
+// $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.Collection;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PostCodeListValidatorForNumber implements
ConstraintValidator<PostCodeList, Collection<? extends Number>> {
+ public void initialize(PostCodeList constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<? extends Number> value,
ConstraintValidatorContext constraintValidatorContext) {
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/PostCodeListValidatorForString.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -0,0 +1,37 @@
+// $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.Collection;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * @author Hardy Ferentschik
+ */
+public class PostCodeListValidatorForString implements
ConstraintValidator<PostCodeList, Collection<String>> {
+ public void initialize(PostCodeList constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<String> value, ConstraintValidatorContext
constraintValidatorContext) {
+ if(value == null) {
+ return true;
+ }
+ return false;
+ }
+}
Modified:
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 2009-03-11
13:55:02 UTC (rev 16145)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/Suburb.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -17,10 +17,12 @@
*/
package org.hibernate.validation.engine.validatorresolution;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import java.util.ArrayList;
import javax.validation.constraints.Size;
/**
@@ -43,6 +45,17 @@
@Size(min = 4, max = 1000)
private Coordinate[] boundingBox;
+ @PostCodeList
+ private Collection<? extends Number> includedPostCodes;
+
+ public void setIncludedPostCodes(Collection<? extends Number> includedPostCodes)
{
+ this.includedPostCodes = includedPostCodes;
+ }
+
+ public Collection<? extends Number> getIncludedPostcodes() {
+ return includedPostCodes;
+ }
+
public void setName(String name) {
this.name = name;
}
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-03-11
13:55:02 UTC (rev 16145)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/validatorresolution/ValidatorResolutionTest.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -18,6 +18,8 @@
package org.hibernate.validation.engine.validatorresolution;
import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
import javax.validation.ConstraintViolation;
import javax.validation.UnexpectedTypeException;
import javax.validation.Validator;
@@ -72,7 +74,7 @@
fail();
}
catch ( UnexpectedTypeException e ) {
- assertEquals( "No validator could be found for type: java.lang.Integer",
e.getMessage() );
+ assertEquals( "No validator could be found for type: class
java.lang.Integer", e.getMessage() );
}
}
@@ -82,6 +84,10 @@
Suburb suburb = new Suburb();
+ List<Integer> postcodes = new ArrayList<Integer>();
+ postcodes.add(12345);
+ suburb.setIncludedPostCodes( postcodes );
+
// all values are null and should pass
Set<ConstraintViolation<Suburb>> constraintViolations = validator.validate(
suburb );
assertNumberOfViolations( constraintViolations, 0 );
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-03-11
13:55:02 UTC (rev 16145)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/ValidatorTypeTest.java 2009-03-12
18:11:00 UTC (rev 16146)
@@ -18,6 +18,7 @@
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;
@@ -41,7 +42,7 @@
List<Class<? extends ConstraintValidator<FrenchZipcode, ?>>>
validators =
new ArrayList<Class<? extends ConstraintValidator<FrenchZipcode,
?>>>();
validators.add( FrenchZipcodeConstraintValidator.class );
- Map<Class<?>, Class<? extends ConstraintValidator<?, ?>>>
validatorsTypes = ValidatorTypeHelper
+ Map<Type, Class<? extends ConstraintValidator<?, ?>>> validatorsTypes
= ValidatorTypeHelper
.getValidatorsTypes( validators );
assertEquals( FrenchZipcodeConstraintValidator.class, validatorsTypes.get( String.class
) );
}