Skip to content

Commit ec6659d

Browse files
committed
Merge pull request #10466 from kedar-joshi:master
* pr/10466: Polish Polish "Adds support for useCodeAsDefaultMessage" Adds support for useCodeAsDefaultMessage
2 parents 9102eb3 + d778173 commit ec6659d

File tree

4 files changed

+101
-85
lines changed

4 files changed

+101
-85
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public MessageSource messageSource() {
7575
messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
7676
messageSource.setCacheSeconds(properties.getCacheSeconds());
7777
messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
78+
messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
7879
return messageSource;
7980
}
8081

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Configuration properties for Message Source.
2323
*
2424
* @author Stephane Nicoll
25+
* @author Kedar Joshi
2526
* @since 2.0.0
2627
*/
2728
public class MessageSourceProperties {
@@ -57,6 +58,12 @@ public class MessageSourceProperties {
5758
*/
5859
private boolean alwaysUseMessageFormat = false;
5960

61+
/**
62+
* Set whether to use the message code as default message instead of throwing a
63+
* "NoSuchMessageException". Recommended during development only.
64+
*/
65+
private boolean useCodeAsDefaultMessage = false;
66+
6067
public String getBasename() {
6168
return this.basename;
6269
}
@@ -97,4 +104,12 @@ public void setAlwaysUseMessageFormat(boolean alwaysUseMessageFormat) {
97104
this.alwaysUseMessageFormat = alwaysUseMessageFormat;
98105
}
99106

107+
public boolean isUseCodeAsDefaultMessage() {
108+
return this.useCodeAsDefaultMessage;
109+
}
110+
111+
public void setUseCodeAsDefaultMessage(final boolean useCodeAsDefaultMessage) {
112+
this.useCodeAsDefaultMessage = useCodeAsDefaultMessage;
113+
}
114+
100115
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/MessageSourceAutoConfigurationTests.java

Lines changed: 84 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818

1919
import java.util.Locale;
2020

21-
import org.junit.After;
2221
import org.junit.Ignore;
2322
import org.junit.Test;
2423

2524
import org.springframework.beans.DirectFieldAccessor;
26-
import org.springframework.boot.test.util.TestPropertyValues;
27-
import org.springframework.context.ConfigurableApplicationContext;
25+
import org.springframework.boot.autoconfigure.AutoConfigurations;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2827
import org.springframework.context.MessageSource;
2928
import org.springframework.context.MessageSourceResolvable;
3029
import org.springframework.context.NoSuchMessageException;
31-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3230
import org.springframework.context.annotation.Bean;
3331
import org.springframework.context.annotation.Configuration;
3432
import org.springframework.context.annotation.PropertySource;
@@ -41,95 +39,93 @@
4139
* @author Dave Syer
4240
* @author Eddú Meléndez
4341
* @author Stephane Nicoll
42+
* @author Kedar Joshi
4443
*/
4544
public class MessageSourceAutoConfigurationTests {
4645

47-
private AnnotationConfigApplicationContext context;
48-
49-
@After
50-
public void closeContext() {
51-
if (this.context != null) {
52-
this.context.close();
53-
}
54-
}
46+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
47+
.withConfiguration(AutoConfigurations.of(
48+
MessageSourceAutoConfiguration.class));
5549

5650
@Test
57-
public void testDefaultMessageSource() throws Exception {
58-
load();
59-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
60-
.isEqualTo("Foo message");
51+
public void testDefaultMessageSource() {
52+
this.contextRunner.run((context) ->
53+
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
54+
.isEqualTo("Foo message"));
6155
}
6256

6357
@Test
64-
public void testMessageSourceCreated() throws Exception {
65-
load("spring.messages.basename:test/messages");
66-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
67-
.isEqualTo("bar");
58+
public void testMessageSourceCreated() {
59+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
60+
.run((context) -> assertThat(context.getMessage(
61+
"foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
6862
}
6963

7064
@Test
71-
public void testEncodingWorks() throws Exception {
72-
load("spring.messages.basename:test/swedish");
73-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
74-
.isEqualTo("Some text with some swedish öäå!");
65+
public void testEncodingWorks() {
66+
this.contextRunner.withPropertyValues("spring.messages.basename:test/swedish")
67+
.run((context) -> assertThat(context.getMessage(
68+
"foo", null, "Foo message", Locale.UK)).isEqualTo(
69+
"Some text with some swedish öäå!"));
7570
}
7671

7772
@Test
78-
public void testMultipleMessageSourceCreated() throws Exception {
79-
load("spring.messages.basename:test/messages,test/messages2");
80-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
81-
.isEqualTo("bar");
82-
assertThat(this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK))
83-
.isEqualTo("bar-bar");
73+
public void testMultipleMessageSourceCreated() {
74+
this.contextRunner.withPropertyValues(
75+
"spring.messages.basename:test/messages,test/messages2").run((context) -> {
76+
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
77+
.isEqualTo("bar");
78+
assertThat(context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK))
79+
.isEqualTo("bar-bar");
80+
});
8481
}
8582

8683
@Test
87-
public void testBadEncoding() throws Exception {
88-
load("spring.messages.encoding:rubbish");
89-
// Bad encoding just means the messages are ignored
90-
assertThat(this.context.getMessage("foo", null, "blah", Locale.UK))
91-
.isEqualTo("blah");
84+
public void testBadEncoding() {
85+
this.contextRunner.withPropertyValues("spring.messages.encoding:rubbish")
86+
.run((context) -> {
87+
// Bad encoding just means the messages are ignored
88+
assertThat(context.getMessage("foo", null, "blah", Locale.UK))
89+
.isEqualTo("blah");
90+
});
9291
}
9392

9493
@Test
9594
@Ignore("Expected to fail per gh-1075")
96-
public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
97-
this.context = new AnnotationConfigApplicationContext();
98-
this.context.register(Config.class, MessageSourceAutoConfiguration.class,
99-
PropertyPlaceholderAutoConfiguration.class);
100-
this.context.refresh();
101-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
102-
.isEqualTo("bar");
95+
public void testMessageSourceFromPropertySourceAnnotation() {
96+
this.contextRunner.withUserConfiguration(Config.class).run((context) ->
97+
assertThat(context.getMessage("foo", null, "Foo message", Locale.UK))
98+
.isEqualTo("bar"));
10399
}
104100

105101
@Test
106-
public void testFallbackDefault() throws Exception {
107-
load("spring.messages.basename:test/messages");
108-
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
109-
.isTrue();
102+
public void testFallbackDefault() {
103+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
104+
.run((context) -> assertThat(isFallbackToSystemLocale(
105+
context.getBean(MessageSource.class))).isTrue());
110106
}
111107

112108
@Test
113-
public void testFallbackTurnOff() throws Exception {
114-
load("spring.messages.basename:test/messages",
115-
"spring.messages.fallback-to-system-locale:false");
116-
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
117-
.isFalse();
109+
public void testFallbackTurnOff() {
110+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
111+
"spring.messages.fallback-to-system-locale:false").run((context) ->
112+
assertThat(isFallbackToSystemLocale(context.getBean(MessageSource.class)))
113+
.isFalse());
118114
}
119115

120116
@Test
121-
public void testFormatMessageDefault() throws Exception {
122-
load("spring.messages.basename:test/messages");
123-
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
124-
.isFalse();
117+
public void testFormatMessageDefault() {
118+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
119+
.run((context) -> assertThat(isAlwaysUseMessageFormat(
120+
context.getBean(MessageSource.class))).isFalse());
125121
}
126122

127123
@Test
128124
public void testFormatMessageOn() throws Exception {
129-
load("spring.messages.basename:test/messages",
130-
"spring.messages.always-use-message-format:true");
131-
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
132-
.isTrue();
125+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
126+
"spring.messages.always-use-message-format:true").run((context) ->
127+
assertThat(isAlwaysUseMessageFormat(context.getBean(MessageSource.class)))
128+
.isTrue());
133129
}
134130

135131
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
@@ -143,37 +139,40 @@ private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
143139
}
144140

145141
@Test
146-
public void existingMessageSourceIsPreferred() {
147-
this.context = new AnnotationConfigApplicationContext();
148-
this.context.register(CustomMessageSource.class,
149-
MessageSourceAutoConfiguration.class,
150-
PropertyPlaceholderAutoConfiguration.class);
151-
this.context.refresh();
152-
assertThat(this.context.getMessage("foo", null, null, null)).isEqualTo("foo");
142+
public void testUseCodeAsDefaultMessageDefault() {
143+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
144+
.run((context) -> assertThat(isUseCodeAsDefaultMessage(
145+
context.getBean(MessageSource.class))).isFalse());
153146
}
154147

155148
@Test
156-
public void existingMessageSourceInParentIsIgnored() {
157-
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) {
158-
parent.refresh();
159-
this.context = new AnnotationConfigApplicationContext();
160-
this.context.setParent(parent);
161-
TestPropertyValues.of("spring.messages.basename:test/messages")
162-
.applyTo(this.context);
163-
this.context.register(MessageSourceAutoConfiguration.class,
164-
PropertyPlaceholderAutoConfiguration.class);
165-
this.context.refresh();
166-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
167-
.isEqualTo("bar");
168-
}
149+
public void testUseCodeAsDefaultMessageOn() {
150+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages",
151+
"spring.messages.use-code-as-default-message:true").run((context) ->
152+
assertThat(isUseCodeAsDefaultMessage(
153+
context.getBean(MessageSource.class))).isTrue());
169154
}
170155

171-
private void load(String... environment) {
172-
this.context = new AnnotationConfigApplicationContext();
173-
TestPropertyValues.of(environment).applyTo(this.context);
174-
this.context.register(MessageSourceAutoConfiguration.class,
175-
PropertyPlaceholderAutoConfiguration.class);
176-
this.context.refresh();
156+
private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
157+
return (boolean) new DirectFieldAccessor(messageSource)
158+
.getPropertyValue("useCodeAsDefaultMessage");
159+
}
160+
161+
@Test
162+
public void existingMessageSourceIsPreferred() {
163+
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
164+
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
165+
.isEqualTo("foo"));
166+
}
167+
168+
@Test
169+
public void existingMessageSourceInParentIsIgnored() {
170+
this.contextRunner.run((parent) -> {
171+
this.contextRunner.withParent(parent)
172+
.withPropertyValues("spring.messages.basename:test/messages")
173+
.run((context) -> assertThat(context.getMessage(
174+
"foo", null, "Foo message", Locale.UK)).isEqualTo("bar"));
175+
});
177176
}
178177

179178
@Configuration

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ content into your application; rather pick only the properties that you need.
117117
spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever.
118118
spring.messages.encoding=UTF-8 # Message bundles encoding.
119119
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.
120+
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.
120121
121122
# OUTPUT
122123
spring.output.ansi.enabled=detect # Configure the ANSI output.

0 commit comments

Comments
 (0)