Assert.assertEquals(0, iv.size());
}
public void testMetadataAPI() {
Validator bookValidator = getValidator();
BeanDescriptor bookBeanDescriptor =
bookValidator.getConstraintsForClass(Book.class);
// expect no constraints on Book's Class-Level
Assert.assertFalse(bookBeanDescriptor.hasConstraints());
// but there are constraints on Book's Property-Level
Assert.assertTrue(bookBeanDescriptor.isBeanConstrained());
Assert.assertTrue(
bookBeanDescriptor.getConstraintDescriptors().size() == 0); //no constraint
//more specifically "author" and "title"
Assert.assertEquals(4, bookBeanDescriptor.getConstrainedProperties().size());
//not a property
Assert.assertTrue(
bookBeanDescriptor.getConstraintsForProperty("doesNotExist") == null);
//property with no constraint
Assert.assertTrue(
bookBeanDescriptor.getConstraintsForProperty("description") == null);
PropertyDescriptor propertyDescriptor =
bookBeanDescriptor.getConstraintsForProperty("title");
Assert.assertEquals(2, propertyDescriptor.getConstraintDescriptors().size());
Assert.assertTrue("title".equals(propertyDescriptor.getPropertyName()));
//assuming the implementation returns the NotEmpty constraint first
Iterator<ConstraintDescriptor<?>> iter =
propertyDescriptor.getConstraintDescriptors().iterator();
ConstraintDescriptor constraintDescriptor = null;
while (iter.hasNext()) {
constraintDescriptor = iter.next();
if (constraintDescriptor.getAnnotation().annotationType()
.equals(NotNull.class)) {
break;
}
}
Assert.assertTrue(constraintDescriptor != null);
Assert.assertTrue(constraintDescriptor.getGroups().size() == 1); //"first"
Assert.assertEquals(NotNullValidator.class,
constraintDescriptor.getConstraintValidatorClasses().get(0));
//assuming the implementation returns the Size constraint first
propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("subtitle");
Iterator<ConstraintDescriptor<?>> iterator =
propertyDescriptor.getConstraintDescriptors().iterator();
constraintDescriptor = iterator.next();
Assert.assertTrue(
constraintDescriptor.getAnnotation().annotationType().equals(Size.class));
Assert.assertTrue(
((Integer) constraintDescriptor.getAttributes().get("max")) == 30);
Assert.assertTrue(constraintDescriptor.getGroups().size() == 1);
propertyDescriptor = bookBeanDescriptor.getConstraintsForProperty("author");
Assert.assertTrue(propertyDescriptor.getConstraintDescriptors().size() == 1);
Assert.assertTrue(propertyDescriptor.isCascaded());
}