Polish "Configure MessageSource if no "messageSource" bean defined"

Closes gh-15212
This commit is contained in:
Stephane Nicoll 2018-12-03 11:28:26 +01:00
parent 82d99da32a
commit ec678eaa3b
1 changed files with 30 additions and 30 deletions

View File

@ -184,7 +184,7 @@ public class MessageSourceAutoConfigurationTests {
@Test
public void existingMessageSourceIsPreferred() {
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
this.contextRunner.withUserConfiguration(CustomMessageSourceConfiguration.class)
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
.isEqualTo("foo"));
}
@ -199,16 +199,42 @@ public class MessageSourceAutoConfigurationTests {
}
@Test
public void messageSourceDeclaredWithNonStandardNameDoesNotBreakAutoConfig() {
public void messageSourceWithNonStandardBeanNameIsIgnored() {
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
.withUserConfiguration(ConfigWithNonStandardMessageSourceBeanName.class)
.withUserConfiguration(CustomBeanNameMessageSourceConfiguration.class)
.run((context) -> {
assertThat(context.getMessage("foo", null, Locale.US))
.isEqualTo("bar");
});
}
private static class CodeReturningMessageSource implements MessageSource {
@Configuration
@PropertySource("classpath:/switch-messages.properties")
protected static class Config {
}
@Configuration
protected static class CustomMessageSourceConfiguration {
@Bean
public MessageSource messageSource() {
return new TestMessageSource();
}
}
@Configuration
protected static class CustomBeanNameMessageSourceConfiguration {
@Bean
public MessageSource codeReturningMessageSource() {
return new TestMessageSource();
}
}
private static class TestMessageSource implements MessageSource {
@Override
public String getMessage(String code, Object[] args, String defaultMessage,
@ -230,30 +256,4 @@ public class MessageSourceAutoConfigurationTests {
}
@Configuration
protected static class ConfigWithNonStandardMessageSourceBeanName {
@Bean
public MessageSource codeReturningMessageSource() {
return new CodeReturningMessageSource();
}
}
@Configuration
@PropertySource("classpath:/switch-messages.properties")
protected static class Config {
}
@Configuration
protected static class CustomMessageSource {
@Bean
public MessageSource messageSource() {
return new CodeReturningMessageSource();
}
}
}