18
18
19
19
import java .util .Locale ;
20
20
21
- import org .junit .After ;
22
21
import org .junit .Ignore ;
23
22
import org .junit .Test ;
24
23
25
24
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 ;
28
27
import org .springframework .context .MessageSource ;
29
28
import org .springframework .context .MessageSourceResolvable ;
30
29
import org .springframework .context .NoSuchMessageException ;
31
- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
32
30
import org .springframework .context .annotation .Bean ;
33
31
import org .springframework .context .annotation .Configuration ;
34
32
import org .springframework .context .annotation .PropertySource ;
41
39
* @author Dave Syer
42
40
* @author Eddú Meléndez
43
41
* @author Stephane Nicoll
42
+ * @author Kedar Joshi
44
43
*/
45
44
public class MessageSourceAutoConfigurationTests {
46
45
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 ));
55
49
56
50
@ 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" ) );
61
55
}
62
56
63
57
@ 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" ) );
68
62
}
69
63
70
64
@ 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 öäå!" ));
75
70
}
76
71
77
72
@ 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
+ });
84
81
}
85
82
86
83
@ 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
+ });
92
91
}
93
92
94
93
@ Test
95
94
@ 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" ));
103
99
}
104
100
105
101
@ 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 () );
110
106
}
111
107
112
108
@ 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 () );
118
114
}
119
115
120
116
@ 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 () );
125
121
}
126
122
127
123
@ Test
128
124
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 () );
133
129
}
134
130
135
131
private boolean isFallbackToSystemLocale (MessageSource messageSource ) {
@@ -143,37 +139,40 @@ private boolean isAlwaysUseMessageFormat(MessageSource messageSource) {
143
139
}
144
140
145
141
@ 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 ());
153
146
}
154
147
155
148
@ 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 ());
169
154
}
170
155
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
+ });
177
176
}
178
177
179
178
@ Configuration
0 commit comments