Polish
This commit is contained in:
parent
6c826ef0b6
commit
d778173089
|
|
@ -18,17 +18,15 @@ package org.springframework.boot.autoconfigure.context;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.DirectFieldAccessor;
|
import org.springframework.beans.DirectFieldAccessor;
|
||||||
import org.springframework.boot.test.util.TestPropertyValues;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
import org.springframework.context.MessageSourceResolvable;
|
import org.springframework.context.MessageSourceResolvable;
|
||||||
import org.springframework.context.NoSuchMessageException;
|
import org.springframework.context.NoSuchMessageException;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
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;
|
||||||
|
|
@ -45,92 +43,89 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
public class MessageSourceAutoConfigurationTests {
|
public class MessageSourceAutoConfigurationTests {
|
||||||
|
|
||||||
private AnnotationConfigApplicationContext context;
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(
|
||||||
|
MessageSourceAutoConfiguration.class));
|
||||||
|
|
||||||
@After
|
@Test
|
||||||
public void closeContext() {
|
public void testDefaultMessageSource() {
|
||||||
if (this.context != null) {
|
this.contextRunner.run((context) ->
|
||||||
this.context.close();
|
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
|
||||||
}
|
.isEqualTo("Foo message"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultMessageSource() throws Exception {
|
public void testMessageSourceCreated() {
|
||||||
load();
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
|
||||||
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
|
.run((context) -> assertThat(context.getMessage(
|
||||||
.isEqualTo("Foo message");
|
"foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMessageSourceCreated() throws Exception {
|
public void testEncodingWorks() {
|
||||||
load("spring.messages.basename:test/messages");
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/swedish")
|
||||||
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
|
.run((context) -> assertThat(context.getMessage(
|
||||||
|
"foo", null, "Foo message", Locale.UK)).isEqualTo(
|
||||||
|
"Some text with some swedish öäå!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultipleMessageSourceCreated() {
|
||||||
|
this.contextRunner.withPropertyValues(
|
||||||
|
"spring.messages.basename:test/messages,test/messages2").run((context) -> {
|
||||||
|
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
|
||||||
.isEqualTo("bar");
|
.isEqualTo("bar");
|
||||||
}
|
assertThat(context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK))
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testEncodingWorks() throws Exception {
|
|
||||||
load("spring.messages.basename:test/swedish");
|
|
||||||
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
|
|
||||||
.isEqualTo("Some text with some swedish öäå!");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testMultipleMessageSourceCreated() throws Exception {
|
|
||||||
load("spring.messages.basename:test/messages,test/messages2");
|
|
||||||
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
|
|
||||||
.isEqualTo("bar");
|
|
||||||
assertThat(this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK))
|
|
||||||
.isEqualTo("bar-bar");
|
.isEqualTo("bar-bar");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBadEncoding() throws Exception {
|
public void testBadEncoding() {
|
||||||
load("spring.messages.encoding:rubbish");
|
this.contextRunner.withPropertyValues("spring.messages.encoding:rubbish")
|
||||||
|
.run((context) -> {
|
||||||
// Bad encoding just means the messages are ignored
|
// Bad encoding just means the messages are ignored
|
||||||
assertThat(this.context.getMessage("foo", null, "blah", Locale.UK))
|
assertThat(context.getMessage("foo", null, "blah", Locale.UK))
|
||||||
.isEqualTo("blah");
|
.isEqualTo("blah");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("Expected to fail per gh-1075")
|
@Ignore("Expected to fail per gh-1075")
|
||||||
public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
|
public void testMessageSourceFromPropertySourceAnnotation() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.contextRunner.withUserConfiguration(Config.class).run((context) ->
|
||||||
this.context.register(Config.class, MessageSourceAutoConfiguration.class,
|
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
.isEqualTo("bar"));
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
|
|
||||||
.isEqualTo("bar");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFallbackDefault() throws Exception {
|
public void testFallbackDefault() {
|
||||||
load("spring.messages.basename:test/messages");
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
|
||||||
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
|
.run((context) -> assertThat(isFallbackToSystemLocale(
|
||||||
.isTrue();
|
context.getBean(MessageSource.class))).isTrue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFallbackTurnOff() throws Exception {
|
public void testFallbackTurnOff() {
|
||||||
load("spring.messages.basename:test/messages",
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
|
||||||
"spring.messages.fallback-to-system-locale:false");
|
"spring.messages.fallback-to-system-locale:false").run((context) ->
|
||||||
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
|
assertThat(isFallbackToSystemLocale(context.getBean(MessageSource.class)))
|
||||||
.isFalse();
|
.isFalse());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormatMessageDefault() throws Exception {
|
public void testFormatMessageDefault() {
|
||||||
load("spring.messages.basename:test/messages");
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
|
||||||
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
|
.run((context) -> assertThat(isAlwaysUseMessageFormat(
|
||||||
.isFalse();
|
context.getBean(MessageSource.class))).isFalse());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFormatMessageOn() throws Exception {
|
public void testFormatMessageOn() throws Exception {
|
||||||
load("spring.messages.basename:test/messages",
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
|
||||||
"spring.messages.always-use-message-format:true");
|
"spring.messages.always-use-message-format:true").run((context) ->
|
||||||
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
|
assertThat(isAlwaysUseMessageFormat(context.getBean(MessageSource.class)))
|
||||||
.isTrue();
|
.isTrue());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
|
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
|
||||||
|
|
@ -145,17 +140,17 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUseCodeAsDefaultMessageDefault() {
|
public void testUseCodeAsDefaultMessageDefault() {
|
||||||
load("spring.messages.basename:test/messages");
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
|
||||||
assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class)))
|
.run((context) -> assertThat(isUseCodeAsDefaultMessage(
|
||||||
.isFalse();
|
context.getBean(MessageSource.class))).isFalse());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUseCodeAsDefaultMessageOn() {
|
public void testUseCodeAsDefaultMessageOn() {
|
||||||
load("spring.messages.basename:test/messages",
|
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
|
||||||
"spring.messages.use-code-as-default-message:true");
|
"spring.messages.use-code-as-default-message:true").run((context) ->
|
||||||
assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class)))
|
assertThat(isUseCodeAsDefaultMessage(
|
||||||
.isTrue();
|
context.getBean(MessageSource.class))).isTrue());
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
|
private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
|
||||||
|
|
@ -165,36 +160,19 @@ public class MessageSourceAutoConfigurationTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void existingMessageSourceIsPreferred() {
|
public void existingMessageSourceIsPreferred() {
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
|
||||||
this.context.register(CustomMessageSource.class,
|
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
|
||||||
MessageSourceAutoConfiguration.class,
|
.isEqualTo("foo"));
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getMessage("foo", null, null, null)).isEqualTo("foo");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void existingMessageSourceInParentIsIgnored() {
|
public void existingMessageSourceInParentIsIgnored() {
|
||||||
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) {
|
this.contextRunner.run((parent) -> {
|
||||||
parent.refresh();
|
this.contextRunner.withParent(parent)
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
.withPropertyValues("spring.messages.basename:test/messages")
|
||||||
this.context.setParent(parent);
|
.run((context) -> assertThat(context.getMessage(
|
||||||
TestPropertyValues.of("spring.messages.basename:test/messages")
|
"foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
|
||||||
.applyTo(this.context);
|
});
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
|
|
||||||
.isEqualTo("bar");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void load(String... environment) {
|
|
||||||
this.context = new AnnotationConfigApplicationContext();
|
|
||||||
TestPropertyValues.of(environment).applyTo(this.context);
|
|
||||||
this.context.register(MessageSourceAutoConfiguration.class,
|
|
||||||
PropertyPlaceholderAutoConfiguration.class);
|
|
||||||
this.context.refresh();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue