Polishing
This commit is contained in:
parent
4cc91a2869
commit
1a52c56bd4
|
@ -23,7 +23,6 @@ import java.lang.annotation.Target;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
@ -32,6 +31,7 @@ import org.springframework.core.annotation.AliasFor;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PatchMapping;
|
import org.springframework.web.bind.annotation.PatchMapping;
|
||||||
|
@ -44,7 +44,6 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||||
import org.springframework.web.method.HandlerTypePredicate;
|
import org.springframework.web.method.HandlerTypePredicate;
|
||||||
import org.springframework.web.reactive.result.condition.ConsumesRequestCondition;
|
import org.springframework.web.reactive.result.condition.ConsumesRequestCondition;
|
||||||
import org.springframework.web.reactive.result.condition.MediaTypeExpression;
|
import org.springframework.web.reactive.result.condition.MediaTypeExpression;
|
||||||
import org.springframework.web.reactive.result.condition.PatternsRequestCondition;
|
|
||||||
import org.springframework.web.reactive.result.method.RequestMappingInfo;
|
import org.springframework.web.reactive.result.method.RequestMappingInfo;
|
||||||
import org.springframework.web.service.annotation.HttpExchange;
|
import org.springframework.web.service.annotation.HttpExchange;
|
||||||
import org.springframework.web.service.annotation.PostExchange;
|
import org.springframework.web.service.annotation.PostExchange;
|
||||||
|
@ -87,16 +86,16 @@ class RequestMappingHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void pathPrefix() throws Exception {
|
void pathPrefix() {
|
||||||
this.handlerMapping.setEmbeddedValueResolver(value -> "/${prefix}".equals(value) ? "/api" : value);
|
this.handlerMapping.setEmbeddedValueResolver(value -> "/${prefix}".equals(value) ? "/api" : value);
|
||||||
this.handlerMapping.setPathPrefixes(Collections.singletonMap(
|
this.handlerMapping.setPathPrefixes(Collections.singletonMap(
|
||||||
"/${prefix}", HandlerTypePredicate.forAnnotation(RestController.class)));
|
"/${prefix}", HandlerTypePredicate.forAnnotation(RestController.class)));
|
||||||
|
|
||||||
Method method = UserController.class.getMethod("getUser");
|
Method method = ReflectionUtils.findMethod(UserController.class, "getUser");
|
||||||
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, UserController.class);
|
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, UserController.class);
|
||||||
|
|
||||||
assertThat(info).isNotNull();
|
assertThat(info).isNotNull();
|
||||||
assertThat(info.getPatternsCondition().getPatterns()).isEqualTo(Collections.singleton(new PathPatternParser().parse("/api/user/{id}")));
|
assertThat(info.getPatternsCondition().getPatterns()).containsOnly(new PathPatternParser().parse("/api/user/{id}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -121,10 +120,7 @@ class RequestMappingHandlerMappingTests {
|
||||||
this.wac.refresh();
|
this.wac.refresh();
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
RequestMappingInfo info = this.handlerMapping.getHandlerMethods().keySet().stream()
|
RequestMappingInfo info = this.handlerMapping.getHandlerMethods().keySet().stream()
|
||||||
.filter(i -> {
|
.filter(i -> i.getPatternsCondition().getPatterns().iterator().next().getPatternString().equals("/post"))
|
||||||
PatternsRequestCondition condition = i.getPatternsCondition();
|
|
||||||
return condition.getPatterns().iterator().next().getPatternString().equals("/post");
|
|
||||||
})
|
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElseThrow(() -> new AssertionError("No /post"));
|
.orElseThrow(() -> new AssertionError("No /post"));
|
||||||
|
|
||||||
|
@ -157,11 +153,11 @@ class RequestMappingHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-32049
|
@Test // gh-32049
|
||||||
void httpExchangeWithMultipleAnnotationsAtClassLevel() throws NoSuchMethodException {
|
void httpExchangeWithMultipleAnnotationsAtClassLevel() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
Class<?> controllerClass = MultipleClassLevelAnnotationsHttpExchangeController.class;
|
Class<?> controllerClass = MultipleClassLevelAnnotationsHttpExchangeController.class;
|
||||||
Method method = controllerClass.getDeclaredMethod("post");
|
Method method = ReflectionUtils.findMethod(controllerClass, "post");
|
||||||
|
|
||||||
assertThatIllegalStateException()
|
assertThatIllegalStateException()
|
||||||
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
||||||
|
@ -173,11 +169,11 @@ class RequestMappingHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-32049
|
@Test // gh-32049
|
||||||
void httpExchangeWithMultipleAnnotationsAtMethodLevel() throws NoSuchMethodException {
|
void httpExchangeWithMultipleAnnotationsAtMethodLevel() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
Class<?> controllerClass = MultipleMethodLevelAnnotationsHttpExchangeController.class;
|
Class<?> controllerClass = MultipleMethodLevelAnnotationsHttpExchangeController.class;
|
||||||
Method method = controllerClass.getDeclaredMethod("post");
|
Method method = ReflectionUtils.findMethod(controllerClass, "post");
|
||||||
|
|
||||||
assertThatIllegalStateException()
|
assertThatIllegalStateException()
|
||||||
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
||||||
|
@ -189,11 +185,11 @@ class RequestMappingHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-32065
|
@Test // gh-32065
|
||||||
void httpExchangeWithMixedAnnotationsAtClassLevel() throws NoSuchMethodException {
|
void httpExchangeWithMixedAnnotationsAtClassLevel() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
Class<?> controllerClass = MixedClassLevelAnnotationsController.class;
|
Class<?> controllerClass = MixedClassLevelAnnotationsController.class;
|
||||||
Method method = controllerClass.getDeclaredMethod("post");
|
Method method = ReflectionUtils.findMethod(controllerClass, "post");
|
||||||
|
|
||||||
assertThatIllegalStateException()
|
assertThatIllegalStateException()
|
||||||
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
||||||
|
@ -206,11 +202,11 @@ class RequestMappingHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-32065
|
@Test // gh-32065
|
||||||
void httpExchangeWithMixedAnnotationsAtMethodLevel() throws NoSuchMethodException {
|
void httpExchangeWithMixedAnnotationsAtMethodLevel() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
Class<?> controllerClass = MixedMethodLevelAnnotationsController.class;
|
Class<?> controllerClass = MixedMethodLevelAnnotationsController.class;
|
||||||
Method method = controllerClass.getDeclaredMethod("post");
|
Method method = ReflectionUtils.findMethod(controllerClass, "post");
|
||||||
|
|
||||||
assertThatIllegalStateException()
|
assertThatIllegalStateException()
|
||||||
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
|
||||||
|
@ -223,43 +219,45 @@ class RequestMappingHandlerMappingTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-32065
|
@Test // gh-32065
|
||||||
void httpExchangeAnnotationsOverriddenAtClassLevel() throws NoSuchMethodException {
|
void httpExchangeAnnotationsOverriddenAtClassLevel() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
Class<?> controllerClass = ClassLevelOverriddenHttpExchangeAnnotationsController.class;
|
Class<?> controllerClass = ClassLevelOverriddenHttpExchangeAnnotationsController.class;
|
||||||
Method method = controllerClass.getDeclaredMethod("post");
|
Method method = ReflectionUtils.findMethod(controllerClass, "post");
|
||||||
|
|
||||||
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, controllerClass);
|
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, controllerClass);
|
||||||
|
|
||||||
assertThat(info).isNotNull();
|
assertThat(info).isNotNull();
|
||||||
assertThat(info.getPatternsCondition()).isNotNull();
|
assertThat(info.getPatternsCondition()).isNotNull();
|
||||||
assertThat(info.getPatternsCondition().getPatterns()).extracting(PathPattern::getPatternString)
|
assertThat(info.getPatternsCondition().getPatterns())
|
||||||
|
.extracting(PathPattern::getPatternString)
|
||||||
.containsOnly("/controller/postExchange");
|
.containsOnly("/controller/postExchange");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // gh-32065
|
@Test // gh-32065
|
||||||
void httpExchangeAnnotationsOverriddenAtMethodLevel() throws NoSuchMethodException {
|
void httpExchangeAnnotationsOverriddenAtMethodLevel() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
Class<?> controllerClass = MethodLevelOverriddenHttpExchangeAnnotationsController.class;
|
Class<?> controllerClass = MethodLevelOverriddenHttpExchangeAnnotationsController.class;
|
||||||
Method method = controllerClass.getDeclaredMethod("post");
|
Method method = ReflectionUtils.findMethod(controllerClass, "post");
|
||||||
|
|
||||||
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, controllerClass);
|
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, controllerClass);
|
||||||
|
|
||||||
assertThat(info).isNotNull();
|
assertThat(info).isNotNull();
|
||||||
assertThat(info.getPatternsCondition()).isNotNull();
|
assertThat(info.getPatternsCondition()).isNotNull();
|
||||||
assertThat(info.getPatternsCondition().getPatterns()).extracting(PathPattern::getPatternString)
|
assertThat(info.getPatternsCondition().getPatterns())
|
||||||
|
.extracting(PathPattern::getPatternString)
|
||||||
.containsOnly("/controller/postMapping");
|
.containsOnly("/controller/postMapping");
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("DataFlowIssue")
|
@SuppressWarnings("DataFlowIssue")
|
||||||
@Test
|
@Test
|
||||||
void httpExchangeWithDefaultValues() throws NoSuchMethodException {
|
void httpExchangeWithDefaultValues() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
RequestMappingInfo mappingInfo = this.handlerMapping.getMappingForMethod(
|
Class<?> clazz = HttpExchangeController.class;
|
||||||
HttpExchangeController.class.getMethod("defaultValuesExchange"),
|
Method method = ReflectionUtils.findMethod(clazz, "defaultValuesExchange");
|
||||||
HttpExchangeController.class);
|
RequestMappingInfo mappingInfo = this.handlerMapping.getMappingForMethod(method, clazz);
|
||||||
|
|
||||||
assertThat(mappingInfo.getPatternsCondition().getPatterns())
|
assertThat(mappingInfo.getPatternsCondition().getPatterns())
|
||||||
.extracting(PathPattern::toString)
|
.extracting(PathPattern::toString)
|
||||||
|
@ -274,16 +272,16 @@ class RequestMappingHandlerMappingTests {
|
||||||
|
|
||||||
@SuppressWarnings("DataFlowIssue")
|
@SuppressWarnings("DataFlowIssue")
|
||||||
@Test
|
@Test
|
||||||
void httpExchangeWithCustomValues() throws NoSuchMethodException {
|
void httpExchangeWithCustomValues() {
|
||||||
this.handlerMapping.afterPropertiesSet();
|
this.handlerMapping.afterPropertiesSet();
|
||||||
|
|
||||||
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
|
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
|
||||||
mapping.setApplicationContext(new StaticWebApplicationContext());
|
mapping.setApplicationContext(new StaticWebApplicationContext());
|
||||||
mapping.afterPropertiesSet();
|
mapping.afterPropertiesSet();
|
||||||
|
|
||||||
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
|
Class<HttpExchangeController> clazz = HttpExchangeController.class;
|
||||||
HttpExchangeController.class.getMethod("customValuesExchange"),
|
Method method = ReflectionUtils.findMethod(clazz, "customValuesExchange");
|
||||||
HttpExchangeController.class);
|
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(method, clazz);
|
||||||
|
|
||||||
assertThat(mappingInfo.getPatternsCondition().getPatterns())
|
assertThat(mappingInfo.getPatternsCondition().getPatterns())
|
||||||
.extracting(PathPattern::toString)
|
.extracting(PathPattern::toString)
|
||||||
|
@ -316,19 +314,18 @@ class RequestMappingHandlerMappingTests {
|
||||||
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
|
RequestMappingInfo info = this.handlerMapping.getMappingForMethod(method, clazz);
|
||||||
|
|
||||||
assertThat(info).isNotNull();
|
assertThat(info).isNotNull();
|
||||||
|
assertThat(info.getPatternsCondition()).isNotNull();
|
||||||
|
assertThat(info.getPatternsCondition().getPatterns())
|
||||||
|
.extracting(PathPattern::getPatternString)
|
||||||
|
.containsOnly(path);
|
||||||
|
|
||||||
Set<PathPattern> paths = info.getPatternsCondition().getPatterns();
|
assertThat(info.getMethodsCondition().getMethods()).containsOnly(requestMethod);
|
||||||
assertThat(paths).hasSize(1);
|
|
||||||
assertThat(paths.iterator().next().getPatternString()).isEqualTo(path);
|
|
||||||
|
|
||||||
Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
|
|
||||||
assertThat(methods).containsExactly(requestMethod);
|
|
||||||
|
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Controller @SuppressWarnings("unused")
|
@Controller
|
||||||
// gh-31962: The presence of multiple @RequestMappings is intentional.
|
// gh-31962: The presence of multiple @RequestMappings is intentional.
|
||||||
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
|
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
|
||||||
@ExtraRequestMapping
|
@ExtraRequestMapping
|
||||||
|
@ -384,7 +381,7 @@ class RequestMappingHandlerMappingTests {
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@interface PostJson {
|
@interface PostJson {
|
||||||
|
|
||||||
@AliasFor(annotation = RequestMapping.class, attribute = "path") @SuppressWarnings("unused")
|
@AliasFor(annotation = RequestMapping.class, attribute = "path")
|
||||||
String[] value() default {};
|
String[] value() default {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue