@Test public void testGetMultiValueConstraints() throws Exception { Engine engine = new Engine(); Field[] fields = engine.getClass().getDeclaredFields(); assertNotNull(fields); assertTrue(fields.length == 1); ReflectionHelper.setAccessibility(fields[0]); Annotation annotation = fields[0].getAnnotation(Pattern.List.class); assertNotNull(annotation); List<Annotation> multiValueConstraintAnnotations = constraintHelper.getMultiValueConstraints(annotation); assertTrue( multiValueConstraintAnnotations.size() == 2, "There should be two constraint annotations"); assertTrue( multiValueConstraintAnnotations.get(0) instanceof Pattern, "Wrong constraint annotation"); assertTrue( multiValueConstraintAnnotations.get(1) instanceof Pattern, "Wrong constraint annotation"); Order order = new Order(); fields = order.getClass().getDeclaredFields(); assertNotNull(fields); assertTrue(fields.length == 1); ReflectionHelper.setAccessibility(fields[0]); annotation = fields[0].getAnnotation(NotNull.class); assertNotNull(annotation); multiValueConstraintAnnotations = constraintHelper.getMultiValueConstraints(annotation); assertTrue( multiValueConstraintAnnotations.size() == 0, "There should be no constraint annotations"); }
/** * Examines the given annotation to see whether it is a single- or multi-valued constraint * annotation. * * @param clazz the class we are currently processing * @param annotation The annotation to examine * @param type the element type on which the annotation/constraint is placed on * @return A list of constraint descriptors or the empty list in case <code>annotation</code> is * neither a single nor multi-valued annotation. */ private <A extends Annotation> List<ConstraintDescriptorImpl<?>> findConstraintAnnotations( Class<?> clazz, A annotation, ElementType type) { List<ConstraintDescriptorImpl<?>> constraintDescriptors = new ArrayList<ConstraintDescriptorImpl<?>>(); List<Annotation> constraints = new ArrayList<Annotation>(); Class<? extends Annotation> annotationType = annotation.annotationType(); if (constraintHelper.isConstraintAnnotation(annotationType) || constraintHelper.isBuiltinConstraint(annotationType)) { constraints.add(annotation); } else if (constraintHelper.isMultiValueConstraint(annotationType)) { constraints.addAll(constraintHelper.getMultiValueConstraints(annotation)); } for (Annotation constraint : constraints) { final ConstraintDescriptorImpl<?> constraintDescriptor = buildConstraintDescriptor(clazz, constraint, type); constraintDescriptors.add(constraintDescriptor); } return constraintDescriptors; }
@Test public void testConstraintWithNoGroups() { Annotation annotation = new NoMessage() { public Class<?>[] groups() { return null; } public Class<? extends Annotation> annotationType() { return this.getClass(); } }; assertFalse( constraintHelper.isConstraintAnnotation(annotation), "The constraint annotation should not be valid"); }
@Test public void testConstraintWithValidInPropertyName() { Annotation annotation = new ValidProperty() { public String message() { return null; } public Class<?>[] groups() { return null; } public int validLength() { return 0; } public Class<? extends Annotation> annotationType() { return this.getClass(); } }; assertFalse( constraintHelper.isConstraintAnnotation(annotation), "The constraint annotation should not be valid"); }