Introduce MessageCodeFormatter abstraction
This commit refactors changes introduced in 21760a8 as follows:
- Introduce top-level MessageCodeFormatter interface and
DefaultMessageCodesResolver#setMessageCodeFormatter property to allow
for user-defined message code formatting strategies
- Rename DefaultMessageCodesResolver.Style enum => DMCR.Format
- Refactor DefaultMessageCodesResolver.Format to implement the new
MessageCodeFormatter interface
The result is that users have convenient access to common formatting
strategies via the Format enum, while retaining the flexibility to
provide their own custom MessageCodeFormatter implementation if desired.
See DefaultMessageCodesResolverTests#shouldSupport*Format tests for
usage examples.
Issue: SPR-9707
This commit is contained in:
@@ -22,7 +22,7 @@ import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.validation.DefaultMessageCodesResolver.Style;
|
||||
import org.springframework.validation.DefaultMessageCodesResolver.Format;
|
||||
|
||||
/**
|
||||
* Tests for {@link DefaultMessageCodesResolver}.
|
||||
@@ -123,8 +123,8 @@ public class DefaultMessageCodesResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportPostfixStyle() throws Exception {
|
||||
resolver.setStyle(Style.POSTFIX_ERROR_CODE);
|
||||
public void shouldSupportPostfixFormat() throws Exception {
|
||||
resolver.setMessageCodeFormatter(Format.POSTFIX_ERROR_CODE);
|
||||
String[] codes = resolver.resolveMessageCodes("errorCode", "objectName");
|
||||
assertThat(codes, is(equalTo(new String[] {
|
||||
"objectName.errorCode",
|
||||
@@ -132,8 +132,8 @@ public class DefaultMessageCodesResolverTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportFieldPostfixStyle() throws Exception {
|
||||
resolver.setStyle(Style.POSTFIX_ERROR_CODE);
|
||||
public void shouldSupportFieldPostfixFormat() throws Exception {
|
||||
resolver.setMessageCodeFormatter(Format.POSTFIX_ERROR_CODE);
|
||||
String[] codes = resolver.resolveMessageCodes("errorCode", "objectName", "field",
|
||||
TestBean.class);
|
||||
assertThat(codes, is(equalTo(new String[] {
|
||||
@@ -142,4 +142,18 @@ public class DefaultMessageCodesResolverTests {
|
||||
"org.springframework.beans.TestBean.errorCode",
|
||||
"errorCode" })));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSupportCustomFormat() throws Exception {
|
||||
resolver.setMessageCodeFormatter(new MessageCodeFormatter() {
|
||||
public String format(String errorCode, String objectName, String field) {
|
||||
return DefaultMessageCodesResolver.Format.toDelimitedString(
|
||||
"CUSTOM-" + errorCode, objectName, field);
|
||||
}
|
||||
});
|
||||
String[] codes = resolver.resolveMessageCodes("errorCode", "objectName");
|
||||
assertThat(codes, is(equalTo(new String[] {
|
||||
"CUSTOM-errorCode.objectName",
|
||||
"CUSTOM-errorCode" })));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user