@@ -71,12 +71,12 @@ class RequestMappingHandlerMappingTests {
71
71
72
72
@ SuppressWarnings ("unused" )
73
73
static Stream <Arguments > pathPatternsArguments () {
74
- RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping ();
75
74
StaticWebApplicationContext wac1 = new StaticWebApplicationContext ();
76
- mapping1 .setApplicationContext (wac1 );
77
-
78
75
StaticWebApplicationContext wac2 = new StaticWebApplicationContext ();
79
76
77
+ RequestMappingHandlerMapping mapping1 = new RequestMappingHandlerMapping ();
78
+ mapping1 .setApplicationContext (wac1 );
79
+
80
80
RequestMappingHandlerMapping mapping2 = new RequestMappingHandlerMapping ();
81
81
mapping2 .setPatternParser (null );
82
82
mapping2 .setApplicationContext (wac2 );
@@ -86,8 +86,7 @@ static Stream<Arguments> pathPatternsArguments() {
86
86
87
87
@ Test
88
88
void builderConfiguration () {
89
- RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping ();
90
- mapping .setApplicationContext (new StaticWebApplicationContext ());
89
+ RequestMappingHandlerMapping mapping = createMapping ();
91
90
92
91
RequestMappingInfo .BuilderConfiguration config = mapping .getBuilderConfiguration ();
93
92
assertThat (config ).isNotNull ();
@@ -99,22 +98,20 @@ void builderConfiguration() {
99
98
@ Test
100
99
@ SuppressWarnings ("deprecation" )
101
100
void useRegisteredSuffixPatternMatch () {
102
-
103
- RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping ();
104
- handlerMapping .setApplicationContext (new StaticWebApplicationContext ());
101
+ RequestMappingHandlerMapping mapping = createMapping ();
105
102
106
103
Map <String , MediaType > fileExtensions = Collections .singletonMap ("json" , MediaType .APPLICATION_JSON );
107
104
org .springframework .web .accept .PathExtensionContentNegotiationStrategy strategy =
108
105
new org .springframework .web .accept .PathExtensionContentNegotiationStrategy (fileExtensions );
109
106
ContentNegotiationManager manager = new ContentNegotiationManager (strategy );
110
107
111
- handlerMapping .setContentNegotiationManager (manager );
112
- handlerMapping .setUseRegisteredSuffixPatternMatch (true );
113
- handlerMapping .afterPropertiesSet ();
108
+ mapping .setContentNegotiationManager (manager );
109
+ mapping .setUseRegisteredSuffixPatternMatch (true );
110
+ mapping .afterPropertiesSet ();
114
111
115
- assertThat (handlerMapping .useSuffixPatternMatch ()).isTrue ();
116
- assertThat (handlerMapping .useRegisteredSuffixPatternMatch ()).isTrue ();
117
- assertThat (handlerMapping .getFileExtensions ()).isEqualTo (Collections .singletonList ("json" ));
112
+ assertThat (mapping .useSuffixPatternMatch ()).isTrue ();
113
+ assertThat (mapping .useRegisteredSuffixPatternMatch ()).isTrue ();
114
+ assertThat (mapping .getFileExtensions ()).isEqualTo (Collections .singletonList ("json" ));
118
115
}
119
116
120
117
@ Test
@@ -150,25 +147,24 @@ protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handler
150
147
@ Test
151
148
@ SuppressWarnings ("deprecation" )
152
149
void suffixPatternMatchSettings () {
153
- RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping ();
150
+ RequestMappingHandlerMapping mapping = createMapping ();
154
151
155
- assertThat (handlerMapping .useSuffixPatternMatch ()).isFalse ();
156
- assertThat (handlerMapping .useRegisteredSuffixPatternMatch ()).isFalse ();
152
+ assertThat (mapping .useSuffixPatternMatch ()).isFalse ();
153
+ assertThat (mapping .useRegisteredSuffixPatternMatch ()).isFalse ();
157
154
158
- handlerMapping .setUseRegisteredSuffixPatternMatch (false );
159
- assertThat (handlerMapping .useSuffixPatternMatch ())
155
+ mapping .setUseRegisteredSuffixPatternMatch (false );
156
+ assertThat (mapping .useSuffixPatternMatch ())
160
157
.as ("'false' registeredSuffixPatternMatch shouldn't impact suffixPatternMatch" )
161
158
.isFalse ();
162
159
163
- handlerMapping .setUseRegisteredSuffixPatternMatch (true );
164
- assertThat (handlerMapping .useSuffixPatternMatch ())
160
+ mapping .setUseRegisteredSuffixPatternMatch (true );
161
+ assertThat (mapping .useSuffixPatternMatch ())
165
162
.as ("'true' registeredSuffixPatternMatch should enable suffixPatternMatch" )
166
163
.isTrue ();
167
164
}
168
165
169
166
@ PathPatternsParameterizedTest
170
167
void resolveEmbeddedValuesInPatterns (RequestMappingHandlerMapping mapping ) {
171
-
172
168
mapping .setEmbeddedValueResolver (
173
169
value -> "/${pattern}/bar" .equals (value ) ? "/foo/bar" : value
174
170
);
@@ -181,7 +177,6 @@ void resolveEmbeddedValuesInPatterns(RequestMappingHandlerMapping mapping) {
181
177
182
178
@ PathPatternsParameterizedTest
183
179
void pathPrefix (RequestMappingHandlerMapping mapping ) throws Exception {
184
-
185
180
mapping .setEmbeddedValueResolver (value -> "/${prefix}" .equals (value ) ? "/api" : value );
186
181
mapping .setPathPrefixes (Collections .singletonMap (
187
182
"/${prefix}" , HandlerTypePredicate .forAnnotation (RestController .class )));
@@ -226,7 +221,6 @@ private void initRequestPath(RequestMappingHandlerMapping mapping, MockHttpServl
226
221
227
222
@ PathPatternsParameterizedTest
228
223
void resolveRequestMappingViaComposedAnnotation (RequestMappingHandlerMapping mapping ) {
229
-
230
224
RequestMappingInfo info = assertComposedAnnotationMapping (
231
225
mapping , "postJson" , "/postJson" , RequestMethod .POST );
232
226
@@ -285,9 +279,7 @@ void patchMapping() {
285
279
@ SuppressWarnings ("DataFlowIssue" )
286
280
@ Test
287
281
void httpExchangeWithDefaultValues () throws NoSuchMethodException {
288
- RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping ();
289
- mapping .setApplicationContext (new StaticWebApplicationContext ());
290
- mapping .afterPropertiesSet ();
282
+ RequestMappingHandlerMapping mapping = createMapping ();
291
283
292
284
RequestMappingInfo mappingInfo = mapping .getMappingForMethod (
293
285
HttpExchangeController .class .getMethod ("defaultValuesExchange" ),
@@ -307,9 +299,7 @@ void httpExchangeWithDefaultValues() throws NoSuchMethodException {
307
299
@ SuppressWarnings ("DataFlowIssue" )
308
300
@ Test
309
301
void httpExchangeWithCustomValues () throws Exception {
310
- RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping ();
311
- mapping .setApplicationContext (new StaticWebApplicationContext ());
312
- mapping .afterPropertiesSet ();
302
+ RequestMappingHandlerMapping mapping = createMapping ();
313
303
314
304
RequestMappingInfo mappingInfo = mapping .getMappingForMethod (
315
305
HttpExchangeController .class .getMethod ("customValuesExchange" ),
@@ -332,17 +322,23 @@ void httpExchangeWithCustomValues() throws Exception {
332
322
.containsOnly (MediaType .valueOf ("text/plain;charset=UTF-8" ));
333
323
}
334
324
335
- private RequestMappingInfo assertComposedAnnotationMapping ( RequestMethod requestMethod ) {
325
+ private static RequestMappingHandlerMapping createMapping ( ) {
336
326
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping ();
337
327
mapping .setApplicationContext (new StaticWebApplicationContext ());
328
+ mapping .afterPropertiesSet ();
329
+ return mapping ;
330
+ }
331
+
332
+ private static RequestMappingInfo assertComposedAnnotationMapping (RequestMethod requestMethod ) {
333
+ RequestMappingHandlerMapping mapping = createMapping ();
338
334
339
335
String methodName = requestMethod .name ().toLowerCase ();
340
336
String path = "/" + methodName ;
341
337
342
338
return assertComposedAnnotationMapping (mapping , methodName , path , requestMethod );
343
339
}
344
340
345
- private RequestMappingInfo assertComposedAnnotationMapping (
341
+ private static RequestMappingInfo assertComposedAnnotationMapping (
346
342
RequestMappingHandlerMapping mapping , String methodName , String path , RequestMethod requestMethod ) {
347
343
348
344
Class <?> clazz = ComposedAnnotationController .class ;
0 commit comments