18
18
19
19
import java .util .List ;
20
20
import java .util .Locale ;
21
+ import java .util .Map ;
21
22
import javax .servlet .http .HttpServletRequest ;
22
23
23
24
import org .joda .time .DateTime ;
24
- import org .junit .Before ;
25
25
import org .junit .Test ;
26
26
import org .springframework .beans .DirectFieldAccessor ;
27
+ import org .springframework .beans .factory .BeanFactoryUtils ;
28
+ import org .springframework .context .ApplicationContext ;
27
29
import org .springframework .context .annotation .Bean ;
28
30
import org .springframework .context .annotation .Configuration ;
29
31
import org .springframework .context .annotation .Scope ;
44
46
import org .springframework .web .bind .annotation .PathVariable ;
45
47
import org .springframework .web .bind .annotation .RequestMapping ;
46
48
import org .springframework .web .bind .support .ConfigurableWebBindingInitializer ;
47
- import org .springframework .web .context .WebApplicationContext ;
48
49
import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
49
50
import org .springframework .web .method .support .CompositeUriComponentsContributor ;
50
51
import org .springframework .web .servlet .HandlerExceptionResolver ;
51
52
import org .springframework .web .servlet .HandlerExecutionChain ;
52
53
import org .springframework .web .servlet .ViewResolver ;
54
+ import org .springframework .web .servlet .view .BeanNameViewResolver ;
55
+ import org .springframework .web .servlet .view .InternalResourceViewResolver ;
53
56
import org .springframework .web .servlet .view .ViewResolverComposite ;
54
57
import org .springframework .web .servlet .handler .AbstractHandlerMapping ;
55
58
import org .springframework .web .servlet .handler .BeanNameUrlHandlerMapping ;
66
69
import org .springframework .web .util .UrlPathHelper ;
67
70
68
71
import static org .junit .Assert .*;
72
+ import static org .junit .Assert .assertEquals ;
69
73
70
74
/**
71
75
* A test fixture with an {@link WebMvcConfigurationSupport} instance.
76
80
*/
77
81
public class WebMvcConfigurationSupportTests {
78
82
79
- private WebApplicationContext wac ;
80
-
81
-
82
- @ Before
83
- public void setUp () {
84
- AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext ();
85
- cxt .setServletContext (new MockServletContext ());
86
- cxt .register (TestConfig .class , ScopedController .class , ScopedProxyController .class );
87
- cxt .refresh ();
88
-
89
- this .wac = cxt ;
90
- }
91
-
92
83
93
84
@ Test
94
85
public void requestMappingHandlerMapping () throws Exception {
95
- RequestMappingHandlerMapping handlerMapping = this .wac .getBean (RequestMappingHandlerMapping .class );
86
+ ApplicationContext context = initContext (WebConfig .class , ScopedController .class , ScopedProxyController .class );
87
+ RequestMappingHandlerMapping handlerMapping = context .getBean (RequestMappingHandlerMapping .class );
96
88
assertEquals (0 , handlerMapping .getOrder ());
97
89
98
90
HandlerExecutionChain chain = handlerMapping .getHandler (new MockHttpServletRequest ("GET" , "/" ));
@@ -109,8 +101,9 @@ public void requestMappingHandlerMapping() throws Exception {
109
101
110
102
@ Test
111
103
public void emptyViewControllerHandlerMapping () {
112
- AbstractHandlerMapping handlerMapping = this .wac .getBean (
113
- "viewControllerHandlerMapping" , AbstractHandlerMapping .class );
104
+ ApplicationContext context = initContext (WebConfig .class );
105
+ String name = "viewControllerHandlerMapping" ;
106
+ AbstractHandlerMapping handlerMapping = context .getBean (name , AbstractHandlerMapping .class );
114
107
115
108
assertNotNull (handlerMapping );
116
109
assertEquals (Integer .MAX_VALUE , handlerMapping .getOrder ());
@@ -119,7 +112,8 @@ public void emptyViewControllerHandlerMapping() {
119
112
120
113
@ Test
121
114
public void beanNameHandlerMapping () throws Exception {
122
- BeanNameUrlHandlerMapping handlerMapping = this .wac .getBean (BeanNameUrlHandlerMapping .class );
115
+ ApplicationContext context = initContext (WebConfig .class );
116
+ BeanNameUrlHandlerMapping handlerMapping = context .getBean (BeanNameUrlHandlerMapping .class );
123
117
assertEquals (2 , handlerMapping .getOrder ());
124
118
125
119
HttpServletRequest request = new MockHttpServletRequest ("GET" , "/testController" );
@@ -133,8 +127,8 @@ public void beanNameHandlerMapping() throws Exception {
133
127
134
128
@ Test
135
129
public void emptyResourceHandlerMapping () {
136
- AbstractHandlerMapping handlerMapping = this . wac . getBean (
137
- "resourceHandlerMapping" , AbstractHandlerMapping .class );
130
+ ApplicationContext context = initContext ( WebConfig . class );
131
+ AbstractHandlerMapping handlerMapping = context . getBean ( "resourceHandlerMapping" , AbstractHandlerMapping .class );
138
132
139
133
assertNotNull (handlerMapping );
140
134
assertEquals (Integer .MAX_VALUE , handlerMapping .getOrder ());
@@ -143,8 +137,9 @@ public void emptyResourceHandlerMapping() {
143
137
144
138
@ Test
145
139
public void emptyDefaultServletHandlerMapping () {
146
- AbstractHandlerMapping handlerMapping = this .wac .getBean (
147
- "defaultServletHandlerMapping" , AbstractHandlerMapping .class );
140
+ ApplicationContext context = initContext (WebConfig .class );
141
+ String name = "defaultServletHandlerMapping" ;
142
+ AbstractHandlerMapping handlerMapping = context .getBean (name , AbstractHandlerMapping .class );
148
143
149
144
assertNotNull (handlerMapping );
150
145
assertEquals (Integer .MAX_VALUE , handlerMapping .getOrder ());
@@ -153,7 +148,8 @@ public void emptyDefaultServletHandlerMapping() {
153
148
154
149
@ Test
155
150
public void requestMappingHandlerAdapter () throws Exception {
156
- RequestMappingHandlerAdapter adapter = this .wac .getBean (RequestMappingHandlerAdapter .class );
151
+ ApplicationContext context = initContext (WebConfig .class );
152
+ RequestMappingHandlerAdapter adapter = context .getBean (RequestMappingHandlerAdapter .class );
157
153
assertEquals (9 , adapter .getMessageConverters ().size ());
158
154
159
155
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer ) adapter .getWebBindingInitializer ();
@@ -175,7 +171,8 @@ public void requestMappingHandlerAdapter() throws Exception {
175
171
176
172
@ Test
177
173
public void uriComponentsContributor () throws Exception {
178
- CompositeUriComponentsContributor uriComponentsContributor = this .wac .getBean (
174
+ ApplicationContext context = initContext (WebConfig .class );
175
+ CompositeUriComponentsContributor uriComponentsContributor = context .getBean (
179
176
MvcUriComponentsBuilder .MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME ,
180
177
CompositeUriComponentsContributor .class );
181
178
@@ -184,8 +181,9 @@ public void uriComponentsContributor() throws Exception {
184
181
185
182
@ Test
186
183
public void handlerExceptionResolver () throws Exception {
184
+ ApplicationContext context = initContext (WebConfig .class );
187
185
HandlerExceptionResolverComposite compositeResolver =
188
- this . wac .getBean ("handlerExceptionResolver" , HandlerExceptionResolverComposite .class );
186
+ context .getBean ("handlerExceptionResolver" , HandlerExceptionResolverComposite .class );
189
187
190
188
assertEquals (0 , compositeResolver .getOrder ());
191
189
@@ -203,37 +201,72 @@ public void handlerExceptionResolver() throws Exception {
203
201
assertEquals (1 , interceptors .size ());
204
202
assertEquals (JsonViewResponseBodyAdvice .class , interceptors .get (0 ).getClass ());
205
203
}
206
-
204
+
207
205
@ Test
208
- public void emptyViewResolver () throws Exception {
209
- ViewResolverComposite compositeResolver = this .wac .getBean (ViewResolverComposite .class );
210
- assertEquals (Ordered .LOWEST_PRECEDENCE , compositeResolver .getOrder ());
211
- List <ViewResolver > resolvers = compositeResolver .getViewResolvers ();
212
- assertEquals (0 , resolvers .size ());
213
- assertNull (compositeResolver .resolveViewName ("anyViewName" , Locale .ENGLISH ));
206
+ public void mvcViewResolver () {
207
+ ApplicationContext context = initContext (WebConfig .class );
208
+ ViewResolverComposite resolver = context .getBean ("mvcViewResolver" , ViewResolverComposite .class );
209
+
210
+ Map <String , ViewResolver > map = BeanFactoryUtils .beansOfTypeIncludingAncestors (
211
+ context , ViewResolver .class , true , false );
212
+
213
+ assertNotNull (resolver );
214
+ assertEquals (1 , resolver .getViewResolvers ().size ());
215
+ assertEquals (InternalResourceViewResolver .class , resolver .getViewResolvers ().get (0 ).getClass ());
216
+ assertEquals (Ordered .LOWEST_PRECEDENCE , resolver .getOrder ());
217
+ }
218
+
219
+ @ Test
220
+ public void mvcViewResolverWithExistingResolver () throws Exception {
221
+ ApplicationContext context = initContext (WebConfig .class , ViewResolverConfig .class );
222
+ ViewResolverComposite resolver = context .getBean ("mvcViewResolver" , ViewResolverComposite .class );
223
+
224
+ assertNotNull (resolver );
225
+ assertEquals (0 , resolver .getViewResolvers ().size ());
226
+ assertEquals (Ordered .LOWEST_PRECEDENCE , resolver .getOrder ());
227
+ assertNull (resolver .resolveViewName ("anyViewName" , Locale .ENGLISH ));
214
228
}
215
229
216
230
@ Test
217
231
public void defaultPathMatchConfiguration () throws Exception {
218
- UrlPathHelper urlPathHelper = this .wac .getBean (UrlPathHelper .class );
219
- PathMatcher pathMatcher = this .wac .getBean (PathMatcher .class );
232
+ ApplicationContext context = initContext (WebConfig .class );
233
+ UrlPathHelper urlPathHelper = context .getBean (UrlPathHelper .class );
234
+ PathMatcher pathMatcher = context .getBean (PathMatcher .class );
220
235
221
236
assertNotNull (urlPathHelper );
222
237
assertNotNull (pathMatcher );
223
238
assertEquals (AntPathMatcher .class , pathMatcher .getClass ());
224
239
}
225
240
226
241
242
+ private ApplicationContext initContext (Class ... configClasses ) {
243
+ AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ();
244
+ context .setServletContext (new MockServletContext ());
245
+ context .register (configClasses );
246
+ context .refresh ();
247
+ return context ;
248
+ }
249
+
250
+
227
251
@ EnableWebMvc
228
252
@ Configuration
229
- public static class TestConfig {
253
+ public static class WebConfig {
230
254
231
255
@ Bean (name ="/testController" )
232
256
public TestController testController () {
233
257
return new TestController ();
234
258
}
235
259
}
236
260
261
+ @ Configuration
262
+ public static class ViewResolverConfig {
263
+
264
+ @ Bean
265
+ public ViewResolver beanNameViewResolver () {
266
+ return new BeanNameViewResolver ();
267
+ }
268
+ }
269
+
237
270
238
271
@ Controller
239
272
public static class TestController {
0 commit comments