23
23
import java .lang .reflect .Method ;
24
24
import java .security .Principal ;
25
25
import java .util .Collections ;
26
- import java .util .Set ;
27
26
28
27
import org .junit .jupiter .api .BeforeEach ;
29
28
import org .junit .jupiter .api .Test ;
32
31
import org .springframework .http .MediaType ;
33
32
import org .springframework .stereotype .Controller ;
34
33
import org .springframework .util .ClassUtils ;
34
+ import org .springframework .util .ReflectionUtils ;
35
35
import org .springframework .web .bind .annotation .DeleteMapping ;
36
36
import org .springframework .web .bind .annotation .GetMapping ;
37
37
import org .springframework .web .bind .annotation .PatchMapping ;
44
44
import org .springframework .web .method .HandlerTypePredicate ;
45
45
import org .springframework .web .reactive .result .condition .ConsumesRequestCondition ;
46
46
import org .springframework .web .reactive .result .condition .MediaTypeExpression ;
47
- import org .springframework .web .reactive .result .condition .PatternsRequestCondition ;
48
47
import org .springframework .web .reactive .result .method .RequestMappingInfo ;
49
48
import org .springframework .web .service .annotation .HttpExchange ;
50
49
import org .springframework .web .service .annotation .PostExchange ;
@@ -87,16 +86,16 @@ void resolveEmbeddedValuesInPatterns() {
87
86
}
88
87
89
88
@ Test
90
- void pathPrefix () throws Exception {
89
+ void pathPrefix () {
91
90
this .handlerMapping .setEmbeddedValueResolver (value -> "/${prefix}" .equals (value ) ? "/api" : value );
92
91
this .handlerMapping .setPathPrefixes (Collections .singletonMap (
93
92
"/${prefix}" , HandlerTypePredicate .forAnnotation (RestController .class )));
94
93
95
- Method method = UserController .class . getMethod ( "getUser" );
94
+ Method method = ReflectionUtils . findMethod ( UserController .class , "getUser" );
96
95
RequestMappingInfo info = this .handlerMapping .getMappingForMethod (method , UserController .class );
97
96
98
97
assertThat (info ).isNotNull ();
99
- assertThat (info .getPatternsCondition ().getPatterns ()).isEqualTo ( Collections . singleton ( new PathPatternParser ().parse ("/api/user/{id}" ) ));
98
+ assertThat (info .getPatternsCondition ().getPatterns ()).containsOnly ( new PathPatternParser ().parse ("/api/user/{id}" ));
100
99
}
101
100
102
101
@ Test
@@ -121,10 +120,7 @@ void consumesWithOptionalRequestBody() {
121
120
this .wac .refresh ();
122
121
this .handlerMapping .afterPropertiesSet ();
123
122
RequestMappingInfo info = this .handlerMapping .getHandlerMethods ().keySet ().stream ()
124
- .filter (i -> {
125
- PatternsRequestCondition condition = i .getPatternsCondition ();
126
- return condition .getPatterns ().iterator ().next ().getPatternString ().equals ("/post" );
127
- })
123
+ .filter (i -> i .getPatternsCondition ().getPatterns ().iterator ().next ().getPatternString ().equals ("/post" ))
128
124
.findFirst ()
129
125
.orElseThrow (() -> new AssertionError ("No /post" ));
130
126
@@ -157,11 +153,11 @@ void patchMapping() {
157
153
}
158
154
159
155
@ Test // gh-32049
160
- void httpExchangeWithMultipleAnnotationsAtClassLevel () throws NoSuchMethodException {
156
+ void httpExchangeWithMultipleAnnotationsAtClassLevel () {
161
157
this .handlerMapping .afterPropertiesSet ();
162
158
163
159
Class <?> controllerClass = MultipleClassLevelAnnotationsHttpExchangeController .class ;
164
- Method method = controllerClass . getDeclaredMethod ( "post" );
160
+ Method method = ReflectionUtils . findMethod ( controllerClass , "post" );
165
161
166
162
assertThatIllegalStateException ()
167
163
.isThrownBy (() -> this .handlerMapping .getMappingForMethod (method , controllerClass ))
@@ -173,11 +169,11 @@ void httpExchangeWithMultipleAnnotationsAtClassLevel() throws NoSuchMethodExcept
173
169
}
174
170
175
171
@ Test // gh-32049
176
- void httpExchangeWithMultipleAnnotationsAtMethodLevel () throws NoSuchMethodException {
172
+ void httpExchangeWithMultipleAnnotationsAtMethodLevel () {
177
173
this .handlerMapping .afterPropertiesSet ();
178
174
179
175
Class <?> controllerClass = MultipleMethodLevelAnnotationsHttpExchangeController .class ;
180
- Method method = controllerClass . getDeclaredMethod ( "post" );
176
+ Method method = ReflectionUtils . findMethod ( controllerClass , "post" );
181
177
182
178
assertThatIllegalStateException ()
183
179
.isThrownBy (() -> this .handlerMapping .getMappingForMethod (method , controllerClass ))
@@ -189,11 +185,11 @@ void httpExchangeWithMultipleAnnotationsAtMethodLevel() throws NoSuchMethodExcep
189
185
}
190
186
191
187
@ Test // gh-32065
192
- void httpExchangeWithMixedAnnotationsAtClassLevel () throws NoSuchMethodException {
188
+ void httpExchangeWithMixedAnnotationsAtClassLevel () {
193
189
this .handlerMapping .afterPropertiesSet ();
194
190
195
191
Class <?> controllerClass = MixedClassLevelAnnotationsController .class ;
196
- Method method = controllerClass . getDeclaredMethod ( "post" );
192
+ Method method = ReflectionUtils . findMethod ( controllerClass , "post" );
197
193
198
194
assertThatIllegalStateException ()
199
195
.isThrownBy (() -> this .handlerMapping .getMappingForMethod (method , controllerClass ))
@@ -206,11 +202,11 @@ void httpExchangeWithMixedAnnotationsAtClassLevel() throws NoSuchMethodException
206
202
}
207
203
208
204
@ Test // gh-32065
209
- void httpExchangeWithMixedAnnotationsAtMethodLevel () throws NoSuchMethodException {
205
+ void httpExchangeWithMixedAnnotationsAtMethodLevel () {
210
206
this .handlerMapping .afterPropertiesSet ();
211
207
212
208
Class <?> controllerClass = MixedMethodLevelAnnotationsController .class ;
213
- Method method = controllerClass . getDeclaredMethod ( "post" );
209
+ Method method = ReflectionUtils . findMethod ( controllerClass , "post" );
214
210
215
211
assertThatIllegalStateException ()
216
212
.isThrownBy (() -> this .handlerMapping .getMappingForMethod (method , controllerClass ))
@@ -223,43 +219,45 @@ void httpExchangeWithMixedAnnotationsAtMethodLevel() throws NoSuchMethodExceptio
223
219
}
224
220
225
221
@ Test // gh-32065
226
- void httpExchangeAnnotationsOverriddenAtClassLevel () throws NoSuchMethodException {
222
+ void httpExchangeAnnotationsOverriddenAtClassLevel () {
227
223
this .handlerMapping .afterPropertiesSet ();
228
224
229
225
Class <?> controllerClass = ClassLevelOverriddenHttpExchangeAnnotationsController .class ;
230
- Method method = controllerClass . getDeclaredMethod ( "post" );
226
+ Method method = ReflectionUtils . findMethod ( controllerClass , "post" );
231
227
232
228
RequestMappingInfo info = this .handlerMapping .getMappingForMethod (method , controllerClass );
233
229
234
230
assertThat (info ).isNotNull ();
235
231
assertThat (info .getPatternsCondition ()).isNotNull ();
236
- assertThat (info .getPatternsCondition ().getPatterns ()).extracting (PathPattern ::getPatternString )
232
+ assertThat (info .getPatternsCondition ().getPatterns ())
233
+ .extracting (PathPattern ::getPatternString )
237
234
.containsOnly ("/controller/postExchange" );
238
235
}
239
236
240
237
@ Test // gh-32065
241
- void httpExchangeAnnotationsOverriddenAtMethodLevel () throws NoSuchMethodException {
238
+ void httpExchangeAnnotationsOverriddenAtMethodLevel () {
242
239
this .handlerMapping .afterPropertiesSet ();
243
240
244
241
Class <?> controllerClass = MethodLevelOverriddenHttpExchangeAnnotationsController .class ;
245
- Method method = controllerClass . getDeclaredMethod ( "post" );
242
+ Method method = ReflectionUtils . findMethod ( controllerClass , "post" );
246
243
247
244
RequestMappingInfo info = this .handlerMapping .getMappingForMethod (method , controllerClass );
248
245
249
246
assertThat (info ).isNotNull ();
250
247
assertThat (info .getPatternsCondition ()).isNotNull ();
251
- assertThat (info .getPatternsCondition ().getPatterns ()).extracting (PathPattern ::getPatternString )
248
+ assertThat (info .getPatternsCondition ().getPatterns ())
249
+ .extracting (PathPattern ::getPatternString )
252
250
.containsOnly ("/controller/postMapping" );
253
251
}
254
252
255
253
@ SuppressWarnings ("DataFlowIssue" )
256
254
@ Test
257
- void httpExchangeWithDefaultValues () throws NoSuchMethodException {
255
+ void httpExchangeWithDefaultValues () {
258
256
this .handlerMapping .afterPropertiesSet ();
259
257
260
- RequestMappingInfo mappingInfo = this . handlerMapping . getMappingForMethod (
261
- HttpExchangeController . class . getMethod ( "defaultValuesExchange" ),
262
- HttpExchangeController . class );
258
+ Class <?> clazz = HttpExchangeController . class ;
259
+ Method method = ReflectionUtils . findMethod ( clazz , "defaultValuesExchange" );
260
+ RequestMappingInfo mappingInfo = this . handlerMapping . getMappingForMethod ( method , clazz );
263
261
264
262
assertThat (mappingInfo .getPatternsCondition ().getPatterns ())
265
263
.extracting (PathPattern ::toString )
@@ -274,16 +272,16 @@ void httpExchangeWithDefaultValues() throws NoSuchMethodException {
274
272
275
273
@ SuppressWarnings ("DataFlowIssue" )
276
274
@ Test
277
- void httpExchangeWithCustomValues () throws NoSuchMethodException {
275
+ void httpExchangeWithCustomValues () {
278
276
this .handlerMapping .afterPropertiesSet ();
279
277
280
278
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping ();
281
279
mapping .setApplicationContext (new StaticWebApplicationContext ());
282
280
mapping .afterPropertiesSet ();
283
281
284
- RequestMappingInfo mappingInfo = mapping . getMappingForMethod (
285
- HttpExchangeController . class . getMethod ( "customValuesExchange" ),
286
- HttpExchangeController . class );
282
+ Class < HttpExchangeController > clazz = HttpExchangeController . class ;
283
+ Method method = ReflectionUtils . findMethod ( clazz , "customValuesExchange" );
284
+ RequestMappingInfo mappingInfo = mapping . getMappingForMethod ( method , clazz );
287
285
288
286
assertThat (mappingInfo .getPatternsCondition ().getPatterns ())
289
287
.extracting (PathPattern ::toString )
@@ -316,19 +314,18 @@ private RequestMappingInfo assertComposedAnnotationMapping(
316
314
RequestMappingInfo info = this .handlerMapping .getMappingForMethod (method , clazz );
317
315
318
316
assertThat (info ).isNotNull ();
317
+ assertThat (info .getPatternsCondition ()).isNotNull ();
318
+ assertThat (info .getPatternsCondition ().getPatterns ())
319
+ .extracting (PathPattern ::getPatternString )
320
+ .containsOnly (path );
319
321
320
- Set <PathPattern > paths = info .getPatternsCondition ().getPatterns ();
321
- assertThat (paths ).hasSize (1 );
322
- assertThat (paths .iterator ().next ().getPatternString ()).isEqualTo (path );
323
-
324
- Set <RequestMethod > methods = info .getMethodsCondition ().getMethods ();
325
- assertThat (methods ).containsExactly (requestMethod );
322
+ assertThat (info .getMethodsCondition ().getMethods ()).containsOnly (requestMethod );
326
323
327
324
return info ;
328
325
}
329
326
330
327
331
- @ Controller @ SuppressWarnings ( "unused" )
328
+ @ Controller
332
329
// gh-31962: The presence of multiple @RequestMappings is intentional.
333
330
@ RequestMapping (consumes = MediaType .APPLICATION_JSON_VALUE )
334
331
@ ExtraRequestMapping
@@ -384,7 +381,7 @@ private static class Foo {
384
381
@ Retention (RetentionPolicy .RUNTIME )
385
382
@interface PostJson {
386
383
387
- @ AliasFor (annotation = RequestMapping .class , attribute = "path" ) @ SuppressWarnings ( "unused" )
384
+ @ AliasFor (annotation = RequestMapping .class , attribute = "path" )
388
385
String [] value () default {};
389
386
}
390
387
0 commit comments