Skip to content

Commit d778173

Browse files
committed
Polish
1 parent 6c826ef commit d778173

File tree

1 file changed

+70
-92
lines changed

1 file changed

+70
-92
lines changed

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

Lines changed: 70 additions & 92 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;
@@ -45,92 +43,89 @@
4543
*/
4644
public class MessageSourceAutoConfigurationTests {
4745

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

5750
@Test
58-
public void testDefaultMessageSource() throws Exception {
59-
load();
60-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
61-
.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"));
6255
}
6356

6457
@Test
65-
public void testMessageSourceCreated() throws Exception {
66-
load("spring.messages.basename:test/messages");
67-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
68-
.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"));
6962
}
7063

7164
@Test
72-
public void testEncodingWorks() throws Exception {
73-
load("spring.messages.basename:test/swedish");
74-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
75-
.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 öäå!"));
7670
}
7771

7872
@Test
79-
public void testMultipleMessageSourceCreated() throws Exception {
80-
load("spring.messages.basename:test/messages,test/messages2");
81-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
82-
.isEqualTo("bar");
83-
assertThat(this.context.getMessage("foo-foo", null, "Foo-Foo message", Locale.UK))
84-
.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+
});
8581
}
8682

8783
@Test
88-
public void testBadEncoding() throws Exception {
89-
load("spring.messages.encoding:rubbish");
90-
// Bad encoding just means the messages are ignored
91-
assertThat(this.context.getMessage("foo", null, "blah", Locale.UK))
92-
.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+
});
9391
}
9492

9593
@Test
9694
@Ignore("Expected to fail per gh-1075")
97-
public void testMessageSourceFromPropertySourceAnnotation() throws Exception {
98-
this.context = new AnnotationConfigApplicationContext();
99-
this.context.register(Config.class, MessageSourceAutoConfiguration.class,
100-
PropertyPlaceholderAutoConfiguration.class);
101-
this.context.refresh();
102-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
103-
.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"));
10499
}
105100

106101
@Test
107-
public void testFallbackDefault() throws Exception {
108-
load("spring.messages.basename:test/messages");
109-
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
110-
.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());
111106
}
112107

113108
@Test
114-
public void testFallbackTurnOff() throws Exception {
115-
load("spring.messages.basename:test/messages",
116-
"spring.messages.fallback-to-system-locale:false");
117-
assertThat(isFallbackToSystemLocale(this.context.getBean(MessageSource.class)))
118-
.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());
119114
}
120115

121116
@Test
122-
public void testFormatMessageDefault() throws Exception {
123-
load("spring.messages.basename:test/messages");
124-
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
125-
.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());
126121
}
127122

128123
@Test
129124
public void testFormatMessageOn() throws Exception {
130-
load("spring.messages.basename:test/messages",
131-
"spring.messages.always-use-message-format:true");
132-
assertThat(isAlwaysUseMessageFormat(this.context.getBean(MessageSource.class)))
133-
.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());
134129
}
135130

136131
private boolean isFallbackToSystemLocale(MessageSource messageSource) {
@@ -145,17 +140,17 @@ private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
145140

146141
@Test
147142
public void testUseCodeAsDefaultMessageDefault() {
148-
load("spring.messages.basename:test/messages");
149-
assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class)))
150-
.isFalse();
143+
this.contextRunner.withPropertyValues("spring.messages.basename:test/messages")
144+
.run((context) -> assertThat(isUseCodeAsDefaultMessage(
145+
context.getBean(MessageSource.class))).isFalse());
151146
}
152147

153148
@Test
154149
public void testUseCodeAsDefaultMessageOn() {
155-
load("spring.messages.basename:test/messages",
156-
"spring.messages.use-code-as-default-message:true");
157-
assertThat(isUseCodeAsDefaultMessage(this.context.getBean(MessageSource.class)))
158-
.isTrue();
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());
159154
}
160155

161156
private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
@@ -165,36 +160,19 @@ private boolean isUseCodeAsDefaultMessage(MessageSource messageSource) {
165160

166161
@Test
167162
public void existingMessageSourceIsPreferred() {
168-
this.context = new AnnotationConfigApplicationContext();
169-
this.context.register(CustomMessageSource.class,
170-
MessageSourceAutoConfiguration.class,
171-
PropertyPlaceholderAutoConfiguration.class);
172-
this.context.refresh();
173-
assertThat(this.context.getMessage("foo", null, null, null)).isEqualTo("foo");
163+
this.contextRunner.withUserConfiguration(CustomMessageSource.class)
164+
.run((context) -> assertThat(context.getMessage("foo", null, null, null))
165+
.isEqualTo("foo"));
174166
}
175167

176168
@Test
177169
public void existingMessageSourceInParentIsIgnored() {
178-
try (ConfigurableApplicationContext parent = new AnnotationConfigApplicationContext()) {
179-
parent.refresh();
180-
this.context = new AnnotationConfigApplicationContext();
181-
this.context.setParent(parent);
182-
TestPropertyValues.of("spring.messages.basename:test/messages")
183-
.applyTo(this.context);
184-
this.context.register(MessageSourceAutoConfiguration.class,
185-
PropertyPlaceholderAutoConfiguration.class);
186-
this.context.refresh();
187-
assertThat(this.context.getMessage("foo", null, "Foo message", Locale.UK))
188-
.isEqualTo("bar");
189-
}
190-
}
191-
192-
private void load(String... environment) {
193-
this.context = new AnnotationConfigApplicationContext();
194-
TestPropertyValues.of(environment).applyTo(this.context);
195-
this.context.register(MessageSourceAutoConfiguration.class,
196-
PropertyPlaceholderAutoConfiguration.class);
197-
this.context.refresh();
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+
});
198176
}
199177

200178
@Configuration

0 commit comments

Comments
 (0)