ConstraintAnnotationAttributeHandler implements AnnotationAttributeHandler { @Override public Map<String, Object> generateFrom(AnnotatedElement annotatedElement, Annotation annotation) { final AnnotationAttributes annotationAttributes = AnnotationUtils.getAnnotationAttributes(annotatedElement, annotation, false, true); final HashMap<String, Object> map = new HashMap<>(); for (final Entry<String, Object> entry : annotationAttributes.entrySet()) { Object resolvedValue = entry.getValue(); if (entry.getKey().equals(ATTRIBUTE_MESSAGE_NAME) && ((String) entry.getValue()).matches(JSR_DEFAULT_MESSAGE_REGEX)) { resolvedValue = this.resolveMessage((String) entry.getValue(), annotatedElement, annotation); } map.put(entry.getKey(), resolvedValue); } return map; } ConstraintAnnotationAttributeHandler(BeanResolverStrategy beanResolver);  ConstraintAnnotationAttributeHandler(BeanResolverStrategy beanResolver,
			String defaultValidationMessagesPath); @Override Map<String, Object> generateFrom(AnnotatedElement annotatedElement, Annotation annotation); static final String ATTRIBUTE_MESSAGE_NAME; static final String ATTRIBUTE_MESSAGE_VALUE_DEFAULT; static final String DEFAULT_VALIDATION_MESSAGES_PATH; static final String JSR_DEFAULT_MESSAGE_REGEX; static final String SPEL_CHAR_KEY; static final String REPLACE_REGEX; static final String ANNOTATION_PREFIX; }
@Test public void testUseCustomMessage() throws Exception { final Field annotatedElement = this.getClass().getDeclaredField("t1_str"); final Annotation annotation = annotatedElement.getAnnotation(NotNull.class); final Map<String, Object> actual = this.testee.generateFrom(annotatedElement, annotation); Assert.assertEquals("Enter a value for t1_str", actual.get("message")); }
@Test public void testUseCustomMessageWithSpel() throws Exception { String expected = "${spel.validation.message}"; final Field annotatedElement = this.getClass().getDeclaredField("t3_str"); final Annotation annotation = annotatedElement.getAnnotation(NotNull.class); final Map<String, Object> actual = this.testee.generateFrom(annotatedElement, annotation); Assert.assertEquals(expected, actual.get("message")); }
@Test public void testUseClientDefinedDefaultMessage() throws Exception { String expected = "Very Important Field!"; final Field annotatedElement = this.getClass().getDeclaredField("t2_str"); final Annotation annotation = annotatedElement.getAnnotation(NotNull.class); Mockito.when(this.propertyResolver.getProperty("javax.validation.constraints.NotNull.message")).thenReturn(expected); final Map<String, Object> actual = this.testee.generateFrom(annotatedElement, annotation); Assert.assertEquals(expected, actual.get("message")); }
@Test public void testUseFrameworkDefinedDefaultMessage() throws Exception { String expected = "Field is required."; final Field annotatedElement = this.getClass().getDeclaredField("t2_str"); final Annotation annotation = annotatedElement.getAnnotation(NotNull.class); final Map<String, Object> actual = this.testee.generateFrom(annotatedElement, annotation); Assert.assertEquals(expected, actual.get("message")); }
@Test public void testUndefinedDefaultMessage() throws Exception { this.testee = new ConstraintAnnotationAttributeHandler(this.beanResolver, "blank"); String expected = "{javax.validation.constraints.NotNull.message}"; final Field annotatedElement = this.getClass().getDeclaredField("t2_str"); final Annotation annotation = annotatedElement.getAnnotation(NotNull.class); final Map<String, Object> actual = this.testee.generateFrom(annotatedElement, annotation); Assert.assertEquals(expected, actual.get("message")); }