@Test public void testIsValidMinValidator() { AnnotationDescriptor<Min> descriptor = new AnnotationDescriptor<Min>(Min.class); descriptor.setValue("value", 15L); descriptor.setValue("message", "{validator.min}"); Min m = AnnotationFactory.create(descriptor); MinValidatorForCharSequence constraint = new MinValidatorForCharSequence(); constraint.initialize(m); testMinValidator(constraint, true); }
private <A extends Annotation> Annotation createAnnotation( AnnotationType annotationType, Class<A> returnType) { AnnotationDescriptor<A> annotationDescriptor = new AnnotationDescriptor<A>(returnType); for (ElementType elementType : annotationType.getElement()) { String name = elementType.getName(); Class<?> parameterType = getAnnotationParameterType(returnType, name); Object elementValue = getElementValue(elementType, parameterType); annotationDescriptor.setValue(name, elementValue); } return AnnotationFactory.create(annotationDescriptor); }
@Test public void testIsValidDecimalMax() { AnnotationDescriptor<DecimalMax> descriptor = new AnnotationDescriptor<DecimalMax>(DecimalMax.class); descriptor.setValue("value", "15.0E0"); descriptor.setValue("message", "{validator.max}"); DecimalMax m = AnnotationFactory.create(descriptor); DecimalMaxValidatorForCharSequence constraint = new DecimalMaxValidatorForCharSequence(); constraint.initialize(m); testMaxValidator(constraint, true); }
@Test @TestForIssue(jiraKey = "HV-256") public void testIsValidDecimalMinExclusive() { boolean inclusive = false; AnnotationDescriptor<DecimalMin> descriptor = new AnnotationDescriptor<DecimalMin>(DecimalMin.class); descriptor.setValue("value", "1500E-2"); descriptor.setValue("inclusive", inclusive); descriptor.setValue("message", "{validator.min}"); DecimalMin m = AnnotationFactory.create(descriptor); DecimalMinValidatorForCharSequence constraint = new DecimalMinValidatorForCharSequence(); constraint.initialize(m); testMinValidator(constraint, inclusive); }
@Test public void testInitializeDecimalMaxWithInvalidValue() { AnnotationDescriptor<DecimalMin> descriptor = new AnnotationDescriptor<DecimalMin>(DecimalMin.class); descriptor.setValue("value", "foobar"); descriptor.setValue("message", "{validator.min}"); DecimalMin m = AnnotationFactory.create(descriptor); DecimalMinValidatorForNumber constraint = new DecimalMinValidatorForNumber(); try { constraint.initialize(m); fail(); } catch (IllegalArgumentException e) { // success } }
private <A extends Annotation, T> MetaConstraint<?> createMetaConstraint( ConstraintType constraint, Class<T> beanClass, Member member, String defaultPackage) { @SuppressWarnings("unchecked") Class<A> annotationClass = (Class<A>) getClass(constraint.getAnnotation(), defaultPackage); AnnotationDescriptor<A> annotationDescriptor = new AnnotationDescriptor<A>(annotationClass); if (constraint.getMessage() != null) { annotationDescriptor.setValue(MESSAGE_PARAM, constraint.getMessage()); } annotationDescriptor.setValue(GROUPS_PARAM, getGroups(constraint.getGroups(), defaultPackage)); annotationDescriptor.setValue( PAYLOAD_PARAM, getPayload(constraint.getPayload(), defaultPackage)); for (ElementType elementType : constraint.getElement()) { String name = elementType.getName(); checkNameIsValid(name); Class<?> returnType = getAnnotationParameterType(annotationClass, name); Object elementValue = getElementValue(elementType, returnType); annotationDescriptor.setValue(name, elementValue); } A annotation; try { annotation = AnnotationFactory.create(annotationDescriptor); } catch (RuntimeException e) { throw log.getUnableToCreateAnnotationForConfiguredConstraintException(e); } java.lang.annotation.ElementType type = java.lang.annotation.ElementType.TYPE; if (member instanceof Method) { type = java.lang.annotation.ElementType.METHOD; } else if (member instanceof Field) { type = java.lang.annotation.ElementType.FIELD; } // we set initially ConstraintOrigin.DEFINED_LOCALLY for all xml configured constraints // later we will make copies of this constraint descriptor when needed and adjust the // ConstraintOrigin ConstraintDescriptorImpl<A> constraintDescriptor = new ConstraintDescriptorImpl<A>( annotation, constraintHelper, type, ConstraintOrigin.DEFINED_LOCALLY); return new MetaConstraint<A>( constraintDescriptor, new BeanConstraintLocation(beanClass, member)); }