Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jan 19, 2024
1 parent 4cc91a2 commit 1a52c56
Showing 1 changed file with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.reflect.Method;
import java.security.Principal;
import java.util.Collections;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -32,6 +31,7 @@
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand All @@ -44,7 +44,6 @@
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.reactive.result.condition.ConsumesRequestCondition;
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.service.annotation.HttpExchange;
import org.springframework.web.service.annotation.PostExchange;
Expand Down Expand Up @@ -87,16 +86,16 @@ void resolveEmbeddedValuesInPatterns() {
}

@Test
void pathPrefix() throws Exception {
void pathPrefix() {
this.handlerMapping.setEmbeddedValueResolver(value -> "/${prefix}".equals(value) ? "/api" : value);
this.handlerMapping.setPathPrefixes(Collections.singletonMap(
"/${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);

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
Expand All @@ -121,10 +120,7 @@ void consumesWithOptionalRequestBody() {
this.wac.refresh();
this.handlerMapping.afterPropertiesSet();
RequestMappingInfo info = this.handlerMapping.getHandlerMethods().keySet().stream()
.filter(i -> {
PatternsRequestCondition condition = i.getPatternsCondition();
return condition.getPatterns().iterator().next().getPatternString().equals("/post");
})
.filter(i -> i.getPatternsCondition().getPatterns().iterator().next().getPatternString().equals("/post"))
.findFirst()
.orElseThrow(() -> new AssertionError("No /post"));

Expand Down Expand Up @@ -157,11 +153,11 @@ void patchMapping() {
}

@Test // gh-32049
void httpExchangeWithMultipleAnnotationsAtClassLevel() throws NoSuchMethodException {
void httpExchangeWithMultipleAnnotationsAtClassLevel() {
this.handlerMapping.afterPropertiesSet();

Class<?> controllerClass = MultipleClassLevelAnnotationsHttpExchangeController.class;
Method method = controllerClass.getDeclaredMethod("post");
Method method = ReflectionUtils.findMethod(controllerClass, "post");

assertThatIllegalStateException()
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
Expand All @@ -173,11 +169,11 @@ void httpExchangeWithMultipleAnnotationsAtClassLevel() throws NoSuchMethodExcept
}

@Test // gh-32049
void httpExchangeWithMultipleAnnotationsAtMethodLevel() throws NoSuchMethodException {
void httpExchangeWithMultipleAnnotationsAtMethodLevel() {
this.handlerMapping.afterPropertiesSet();

Class<?> controllerClass = MultipleMethodLevelAnnotationsHttpExchangeController.class;
Method method = controllerClass.getDeclaredMethod("post");
Method method = ReflectionUtils.findMethod(controllerClass, "post");

assertThatIllegalStateException()
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
Expand All @@ -189,11 +185,11 @@ void httpExchangeWithMultipleAnnotationsAtMethodLevel() throws NoSuchMethodExcep
}

@Test // gh-32065
void httpExchangeWithMixedAnnotationsAtClassLevel() throws NoSuchMethodException {
void httpExchangeWithMixedAnnotationsAtClassLevel() {
this.handlerMapping.afterPropertiesSet();

Class<?> controllerClass = MixedClassLevelAnnotationsController.class;
Method method = controllerClass.getDeclaredMethod("post");
Method method = ReflectionUtils.findMethod(controllerClass, "post");

assertThatIllegalStateException()
.isThrownBy(() -> this.handlerMapping.getMappingForMethod(method, controllerClass))
Expand All @@ -206,11 +202,11 @@ void httpExchangeWithMixedAnnotationsAtClassLevel() throws NoSuchMethodException
}

@Test // gh-32065
void httpExchangeWithMixedAnnotationsAtMethodLevel() throws NoSuchMethodException {
void httpExchangeWithMixedAnnotationsAtMethodLevel() {
this.handlerMapping.afterPropertiesSet();

Class<?> controllerClass = MixedMethodLevelAnnotationsController.class;
Method method = controllerClass.getDeclaredMethod("post");
Method method = ReflectionUtils.findMethod(controllerClass, "post");

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

@Test // gh-32065
void httpExchangeAnnotationsOverriddenAtClassLevel() throws NoSuchMethodException {
void httpExchangeAnnotationsOverriddenAtClassLevel() {
this.handlerMapping.afterPropertiesSet();

Class<?> controllerClass = ClassLevelOverriddenHttpExchangeAnnotationsController.class;
Method method = controllerClass.getDeclaredMethod("post");
Method method = ReflectionUtils.findMethod(controllerClass, "post");

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

assertThat(info).isNotNull();
assertThat(info.getPatternsCondition()).isNotNull();
assertThat(info.getPatternsCondition().getPatterns()).extracting(PathPattern::getPatternString)
assertThat(info.getPatternsCondition().getPatterns())
.extracting(PathPattern::getPatternString)
.containsOnly("/controller/postExchange");
}

@Test // gh-32065
void httpExchangeAnnotationsOverriddenAtMethodLevel() throws NoSuchMethodException {
void httpExchangeAnnotationsOverriddenAtMethodLevel() {
this.handlerMapping.afterPropertiesSet();

Class<?> controllerClass = MethodLevelOverriddenHttpExchangeAnnotationsController.class;
Method method = controllerClass.getDeclaredMethod("post");
Method method = ReflectionUtils.findMethod(controllerClass, "post");

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

assertThat(info).isNotNull();
assertThat(info.getPatternsCondition()).isNotNull();
assertThat(info.getPatternsCondition().getPatterns()).extracting(PathPattern::getPatternString)
assertThat(info.getPatternsCondition().getPatterns())
.extracting(PathPattern::getPatternString)
.containsOnly("/controller/postMapping");
}

@SuppressWarnings("DataFlowIssue")
@Test
void httpExchangeWithDefaultValues() throws NoSuchMethodException {
void httpExchangeWithDefaultValues() {
this.handlerMapping.afterPropertiesSet();

RequestMappingInfo mappingInfo = this.handlerMapping.getMappingForMethod(
HttpExchangeController.class.getMethod("defaultValuesExchange"),
HttpExchangeController.class);
Class<?> clazz = HttpExchangeController.class;
Method method = ReflectionUtils.findMethod(clazz, "defaultValuesExchange");
RequestMappingInfo mappingInfo = this.handlerMapping.getMappingForMethod(method, clazz);

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

@SuppressWarnings("DataFlowIssue")
@Test
void httpExchangeWithCustomValues() throws NoSuchMethodException {
void httpExchangeWithCustomValues() {
this.handlerMapping.afterPropertiesSet();

RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setApplicationContext(new StaticWebApplicationContext());
mapping.afterPropertiesSet();

RequestMappingInfo mappingInfo = mapping.getMappingForMethod(
HttpExchangeController.class.getMethod("customValuesExchange"),
HttpExchangeController.class);
Class<HttpExchangeController> clazz = HttpExchangeController.class;
Method method = ReflectionUtils.findMethod(clazz, "customValuesExchange");
RequestMappingInfo mappingInfo = mapping.getMappingForMethod(method, clazz);

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

assertThat(info).isNotNull();
assertThat(info.getPatternsCondition()).isNotNull();
assertThat(info.getPatternsCondition().getPatterns())
.extracting(PathPattern::getPatternString)
.containsOnly(path);

Set<PathPattern> paths = info.getPatternsCondition().getPatterns();
assertThat(paths).hasSize(1);
assertThat(paths.iterator().next().getPatternString()).isEqualTo(path);

Set<RequestMethod> methods = info.getMethodsCondition().getMethods();
assertThat(methods).containsExactly(requestMethod);
assertThat(info.getMethodsCondition().getMethods()).containsOnly(requestMethod);

return info;
}


@Controller @SuppressWarnings("unused")
@Controller
// gh-31962: The presence of multiple @RequestMappings is intentional.
@RequestMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
@ExtraRequestMapping
Expand Down Expand Up @@ -384,7 +381,7 @@ private static class Foo {
@Retention(RetentionPolicy.RUNTIME)
@interface PostJson {

@AliasFor(annotation = RequestMapping.class, attribute = "path") @SuppressWarnings("unused")
@AliasFor(annotation = RequestMapping.class, attribute = "path")
String[] value() default {};
}

Expand Down

0 comments on commit 1a52c56

Please sign in to comment.