diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java index 76d76ddaf3..87f8d87779 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/MessageSourceAutoConfigurationTests.java @@ -22,7 +22,12 @@ import org.junit.After; import org.junit.Ignore; import org.junit.Test; import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.MessageSource; +import org.springframework.context.MessageSourceResolvable; +import org.springframework.context.NoSuchMessageException; import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @@ -112,6 +117,36 @@ public class MessageSourceAutoConfigurationTests { .isFallbackToSystemLocale()); } + @Test + public void existingMessageSourceIsPreferred() { + this.context = new AnnotationConfigApplicationContext(); + this.context.register(CustomMessageSource.class, + MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertEquals("foo", this.context.getMessage("foo", null, null, null)); + } + + @Test + public void existingMessageSourceInParentIsIgnored() { + ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext(); + parent.refresh(); + try { + this.context = new AnnotationConfigApplicationContext(); + this.context.setParent(parent); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.messages.basename:test/messages"); + this.context.register(MessageSourceAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + assertEquals("bar", + this.context.getMessage("foo", null, "Foo message", Locale.UK)); + } + finally { + parent.close(); + } + } + private void load(String... environment) { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, environment); @@ -126,4 +161,33 @@ public class MessageSourceAutoConfigurationTests { } + @Configuration + protected static class CustomMessageSource { + + @Bean + public MessageSource messageSource() { + return new MessageSource() { + + @Override + public String getMessage(String code, Object[] args, + String defaultMessage, Locale locale) { + return code; + } + + @Override + public String getMessage(String code, Object[] args, Locale locale) + throws NoSuchMessageException { + return code; + } + + @Override + public String getMessage(MessageSourceResolvable resolvable, + Locale locale) throws NoSuchMessageException { + return resolvable.getCodes()[0]; + } + + }; + } + + } }