Merge pull request #10466 from kedar-joshi:master

* pr/10466:
  Polish
  Polish "Adds support for useCodeAsDefaultMessage"
  Adds support for useCodeAsDefaultMessage
pull/10457/merge
Stephane Nicoll 7 years ago
commit ec6659db2e

@ -75,6 +75,7 @@ public class MessageSourceAutoConfiguration {
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale()); messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
messageSource.setCacheSeconds(properties.getCacheSeconds()); messageSource.setCacheSeconds(properties.getCacheSeconds());
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat()); messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
return messageSource; return messageSource;
} }

@ -22,6 +22,7 @@ import java.nio.charset.Charset;
* Configuration properties for Message Source. * Configuration properties for Message Source.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Kedar Joshi
* @since 2.0.0 * @since 2.0.0
*/ */
public class MessageSourceProperties { public class MessageSourceProperties {
@ -57,6 +58,12 @@ public class MessageSourceProperties {
*/ */
private boolean alwaysUseMessageFormat = false; private boolean alwaysUseMessageFormat = false;
/**
* Set whether to use the message code as default message instead of throwing a
* "NoSuchMessageException". Recommended during development only.
*/
private boolean useCodeAsDefaultMessage = false;
public String getBasename() { public String getBasename() {
return this.basename; return this.basename;
} }
@ -97,4 +104,12 @@ public class MessageSourceProperties {
this.alwaysUseMessageFormat = alwaysUseMessageFormat; this.alwaysUseMessageFormat = alwaysUseMessageFormat;
} }
public boolean isUseCodeAsDefaultMessage() {
return this.useCodeAsDefaultMessage;
}
public void setUseCodeAsDefaultMessage(final boolean useCodeAsDefaultMessage) {
this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
}
} }

@ -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;
@ -41,95 +39,93 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer * @author Dave Syer
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Kedar Joshi
*/ */
public class MessageSourceAutoConfigurationTests { public class MessageSourceAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
@After MessageSourceAutoConfiguration.class));
public void closeContext() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void testDefaultMessageSource() throws Exception { public void testDefaultMessageSource() {
load(); this.contextRunner.run((context) ->
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK)) assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
.isEqualTo("Foo message"); .isEqualTo("Foo message"));
} }
@Test @Test
public void testMessageSourceCreated() throws Exception { public void testMessageSourceCreated() {
load("spring.messages.basename:test/messages"); this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK)) .run((context) -> assertThat(context.getMessage(
.isEqualTo("bar"); "foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
} }
@Test @Test
public void testEncodingWorks() throws Exception { public void testEncodingWorks() {
load("spring.messages.basename:test/swedish"); this.contextRunner.withPropertyValues("spring.messages.basename:test/swedish")
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK)) .run((context) -> assertThat(context.getMessage(
.isEqualTo("Some text with some swedish öäå!"); "foo", null, "Foo message", Locale.UK)).isEqualTo(
"Some text with some swedish öäå!"));
} }
@Test @Test
public void testMultipleMessageSourceCreated() throws Exception { public void testMultipleMessageSourceCreated() {
load("spring.messages.basename:test/messages,test/messages2"); this.contextRunner.withPropertyValues(
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK)) "spring.messages.basename:test/messages,test/messages2").run((context) -> {
.isEqualTo("bar"); assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
assertThat(this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK)) .isEqualTo("bar");
.isEqualTo("bar-bar"); assertThat(context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK))
.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")
// Bad encoding just means the messages are ignored .run((context) -> {
assertThat(this.context.getMessage("foo", null, "blah", Locale.UK)) // Bad encoding just means the messages are ignored
.isEqualTo("blah"); assertThat(context.getMessage("foo", null, "blah", Locale.UK))
.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) {
@ -143,37 +139,40 @@ public class MessageSourceAutoConfigurationTests {
} }
@Test @Test
public void existingMessageSourceIsPreferred() { public void testUseCodeAsDefaultMessageDefault() {
this.context = new AnnotationConfigApplicationContext(); this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
this.context.register(CustomMessageSource.class, .run((context) -> assertThat(isUseCodeAsDefaultMessage(
MessageSourceAutoConfiguration.class, context.getBean(MessageSource.class))).isFalse());
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getMessage("foo", null, null, null)).isEqualTo("foo");
} }
@Test @Test
public void existingMessageSourceInParentIsIgnored() { public void testUseCodeAsDefaultMessageOn() {
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) { this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
parent.refresh(); "spring.messages.use-code-as-default-message:true").run((context) ->
this.context = new AnnotationConfigApplicationContext(); assertThat(isUseCodeAsDefaultMessage(
this.context.setParent(parent); context.getBean(MessageSource.class))).isTrue());
TestPropertyValues.of("spring.messages.basename:test/messages")
.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) { private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
this.context = new AnnotationConfigApplicationContext(); return (boolean) new DirectFieldAccessor(messageSource)
TestPropertyValues.of(environment).applyTo(this.context); .getPropertyValue("useCodeAsDefaultMessage");
this.context.register(MessageSourceAutoConfiguration.class, }
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); @Test
public void existingMessageSourceIsPreferred() {
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
.isEqualTo("foo"));
}
@Test
public void existingMessageSourceInParentIsIgnored() {
this.contextRunner.run((parent) -> {
this.contextRunner.withParent(parent)
.withPropertyValues("spring.messages.basename:test/messages")
.run((context) -> assertThat(context.getMessage(
"foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
});
} }
@Configuration @Configuration

@ -117,6 +117,7 @@ content into your application; rather pick only the properties that you need.
spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever. spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever.
spring.messages.encoding=UTF-8 # Message bundles encoding. spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Set whether to fall back to the system Locale if no files for a specific Locale have been found. spring.messages.fallback-to-system-locale=true # Set whether to fall back to the system Locale if no files for a specific Locale have been found.
spring.messages.use-code-as-default-message=false # Set whether to use the message code as default message instead of throwing a "NoSuchMessageException". Recommended during development only.
# OUTPUT # OUTPUT
spring.output.ansi.enabled=detect # Configure the ANSI output. spring.output.ansi.enabled=detect # Configure the ANSI output.

Loading…
Cancel
Save