16
16
17
17
package org .springframework .web .servlet .handler ;
18
18
19
+ import java .lang .annotation .Documented ;
20
+ import java .lang .annotation .ElementType ;
21
+ import java .lang .annotation .Retention ;
22
+ import java .lang .annotation .RetentionPolicy ;
23
+ import java .lang .annotation .Target ;
19
24
import java .util .Collections ;
20
25
import java .util .Map ;
21
26
import java .util .stream .Stream ;
40
45
41
46
import static org .assertj .core .api .Assertions .assertThat ;
42
47
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
48
+ import static org .junit .jupiter .api .Named .named ;
49
+ import static org .junit .jupiter .params .provider .Arguments .arguments ;
43
50
import static org .springframework .web .servlet .HandlerMapping .BEST_MATCHING_HANDLER_ATTRIBUTE ;
44
51
import static org .springframework .web .servlet .HandlerMapping .PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE ;
45
52
import static org .springframework .web .servlet .HandlerMapping .URI_TEMPLATE_VARIABLES_ATTRIBUTE ;
46
53
47
54
/**
48
55
* Tests for {@link SimpleUrlHandlerMapping}.
56
+ *
49
57
* @author Brian Clozel
50
58
*/
51
59
class SimpleUrlHandlerMappingTests {
@@ -73,8 +81,7 @@ void newlineInRequestShouldMatch() throws Exception {
73
81
assertThat (hec .getHandler ()).isSameAs (controller );
74
82
}
75
83
76
- @ ParameterizedTest
77
- @ MethodSource ("handlerMappings" )
84
+ @ HandlerMappingsTest
78
85
void resolveFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
79
86
StaticApplicationContext applicationContext = new StaticApplicationContext ();
80
87
applicationContext .registerSingleton ("mainController" , Object .class );
@@ -91,8 +98,7 @@ void resolveFromMap(SimpleUrlHandlerMapping handlerMapping) throws Exception {
91
98
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (mainController );
92
99
}
93
100
94
- @ ParameterizedTest
95
- @ MethodSource ("handlerMappings" )
101
+ @ HandlerMappingsTest
96
102
void resolvePatternFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
97
103
StaticApplicationContext applicationContext = new StaticApplicationContext ();
98
104
applicationContext .registerSingleton ("mainController" , Object .class );
@@ -109,8 +115,7 @@ void resolvePatternFromMap(SimpleUrlHandlerMapping handlerMapping) throws Except
109
115
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (mainController );
110
116
}
111
117
112
- @ ParameterizedTest
113
- @ MethodSource ("handlerMappings" )
118
+ @ HandlerMappingsTest
114
119
void resolvePathWithParamFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
115
120
StaticApplicationContext applicationContext = new StaticApplicationContext ();
116
121
applicationContext .registerSingleton ("mainController" , Object .class );
@@ -127,8 +132,7 @@ void resolvePathWithParamFromMap(SimpleUrlHandlerMapping handlerMapping) throws
127
132
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (mainController );
128
133
}
129
134
130
- @ ParameterizedTest
131
- @ MethodSource ("handlerMappings" )
135
+ @ HandlerMappingsTest
132
136
void resolvePathWithContextFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
133
137
StaticApplicationContext applicationContext = new StaticApplicationContext ();
134
138
applicationContext .registerSingleton ("mainController" , Object .class );
@@ -145,8 +149,7 @@ void resolvePathWithContextFromMap(SimpleUrlHandlerMapping handlerMapping) throw
145
149
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (mainController );
146
150
}
147
151
148
- @ ParameterizedTest
149
- @ MethodSource ("handlerMappings" )
152
+ @ HandlerMappingsTest
150
153
void resolvePathWithIncludeFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
151
154
StaticApplicationContext applicationContext = new StaticApplicationContext ();
152
155
applicationContext .registerSingleton ("mainController" , Object .class );
@@ -164,8 +167,7 @@ void resolvePathWithIncludeFromMap(SimpleUrlHandlerMapping handlerMapping) throw
164
167
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (mainController );
165
168
}
166
169
167
- @ ParameterizedTest
168
- @ MethodSource ("handlerMappings" )
170
+ @ HandlerMappingsTest
169
171
void resolveDefaultPathFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
170
172
StaticApplicationContext applicationContext = new StaticApplicationContext ();
171
173
applicationContext .registerSingleton ("mainController" , Object .class );
@@ -182,8 +184,7 @@ void resolveDefaultPathFromMap(SimpleUrlHandlerMapping handlerMapping) throws Ex
182
184
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (mainController );
183
185
}
184
186
185
- @ ParameterizedTest
186
- @ MethodSource ("handlerMappings" )
187
+ @ HandlerMappingsTest
187
188
void resolveParameterizedControllerFromMap (SimpleUrlHandlerMapping handlerMapping ) throws Exception {
188
189
ParameterizableViewController viewController = new ParameterizableViewController ();
189
190
viewController .setView (new RedirectView ("/after/{variable}" ));
@@ -196,18 +197,12 @@ void resolveParameterizedControllerFromMap(SimpleUrlHandlerMapping handlerMappin
196
197
HandlerExecutionChain chain = getHandler (handlerMapping , request );
197
198
198
199
assertThat (chain .getHandler ()).isSameAs (viewController );
200
+ @ SuppressWarnings ("unchecked" )
199
201
Map <String , String > variables = (Map <String , String >) request .getAttribute (URI_TEMPLATE_VARIABLES_ATTRIBUTE );
200
202
assertThat (variables ).containsEntry ("variable" , "test" );
201
203
assertThat (request .getAttribute (BEST_MATCHING_HANDLER_ATTRIBUTE )).isEqualTo (viewController );
202
204
}
203
205
204
- static Stream <Arguments > handlerMappings () {
205
- SimpleUrlHandlerMapping defaultConfig = new SimpleUrlHandlerMapping ();
206
- SimpleUrlHandlerMapping antPatternConfig = new SimpleUrlHandlerMapping ();
207
- antPatternConfig .setPatternParser (null );
208
- return Stream .of (Arguments .of (defaultConfig , "with PathPattern" ), Arguments .of (antPatternConfig , "with AntPathMatcher" ));
209
- }
210
-
211
206
private HandlerExecutionChain getHandler (HandlerMapping mapping , MockHttpServletRequest request ) throws Exception {
212
207
HandlerExecutionChain chain = mapping .getHandler (request );
213
208
Assert .notNull (chain , "No handler found for request: " + request .getRequestURI ());
@@ -217,4 +212,23 @@ private HandlerExecutionChain getHandler(HandlerMapping mapping, MockHttpServlet
217
212
return chain ;
218
213
}
219
214
215
+
216
+ @ Target (ElementType .METHOD )
217
+ @ Retention (RetentionPolicy .RUNTIME )
218
+ @ Documented
219
+ @ ParameterizedTest (name ="[{index}] {0}" )
220
+ @ MethodSource ("handlerMappings" )
221
+ @interface HandlerMappingsTest {
222
+ }
223
+
224
+ static Stream <Arguments > handlerMappings () {
225
+ SimpleUrlHandlerMapping defaultConfig = new SimpleUrlHandlerMapping ();
226
+ SimpleUrlHandlerMapping antPatternConfig = new SimpleUrlHandlerMapping ();
227
+ antPatternConfig .setPatternParser (null );
228
+ return Stream .of (
229
+ arguments (named ("with PathPattern" , defaultConfig )),
230
+ arguments (named ("with AntPathMatcher" , antPatternConfig ))
231
+ );
232
+ }
233
+
220
234
}
0 commit comments