Commit 56977c03 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '1.2.x'

parents f8cffd74 c236db04
...@@ -22,7 +22,12 @@ import org.junit.After; ...@@ -22,7 +22,12 @@ import org.junit.After;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils; 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.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
...@@ -112,6 +117,36 @@ public class MessageSourceAutoConfigurationTests { ...@@ -112,6 +117,36 @@ public class MessageSourceAutoConfigurationTests {
.isFallbackToSystemLocale()); .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) { private void load(String... environment) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment); EnvironmentTestUtils.addEnvironment(this.context, environment);
...@@ -126,4 +161,33 @@ public class MessageSourceAutoConfigurationTests { ...@@ -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];
}
};
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment