Skip to content

Commit 1a52c56

Browse files
committed
Polishing
1 parent 4cc91a2 commit 1a52c56

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMappingTests.java

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.lang.reflect.Method;
2424
import java.security.Principal;
2525
import java.util.Collections;
26-
import java.util.Set;
2726

2827
import org.junit.jupiter.api.BeforeEach;
2928
import org.junit.jupiter.api.Test;
@@ -32,6 +31,7 @@
3231
import org.springframework.http.MediaType;
3332
import org.springframework.stereotype.Controller;
3433
import org.springframework.util.ClassUtils;
34+
import org.springframework.util.ReflectionUtils;
3535
import org.springframework.web.bind.annotation.DeleteMapping;
3636
import org.springframework.web.bind.annotation.GetMapping;
3737
import org.springframework.web.bind.annotation.PatchMapping;
@@ -44,7 +44,6 @@
4444
import org.springframework.web.method.HandlerTypePredicate;
4545
import org.springframework.web.reactive.result.condition.ConsumesRequestCondition;
4646
import org.springframework.web.reactive.result.condition.MediaTypeExpression;
47-
import org.springframework.web.reactive.result.condition.PatternsRequestCondition;
4847
import org.springframework.web.reactive.result.method.RequestMappingInfo;
4948
import org.springframework.web.service.annotation.HttpExchange;
5049
import org.springframework.web.service.annotation.PostExchange;
@@ -87,16 +86,16 @@ void resolveEmbeddedValuesInPatterns() {
8786
}
8887

8988
@Test
90-
void pathPrefix() throws Exception {
89+
void pathPrefix() {
9190
this.handlerMapping.setEmbeddedValueResolver(value -> "/${prefix}".equals(value) ? "/api" : value);
9291
this.handlerMapping.setPathPrefixes(Collections.singletonMap(
9392
"/${prefix}", HandlerTypePredicate.forAnnotation(RestController.class)));
9493

95-
Method method = UserController.class.getMethod("getUser");
94+
Method method = ReflectionUtils.findMethod(UserController.class, "getUser");
9695
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, UserController.class);
9796

9897
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}"));
10099
}
101100

102101
@Test
@@ -121,10 +120,7 @@ void consumesWithOptionalRequestBody() {
121120
this.wac.refresh();
122121
this.handlerMapping.afterPropertiesSet();
123122
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"))
128124
.findFirst()
129125
.orElseThrow(() -> new AssertionError("No /post"));
130126

@@ -157,11 +153,11 @@ void patchMapping() {
157153
}
158154

159155
@Test // gh-32049
160-
void httpExchangeWithMultipleAnnotationsAtClassLevel() throws NoSuchMethodException {
156+
void httpExchangeWithMultipleAnnotationsAtClassLevel() {
161157
this.handlerMapping.afterPropertiesSet();
162158

163159
Class<?> controllerClass = MultipleClassLevelAnnotationsHttpExchangeController.class;
164-
Method method = controllerClass.getDeclaredMethod("post");
160+
Method method = ReflectionUtils.findMethod(controllerClass, "post");
165161

166162
assertThatIllegalStateException()
167163
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
@@ -173,11 +169,11 @@ void httpExchangeWithMultipleAnnotationsAtClassLevel() throws NoSuchMethodExcept
173169
}
174170

175171
@Test // gh-32049
176-
void httpExchangeWithMultipleAnnotationsAtMethodLevel() throws NoSuchMethodException {
172+
void httpExchangeWithMultipleAnnotationsAtMethodLevel() {
177173
this.handlerMapping.afterPropertiesSet();
178174

179175
Class<?> controllerClass = MultipleMethodLevelAnnotationsHttpExchangeController.class;
180-
Method method = controllerClass.getDeclaredMethod("post");
176+
Method method = ReflectionUtils.findMethod(controllerClass, "post");
181177

182178
assertThatIllegalStateException()
183179
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
@@ -189,11 +185,11 @@ void httpExchangeWithMultipleAnnotationsAtMethodLevel() throws NoSuchMethodExcep
189185
}
190186

191187
@Test // gh-32065
192-
void httpExchangeWithMixedAnnotationsAtClassLevel() throws NoSuchMethodException {
188+
void httpExchangeWithMixedAnnotationsAtClassLevel() {
193189
this.handlerMapping.afterPropertiesSet();
194190

195191
Class<?> controllerClass = MixedClassLevelAnnotationsController.class;
196-
Method method = controllerClass.getDeclaredMethod("post");
192+
Method method = ReflectionUtils.findMethod(controllerClass, "post");
197193

198194
assertThatIllegalStateException()
199195
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
@@ -206,11 +202,11 @@ void httpExchangeWithMixedAnnotationsAtClassLevel() throws NoSuchMethodException
206202
}
207203

208204
@Test // gh-32065
209-
void httpExchangeWithMixedAnnotationsAtMethodLevel() throws NoSuchMethodException {
205+
void httpExchangeWithMixedAnnotationsAtMethodLevel() {
210206
this.handlerMapping.afterPropertiesSet();
211207

212208
Class<?> controllerClass = MixedMethodLevelAnnotationsController.class;
213-
Method method = controllerClass.getDeclaredMethod("post");
209+
Method method = ReflectionUtils.findMethod(controllerClass, "post");
214210

215211
assertThatIllegalStateException()
216212
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
@@ -223,43 +219,45 @@ void httpExchangeWithMixedAnnotationsAtMethodLevel() throws NoSuchMethodExceptio
223219
}
224220

225221
@Test // gh-32065
226-
void httpExchangeAnnotationsOverriddenAtClassLevel() throws NoSuchMethodException {
222+
void httpExchangeAnnotationsOverriddenAtClassLevel() {
227223
this.handlerMapping.afterPropertiesSet();
228224

229225
Class<?> controllerClass = ClassLevelOverriddenHttpExchangeAnnotationsController.class;
230-
Method method = controllerClass.getDeclaredMethod("post");
226+
Method method = ReflectionUtils.findMethod(controllerClass, "post");
231227

232228
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, controllerClass);
233229

234230
assertThat(info).isNotNull();
235231
assertThat(info.getPatternsCondition()).isNotNull();
236-
assertThat(info.getPatternsCondition().getPatterns()).extracting(PathPattern::getPatternString)
232+
assertThat(info.getPatternsCondition().getPatterns())
233+
.extracting(PathPattern::getPatternString)
237234
.containsOnly("/controller/postExchange");
238235
}
239236

240237
@Test // gh-32065
241-
void httpExchangeAnnotationsOverriddenAtMethodLevel() throws NoSuchMethodException {
238+
void httpExchangeAnnotationsOverriddenAtMethodLevel() {
242239
this.handlerMapping.afterPropertiesSet();
243240

244241
Class<?> controllerClass = MethodLevelOverriddenHttpExchangeAnnotationsController.class;
245-
Method method = controllerClass.getDeclaredMethod("post");
242+
Method method = ReflectionUtils.findMethod(controllerClass, "post");
246243

247244
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, controllerClass);
248245

249246
assertThat(info).isNotNull();
250247
assertThat(info.getPatternsCondition()).isNotNull();
251-
assertThat(info.getPatternsCondition().getPatterns()).extracting(PathPattern::getPatternString)
248+
assertThat(info.getPatternsCondition().getPatterns())
249+
.extracting(PathPattern::getPatternString)
252250
.containsOnly("/controller/postMapping");
253251
}
254252

255253
@SuppressWarnings("DataFlowIssue")
256254
@Test
257-
void httpExchangeWithDefaultValues() throws NoSuchMethodException {
255+
void httpExchangeWithDefaultValues() {
258256
this.handlerMapping.afterPropertiesSet();
259257

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);
263261

264262
assertThat(mappingInfo.getPatternsCondition().getPatterns())
265263
.extracting(PathPattern::toString)
@@ -274,16 +272,16 @@ void httpExchangeWithDefaultValues() throws NoSuchMethodException {
274272

275273
@SuppressWarnings("DataFlowIssue")
276274
@Test
277-
void httpExchangeWithCustomValues() throws NoSuchMethodException {
275+
void httpExchangeWithCustomValues() {
278276
this.handlerMapping.afterPropertiesSet();
279277

280278
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
281279
mapping.setApplicationContext(new StaticWebApplicationContext());
282280
mapping.afterPropertiesSet();
283281

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);
287285

288286
assertThat(mappingInfo.getPatternsCondition().getPatterns())
289287
.extracting(PathPattern::toString)
@@ -316,19 +314,18 @@ private RequestMappingInfo assertComposedAnnotationMapping(
316314
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
317315

318316
assertThat(info).isNotNull();
317+
assertThat(info.getPatternsCondition()).isNotNull();
318+
assertThat(info.getPatternsCondition().getPatterns())
319+
.extracting(PathPattern::getPatternString)
320+
.containsOnly(path);
319321

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);
326323

327324
return info;
328325
}
329326

330327

331-
@Controller @SuppressWarnings("unused")
328+
@Controller
332329
// gh-31962: The presence of multiple @RequestMappings is intentional.
333330
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
334331
@ExtraRequestMapping
@@ -384,7 +381,7 @@ private static class Foo {
384381
@Retention(RetentionPolicy.RUNTIME)
385382
@interface PostJson {
386383

387-
@AliasFor(annotation = RequestMapping.class, attribute = "path") @SuppressWarnings("unused")
384+
@AliasFor(annotation = RequestMapping.class, attribute = "path")
388385
String[] value() default {};
389386
}
390387

0 commit comments

Comments
 (0)