Closes gh-3750
This commit is contained in:
Stephane Nicoll 2015-08-14 09:24:24 +02:00
parent 74e1a1c940
commit 4e0f99ed8b
3 changed files with 28 additions and 44 deletions

View File

@ -79,7 +79,9 @@ public class MessageSourceAutoConfiguration {
private int cacheSeconds = -1;
/**
* Fall back to the system locale.
* Set whether to fall back to the system Locale if no files for a specific Locale
* have been found. if this is turned off, the only fallback will be the default
* file (e.g. "messages.properties" for basename "messages").
*/
private boolean fallbackToSystemLocale = true;

View File

@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure;
import java.util.Locale;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -34,53 +34,44 @@ import static org.junit.Assert.assertTrue;
* Tests for {@link MessageSourceAutoConfiguration}.
*
* @author Dave Syer
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
public class MessageSourceAutoConfigurationTests {
private AnnotationConfigApplicationContext context;
@After
public void closeContext() {
if (context != null) {
context.close();
}
}
@Test
public void testDefaultMessageSource() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load();
assertEquals("Foo message",
this.context.getMessage("foo", null, "Foo message", Locale.UK));
}
@Test
public void testMessageSourceCreated() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/messages");
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load("spring.messages.basename:test/messages");
assertEquals("bar",
this.context.getMessage("foo", null, "Foo message", Locale.UK));
}
@Test
public void testEncodingWorks() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/swedish");
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load("spring.messages.basename:test/swedish");
assertEquals("Some text with some swedish öäå!",
this.context.getMessage("foo", null, "Foo message", Locale.UK));
}
@Test
public void testMultipleMessageSourceCreated() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/messages,test/messages2");
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load("spring.messages.basename:test/messages,test/messages2");
assertEquals("bar", this.context.getMessage("foo", null, "Foo message", Locale.UK));
assertEquals("bar-bar",
this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK));
@ -88,11 +79,7 @@ public class MessageSourceAutoConfigurationTests {
@Test
public void testBadEncoding() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, "spring.messages.encoding:rubbish");
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load("spring.messages.encoding:rubbish");
// Bad encoding just means the messages are ignored
assertEquals("blah", this.context.getMessage("foo", null, "blah", Locale.UK));
}
@ -110,31 +97,26 @@ public class MessageSourceAutoConfigurationTests {
@Test
public void testFallbackDefault() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/messages");
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
load("spring.messages.basename:test/messages");
assertTrue(this.context.getBean(MessageSourceAutoConfiguration.class)
.isFallbackToSystemLocale());
assertEquals("bar", this.context.getMessage("foo", null, "bar", Locale.UK));
}
@Test
public void testFallbackTurnOff() throws Exception {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.messages.basename:test/messages",
load("spring.messages.basename:test/messages",
"spring.messages.fallback-to-system-locale:false");
assertFalse(this.context.getBean(MessageSourceAutoConfiguration.class)
.isFallbackToSystemLocale());
}
private void load(String... environment) {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, environment);
this.context.register(MessageSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertFalse(this.context.getBean(MessageSourceAutoConfiguration.class)
.isFallbackToSystemLocale());
assertEquals("bar", this.context.getMessage("foo", null, "bar", Locale.UK));
}
@Configuration

View File

@ -279,7 +279,7 @@ content into your application; rather pick only the properties that you need.
spring.messages.basename=messages
spring.messages.cache-seconds=-1
spring.messages.encoding=UTF-8
spring.messages.fallback-to-system-locale=true
spring.messages.fallback-to-system-locale=true # set whether to fall back to the system Locale
[[common-application-properties-security]]
# SECURITY ({sc-spring-boot-autoconfigure}/security/SecurityProperties.{sc-ext}[SecurityProperties])