@Test
 @SpecAssertions({
   @SpecAssertion(section = "6.11", id = "a"),
   @SpecAssertion(section = "6.11", id = "b"),
   @SpecAssertion(section = "6.11", id = "d")
 })
 public void testAnnotationAndMapParametersReflectParameterOverriding() {
   ConstraintDescriptor<?> descriptor = getConstraintDescriptor(Person.class, "firstName");
   Set<ConstraintDescriptor<?>> composingDescriptors = descriptor.getComposingConstraints();
   assertEquals(composingDescriptors.size(), 2, "Wrong number of composing constraints");
   boolean hasSize = false;
   for (ConstraintDescriptor<?> desc : composingDescriptors) {
     if (desc.getAnnotation().annotationType().equals(Size.class)) {
       hasSize = true;
       Size sizeAnn = (Size) desc.getAnnotation();
       assertEquals(sizeAnn.min(), 5, "The min parameter should reflect the overridden parameter");
       assertEquals(
           desc.getAttributes().get("min"),
           5,
           "The min parameter should reflect the overridden parameter");
     } else if (desc.getAnnotation().annotationType().equals(NotNull.class)) {
     } else {
       fail("Unexpected annotation.");
     }
   }
   assertTrue(hasSize, "Size composed annotation not found");
 }
 @Test
 @SpecAssertion(section = "6.11", id = "c")
 public void testGetAttributesFromConstraintDescriptor() {
   ConstraintDescriptor<?> descriptor = getConstraintDescriptor(Order.class, "orderNumber");
   Map<String, Object> attributes = descriptor.getAttributes();
   assertTrue(attributes.containsKey("message"));
   assertTrue(attributes.containsKey("groups"));
 }
  public Map<String, Object> getMetadata(ConstraintDescriptor constraintDescriptor) {
    Map<String, Object> metadata = new HashMap<String, Object>();
    Map attrs = constraintDescriptor.getAttributes();
    Object message = attrs.get("message");

    metadata.put(HTML.VALIDATION_METADATA.MAX_VALUE, attrs.get("value"));

    if (!message.equals(MESSAGE_ID)) {
      metadata.put(MESSAGE_METADATA, message);
    }

    return metadata;
  }
Ejemplo n.º 4
0
 protected Object[] getArgumentsForConstraint(
     String objectName, String field, ConstraintDescriptor<?> descriptor) {
   List<Object> arguments = new LinkedList<>();
   String[] codes = new String[] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field};
   arguments.add(new DefaultMessageSourceResolvable(codes, field));
   // Using a TreeMap for alphabetical ordering of attribute names
   Map<String, Object> attributesToExpose = new TreeMap<>();
   for (Map.Entry<String, Object> entry : descriptor.getAttributes().entrySet()) {
     String attributeName = entry.getKey();
     Object attributeValue = entry.getValue();
     if (!internalAnnotationAttributes.contains(attributeName)) {
       attributesToExpose.put(attributeName, attributeValue);
     }
   }
   arguments.addAll(attributesToExpose.values());
   return arguments.toArray(new Object[arguments.size()]);
 }