1717package org .springframework .boot .autoconfigure .web ;
1818
1919import java .lang .reflect .Field ;
20+ import java .util .ArrayList ;
21+ import java .util .Arrays ;
2022import java .util .Date ;
2123import java .util .LinkedHashMap ;
2224import java .util .List ;
7981 * @author Phillip Webb
8082 * @author Dave Syer
8183 * @author Andy Wilkinson
84+ * @author Stephane Nicoll
8285 */
8386public class WebMvcAutoConfigurationTests {
8487
@@ -98,11 +101,7 @@ public void close() {
98101
99102 @ Test
100103 public void handerAdaptersCreated () throws Exception {
101- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
102- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
103- HttpMessageConvertersAutoConfiguration .class ,
104- PropertyPlaceholderAutoConfiguration .class );
105- this .context .refresh ();
104+ load ();
106105 assertEquals (3 , this .context .getBeanNamesForType (HandlerAdapter .class ).length );
107106 assertFalse (this .context .getBean (RequestMappingHandlerAdapter .class )
108107 .getMessageConverters ().isEmpty ());
@@ -113,21 +112,13 @@ public void handerAdaptersCreated() throws Exception {
113112
114113 @ Test
115114 public void handerMappingsCreated () throws Exception {
116- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
117- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
118- HttpMessageConvertersAutoConfiguration .class ,
119- PropertyPlaceholderAutoConfiguration .class );
120- this .context .refresh ();
115+ load ();
121116 assertEquals (6 , this .context .getBeanNamesForType (HandlerMapping .class ).length );
122117 }
123118
124119 @ Test
125120 public void resourceHandlerMapping () throws Exception {
126- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
127- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
128- HttpMessageConvertersAutoConfiguration .class ,
129- PropertyPlaceholderAutoConfiguration .class );
130- this .context .refresh ();
121+ load ();
131122 Map <String , List <Resource >> mappingLocations = getResourceMappingLocations ();
132123 assertThat (mappingLocations .get ("/**" ).size (), equalTo (5 ));
133124 assertThat (mappingLocations .get ("/webjars/**" ).size (), equalTo (1 ));
@@ -137,25 +128,16 @@ public void resourceHandlerMapping() throws Exception {
137128
138129 @ Test
139130 public void resourceHandlerMappingOverrideWebjars () throws Exception {
140- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
141- this .context .register (WebJars .class , Config .class , WebMvcAutoConfiguration .class ,
142- HttpMessageConvertersAutoConfiguration .class ,
143- PropertyPlaceholderAutoConfiguration .class );
144- this .context .refresh ();
131+ load (WebJars .class );
145132 Map <String , List <Resource >> mappingLocations = getResourceMappingLocations ();
146133 assertThat (mappingLocations .get ("/webjars/**" ).size (), equalTo (1 ));
147134 assertThat (mappingLocations .get ("/webjars/**" ).get (0 ),
148135 equalTo ((Resource ) new ClassPathResource ("/foo/" )));
149136 }
150137
151138 @ Test
152- public void resourceHandlerMappingOverrideAll () throws Exception {
153- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
154- this .context .register (AllResources .class , Config .class ,
155- WebMvcAutoConfiguration .class ,
156- HttpMessageConvertersAutoConfiguration .class ,
157- PropertyPlaceholderAutoConfiguration .class );
158- this .context .refresh ();
139+ public void resourceHandlerMappingOverrideAll () throws Exception {
140+ load (AllResources .class );
159141 Map <String , List <Resource >> mappingLocations = getResourceMappingLocations ();
160142 assertThat (mappingLocations .get ("/**" ).size (), equalTo (1 ));
161143 assertThat (mappingLocations .get ("/**" ).get (0 ),
@@ -164,39 +146,22 @@ public void resourceHandlerMappingOverrideAll() throws Exception {
164146
165147 @ Test
166148 public void resourceHandlerMappingDisabled () throws Exception {
167- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
168- EnvironmentTestUtils .addEnvironment (this .context ,
169- "spring.resources.add-mappings:false" );
170- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
171- HttpMessageConvertersAutoConfiguration .class ,
172- PropertyPlaceholderAutoConfiguration .class );
173- this .context .refresh ();
149+ load ("spring.resources.add-mappings:false" );
174150 Map <String , List <Resource >> mappingLocations = getResourceMappingLocations ();
175151 assertThat (mappingLocations .size (), equalTo (0 ));
176152 }
177153
178154 @ Test
179155 public void noLocaleResolver () throws Exception {
180- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
181- this .context .register (AllResources .class , Config .class ,
182- WebMvcAutoConfiguration .class ,
183- HttpMessageConvertersAutoConfiguration .class ,
184- PropertyPlaceholderAutoConfiguration .class );
185- this .context .refresh ();
156+ load (AllResources .class );
186157 this .thrown .expect (NoSuchBeanDefinitionException .class );
187158 this .context .getBean (LocaleResolver .class );
188159 }
189160
190161 @ Test
191162 public void overrideLocale () throws Exception {
192- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
193- // set fixed locale
194- EnvironmentTestUtils .addEnvironment (this .context , "spring.mvc.locale:en_UK" );
195- this .context .register (AllResources .class , Config .class ,
196- WebMvcAutoConfiguration .class ,
197- HttpMessageConvertersAutoConfiguration .class ,
198- PropertyPlaceholderAutoConfiguration .class );
199- this .context .refresh ();
163+ load (AllResources .class , "spring.mvc.locale:en_UK" );
164+
200165 // mock request and set user preferred locale
201166 MockHttpServletRequest request = new MockHttpServletRequest ();
202167 request .addPreferredLocale (StringUtils .parseLocaleString ("nl_NL" ));
@@ -209,12 +174,7 @@ public void overrideLocale() throws Exception {
209174
210175 @ Test
211176 public void noDateFormat () throws Exception {
212- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
213- this .context .register (AllResources .class , Config .class ,
214- WebMvcAutoConfiguration .class ,
215- HttpMessageConvertersAutoConfiguration .class ,
216- PropertyPlaceholderAutoConfiguration .class );
217- this .context .refresh ();
177+ load (AllResources .class );
218178 FormattingConversionService cs = this .context
219179 .getBean (FormattingConversionService .class );
220180 Date date = new DateTime (1988 , 6 , 25 , 20 , 30 ).toDate ();
@@ -224,15 +184,7 @@ public void noDateFormat() throws Exception {
224184
225185 @ Test
226186 public void overrideDateFormat () throws Exception {
227- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
228- // set fixed date format
229- EnvironmentTestUtils .addEnvironment (this .context ,
230- "spring.mvc.dateFormat:dd*MM*yyyy" );
231- this .context .register (AllResources .class , Config .class ,
232- WebMvcAutoConfiguration .class ,
233- HttpMessageConvertersAutoConfiguration .class ,
234- PropertyPlaceholderAutoConfiguration .class );
235- this .context .refresh ();
187+ load (AllResources .class , "spring.mvc.dateFormat:dd*MM*yyyy" );
236188 FormattingConversionService cs = this .context
237189 .getBean (FormattingConversionService .class );
238190 Date date = new DateTime (1988 , 6 , 25 , 20 , 30 ).toDate ();
@@ -241,26 +193,14 @@ public void overrideDateFormat() throws Exception {
241193
242194 @ Test
243195 public void noMessageCodesResolver () throws Exception {
244- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
245- this .context .register (AllResources .class , Config .class ,
246- WebMvcAutoConfiguration .class ,
247- HttpMessageConvertersAutoConfiguration .class ,
248- PropertyPlaceholderAutoConfiguration .class );
249- this .context .refresh ();
196+ load (AllResources .class );
250197 assertNull (this .context .getBean (WebMvcAutoConfigurationAdapter .class )
251198 .getMessageCodesResolver ());
252199 }
253200
254201 @ Test
255202 public void overrideMessageCodesFormat () throws Exception {
256- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
257- EnvironmentTestUtils .addEnvironment (this .context ,
258- "spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE" );
259- this .context .register (AllResources .class , Config .class ,
260- WebMvcAutoConfiguration .class ,
261- HttpMessageConvertersAutoConfiguration .class ,
262- PropertyPlaceholderAutoConfiguration .class );
263- this .context .refresh ();
203+ load (AllResources .class , "spring.mvc.messageCodesResolverFormat:POSTFIX_ERROR_CODE" );
264204 assertNotNull (this .context .getBean (WebMvcAutoConfigurationAdapter .class )
265205 .getMessageCodesResolver ());
266206 }
@@ -300,11 +240,7 @@ protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping
300240
301241 @ Test
302242 public void ignoreDefaultModelOnRedirectIsTrue () throws Exception {
303- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
304- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
305- HttpMessageConvertersAutoConfiguration .class ,
306- PropertyPlaceholderAutoConfiguration .class );
307- this .context .refresh ();
243+ load ();
308244 RequestMappingHandlerAdapter adapter = this .context
309245 .getBean (RequestMappingHandlerAdapter .class );
310246 assertEquals (true ,
@@ -328,23 +264,13 @@ public void overrideIgnoreDefaultModelOnRedirect() throws Exception {
328264
329265 @ Test
330266 public void customViewResolver () throws Exception {
331- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
332- this .context .register (Config .class , CustomViewResolver .class ,
333- WebMvcAutoConfiguration .class ,
334- HttpMessageConvertersAutoConfiguration .class ,
335- PropertyPlaceholderAutoConfiguration .class );
336- this .context .refresh ();
267+ load (CustomViewResolver .class );
337268 assertThat (this .context .getBean ("viewResolver" ), instanceOf (MyViewResolver .class ));
338269 }
339270
340271 @ Test
341272 public void customContentNegotiatingViewResolver () throws Exception {
342- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
343- this .context .register (Config .class , CustomContentNegotiatingViewResolver .class ,
344- WebMvcAutoConfiguration .class ,
345- HttpMessageConvertersAutoConfiguration .class ,
346- PropertyPlaceholderAutoConfiguration .class );
347- this .context .refresh ();
273+ load (CustomContentNegotiatingViewResolver .class );
348274 Map <String , ContentNegotiatingViewResolver > beans = this .context
349275 .getBeansOfType (ContentNegotiatingViewResolver .class );
350276 assertThat (beans .size (), equalTo (1 ));
@@ -353,11 +279,7 @@ public void customContentNegotiatingViewResolver() throws Exception {
353279
354280 @ Test
355281 public void faviconMapping () throws IllegalAccessException {
356- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
357- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
358- HttpMessageConvertersAutoConfiguration .class ,
359- PropertyPlaceholderAutoConfiguration .class );
360- this .context .refresh ();
282+ load ();
361283 assertThat (
362284 this .context .getBeansOfType (ResourceHttpRequestHandler .class ).get (
363285 "faviconRequestHandler" ), is (notNullValue ()));
@@ -370,13 +292,7 @@ public void faviconMapping() throws IllegalAccessException {
370292
371293 @ Test
372294 public void faviconMappingDisabled () throws IllegalAccessException {
373- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
374- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
375- HttpMessageConvertersAutoConfiguration .class ,
376- PropertyPlaceholderAutoConfiguration .class );
377- EnvironmentTestUtils .addEnvironment (this .context ,
378- "spring.mvc.favicon.enabled:false" );
379- this .context .refresh ();
295+ load ("spring.mvc.favicon.enabled:false" );
380296 assertThat (
381297 this .context .getBeansOfType (ResourceHttpRequestHandler .class ).get (
382298 "faviconRequestHandler" ), is (nullValue ()));
@@ -387,31 +303,42 @@ public void faviconMappingDisabled() throws IllegalAccessException {
387303
388304 @ Test
389305 public void defaultAsyncRequestTimeout () throws Exception {
390- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
391- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
392- HttpMessageConvertersAutoConfiguration .class ,
393- PropertyPlaceholderAutoConfiguration .class );
394- this .context .refresh ();
306+ load ();
395307 RequestMappingHandlerAdapter adapter = this .context
396308 .getBean (RequestMappingHandlerAdapter .class );
397309 assertNull (ReflectionTestUtils .getField (adapter , "asyncRequestTimeout" ));
398310 }
399311
400312 @ Test
401313 public void customAsyncRequestTimeout () throws Exception {
402- this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
403- EnvironmentTestUtils .addEnvironment (this .context ,
404- "spring.mvc.async.request-timeout:123456" );
405- this .context .register (Config .class , WebMvcAutoConfiguration .class ,
406- HttpMessageConvertersAutoConfiguration .class ,
407- PropertyPlaceholderAutoConfiguration .class );
408- this .context .refresh ();
314+ load ("spring.mvc.async.request-timeout:123456" );
409315 RequestMappingHandlerAdapter adapter = this .context
410316 .getBean (RequestMappingHandlerAdapter .class );
411317 Object actual = ReflectionTestUtils .getField (adapter , "asyncRequestTimeout" );
412318 assertEquals (123456L , actual );
413319 }
414320
321+
322+ @ SuppressWarnings ("unchecked" )
323+ private void load (Class <?> config , String ... environment ) {
324+ this .context = new AnnotationConfigEmbeddedWebApplicationContext ();
325+ EnvironmentTestUtils .addEnvironment (this .context , environment );
326+ List <Class <?>> configClasses = new ArrayList <Class <?>>();
327+ if (config != null ) {
328+ configClasses .add (config );
329+ }
330+ configClasses .addAll (Arrays .asList (Config .class , WebMvcAutoConfiguration .class ,
331+ HttpMessageConvertersAutoConfiguration .class ,
332+ PropertyPlaceholderAutoConfiguration .class ));
333+ this .context .register (configClasses .toArray (new Class <?>[configClasses .size ()]));
334+ this .context .refresh ();
335+ }
336+
337+ private void load (String ... environment ) {
338+ load (null , environment );
339+ }
340+
341+
415342 @ Configuration
416343 protected static class ViewConfig {
417344
0 commit comments