Improved annnotation support in ResolvableMethod
This commit is contained in:
@@ -161,7 +161,7 @@ public class InvocableHandlerMethodTests {
|
||||
@Test
|
||||
public void invokeMethodWithResponseStatus() throws Exception {
|
||||
|
||||
Method method = on(TestController.class).annotated(ResponseStatus.class).resolveMethod();
|
||||
Method method = on(TestController.class).annotPresent(ResponseStatus.class).resolveMethod();
|
||||
Mono<HandlerResult> mono = invoke(new TestController(), method);
|
||||
|
||||
assertHandlerResultValue(mono, "created");
|
||||
|
||||
@@ -25,7 +25,6 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -46,12 +45,10 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.reactive.BindingContext;
|
||||
import org.springframework.web.reactive.HandlerMapping;
|
||||
import org.springframework.web.reactive.HandlerResult;
|
||||
import org.springframework.web.method.ResolvableMethod;
|
||||
import org.springframework.web.reactive.result.method.RequestMappingInfo.BuilderConfiguration;
|
||||
import org.springframework.web.server.MethodNotAllowedException;
|
||||
import org.springframework.web.server.NotAcceptableStatusException;
|
||||
@@ -70,6 +67,9 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.GET;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.getMapping;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.requestMapping;
|
||||
import static org.springframework.web.method.ResolvableMethod.on;
|
||||
import static org.springframework.web.reactive.result.method.RequestMappingInfo.paths;
|
||||
|
||||
/**
|
||||
@@ -102,11 +102,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
|
||||
@Test
|
||||
public void getHandlerDirectMatch() throws Exception {
|
||||
|
||||
Method expected = ResolvableMethod.on(TestController.class)
|
||||
.annotated(RequestMapping.class, patterns("/foo"), params())
|
||||
.resolveMethod();
|
||||
|
||||
Method expected = on(TestController.class).annot(getMapping("/foo").params()).resolveMethod();
|
||||
this.request = MockServerHttpRequest.get("/foo").build();
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block();
|
||||
|
||||
@@ -115,11 +111,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
|
||||
@Test
|
||||
public void getHandlerGlobMatch() throws Exception {
|
||||
|
||||
Method expected = ResolvableMethod.on(TestController.class)
|
||||
.annotated(RequestMapping.class, patterns("/ba*"), methods(GET, HEAD))
|
||||
.resolveMethod();
|
||||
|
||||
Method expected = on(TestController.class).annot(requestMapping("/ba*").method(GET, HEAD)).resolveMethod();
|
||||
this.request = MockServerHttpRequest.get("/bar").build();
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block();
|
||||
|
||||
@@ -128,11 +120,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
|
||||
@Test
|
||||
public void getHandlerEmptyPathMatch() throws Exception {
|
||||
|
||||
Method expected = ResolvableMethod.on(TestController.class)
|
||||
.annotated(RequestMapping.class, patterns(""))
|
||||
.resolveMethod();
|
||||
|
||||
Method expected = on(TestController.class).annot(requestMapping("")).resolveMethod();
|
||||
this.request = MockServerHttpRequest.get("").build();
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block();
|
||||
assertEquals(expected, hm.getMethod());
|
||||
@@ -144,11 +132,7 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
|
||||
@Test
|
||||
public void getHandlerBestMatch() throws Exception {
|
||||
|
||||
Method expected = ResolvableMethod.on(TestController.class)
|
||||
.annotated(RequestMapping.class, patterns("/foo"), params("p"))
|
||||
.resolveMethod();
|
||||
|
||||
Method expected = on(TestController.class).annot(getMapping("/foo").params("p")).resolveMethod();
|
||||
this.request = MockServerHttpRequest.get("/foo?p=anything").build();
|
||||
HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(createExchange()).block();
|
||||
|
||||
@@ -428,18 +412,6 @@ public class RequestMappingInfoHandlerMappingTests {
|
||||
return (Map<String, String>) exchange.getAttributes().get(attrName);
|
||||
}
|
||||
|
||||
private Predicate<RequestMapping> patterns(String... patterns) {
|
||||
return rm -> Arrays.equals(patterns, rm.path());
|
||||
}
|
||||
|
||||
private Predicate<RequestMapping> methods(RequestMethod... methods) {
|
||||
return rm -> Arrays.equals(methods, rm.method());
|
||||
}
|
||||
|
||||
private Predicate<RequestMapping> params(String... params) {
|
||||
return rm -> Arrays.equals(params, rm.params());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Controller
|
||||
|
||||
@@ -76,16 +76,16 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
ModelAttributeMethodArgumentResolver resolver =
|
||||
new ModelAttributeMethodArgumentResolver(new ReactiveAdapterRegistry(), false);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(ModelAttribute.class).arg(Foo.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
param = this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(ModelAttribute.class).arg(Foo.class);
|
||||
param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
@@ -94,22 +94,22 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
ModelAttributeMethodArgumentResolver resolver =
|
||||
new ModelAttributeMethodArgumentResolver(new ReactiveAdapterRegistry(), true);
|
||||
|
||||
MethodParameter param = this.testMethod.notAnnotated(ModelAttribute.class).arg(Foo.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(ModelAttribute.class).arg(String.class);
|
||||
param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(String.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(ModelAttribute.class).arg(Mono.class, String.class);
|
||||
param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, String.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAndBind() throws Exception {
|
||||
testBindFoo(this.testMethod.annotated(ModelAttribute.class).arg(Foo.class), value -> {
|
||||
testBindFoo(this.testMethod.annotPresent(ModelAttribute.class).arg(Foo.class), value -> {
|
||||
assertEquals(Foo.class, value.getClass());
|
||||
return (Foo) value;
|
||||
});
|
||||
@@ -119,7 +119,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
public void createAndBindToMono() throws Exception {
|
||||
|
||||
MethodParameter parameter = this.testMethod
|
||||
.notAnnotated(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
|
||||
testBindFoo(parameter, mono -> {
|
||||
assertTrue(mono.getClass().getName(), mono instanceof Mono);
|
||||
@@ -133,7 +133,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
public void createAndBindToSingle() throws Exception {
|
||||
|
||||
MethodParameter parameter = this.testMethod
|
||||
.annotated(ModelAttribute.class).arg(Single.class, Foo.class);
|
||||
.annotPresent(ModelAttribute.class).arg(Single.class, Foo.class);
|
||||
|
||||
testBindFoo(parameter, single -> {
|
||||
assertTrue(single.getClass().getName(), single instanceof Single);
|
||||
@@ -149,7 +149,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
foo.setName("Jim");
|
||||
this.bindContext.getModel().addAttribute(foo);
|
||||
|
||||
MethodParameter parameter = this.testMethod.notAnnotated(ModelAttribute.class).arg(Foo.class);
|
||||
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
testBindFoo(parameter, value -> {
|
||||
assertEquals(Foo.class, value.getClass());
|
||||
return (Foo) value;
|
||||
@@ -164,7 +164,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
foo.setName("Jim");
|
||||
this.bindContext.getModel().addAttribute("foo", Mono.just(foo));
|
||||
|
||||
MethodParameter parameter = this.testMethod.notAnnotated(ModelAttribute.class).arg(Foo.class);
|
||||
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
testBindFoo(parameter, value -> {
|
||||
assertEquals(Foo.class, value.getClass());
|
||||
return (Foo) value;
|
||||
@@ -179,7 +179,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
foo.setName("Jim");
|
||||
this.bindContext.getModel().addAttribute("foo", Single.just(foo));
|
||||
|
||||
MethodParameter parameter = this.testMethod.notAnnotated(ModelAttribute.class).arg(Foo.class);
|
||||
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
testBindFoo(parameter, value -> {
|
||||
assertEquals(Foo.class, value.getClass());
|
||||
return (Foo) value;
|
||||
@@ -195,7 +195,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
this.bindContext.getModel().addAttribute("foo", Mono.just(foo));
|
||||
|
||||
MethodParameter parameter = this.testMethod
|
||||
.notAnnotated(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
|
||||
testBindFoo(parameter, mono -> {
|
||||
assertTrue(mono.getClass().getName(), mono instanceof Mono);
|
||||
@@ -225,7 +225,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void validationError() throws Exception {
|
||||
MethodParameter parameter = this.testMethod.notAnnotated(ModelAttribute.class).arg(Foo.class);
|
||||
MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
|
||||
testValidationError(parameter, Function.identity());
|
||||
}
|
||||
|
||||
@@ -234,7 +234,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
public void validationErrorToMono() throws Exception {
|
||||
|
||||
MethodParameter parameter = this.testMethod
|
||||
.notAnnotated(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
|
||||
|
||||
testValidationError(parameter,
|
||||
resolvedArgumentMono -> {
|
||||
@@ -250,7 +250,7 @@ public class ModelAttributeMethodArgumentResolverTests {
|
||||
public void validationErrorToSingle() throws Exception {
|
||||
|
||||
MethodParameter parameter = this.testMethod
|
||||
.annotated(ModelAttribute.class).arg(Single.class, Foo.class);
|
||||
.annotPresent(ModelAttribute.class).arg(Single.class, Foo.class);
|
||||
|
||||
testValidationError(parameter,
|
||||
resolvedArgumentMono -> {
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import io.reactivex.Maybe;
|
||||
import org.junit.Before;
|
||||
@@ -50,6 +49,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.requestBody;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RequestBodyArgumentResolver}. When adding a test also
|
||||
@@ -77,17 +77,17 @@ public class RequestBodyArgumentResolverTests {
|
||||
public void supports() throws Exception {
|
||||
MethodParameter param;
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, required()).arg(Mono.class, String.class);
|
||||
param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestBody.class).arg(String.class);
|
||||
param = this.testMethod.annotNotPresent(RequestBody.class).arg(String.class);
|
||||
assertFalse(this.resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stringBody() throws Exception {
|
||||
String body = "line1";
|
||||
MethodParameter param = this.testMethod.annotated(RequestBody.class, required()).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(String.class);
|
||||
String value = resolveValue(param, body);
|
||||
|
||||
assertEquals(body, value);
|
||||
@@ -95,13 +95,13 @@ public class RequestBodyArgumentResolverTests {
|
||||
|
||||
@Test(expected = ServerWebInputException.class)
|
||||
public void emptyBodyWithString() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestBody.class, required()).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(String.class);
|
||||
resolveValueWithEmptyBody(param);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void emptyBodyWithStringNotRequired() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestBody.class, notRequired()).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody().notRequired()).arg(String.class);
|
||||
String body = resolveValueWithEmptyBody(param);
|
||||
|
||||
assertNull(body);
|
||||
@@ -110,15 +110,14 @@ public class RequestBodyArgumentResolverTests {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void emptyBodyWithMono() throws Exception {
|
||||
MethodParameter param;
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, required()).arg(Mono.class, String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(Mono.class, String.class);
|
||||
StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param))
|
||||
.expectNextCount(0)
|
||||
.expectError(ServerWebInputException.class)
|
||||
.verify();
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, notRequired()).arg(Mono.class, String.class);
|
||||
param = this.testMethod.annot(requestBody().notRequired()).arg(Mono.class, String.class);
|
||||
StepVerifier.create((Mono<Void>) resolveValueWithEmptyBody(param))
|
||||
.expectNextCount(0)
|
||||
.expectComplete()
|
||||
@@ -128,15 +127,14 @@ public class RequestBodyArgumentResolverTests {
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void emptyBodyWithFlux() throws Exception {
|
||||
MethodParameter param;
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, required()).arg(Flux.class, String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(Flux.class, String.class);
|
||||
StepVerifier.create((Flux<Void>) resolveValueWithEmptyBody(param))
|
||||
.expectNextCount(0)
|
||||
.expectError(ServerWebInputException.class)
|
||||
.verify();
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, notRequired()).arg(Flux.class, String.class);
|
||||
param = this.testMethod.annot(requestBody().notRequired()).arg(Flux.class, String.class);
|
||||
StepVerifier.create((Flux<Void>) resolveValueWithEmptyBody(param))
|
||||
.expectNextCount(0)
|
||||
.expectComplete()
|
||||
@@ -145,16 +143,15 @@ public class RequestBodyArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void emptyBodyWithSingle() throws Exception {
|
||||
MethodParameter param;
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, required()).arg(Single.class, String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(Single.class, String.class);
|
||||
Single<String> single = resolveValueWithEmptyBody(param);
|
||||
StepVerifier.create(RxReactiveStreams.toPublisher(single))
|
||||
.expectNextCount(0)
|
||||
.expectError(ServerWebInputException.class)
|
||||
.verify();
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, notRequired()).arg(Single.class, String.class);
|
||||
param = this.testMethod.annot(requestBody().notRequired()).arg(Single.class, String.class);
|
||||
single = resolveValueWithEmptyBody(param);
|
||||
StepVerifier.create(RxReactiveStreams.toPublisher(single))
|
||||
.expectNextCount(0)
|
||||
@@ -164,16 +161,15 @@ public class RequestBodyArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void emptyBodyWithMaybe() throws Exception {
|
||||
MethodParameter param;
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, required()).arg(Maybe.class, String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(Maybe.class, String.class);
|
||||
Maybe<String> maybe = resolveValueWithEmptyBody(param);
|
||||
StepVerifier.create(maybe.toFlowable())
|
||||
.expectNextCount(0)
|
||||
.expectError(ServerWebInputException.class)
|
||||
.verify();
|
||||
|
||||
param = this.testMethod.annotated(RequestBody.class, notRequired()).arg(Maybe.class, String.class);
|
||||
param = this.testMethod.annot(requestBody().notRequired()).arg(Maybe.class, String.class);
|
||||
maybe = resolveValueWithEmptyBody(param);
|
||||
StepVerifier.create(maybe.toFlowable())
|
||||
.expectNextCount(0)
|
||||
@@ -184,18 +180,14 @@ public class RequestBodyArgumentResolverTests {
|
||||
@Test
|
||||
public void emptyBodyWithObservable() throws Exception {
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestBody.class, required()).arg(Observable.class, String.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(Observable.class, String.class);
|
||||
Observable<String> observable = resolveValueWithEmptyBody(param);
|
||||
StepVerifier.create(RxReactiveStreams.toPublisher(observable))
|
||||
.expectNextCount(0)
|
||||
.expectError(ServerWebInputException.class)
|
||||
.verify();
|
||||
|
||||
param = this.testMethod
|
||||
.annotated(RequestBody.class, notRequired()).arg(Observable.class, String.class);
|
||||
|
||||
param = this.testMethod.annot(requestBody().notRequired()).arg(Observable.class, String.class);
|
||||
observable = resolveValueWithEmptyBody(param);
|
||||
StepVerifier.create(RxReactiveStreams.toPublisher(observable))
|
||||
.expectNextCount(0)
|
||||
@@ -206,18 +198,14 @@ public class RequestBodyArgumentResolverTests {
|
||||
@Test
|
||||
public void emptyBodyWithCompletableFuture() throws Exception {
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestBody.class, required()).arg(CompletableFuture.class, String.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annot(requestBody()).arg(CompletableFuture.class, String.class);
|
||||
CompletableFuture<String> future = resolveValueWithEmptyBody(param);
|
||||
future.whenComplete((text, ex) -> {
|
||||
assertNull(text);
|
||||
assertNotNull(ex);
|
||||
});
|
||||
|
||||
param = this.testMethod
|
||||
.annotated(RequestBody.class, notRequired()).arg(CompletableFuture.class, String.class);
|
||||
|
||||
param = this.testMethod.annot(requestBody().notRequired()).arg(CompletableFuture.class, String.class);
|
||||
future = resolveValueWithEmptyBody(param);
|
||||
future.whenComplete((text, ex) -> {
|
||||
assertNotNull(text);
|
||||
@@ -256,14 +244,6 @@ public class RequestBodyArgumentResolverTests {
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
private Predicate<RequestBody> required() {
|
||||
return RequestBody::required;
|
||||
}
|
||||
|
||||
private Predicate<RequestBody> notRequired() {
|
||||
return a -> !a.required();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
void handle(
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -38,6 +37,7 @@ import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RequestParamMapMethodArgumentResolver}.
|
||||
@@ -58,22 +58,22 @@ public class RequestParamMapMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().name("")).arg(Map.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(MultiValueMap.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, name("name")).arg(Map.class);
|
||||
param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
|
||||
assertFalse(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(Map.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Map.class);
|
||||
assertFalse(this.resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMapArgumentWithQueryString() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().name("")).arg(Map.class);
|
||||
Object result= resolve(param, exchangeWithQuery("foo=bar"));
|
||||
assertTrue(result instanceof Map);
|
||||
assertEquals(Collections.singletonMap("foo", "bar"), result);
|
||||
@@ -81,7 +81,7 @@ public class RequestParamMapMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveMapArgumentWithFormData() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().name("")).arg(Map.class);
|
||||
Object result= resolve(param, exchangeWithFormData("foo=bar"));
|
||||
assertTrue(result instanceof Map);
|
||||
assertEquals(Collections.singletonMap("foo", "bar"), result);
|
||||
@@ -89,7 +89,7 @@ public class RequestParamMapMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveMultiValueMapArgument() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultiValueMap.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class);
|
||||
ServerWebExchange exchange = exchangeWithQuery("foo=bar&foo=baz");
|
||||
Object result= resolve(param, exchange);
|
||||
|
||||
@@ -114,10 +114,6 @@ public class RequestParamMapMethodArgumentResolverTests {
|
||||
return this.resolver.resolveArgument(parameter, null, exchange).blockMillis(0);
|
||||
}
|
||||
|
||||
private Predicate<RequestParam> name(String name) {
|
||||
return a -> name.equals(a.name());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void handle(
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.web.reactive.result.method.annotation;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -32,7 +31,6 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ValueConstants;
|
||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.method.ResolvableMethod;
|
||||
import org.springframework.web.reactive.BindingContext;
|
||||
@@ -46,6 +44,7 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.core.ResolvableType.forClassWithGenerics;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RequestParamMethodArgumentResolver}.
|
||||
@@ -76,47 +75,47 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
public void supportsParameter() {
|
||||
this.resolver = new RequestParamMethodArgumentResolver(null, true);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, value("bar")).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(String[].class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, name("name")).arg(Map.class);
|
||||
param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
param = this.testMethod.annot(requestParam().name("")).arg(Map.class);
|
||||
assertFalse(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, required(), value("")).arg(String.class);
|
||||
param = this.testMethod.annot(requestParam()).arg(String.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, required().negate()).arg(String.class);
|
||||
param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
|
||||
assertTrue(this.resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
this.resolver = new RequestParamMethodArgumentResolver(null, false);
|
||||
assertFalse(this.resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveWithQueryString() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, value("bar")).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
|
||||
assertEquals("foo", resolve(param, exchangeWithQuery("name=foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveWithFormData() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, value("bar")).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
|
||||
assertEquals("foo", resolve(param, exchangeWithFormData("name=foo")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveStringArray() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(String[].class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
|
||||
Object result = resolve(param, exchangeWithQuery("name=foo&name=bar"));
|
||||
assertTrue(result instanceof String[]);
|
||||
assertArrayEquals(new String[] {"foo", "bar"}, (String[]) result);
|
||||
@@ -124,14 +123,14 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
|
||||
@Test
|
||||
public void resolveDefaultValue() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, value("bar")).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
|
||||
assertEquals("bar", resolve(param, exchange()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingRequestParam() throws Exception {
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(String[].class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
|
||||
Mono<Object> mono = this.resolver.resolveArgument(param, this.bindContext, exchange());
|
||||
|
||||
StepVerifier.create(mono)
|
||||
@@ -143,37 +142,34 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
@Test
|
||||
public void resolveSimpleTypeParam() throws Exception {
|
||||
ServerWebExchange exchange = exchangeWithQuery("stringNotAnnot=plainValue");
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
Object result = resolve(param, exchange);
|
||||
assertEquals("plainValue", result);
|
||||
}
|
||||
|
||||
@Test // SPR-8561
|
||||
public void resolveSimpleTypeParamToNull() throws Exception {
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
assertNull(resolve(param, exchange()));
|
||||
}
|
||||
|
||||
@Test // SPR-10180
|
||||
public void resolveEmptyValueToDefault() throws Exception {
|
||||
ServerWebExchange exchange = exchangeWithQuery("name=");
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, value("bar")).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
|
||||
Object result = resolve(param, exchange);
|
||||
assertEquals("bar", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveEmptyValueWithoutDefault() throws Exception {
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
assertEquals("", resolve(param, exchangeWithQuery("stringNotAnnot=")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveEmptyValueRequiredWithoutDefault() throws Exception {
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestParam.class, required(), value(""))
|
||||
.arg(String.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annot(requestParam()).arg(String.class);
|
||||
assertEquals("", resolve(param, exchangeWithQuery("name=")));
|
||||
}
|
||||
|
||||
@@ -215,20 +211,6 @@ public class RequestParamMethodArgumentResolverTests {
|
||||
return this.resolver.resolveArgument(parameter, this.bindContext, exchange).blockMillis(0);
|
||||
}
|
||||
|
||||
private Predicate<RequestParam> name(String name) {
|
||||
return a -> name.equals(a.name());
|
||||
}
|
||||
|
||||
private Predicate<RequestParam> required() {
|
||||
return RequestParam::required;
|
||||
}
|
||||
|
||||
private Predicate<RequestParam> value(String value) {
|
||||
return !value.isEmpty() ?
|
||||
requestParam -> value.equals(requestParam.defaultValue()) :
|
||||
requestParam -> ValueConstants.DEFAULT_NONE.equals(requestParam.defaultValue());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({"unused", "OptionalUsedAsFieldOrParameterType"})
|
||||
public void handle(
|
||||
|
||||
@@ -61,7 +61,6 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.springframework.core.ResolvableType.forClass;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON;
|
||||
import static org.springframework.web.method.ResolvableMethod.on;
|
||||
|
||||
@@ -99,7 +98,7 @@ public class ViewResolutionResultHandlerTests {
|
||||
testSupports(on(TestController.class).resolveReturnType(Map.class));
|
||||
testSupports(on(TestController.class).resolveReturnType(TestBean.class));
|
||||
|
||||
testSupports(on(TestController.class).annotated(ModelAttribute.class).resolveReturnType());
|
||||
testSupports(on(TestController.class).annotPresent(ModelAttribute.class).resolveReturnType());
|
||||
}
|
||||
|
||||
private void testSupports(MethodParameter returnType) {
|
||||
@@ -168,7 +167,7 @@ public class ViewResolutionResultHandlerTests {
|
||||
"}";
|
||||
testHandle("/account", returnType, returnValue, responseBody, resolver);
|
||||
|
||||
returnType = on(TestController.class).annotated(ModelAttribute.class).resolveReturnType();
|
||||
returnType = on(TestController.class).annotPresent(ModelAttribute.class).resolveReturnType();
|
||||
testHandle("/account", returnType, 99L, "account: {id=123, num=99}", resolver);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user