Author: hardy.ferentschik
Date: 2009-12-10 10:44:02 -0500 (Thu, 10 Dec 2009)
New Revision: 18199
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLong.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLongValidator.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidationUsingBoxing.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ClassLevelValidation.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomer.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomerValidator.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/FieldLevelValidationUsingComposedConstraint.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/ValidOrderNumber.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposedConstraint.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForGregorianCalendar.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForList.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForString.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForArrayList.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCalendar.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCollection.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/FieldLevelValidationUsingComplexComposedConstraint.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/ValidCustomerNumber.java
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java
validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/FieldLevelValidationUsingBuiltInConstraints.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CaseMode.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCase.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCaseValidator.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/util/DiagnosticExpection.java
Log:
HV-269 - Applied Gunnar's latest patch (with some additional formatting)
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintAnnotationVisitor.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -20,14 +20,17 @@
import java.text.MessageFormat;
import java.util.List;
import java.util.ResourceBundle;
-
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
+import javax.lang.model.element.Element;
import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.element.Modifier;
+import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementKindVisitor6;
import javax.tools.Diagnostic.Kind;
+import javax.validation.Constraint;
import org.hibernate.validator.ap.util.ConstraintHelper;
@@ -35,53 +38,73 @@
* An {@link ElementVisitor} that visits elements (type declarations, methods
* and fields) annotated with constraint annotations from the Bean Validation
* API.
- *
- * TODO GM: visit type declarations.
- *
+ *
* @author Gunnar Morling.
- *
*/
-final class ConstraintAnnotationVisitor extends
- ElementKindVisitor6<Void, List<AnnotationMirror>> {
-
- private ProcessingEnvironment processingEnvironment;
-
- private ResourceBundle errorMessages;
+final class ConstraintAnnotationVisitor extends ElementKindVisitor6<Void,
List<AnnotationMirror>> {
+ private final ProcessingEnvironment processingEnvironment;
+ private final ResourceBundle errorMessages;
+
//TODO GM: make configurable using Options API
- private Kind messagingKind = Kind.ERROR;
+ private final Kind messagingKind = Kind.ERROR;
+ private final ConstraintHelper constraintHelper;
+
public ConstraintAnnotationVisitor(ProcessingEnvironment processingEnvironment) {
-
+
this.processingEnvironment = processingEnvironment;
- errorMessages =
ResourceBundle.getBundle("org.hibernate.validator.ap.ValidationProcessorMessages");
+
+ errorMessages = ResourceBundle.getBundle(
"org.hibernate.validator.ap.ValidationProcessorMessages" );
+ constraintHelper = new ConstraintHelper(
+ processingEnvironment.getElementUtils(), processingEnvironment.getTypeUtils()
+ );
}
-
+
/**
* <p>
* Checks whether the given mirrors representing one or more constraint annotations are
correctly
* specified at the given method. The following checks are performed:</p>
* <ul>
* <li>
- * The method must be a JavaBeans getter method.
- * </li>
+ * The method must be a JavaBeans getter method (name starts with "is",
"get" or "has", no parameters)
+ * </li>
* <li>
- * TODO GM:
* The return type of the method must be supported by the constraints.
* </li>
+ * <li>
+ * The method must not be static.
+ * </li>
* </ul>
*/
@Override
public Void visitExecutableAsMethod(ExecutableElement element,
- List<AnnotationMirror> mirrors) {
-
- for (AnnotationMirror oneAnnotationMirror : mirrors) {
- if(!isJavaBeanGetterName(element.getSimpleName().toString())) {
-
- processingEnvironment.getMessager().printMessage(
- messagingKind,
- errorMessages.getString("ONLY_GETTERS_MAY_BE_ANNOTATED"), element,
oneAnnotationMirror);
+ List<AnnotationMirror> mirrors) {
+
+ for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+
+ if ( !isJavaBeanGetterName( element.getSimpleName().toString() ) ||
+ hasParameters( element ) ) {
+
+ reportError( element, oneAnnotationMirror, "ONLY_GETTERS_MAY_BE_ANNOTATED"
);
+
+ continue;
}
+
+ if ( isStaticMethod( element ) ) {
+
+ reportError( element, oneAnnotationMirror,
"STATIC_METHODS_MAY_NOT_BE_ANNOTATED" );
+
+ continue;
+ }
+
+ if ( !constraintHelper.isAnnotationAllowedAtMethod(
oneAnnotationMirror.getAnnotationType(), element ) ) {
+
+ reportError(
+ element, oneAnnotationMirror, "NOT_SUPPORTED_RETURN_TYPE",
+ oneAnnotationMirror.getAnnotationType().asElement().getSimpleName()
+ );
+ }
}
return null;
}
@@ -98,27 +121,124 @@
*/
@Override
public Void visitVariableAsField(VariableElement annotatedField,
List<AnnotationMirror> mirrors) {
-
- for (AnnotationMirror oneAnnotationMirror : mirrors) {
-
- ConstraintHelper builtInConstraintHelper =
- new ConstraintHelper(processingEnvironment.getElementUtils(),
processingEnvironment.getTypeUtils());
-
- boolean annotationAllowedAtElementType =
- builtInConstraintHelper.isAnnotationAllowedAtElement(oneAnnotationMirror.getAnnotationType(),
annotatedField);
-
- if(!annotationAllowedAtElementType) {
- processingEnvironment.getMessager().printMessage(
- messagingKind,
- MessageFormat.format(errorMessages.getString("NOT_SUPPORTED_TYPE"),
oneAnnotationMirror.getAnnotationType().asElement().getSimpleName()), annotatedField,
oneAnnotationMirror);
+
+ for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+
+ if ( !constraintHelper.isAnnotationAllowedAtField(
+ oneAnnotationMirror.getAnnotationType(), annotatedField
+ ) ) {
+
+ reportError(
+ annotatedField, oneAnnotationMirror, "NOT_SUPPORTED_TYPE",
+ oneAnnotationMirror.getAnnotationType().asElement().getSimpleName()
+ );
}
}
-
+
return null;
}
-
- private boolean isJavaBeanGetterName(String name) {
- return name.startsWith("is") || name.startsWith("has") ||
name.startsWith("get");
+
+ /**
+ * <p>
+ * Checks whether the given mirrors representing one or more constraint
+ * annotations are correctly specified at the given annotation type
+ * declaration. The only annotation types allowed to be annotated with a
+ * constraint annotation are other (composed) constraint annotation type
+ * declarations.
+ * </p>
+ */
+ @Override
+ public Void visitTypeAsAnnotationType(TypeElement e,
+ List<AnnotationMirror> mirrors) {
+
+ if ( !isConstraintAnnotation( e ) ) {
+
+ for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+ reportError( e, oneAnnotationMirror,
"ONLY_CONSTRAINT_ANNOTATIONS_MAY_BE_ANNOTATED" );
+ }
+ }
+
+ return null;
}
-
+
+ @Override
+ public Void visitTypeAsClass(TypeElement e, List<AnnotationMirror> p) {
+ return visitClassOrInterfaceOrEnumType( e, p );
+ }
+
+ @Override
+ public Void visitTypeAsEnum(TypeElement e, List<AnnotationMirror> p) {
+ return visitClassOrInterfaceOrEnumType( e, p );
+ }
+
+ @Override
+ public Void visitTypeAsInterface(TypeElement e, List<AnnotationMirror> p) {
+ return visitClassOrInterfaceOrEnumType( e, p );
+ }
+
+ // ==================================
+ // private API below
+ // ==================================
+
+ private Void visitClassOrInterfaceOrEnumType(TypeElement annotatedType,
+ List<AnnotationMirror> mirrors) {
+
+ for ( AnnotationMirror oneAnnotationMirror : mirrors ) {
+
+ if ( !constraintHelper.isAnnotationAllowedAtType(
+ oneAnnotationMirror.getAnnotationType(), annotatedType
+ ) ) {
+
+ reportError(
+ annotatedType, oneAnnotationMirror, "NOT_SUPPORTED_TYPE",
+ oneAnnotationMirror.getAnnotationType().asElement().getSimpleName()
+ );
+ }
+ }
+
+ return null;
+ }
+
+ private boolean hasParameters(ExecutableElement method) {
+ return !method.getParameters().isEmpty();
+ }
+
+ private boolean isJavaBeanGetterName(String methodName) {
+ return methodName.startsWith( "is" ) || methodName.startsWith(
"has" ) || methodName.startsWith( "get" );
+ }
+
+ private boolean isStaticMethod(ExecutableElement method) {
+ return method.getModifiers().contains( Modifier.STATIC );
+ }
+
+ private boolean isConstraintAnnotation(TypeElement e) {
+ return e.getAnnotation( Constraint.class ) != null;
+ }
+
+ /**
+ * Reports an error at the given location using the given message key and
+ * optionally the given message parameters.
+ *
+ * @param element The element at which the error shall be reported.
+ * @param annotationMirror The annotation mirror at which the error shall be reported.
+ * @param messageKey The message key to be used to retrieve the text.
+ * @param messageParameters An optional array of message parameters to be put into the
+ * message using a {@link MessageFormat}.
+ */
+ private void reportError(Element element, AnnotationMirror annotationMirror, String
messageKey, Object... messageParameters) {
+
+ String message;
+
+ if ( messageParameters == null ) {
+ message = errorMessages.getString( messageKey );
+ }
+ else {
+ message = MessageFormat.format( errorMessages.getString( messageKey ),
messageParameters );
+ }
+
+ processingEnvironment.getMessager().printMessage(
+ messagingKind, message, element, annotationMirror
+ );
+ }
+
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/ConstraintValidationProcessor.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -19,7 +19,6 @@
import java.util.List;
import java.util.Set;
-
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
@@ -27,12 +26,12 @@
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
+import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.TypeElement;
import org.hibernate.validator.ap.util.AnnotationApiHelper;
import org.hibernate.validator.ap.util.ConstraintHelper;
-
/**
* Annotation processor for validating Bean Validation constraints.
*
@@ -42,43 +41,44 @@
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class ConstraintValidationProcessor extends AbstractProcessor {
-
/**
* Whether this processor claims all processed annotations exclusively or not.
*/
private static final boolean ANNOTATIONS_CLAIMED_EXCLUSIVELY = false;
-
-
+
+
@Override
public boolean process(
- final Set<? extends TypeElement> annotations,
- final RoundEnvironment roundEnvironment) {
-
- AnnotationApiHelper typeHelper = new
AnnotationApiHelper(processingEnv.getElementUtils(), processingEnv.getTypeUtils());
- ConstraintAnnotationVisitor v = new ConstraintAnnotationVisitor(processingEnv);
- ConstraintHelper constraintHelper = new
ConstraintHelper(processingEnv.getElementUtils(), processingEnv.getTypeUtils());
-
- for (TypeElement oneAnnotation : annotations) {
-
+ final Set<? extends TypeElement> annotations,
+ final RoundEnvironment roundEnvironment) {
+
+ AnnotationApiHelper typeHelper = new AnnotationApiHelper(
+ processingEnv.getElementUtils(), processingEnv.getTypeUtils()
+ );
+ ElementVisitor<Void, List<AnnotationMirror>> visitor = new
ConstraintAnnotationVisitor( processingEnv );
+ ConstraintHelper constraintHelper = new ConstraintHelper(
+ processingEnv.getElementUtils(), processingEnv.getTypeUtils()
+ );
+
+ for ( TypeElement oneAnnotation : annotations ) {
+
//only constraint annotations are relevant
- if(!constraintHelper.isConstraintAnnotation(oneAnnotation)) {
+ if ( !constraintHelper.isConstraintAnnotation( oneAnnotation ) ) {
continue;
}
-
- Set<? extends Element> elementsWithConstraintAnnotation =
- roundEnvironment.getElementsAnnotatedWith(oneAnnotation);
-
- for (Element oneAnnotatedElement : elementsWithConstraintAnnotation) {
-
- List<AnnotationMirror> mirrorsOfCurrentAnnotation =
- typeHelper.filterByType(oneAnnotatedElement.getAnnotationMirrors(),
oneAnnotation.asType());
-
-
- oneAnnotatedElement.accept(v, mirrorsOfCurrentAnnotation);
+
+ Set<? extends Element> elementsWithConstraintAnnotation =
+ roundEnvironment.getElementsAnnotatedWith( oneAnnotation );
+
+ for ( Element oneAnnotatedElement : elementsWithConstraintAnnotation ) {
+
+ List<AnnotationMirror> mirrorsOfCurrentAnnotation =
+ typeHelper.filterByType( oneAnnotatedElement.getAnnotationMirrors(),
oneAnnotation.asType() );
+
+ oneAnnotatedElement.accept( visitor, mirrorsOfCurrentAnnotation );
}
- }
-
+ }
+
return ANNOTATIONS_CLAIMED_EXCLUSIVELY;
}
-
}
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/java/org/hibernate/validator/ap/util/ConstraintHelper.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -18,8 +18,10 @@
package org.hibernate.validator.ap.util;
import java.lang.annotation.Annotation;
+import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
+import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -31,15 +33,29 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
+import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.PrimitiveType;
+import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
+import javax.lang.model.util.SimpleAnnotationValueVisitor6;
+import javax.lang.model.util.TypeKindVisitor6;
import javax.lang.model.util.Types;
import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
+import javax.validation.constraints.AssertFalse;
import javax.validation.constraints.AssertTrue;
+import javax.validation.constraints.DecimalMax;
+import javax.validation.constraints.DecimalMin;
+import javax.validation.constraints.Digits;
+import javax.validation.constraints.Future;
+import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Null;
+import javax.validation.constraints.Past;
+import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
/**
@@ -58,7 +74,7 @@
*/
private final Name CONSTRAINT_ANNOTATION_PACKAGE_NAME;
- private static Map<Name, Set<TypeElement>> builtInConstraints;
+ private static Map<Name, Set<TypeMirror>> builtInConstraints;
private Elements elementUtils;
@@ -73,11 +89,21 @@
builtInConstraints = CollectionHelper.newHashMap();
- //TODO GM: register all types
- registerAllowedTypesForBuiltInConstraint(Size.class,
CollectionHelper.<Class<?>>asSet(Collection.class, String.class));
- registerAllowedTypesForBuiltInConstraint(AssertTrue.class,
CollectionHelper.<Class<?>>asSet(Boolean.class, boolean.class));
+ registerAllowedTypesForBuiltInConstraint(AssertFalse.class,
CollectionHelper.<Class<?>>asSet(Boolean.class));
+ registerAllowedTypesForBuiltInConstraint(AssertTrue.class,
CollectionHelper.<Class<?>>asSet(Boolean.class));
+ registerAllowedTypesForBuiltInConstraint(DecimalMax.class,
CollectionHelper.<Class<?>>asSet(Number.class, String.class));
+ registerAllowedTypesForBuiltInConstraint(DecimalMin.class,
CollectionHelper.<Class<?>>asSet(Number.class, String.class));
+ registerAllowedTypesForBuiltInConstraint(Digits.class,
CollectionHelper.<Class<?>>asSet(Number.class, String.class));
+ registerAllowedTypesForBuiltInConstraint(Future.class,
CollectionHelper.<Class<?>>asSet(Calendar.class, Date.class));
+ registerAllowedTypesForBuiltInConstraint(Max.class,
CollectionHelper.<Class<?>>asSet(Number.class, String.class));
+ registerAllowedTypesForBuiltInConstraint(Min.class,
CollectionHelper.<Class<?>>asSet(Number.class, String.class));
registerAllowedTypesForBuiltInConstraint(NotNull.class,
CollectionHelper.<Class<?>>asSet(Object.class));
- registerAllowedTypesForBuiltInConstraint(Min.class,
CollectionHelper.<Class<?>>asSet(Integer.class, Long.class));
+ registerAllowedTypesForBuiltInConstraint(Null.class,
CollectionHelper.<Class<?>>asSet(Object.class));
+ registerAllowedTypesForBuiltInConstraint(Past.class,
CollectionHelper.<Class<?>>asSet(Calendar.class, Date.class));
+ registerAllowedTypesForBuiltInConstraint(Pattern.class,
CollectionHelper.<Class<?>>asSet(String.class));
+
+ //TODO GM: register all array types
+ registerAllowedTypesForBuiltInConstraint(Size.class,
CollectionHelper.<Class<?>>asSet(Collection.class, Map.class, String.class,
boolean[].class));
}
/**
@@ -86,7 +112,8 @@
* {@link Constraint} meta-annotation (which is only allowed at annotation
* declarations).
*
- * @param typeElement The element of interest.
+ * @param The
+ * element of interest.
* @return True, if the given element is a constraint annotation type, false
* otherwise.
*/
@@ -94,47 +121,77 @@
return typeElement.getAnnotation(Constraint.class) != null;
}
- public boolean isAnnotationAllowedAtElement(DeclaredType annotationType, Element
annotatedElement) {
+ public boolean isAnnotationAllowedAtType(DeclaredType annotationType, TypeElement
annotatedType) {
- Set<TypeElement> allowedTypesForConstraint =
getAllowedTypesForConstraint(annotationType);
+ return isAnnotationAllowed(annotationType, annotatedType.asType());
+ }
- if(allowedTypesForConstraint.isEmpty()) {
- return false;
- }
+ public boolean isAnnotationAllowedAtMethod(DeclaredType annotationType,
ExecutableElement annotatedMethod) {
- Element typeElementOfAnnotatedElement =
typeUtils.asElement(annotatedElement.asType());
+ return isAnnotationAllowed(annotationType, annotatedMethod.getReturnType());
+ }
+ public boolean isAnnotationAllowedAtField(DeclaredType annotationType, VariableElement
annotatedField) {
- if(allowedTypesForConstraint.contains(typeElementOfAnnotatedElement)) {
- return true;
- }
+ return isAnnotationAllowed(annotationType, annotatedField.asType());
+ }
- List<? extends TypeMirror> directSupertypes =
typeUtils.directSupertypes(annotatedElement.asType());
+ // ==================================
+ // private API below
+ // ==================================
+ private Set<TypeMirror> getAllSuperTypes(TypeMirror type) {
+
+ Set<TypeMirror> allSuperTypes = CollectionHelper.newHashSet();
+
+ List<? extends TypeMirror> directSupertypes = typeUtils.directSupertypes(type);
+
while(!directSupertypes.isEmpty()) {
- List<TypeMirror> nextSupertypes = CollectionHelper.newArrayList();
- for (TypeMirror oneSuperType : directSupertypes) {
- Element oneSuperTypeAsElement = typeUtils.asElement(oneSuperType);
+ for (TypeMirror typeMirror : directSupertypes) {
+ allSuperTypes.add(typeMirror);
+ }
- if(allowedTypesForConstraint.contains(oneSuperTypeAsElement)) {
- return true;
- }
+ List<TypeMirror> nextSuperTypes = CollectionHelper.newArrayList();
- nextSupertypes.addAll(typeUtils.directSupertypes(oneSuperType));
+ for (TypeMirror typeMirror : directSupertypes) {
+ nextSuperTypes.addAll(typeUtils.directSupertypes(typeMirror));
}
+ directSupertypes = nextSuperTypes;
+ }
- directSupertypes = nextSupertypes;
+
+ return allSuperTypes;
+ }
+
+ /**
+ * Checks whether the given annotation type may be specified at elements of
+ * the specified type.
+ */
+ private boolean isAnnotationAllowed(DeclaredType annotationType, TypeMirror
typeOfAnnotatedElement) {
+
+ //TODO GM: implement array type checking
+ if(typeOfAnnotatedElement.getKind() == TypeKind.ARRAY) {
+ return true;
}
+ //convert primitive types into their corresponding boxed type
+ if(typeOfAnnotatedElement.getKind().isPrimitive()) {
+ typeOfAnnotatedElement =
(DeclaredType)typeUtils.boxedClass((PrimitiveType)typeOfAnnotatedElement).asType();
+ }
+ Set<TypeMirror> allowedTypesForConstraint =
getAllowedTypesForConstraint(annotationType);
+
+ //is the annotation allowed at the given type?
+ for (TypeMirror oneAllowedType : allowedTypesForConstraint) {
+ if(typeUtils.isAssignable(typeOfAnnotatedElement, oneAllowedType)) {
+ return true;
+ }
+ }
+
return false;
}
- // ==================================
- // private API below
- // ==================================
-
/**
* Returns a set with all those type elements, at which the given constraint annotation
* type may be specified.
@@ -142,19 +199,23 @@
* @param annotationType
* @return
*/
- private Set<TypeElement> getAllowedTypesForConstraint(DeclaredType annotationType)
{
+ private Set<TypeMirror> getAllowedTypesForConstraint(DeclaredType annotationType)
{
+ Set<TypeMirror> theValue;
+
if(isBuiltInConstraint(annotationType)) {
- return getAllowedTypesFromBuiltInConstraint(annotationType);
+ theValue = getAllowedTypesFromBuiltInConstraint(annotationType);
}
else {
- return getAllowedTypesFromCustomConstraint(annotationType);
+ theValue = getAllowedTypesFromCustomConstraint(annotationType);
}
+
+ return theValue;
}
- private Set<TypeElement> getAllowedTypesFromBuiltInConstraint(DeclaredType
builtInAnnotationType) {
+ private Set<TypeMirror> getAllowedTypesFromBuiltInConstraint(DeclaredType
builtInAnnotationType) {
- Set<TypeElement> theValue =
builtInConstraints.get(builtInAnnotationType.asElement().getSimpleName());
+ Set<TypeMirror> theValue =
builtInConstraints.get(builtInAnnotationType.asElement().getSimpleName());
if(theValue == null) {
theValue = Collections.emptySet();
@@ -167,24 +228,22 @@
* Returns a set containing all those types, at which the specified custom
* constraint-annotation is allowed.
*
- * @param customInAnnotationType
+ * @param customAnnotationType
* A custom constraint type.
*
* @return A set with all types supported by the given constraint. May be
* empty in case of constraint composition, if there is no common
* type supported by all composing constraints.
- *
- * TODO GM: consider constraint composition
*/
- private Set<TypeElement> getAllowedTypesFromCustomConstraint(DeclaredType
customInAnnotationType) {
+ private Set<TypeMirror> getAllowedTypesFromCustomConstraint(DeclaredType
customAnnotationType) {
- Set<TypeElement> theValue = CollectionHelper.newHashSet();
+ Set<TypeMirror> theValue = null;
//the Constraint meta-annotation at the type declaration, e.g.
"@Constraint(validatedBy = CheckCaseValidator.class)"
- AnnotationMirror constraintMetaAnnotation =
getConstraintMetaAnnotation(customInAnnotationType);
+ AnnotationMirror constraintMetaAnnotation =
getConstraintMetaAnnotation(customAnnotationType);
if(constraintMetaAnnotation == null) {
- return theValue;
+ throw new IllegalArgumentException("Given type " + customAnnotationType +
" isn't a constraint annotation type.");
}
//the validator classes, e.g. [CheckCaseValidator.class]
@@ -192,34 +251,125 @@
for (AnnotationValue oneValidatorClassReference : validatorClassReferences) {
- DeclaredType validatorType = (DeclaredType)oneValidatorClassReference.getValue();
+ if(theValue == null) {
+ theValue = CollectionHelper.newHashSet();
+ }
- //contains the bindings of the type parameters from the implemented
ConstraintValidator
- //interface, e.g. "ConstraintValidator<CheckCase, String>"
- DeclaredType constraintValidatorImplementation =
getConstraintValidatorSuperType(validatorType);
+ theValue.add(getSupportedType(oneValidatorClassReference));
+ }
- if(constraintValidatorImplementation != null) {
+ Set<AnnotationMirror> composingConstraints =
getComposingConstraints(customAnnotationType);
+ for (AnnotationMirror oneComposingConstraint : composingConstraints) {
+
+ Set<TypeMirror> allowedTypesForComposingConstraint =
getAllowedTypesForConstraint(oneComposingConstraint.getAnnotationType());
+
+ if(theValue == null) {
+ theValue = allowedTypesForComposingConstraint;
+ }
+ else {
+ theValue = intersect(theValue, allowedTypesForComposingConstraint);
+ }
+ }
+
+ if(theValue == null) {
+ theValue = Collections.emptySet();
+ }
+
+ return theValue;
+ }
+
+ private TypeMirror getSupportedType(AnnotationValue oneValidatorClassReference) {
+
+ TypeMirror validatorType = oneValidatorClassReference.accept(new
SimpleAnnotationValueVisitor6<TypeMirror, Void>() {
+
+ @Override
+ public TypeMirror visitType(TypeMirror t, Void p) {
+ return t;
+ }
+ }, null);
+
+ //contains the bindings of the type parameters from the implemented
ConstraintValidator
+ //interface, e.g. "ConstraintValidator<CheckCase, String>"
+ TypeMirror constraintValidatorImplementation =
getConstraintValidatorSuperType(validatorType);
+
+
+ TypeMirror supportedType = constraintValidatorImplementation.accept(new
TypeKindVisitor6<TypeMirror, Void>() {
+
+ @Override
+ public TypeMirror visitDeclared(DeclaredType constraintValidatorImplementation, Void
p) {
//2nd type parameter contains the data type supported by current validator class,
e.g. "String"
- TypeMirror supportedTypeParameter =
constraintValidatorImplementation.getTypeArguments().get(1);
- theValue.add((TypeElement)typeUtils.asElement(supportedTypeParameter));
+ return constraintValidatorImplementation.getTypeArguments().get(1);
}
+
+ }, null);
+
+ return supportedType;
+ }
+
+ private Set<TypeMirror> intersect(Set<TypeMirror> set1,
Set<TypeMirror> set2) {
+
+ Set<TypeMirror> theValue = keepThoseWithSuperTypeAndNoSubType(set1, set2);
+
+ theValue.addAll(keepThoseWithSuperTypeAndNoSubType(set2, set1));
+
+ return theValue;
+ }
+
+ //TODO GM: refactor
+ private Set<TypeMirror> keepThoseWithSuperTypeAndNoSubType(Set<TypeMirror>
set1, Set<TypeMirror> set2) {
+
+ Set<TypeMirror> theValue = CollectionHelper.newHashSet();
+
+ for (TypeMirror oneType : set1) {
+
+ for (TypeMirror typeMirror : set2) {
+ if(typeUtils.isSameType(oneType, typeMirror)) {
+ theValue.add(oneType);
+ continue;
+ }
+ }
+
+ Set<TypeMirror> superTypes = getAllSuperTypes(oneType);
+ for (TypeMirror oneSuperType : superTypes) {
+
+ for (TypeMirror typeMirror : set2) {
+ if(typeUtils.isSameType(oneSuperType, typeMirror)) {
+
+ if(!containsSubType(oneType, set2)) {
+ theValue.add(oneType);
+ }
+ }
+ }
+ }
}
return theValue;
}
- private DeclaredType getConstraintValidatorSuperType(DeclaredType type) {
+ private boolean containsSubType(TypeMirror type, Set<TypeMirror>
potentialSubTypes) {
+ for (TypeMirror onePotentialSubType : potentialSubTypes) {
+
+ if(typeUtils.isAssignable(onePotentialSubType, type)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private TypeMirror getConstraintValidatorSuperType(TypeMirror type) {
+
List<? extends TypeMirror> directSupertypes = typeUtils.directSupertypes(type);
for (TypeMirror typeMirror : directSupertypes) {
if(typeUtils.asElement(typeMirror).getSimpleName().contentEquals(ConstraintValidator.class.getSimpleName()))
{
- return (DeclaredType)typeMirror;
+ return typeMirror;
}
}
- return null;
+ throw new AssertionError("Class " + type + " specified in
@Constraint.validatedBy doesn't implement ConstraintValidator.");
}
/**
@@ -257,13 +407,19 @@
private void registerAllowedTypesForBuiltInConstraint(Class<? extends Annotation>
annotation, Set<Class<?>> allowedTypes) {
- Set<TypeElement> allowedTypesAsElements = CollectionHelper.newHashSet();
+ Set<TypeMirror> allowedTypesAsElements = CollectionHelper.newHashSet();
for (Class<?> oneType : allowedTypes) {
- TypeElement typeElement = elementUtils.getTypeElement(oneType.getCanonicalName());
+ if(oneType.isArray()) {
+
+ //TODO GM: handle array types
+ }
+ else {
+ TypeElement typeElement = elementUtils.getTypeElement(oneType.getCanonicalName());
- if(typeElement != null) {
- allowedTypesAsElements.add(typeElement);
+ if(typeElement != null) {
+ allowedTypesAsElements.add(typeUtils.getDeclaredType(typeElement));
+ }
}
}
@@ -290,4 +446,20 @@
return
CONSTRAINT_ANNOTATION_PACKAGE_NAME.equals(elementUtils.getPackageOf(element).getQualifiedName());
}
+
+ private Set<AnnotationMirror> getComposingConstraints(DeclaredType
constraintAnnotationType) {
+
+ Set<AnnotationMirror> theValue = CollectionHelper.newHashSet();
+
+ List<? extends AnnotationMirror> annotationMirrors =
constraintAnnotationType.asElement().getAnnotationMirrors();
+
+ for (AnnotationMirror oneAnnotationMirror : annotationMirrors) {
+ if(getConstraintMetaAnnotation(oneAnnotationMirror.getAnnotationType()) != null) {
+ theValue.add(oneAnnotationMirror);
+ }
+ }
+
+ return theValue;
+ }
+
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/main/resources/org/hibernate/validator/ap/ValidationProcessorMessages.properties 2009-12-10
15:44:02 UTC (rev 18199)
@@ -3,4 +3,7 @@
# Contains error messages to be used by the ConstraintValidationProcessor.
ONLY_GETTERS_MAY_BE_ANNOTATED=Constraint annotations must not be specified at methods,
which are no valid JavaBeans getter methods.
-NOT_SUPPORTED_TYPE=The annotation @{0} is disallowed for this data type.
\ No newline at end of file
+NOT_SUPPORTED_TYPE=The annotation @{0} is disallowed for this data type.
+NOT_SUPPORTED_RETURN_TYPE=The annotation @{0} is disallowed for the return type of this
method.
+ONLY_CONSTRAINT_ANNOTATIONS_MAY_BE_ANNOTATED=Constraint annotations must not be specified
at annotation types, which are no constraint annotation types themselves.
+STATIC_METHODS_MAY_NOT_BE_ANNOTATED=Only non-static methods may be annotated with
constraint annotations.
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/ConstraintValidationProcessorTest.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -17,95 +17,249 @@
*/
package org.hibernate.validator.ap;
-import static org.hibernate.validator.ap.testutil.CompilerTestHelper.assertDiagnostics;
-import static org.testng.Assert.assertFalse;
-import static org.testng.Assert.assertNotNull;
-
import java.io.File;
-
+import javax.tools.Diagnostic.Kind;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
-import javax.tools.Diagnostic.Kind;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
import org.hibernate.validator.ap.testmodel.FieldLevelValidationUsingBuiltInConstraints;
import
org.hibernate.validator.ap.testmodel.MethodLevelValidationUsingBuiltInConstraints;
+import org.hibernate.validator.ap.testmodel.boxing.ValidLong;
+import org.hibernate.validator.ap.testmodel.boxing.ValidLongValidator;
+import org.hibernate.validator.ap.testmodel.boxing.ValidationUsingBoxing;
+import org.hibernate.validator.ap.testmodel.classlevelconstraints.ClassLevelValidation;
+import org.hibernate.validator.ap.testmodel.classlevelconstraints.ValidCustomer;
+import
org.hibernate.validator.ap.testmodel.classlevelconstraints.ValidCustomerValidator;
+import
org.hibernate.validator.ap.testmodel.composedconstraint.FieldLevelValidationUsingComposedConstraint;
+import org.hibernate.validator.ap.testmodel.composedconstraint.ValidOrderNumber;
+import org.hibernate.validator.ap.testmodel.composedconstraint2.ComposedConstraint;
+import org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint1;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint1ValidatorForGregorianCalendar;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint1ValidatorForList;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint1ValidatorForString;
+import org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint2;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint2ValidatorForArrayList;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint2ValidatorForCalendar;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.ComposingConstraint2ValidatorForCollection;
+import
org.hibernate.validator.ap.testmodel.composedconstraint2.FieldLevelValidationUsingComplexComposedConstraint;
import org.hibernate.validator.ap.testmodel.customconstraints.CaseMode;
import org.hibernate.validator.ap.testmodel.customconstraints.CheckCase;
import org.hibernate.validator.ap.testmodel.customconstraints.CheckCaseValidator;
import
org.hibernate.validator.ap.testmodel.customconstraints.FieldLevelValidationUsingCustomConstraints;
+import
org.hibernate.validator.ap.testmodel.invalidcomposedconstraint.ValidCustomerNumber;
import org.hibernate.validator.ap.testutil.CompilerTestHelper;
import org.hibernate.validator.ap.util.DiagnosticExpection;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
+import static org.hibernate.validator.ap.testutil.CompilerTestHelper.assertDiagnostics;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+
/**
* Test for {@link ConstraintValidationProcessor} using the Java compiler
* API as defined by JSR 199.
- *
+ *
* @author Gunnar Morling.
- *
*/
public class ConstraintValidationProcessorTest {
+ private static CompilerTestHelper compilerHelper;
- private static CompilerTestHelper compilerHelper;
-
@BeforeClass
public static void setUpCompilerHelper() {
- String testSourceBaseDir = System.getProperty("testSourceBaseDir");
- String pathToBeanValidationApiJar =
System.getProperty("pathToBeanValidationApiJar");
-
- assertNotNull(testSourceBaseDir, "The system property testSourceBaseDir has to be
set and point to the base directory of the test java sources.");
- assertNotNull(pathToBeanValidationApiJar, "The system property
pathToBeanValidationApiJar has to be set and point to the BV API Jars.");
-
- compilerHelper =
- new CompilerTestHelper(ToolProvider.getSystemJavaCompiler(), testSourceBaseDir,
pathToBeanValidationApiJar);
+ String testSourceBaseDir = System.getProperty( "testSourceBaseDir" );
+ String pathToBeanValidationApiJar = System.getProperty(
"pathToBeanValidationApiJar" );
+
+ assertNotNull(
+ testSourceBaseDir,
+ "The system property testSourceBaseDir has to be set and point to the base
directory of the test java sources."
+ );
+ assertNotNull(
+ pathToBeanValidationApiJar,
+ "The system property pathToBeanValidationApiJar has to be set and point to the
BV API Jars."
+ );
+
+ compilerHelper =
+ new CompilerTestHelper(
+ ToolProvider.getSystemJavaCompiler(), testSourceBaseDir,
pathToBeanValidationApiJar
+ );
}
@Test
public void fieldLevelValidationUsingBuiltInConstraints() {
DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
-
- File sourceFile =
compilerHelper.getSourceFile(FieldLevelValidationUsingBuiltInConstraints.class);
-
- boolean compilationResult =
- compilerHelper.compile(new ConstraintValidationProcessor(), diagnostics, sourceFile);
- assertFalse(compilationResult);
- assertDiagnostics(diagnostics, new DiagnosticExpection(Kind.ERROR, 48));
+ File sourceFile = compilerHelper.getSourceFile(
FieldLevelValidationUsingBuiltInConstraints.class );
+
+ boolean compilationResult =
+ compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, sourceFile
);
+
+ assertFalse( compilationResult );
+ assertDiagnostics( diagnostics, new DiagnosticExpection( Kind.ERROR, 43 ) );
}
-
+
@Test
public void fieldLevelValidationUsingCustomConstraints() {
DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
-
- File sourceFile1 =
compilerHelper.getSourceFile(FieldLevelValidationUsingCustomConstraints.class);
- File sourceFile2 = compilerHelper.getSourceFile(CheckCase.class);
- File sourceFile3 = compilerHelper.getSourceFile(CaseMode.class);
- File sourceFile4 = compilerHelper.getSourceFile(CheckCaseValidator.class);
-
- boolean compilationResult =
- compilerHelper.compile(new ConstraintValidationProcessor(), diagnostics, sourceFile1,
sourceFile2, sourceFile3, sourceFile4);
- assertFalse(compilationResult);
- assertDiagnostics(diagnostics, new DiagnosticExpection(Kind.ERROR, 30));
+ File sourceFile1 = compilerHelper.getSourceFile(
FieldLevelValidationUsingCustomConstraints.class );
+ File sourceFile2 = compilerHelper.getSourceFile( CheckCase.class );
+ File sourceFile3 = compilerHelper.getSourceFile( CaseMode.class );
+ File sourceFile4 = compilerHelper.getSourceFile( CheckCaseValidator.class );
+
+ boolean compilationResult =
+ compilerHelper.compile(
+ new ConstraintValidationProcessor(),
+ diagnostics,
+ sourceFile1,
+ sourceFile2,
+ sourceFile3,
+ sourceFile4
+ );
+
+ assertFalse( compilationResult );
+ assertDiagnostics( diagnostics, new DiagnosticExpection( Kind.ERROR, 30 ) );
}
-
+
@Test
public void methodLevelValidationUsingBuiltInConstraints() {
DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
-
- File sourceFile =
compilerHelper.getSourceFile(MethodLevelValidationUsingBuiltInConstraints.class);
-
- boolean compilationResult =
- compilerHelper.compile(new ConstraintValidationProcessor(), diagnostics, sourceFile);
- assertFalse(compilationResult);
- assertDiagnostics(diagnostics, new DiagnosticExpection(Kind.ERROR, 32));
+ File sourceFile = compilerHelper.getSourceFile(
MethodLevelValidationUsingBuiltInConstraints.class );
+
+ boolean compilationResult =
+ compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, sourceFile
);
+
+ assertFalse( compilationResult );
+ assertDiagnostics(
+ diagnostics,
+ new DiagnosticExpection( Kind.ERROR, 32 ),
+ new DiagnosticExpection( Kind.ERROR, 39 ),
+ new DiagnosticExpection( Kind.ERROR, 47 ),
+ new DiagnosticExpection( Kind.ERROR, 54 )
+ );
}
-
+
+ @Test
+ public void classLevelValidation() {
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
+
+ File sourceFile1 = compilerHelper.getSourceFile( ClassLevelValidation.class );
+ File sourceFile2 = compilerHelper.getSourceFile( ValidCustomer.class );
+ File sourceFile3 = compilerHelper.getSourceFile( ValidCustomerValidator.class );
+
+ boolean compilationResult =
+ compilerHelper.compile(
+ new ConstraintValidationProcessor(), diagnostics, sourceFile1, sourceFile2,
sourceFile3
+ );
+
+ assertFalse( compilationResult );
+ assertDiagnostics( diagnostics, new DiagnosticExpection( Kind.ERROR, 28 ) );
+ }
+
+ @Test
+ public void validationUsingComposedConstraint() {
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
+
+ File sourceFile1 = compilerHelper.getSourceFile(
FieldLevelValidationUsingComposedConstraint.class );
+ File sourceFile2 = compilerHelper.getSourceFile( ValidOrderNumber.class );
+
+ boolean compilationResult =
+ compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics,
sourceFile1, sourceFile2 );
+
+ assertFalse( compilationResult );
+ assertDiagnostics( diagnostics, new DiagnosticExpection( Kind.ERROR, 29 ) );
+ }
+
+ @Test
+ public void validationUsingComplexComposedConstraint() {
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
+
+ File sourceFile1 = compilerHelper.getSourceFile(
FieldLevelValidationUsingComplexComposedConstraint.class );
+ File sourceFile2 = compilerHelper.getSourceFile( ComposedConstraint.class );
+ File sourceFile3 = compilerHelper.getSourceFile( ComposingConstraint1.class );
+ File sourceFile4 = compilerHelper.getSourceFile(
ComposingConstraint1ValidatorForString.class );
+ File sourceFile5 = compilerHelper.getSourceFile(
ComposingConstraint1ValidatorForGregorianCalendar.class );
+ File sourceFile6 = compilerHelper.getSourceFile(
ComposingConstraint1ValidatorForList.class );
+ File sourceFile7 = compilerHelper.getSourceFile( ComposingConstraint2.class );
+ File sourceFile8 = compilerHelper.getSourceFile(
ComposingConstraint2ValidatorForArrayList.class );
+ File sourceFile9 = compilerHelper.getSourceFile(
ComposingConstraint2ValidatorForCalendar.class );
+ File sourceFile10 = compilerHelper.getSourceFile(
ComposingConstraint2ValidatorForCollection.class );
+
+ boolean compilationResult =
+ compilerHelper.compile(
+ new ConstraintValidationProcessor(),
+ diagnostics,
+ sourceFile1,
+ sourceFile2,
+ sourceFile3,
+ sourceFile4,
+ sourceFile5,
+ sourceFile6,
+ sourceFile7,
+ sourceFile8,
+ sourceFile9,
+ sourceFile10
+ );
+
+ assertFalse( compilationResult );
+ assertDiagnostics(
+ diagnostics,
+ new DiagnosticExpection( Kind.ERROR, 29 ),
+ new DiagnosticExpection( Kind.ERROR, 32 ),
+ new DiagnosticExpection( Kind.ERROR, 41 ),
+ new DiagnosticExpection( Kind.ERROR, 50 ),
+ new DiagnosticExpection( Kind.ERROR, 56 )
+ );
+ }
+
+ @Test
+ public void validationUsingBoxing() {
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
+
+ File sourceFile1 = compilerHelper.getSourceFile( ValidationUsingBoxing.class );
+ File sourceFile2 = compilerHelper.getSourceFile( ValidLong.class );
+ File sourceFile3 = compilerHelper.getSourceFile( ValidLongValidator.class );
+
+ boolean compilationResult =
+ compilerHelper.compile(
+ new ConstraintValidationProcessor(), diagnostics, sourceFile1, sourceFile2,
sourceFile3
+ );
+
+ assertFalse( compilationResult );
+ assertDiagnostics(
+ diagnostics,
+ new DiagnosticExpection( Kind.ERROR, 31 ),
+ new DiagnosticExpection( Kind.ERROR, 37 ),
+ new DiagnosticExpection( Kind.ERROR, 43 ),
+ new DiagnosticExpection( Kind.ERROR, 59 ),
+ new DiagnosticExpection( Kind.ERROR, 67 )
+ );
+ }
+
+ @Test
+ public void constraintAnnotationGivenAtNonConstraintAnnotationType() {
+
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
+
+ File sourceFile = compilerHelper.getSourceFile( ValidCustomerNumber.class );
+
+ boolean compilationResult =
+ compilerHelper.compile( new ConstraintValidationProcessor(), diagnostics, sourceFile
);
+
+ assertFalse( compilationResult );
+ assertDiagnostics(
+ diagnostics, new DiagnosticExpection( Kind.ERROR, 28 ), new DiagnosticExpection(
Kind.ERROR, 29 )
+ );
+ }
}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/FieldLevelValidationUsingBuiltInConstraints.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/FieldLevelValidationUsingBuiltInConstraints.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/FieldLevelValidationUsingBuiltInConstraints.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -20,32 +20,29 @@
import java.util.Collection;
import java.util.Date;
import java.util.List;
-
import javax.validation.constraints.Size;
public class FieldLevelValidationUsingBuiltInConstraints {
-
- @Size(min=10)
+ @Size(min = 10)
public String string;
-
- @Size(min=10)
+
+ @Size(min = 10)
public Collection<String> collection;
-
+
//TODO GM: support array-typed elements
-
-// @Size(min=10)
-// public boolean[] array;
-
+
/**
* Allowed, as List extends Collection.
*/
- @Size(min=10)
+ @Size(min = 10)
public List<String> list;
-
+
/**
* Not allowed.
*/
- @Size(min=10)
+ @Size(min = 10)
public Date date;
+ @Size(min = 10)
+ public static String staticString;
}
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/MethodLevelValidationUsingBuiltInConstraints.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -17,21 +17,42 @@
*/
package org.hibernate.validator.ap.testmodel;
+import java.util.Date;
import javax.validation.constraints.Size;
public class MethodLevelValidationUsingBuiltInConstraints {
-
- @Size(min=10)
+ @Size(min = 10)
public String getString() {
return null;
}
-
+
/**
* Not allowed. Method is no getter.
*/
- @Size(min=10)
+ @Size(min = 10)
public void setString() {
-
}
-}
+ /**
+ * Not allowed. Return type doesn't match.
+ */
+ @Size(min = 10)
+ public Date getDate() {
+ return null;
+ }
+
+ /**
+ * Not allowed. No return type.
+ */
+ @Size(min = 10)
+ public void getAnotherString() {
+ }
+
+ /**
+ * Not allowed. Static method.
+ */
+ @Size(min = 10)
+ public static String getStringStatically() {
+ return null;
+ }
+}
\ No newline at end of file
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLong.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLong.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLong.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,41 @@
+// $Id: ValidLong.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.boxing;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = ValidLongValidator.class)
+@Documented
+public @interface ValidLong {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLongValidator.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLongValidator.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidLongValidator.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,30 @@
+// $Id: ValidLongValidator.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.boxing;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ValidLongValidator implements ConstraintValidator<ValidLong, Long> {
+ public void initialize(ValidLong constraintAnnotation) {
+ }
+
+ public boolean isValid(Long object, ConstraintValidatorContext constraintContext) {
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidationUsingBoxing.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidationUsingBoxing.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/boxing/ValidationUsingBoxing.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,72 @@
+// $Id: ValidationUsingBoxing.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.boxing;
+
+public class ValidationUsingBoxing {
+
+ @ValidLong
+ public long primitiveLongField;
+
+ @ValidLong
+ public Long longField;
+
+ /**
+ * Not allowed.
+ */
+ @ValidLong
+ public int intField;
+
+ /**
+ * Not allowed.
+ */
+ @ValidLong
+ public Integer integerField;
+
+ /**
+ * Not allowed.
+ */
+ @ValidLong
+ public double doubleField;
+
+ @ValidLong
+ public long getPrimitiveLong() {
+ return 0;
+ }
+
+ @ValidLong
+ public Long getLong() {
+ return Long.MIN_VALUE;
+ }
+
+ /**
+ * Not allowed.
+ */
+ @ValidLong
+ public int getInt() {
+ return 0;
+ }
+
+ /**
+ * Not allowed.
+ */
+ @ValidLong
+ public Integer getInteger() {
+ return Integer.MIN_VALUE;
+ }
+
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ClassLevelValidation.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ClassLevelValidation.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ClassLevelValidation.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,31 @@
+// $Id: ClassLevelValidation.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.classlevelconstraints;
+
+public class ClassLevelValidation {
+ @ValidCustomer
+ public static class Customer {
+ }
+
+ /**
+ * Not allowed.
+ */
+ @ValidCustomer
+ public static class Order {
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomer.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomer.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomer.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,39 @@
+// $Id: ValidCustomer.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.classlevelconstraints;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = ValidCustomerValidator.class)
+@Documented
+public @interface ValidCustomer {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomerValidator.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomerValidator.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/classlevelconstraints/ValidCustomerValidator.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,32 @@
+// $Id: ValidCustomerValidator.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.classlevelconstraints;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import
org.hibernate.validator.ap.testmodel.classlevelconstraints.ClassLevelValidation.Customer;
+
+public class ValidCustomerValidator implements ConstraintValidator<ValidCustomer,
Customer> {
+ public void initialize(ValidCustomer constraintAnnotation) {
+ }
+
+ public boolean isValid(Customer customer, ConstraintValidatorContext constraintContext)
{
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/FieldLevelValidationUsingComposedConstraint.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/FieldLevelValidationUsingComposedConstraint.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/FieldLevelValidationUsingComposedConstraint.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,31 @@
+// $Id: FieldLevelValidationUsingComposedConstraint.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint;
+
+import java.util.Date;
+
+public class FieldLevelValidationUsingComposedConstraint {
+ @ValidOrderNumber
+ public String string;
+
+ /**
+ * Not allowed.
+ */
+ @ValidOrderNumber
+ public Date date;
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/ValidOrderNumber.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/ValidOrderNumber.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint/ValidOrderNumber.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,45 @@
+// $Id: ValidOrderNumber.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@NotNull
+@Size(min = 10, max = 10)
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = { })
+@Documented
+public @interface ValidOrderNumber {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposedConstraint.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposedConstraint.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposedConstraint.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,43 @@
+// $Id: ComposedConstraint.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@ComposingConstraint1
+@ComposingConstraint2
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = { })
+@Documented
+public @interface ComposedConstraint {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,45 @@
+// $Id: ComposingConstraint1.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = {
+ ComposingConstraint1ValidatorForList.class,
+ ComposingConstraint1ValidatorForString.class,
+ ComposingConstraint1ValidatorForGregorianCalendar.class
+})
+@Documented
+public @interface ComposingConstraint1 {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForGregorianCalendar.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForGregorianCalendar.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForGregorianCalendar.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,32 @@
+// $Id: ComposingConstraint1ValidatorForGregorianCalendar.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.util.GregorianCalendar;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ComposingConstraint1ValidatorForGregorianCalendar
+ implements ConstraintValidator<ComposingConstraint1, GregorianCalendar> {
+ public void initialize(ComposingConstraint1 constraintAnnotation) {
+ }
+
+ public boolean isValid(GregorianCalendar object, ConstraintValidatorContext
constraintContext) {
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForList.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForList.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForList.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,32 @@
+// $Id: ComposingConstraint1ValidatorForList.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.util.List;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ComposingConstraint1ValidatorForList implements
ConstraintValidator<ComposingConstraint1, List<?>> {
+ public void initialize(ComposingConstraint1 constraintAnnotation) {
+
+ }
+
+ public boolean isValid(List<?> object, ConstraintValidatorContext
constraintContext) {
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForString.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForString.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint1ValidatorForString.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,38 @@
+// $Id: ComposingConstraint1ValidatorForString.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ComposingConstraint1ValidatorForString implements
+ConstraintValidator<ComposingConstraint1, String> {
+
+
+
+ public void initialize(ComposingConstraint1 constraintAnnotation) {
+
+ }
+
+ public boolean isValid(String object,
+ ConstraintValidatorContext constraintContext) {
+
+ return true;
+ }
+
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,45 @@
+// $Id: ComposingConstraint2.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
+@Retention(RUNTIME)
+@Constraint(validatedBy = {
+ ComposingConstraint2ValidatorForArrayList.class,
+ ComposingConstraint2ValidatorForCalendar.class,
+ ComposingConstraint2ValidatorForCollection.class
+})
+@Documented
+public @interface ComposingConstraint2 {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForArrayList.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForArrayList.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForArrayList.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,33 @@
+// $Id: ComposingConstraint2ValidatorForArrayList.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.util.ArrayList;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ComposingConstraint2ValidatorForArrayList
+ implements ConstraintValidator<ComposingConstraint2, ArrayList<?>> {
+
+ public void initialize(ComposingConstraint2 constraintAnnotation) {
+ }
+
+ public boolean isValid(ArrayList<?> object, ConstraintValidatorContext
constraintContext) {
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCalendar.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCalendar.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCalendar.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,38 @@
+// $Id: ComposingConstraint2ValidatorForCalendar.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.util.Calendar;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ComposingConstraint2ValidatorForCalendar implements
+ConstraintValidator<ComposingConstraint2, Calendar> {
+
+ public void initialize(ComposingConstraint2 constraintAnnotation) {
+
+ }
+
+ public boolean isValid(Calendar object,
+ ConstraintValidatorContext constraintContext) {
+
+ return true;
+ }
+
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCollection.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCollection.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/ComposingConstraint2ValidatorForCollection.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,32 @@
+// $Id: ComposingConstraint2ValidatorForCollection.java 17946 2009-11-06 18:23:48Z
hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.util.Collection;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+public class ComposingConstraint2ValidatorForCollection
+ implements ConstraintValidator<ComposingConstraint2, Collection<?>> {
+ public void initialize(ComposingConstraint2 constraintAnnotation) {
+ }
+
+ public boolean isValid(Collection<?> object, ConstraintValidatorContext
constraintContext) {
+ return true;
+ }
+}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/FieldLevelValidationUsingComplexComposedConstraint.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/FieldLevelValidationUsingComplexComposedConstraint.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/composedconstraint2/FieldLevelValidationUsingComplexComposedConstraint.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,59 @@
+// $Id: FieldLevelValidationUsingComplexComposedConstraint.java 17946 2009-11-06
18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.composedconstraint2;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
+
+public class FieldLevelValidationUsingComplexComposedConstraint {
+
+ @ComposedConstraint
+ public String string;
+
+ @ComposedConstraint
+ public List<?> list;
+
+ /**
+ * Allowed
+ */
+ @ComposedConstraint
+ public GregorianCalendar gregorianCalendar;
+
+ @ComposedConstraint
+ public Collection<?> collection;
+
+ /**
+ * Allowed
+ */
+ @ComposedConstraint
+ public ArrayList<?> arrayList;
+
+ @ComposedConstraint
+ public Calendar calendar;
+
+ /**
+ * Not allowed.
+ */
+ @ComposedConstraint
+ public Date date;
+
+}
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CaseMode.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CaseMode.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CaseMode.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -18,9 +18,6 @@
package org.hibernate.validator.ap.testmodel.customconstraints;
public enum CaseMode {
-
- UPPER,
-
+ UPPER,
LOWER;
-
}
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCase.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCase.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCase.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -17,27 +17,27 @@
*/
package org.hibernate.validator.ap.testmodel.customconstraints;
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
-import java.lang.annotation.Documented;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import javax.validation.Constraint;
-
-@Target( { METHOD, FIELD, ANNOTATION_TYPE })
+@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CheckCaseValidator.class)
@Documented
public @interface CheckCase {
+ String message() default "";
- String message() default "{validator.checkcase}";
+ Class<?>[] groups() default { };
- Class<?>[] groups() default {};
-
- CaseMode value();
+ Class<? extends Payload>[] payload() default { };
-}
+ CaseMode value();
+}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCaseValidator.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCaseValidator.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/customconstraints/CheckCaseValidator.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -20,25 +20,23 @@
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
-public class CheckCaseValidator implements
-ConstraintValidator<CheckCase, String> {
-
+public class CheckCaseValidator implements ConstraintValidator<CheckCase, String>
{
private CaseMode caseMode;
public void initialize(CheckCase constraintAnnotation) {
this.caseMode = constraintAnnotation.value();
}
- public boolean isValid(String object,
- ConstraintValidatorContext constraintContext) {
-
- if (object == null)
+ public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
+ if ( object == null ) {
return true;
+ }
- if (caseMode == CaseMode.UPPER)
- return object.equals(object.toUpperCase());
- else
- return object.equals(object.toLowerCase());
+ if ( caseMode == CaseMode.UPPER ) {
+ return object.equals( object.toUpperCase() );
+ }
+ else {
+ return object.equals( object.toLowerCase() );
+ }
}
-
}
Added:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/ValidCustomerNumber.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/ValidCustomerNumber.java
(rev 0)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testmodel/invalidcomposedconstraint/ValidCustomerNumber.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -0,0 +1,36 @@
+// $Id: ValidCustomerNumber.java 17946 2009-11-06 18:23:48Z hardy.ferentschik $
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.validator.ap.testmodel.invalidcomposedconstraint;
+
+import javax.validation.Payload;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+/**
+ * Constraint annotations are not allowed here, as ValidOrderNumber
+ * isn't a proper constraint type definition.
+ */
+@NotNull
+@Size(min = 10, max = 10)
+public @interface ValidCustomerNumber {
+ String message() default "";
+
+ Class<?>[] groups() default { };
+
+ Class<? extends Payload>[] payload() default { };
+}
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/testutil/CompilerTestHelper.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -17,33 +17,31 @@
*/
package org.hibernate.validator.ap.testutil;
-import static org.testng.Assert.assertEquals;
-import static org.testng.Assert.assertTrue;
-
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
-
import javax.annotation.processing.Processor;
+import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
+import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
-import javax.tools.JavaCompiler.CompilationTask;
import org.hibernate.validator.ap.util.DiagnosticExpection;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
/**
* Infrastructure for unit tests based on the Java Compiler API.
- *
+ *
* @author Gunnar Morling
- *
*/
public class CompilerTestHelper {
+ private final JavaCompiler compiler;
- private final JavaCompiler compiler;
-
private final String sourceBaseDir;
/**
@@ -51,7 +49,7 @@
* Eclipse, but not on the command line.
*/
private final String pathToBeanValidationApiJar;
-
+
public CompilerTestHelper(JavaCompiler compiler, String sourceBaseDir, String
pathToBeanValidationApiJar) {
this.compiler = compiler;
@@ -60,46 +58,53 @@
}
public File getSourceFile(Class<?> clazz) {
-
+
String sourceFileName =
- File.separator + clazz.getName().replace(".", File.separator) +
".java";
-
- return new File(sourceBaseDir + sourceFileName);
+ File.separator + clazz.getName().replace( ".", File.separator ) +
".java";
+
+ return new File( sourceBaseDir + sourceFileName );
}
-
+
public boolean compile(
- Processor annotationProcessor, DiagnosticCollector<JavaFileObject> diagnostics,
File... sourceFiles) {
-
- StandardJavaFileManager fileManager =
- compiler.getStandardFileManager(null, null, null);
-
- Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjects(sourceFiles);
+ Processor annotationProcessor, DiagnosticCollector<JavaFileObject> diagnostics,
File... sourceFiles) {
+
+ StandardJavaFileManager fileManager =
+ compiler.getStandardFileManager( null, null, null );
+
+ Iterable<? extends JavaFileObject> compilationUnits =
fileManager.getJavaFileObjects( sourceFiles );
+
List<String> optionList = new ArrayList<String>();
- optionList.addAll(Arrays.asList("-classpath", pathToBeanValidationApiJar));
+ optionList.addAll( Arrays.asList( "-classpath", pathToBeanValidationApiJar )
);
- CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList,
null, compilationUnits);
+ CompilationTask task = compiler.getTask( null, fileManager, diagnostics, optionList,
null, compilationUnits );
+ task.setProcessors( Arrays.asList( annotationProcessor ) );
- task.setProcessors(Arrays.asList(annotationProcessor));
-
return task.call();
}
-
+
public static void assertDiagnostics(DiagnosticCollector<JavaFileObject>
diagnostics, DiagnosticExpection... expections) {
-
- if(expections == null) {
- assertTrue(diagnostics.getDiagnostics().isEmpty());
+
+ List<Diagnostic<? extends JavaFileObject>> diagnosticsList =
diagnostics.getDiagnostics();
+
+ if ( expections == null ) {
+ assertTrue( diagnosticsList.isEmpty() );
}
else {
-
- assertEquals(diagnostics.getDiagnostics().size(), expections.length);
-
+
+ if ( diagnosticsList.size() != expections.length ) {
+ System.out.println( diagnosticsList );
+ }
+
+ assertEquals( diagnosticsList.size(), expections.length, "Wrong number of
diagnostic expections." );
+
int i = 0;
- for (DiagnosticExpection oneExpection : expections) {
-
- assertEquals(diagnostics.getDiagnostics().get(i).getKind(), oneExpection.getKind());
- assertEquals(diagnostics.getDiagnostics().get(i).getLineNumber(),
oneExpection.getLineNumber());
+ for ( DiagnosticExpection oneExpection : expections ) {
+
+ assertEquals( diagnosticsList.get( i ).getKind(), oneExpection.getKind() );
+ assertEquals( diagnosticsList.get( i ).getLineNumber(), oneExpection.getLineNumber()
);
+
+ i++;
}
}
}
-
-}
+}
\ No newline at end of file
Modified:
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/util/DiagnosticExpection.java
===================================================================
---
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/util/DiagnosticExpection.java 2009-12-10
15:27:00 UTC (rev 18198)
+++
validator/trunk/hibernate-validator-annotation-processor/src/test/java/org/hibernate/validator/ap/util/DiagnosticExpection.java 2009-12-10
15:44:02 UTC (rev 18199)
@@ -22,17 +22,15 @@
/**
* Expectation value to be matched against a given {@link Diagnostic}.
- *
+ *
* @author Gunnar Morling
*/
public class DiagnosticExpection {
-
private final Kind kind;
-
+
private final long lineNumber;
-
+
public DiagnosticExpection(Kind kind, long lineNumber) {
-
this.kind = kind;
this.lineNumber = lineNumber;
}
@@ -44,4 +42,9 @@
public long getLineNumber() {
return lineNumber;
}
+
+ @Override
+ public String toString() {
+ return "DiagnosticExpection [kind=" + kind + ", lineNumber=" +
lineNumber + "]";
+ }
}
\ No newline at end of file