Improved annnotation support in ResolvableMethod
This commit is contained in:
parent
0296d003af
commit
37726f4214
|
|
@ -0,0 +1,308 @@
|
|||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.web.method;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
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.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.ValueConstants;
|
||||
|
||||
/**
|
||||
* Predicates for {@code @MVC} annotations.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.0
|
||||
*
|
||||
* @see ResolvableMethod#annot(Predicate[])
|
||||
* @see ResolvableMethod.Builder#annot(Predicate[])
|
||||
*/
|
||||
public class MvcAnnotationPredicates {
|
||||
|
||||
|
||||
// Method parameter predicates
|
||||
|
||||
public static ModelAttributePredicate modelAttribute() {
|
||||
return new ModelAttributePredicate();
|
||||
}
|
||||
|
||||
public static RequestBodyPredicate requestBody() {
|
||||
return new RequestBodyPredicate();
|
||||
}
|
||||
|
||||
public static RequestParamPredicate requestParam() {
|
||||
return new RequestParamPredicate();
|
||||
}
|
||||
|
||||
public static RequestPartPredicate requestPart() {
|
||||
return new RequestPartPredicate();
|
||||
}
|
||||
|
||||
|
||||
// Method predicates
|
||||
|
||||
public static ModelAttributeMethodPredicate modelMethod() {
|
||||
return new ModelAttributeMethodPredicate();
|
||||
}
|
||||
|
||||
public static ResponseStatusPredicate responseStatus() {
|
||||
return new ResponseStatusPredicate();
|
||||
}
|
||||
|
||||
public static ResponseStatusPredicate responseStatus(HttpStatus code) {
|
||||
return new ResponseStatusPredicate(code);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate requestMapping(String... path) {
|
||||
return new RequestMappingPredicate(path);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate getMapping(String... path) {
|
||||
return new RequestMappingPredicate(path).method(RequestMethod.GET);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate postMapping(String... path) {
|
||||
return new RequestMappingPredicate(path).method(RequestMethod.POST);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate putMapping(String... path) {
|
||||
return new RequestMappingPredicate(path).method(RequestMethod.PUT);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate deleteMapping(String... path) {
|
||||
return new RequestMappingPredicate(path).method(RequestMethod.DELETE);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate optionsMapping(String... path) {
|
||||
return new RequestMappingPredicate(path).method(RequestMethod.OPTIONS);
|
||||
}
|
||||
|
||||
public static RequestMappingPredicate headMapping(String... path) {
|
||||
return new RequestMappingPredicate(path).method(RequestMethod.HEAD);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class ModelAttributePredicate implements Predicate<MethodParameter> {
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean binding = true;
|
||||
|
||||
|
||||
public ModelAttributePredicate name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModelAttributePredicate noName() {
|
||||
this.name = "";
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModelAttributePredicate noBinding() {
|
||||
this.binding = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean test(MethodParameter parameter) {
|
||||
ModelAttribute annotation = parameter.getParameterAnnotation(ModelAttribute.class);
|
||||
return annotation != null &&
|
||||
(this.name == null || annotation.name().equals(this.name)) &&
|
||||
annotation.binding() == this.binding;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequestBodyPredicate implements Predicate<MethodParameter> {
|
||||
|
||||
private boolean required = true;
|
||||
|
||||
|
||||
public RequestBodyPredicate notRequired() {
|
||||
this.required = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean test(MethodParameter parameter) {
|
||||
RequestBody annotation = parameter.getParameterAnnotation(RequestBody.class);
|
||||
return annotation != null && annotation.required() == this.required;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequestParamPredicate implements Predicate<MethodParameter> {
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean required = true;
|
||||
|
||||
private String defaultValue = ValueConstants.DEFAULT_NONE;
|
||||
|
||||
|
||||
|
||||
public RequestParamPredicate name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestParamPredicate noName() {
|
||||
this.name = "";
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestParamPredicate notRequired() {
|
||||
this.required = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestParamPredicate notRequired(String defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean test(MethodParameter parameter) {
|
||||
RequestParam annotation = parameter.getParameterAnnotation(RequestParam.class);
|
||||
return annotation != null &&
|
||||
(this.name == null || annotation.name().equals(this.name)) &&
|
||||
annotation.required() == this.required &&
|
||||
annotation.defaultValue().equals(this.defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class RequestPartPredicate implements Predicate<MethodParameter> {
|
||||
|
||||
private String name;
|
||||
|
||||
private boolean required = true;
|
||||
|
||||
|
||||
public RequestPartPredicate name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestPartPredicate noName() {
|
||||
this.name = "";
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestPartPredicate notRequired() {
|
||||
this.required = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean test(MethodParameter parameter) {
|
||||
RequestPart annotation = parameter.getParameterAnnotation(RequestPart.class);
|
||||
return annotation != null &&
|
||||
(this.name == null || annotation.name().equals(this.name)) &&
|
||||
annotation.required() == this.required;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ModelAttributeMethodPredicate implements Predicate<Method> {
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
public ModelAttributeMethodPredicate name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModelAttributeMethodPredicate noName() {
|
||||
this.name = "";
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Method method) {
|
||||
ModelAttribute annot = AnnotatedElementUtils.findMergedAnnotation(method, ModelAttribute.class);
|
||||
return annot != null && (this.name == null || annot.name().equals(this.name));
|
||||
}
|
||||
}
|
||||
|
||||
public static class ResponseStatusPredicate implements Predicate<Method> {
|
||||
|
||||
private HttpStatus code = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
|
||||
|
||||
private ResponseStatusPredicate() {
|
||||
}
|
||||
|
||||
private ResponseStatusPredicate(HttpStatus code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Method method) {
|
||||
ResponseStatus annot = AnnotatedElementUtils.findMergedAnnotation(method, ResponseStatus.class);
|
||||
return annot != null && annot.code().equals(this.code);
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequestMappingPredicate implements Predicate<Method> {
|
||||
|
||||
private String[] path;
|
||||
|
||||
private RequestMethod[] method = {};
|
||||
|
||||
private String[] params;
|
||||
|
||||
|
||||
private RequestMappingPredicate(String... path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
|
||||
public RequestMappingPredicate method(RequestMethod... methods) {
|
||||
this.method = methods;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequestMappingPredicate params(String... params) {
|
||||
this.params = params;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(Method method) {
|
||||
RequestMapping annot = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class);
|
||||
return annot != null &&
|
||||
Arrays.equals(this.path, annot.path()) &&
|
||||
Arrays.equals(this.method, annot.method()) &&
|
||||
(this.params == null || Arrays.equals(this.params, annot.params()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,14 +18,15 @@ package org.springframework.web.method;
|
|||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.apache.commons.logging.Log;
|
||||
|
|
@ -44,12 +45,16 @@ import org.springframework.core.MethodParameter;
|
|||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.objenesis.ObjenesisException;
|
||||
import org.springframework.objenesis.SpringObjenesis;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.web.bind.annotation.ValueConstants;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
/**
|
||||
* Convenience class to resolve method parameters from hints.
|
||||
|
|
@ -74,6 +79,7 @@ import org.springframework.util.ReflectionUtils;
|
|||
* <pre>
|
||||
*
|
||||
* import static org.springframework.web.method.ResolvableMethod.on;
|
||||
* import static org.springframework.web.method.MvcAnnotationPredicates.requestMapping;
|
||||
*
|
||||
* // Return type
|
||||
* on(TestController.class).resolveReturnType(Foo.class);
|
||||
|
|
@ -81,16 +87,13 @@ import org.springframework.util.ReflectionUtils;
|
|||
* on(TestController.class).resolveReturnType(Mono.class, responseEntity(Foo.class));
|
||||
*
|
||||
* // Annotation + return type
|
||||
* on(TestController.class).annotated(ResponseBody.class).resolveReturnType(Bar.class);
|
||||
* on(TestController.class).annotPresent(RequestMapping.class).resolveReturnType(Bar.class);
|
||||
*
|
||||
* // Annotation not present
|
||||
* on(TestController.class).notAnnotated(ResponseBody.class).resolveReturnType();
|
||||
* on(TestController.class).annotNotPresent(RequestMapping.class).resolveReturnType();
|
||||
*
|
||||
* // Annotation with attributes
|
||||
* on(TestController.class)
|
||||
* .annotated(RequestMapping.class, patterns("/foo"), params("p"))
|
||||
* .annotated(ResponseBody.class)
|
||||
* .resolveReturnType();
|
||||
* on(TestController.class).annot(requestMapping("/foo").params("p")).resolveReturnType();
|
||||
* </pre>
|
||||
*
|
||||
* <h2>2. Method Arguments</h2>
|
||||
|
|
@ -100,12 +103,14 @@ import org.springframework.util.ReflectionUtils;
|
|||
*
|
||||
* <pre>
|
||||
*
|
||||
* import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
|
||||
*
|
||||
* ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();
|
||||
*
|
||||
* testMethod.arg(Foo.class);
|
||||
* testMethod.annotated(RequestBody.class)).arg(Bar.class);
|
||||
* testMethod.annotated(RequestBody.class), required()).arg(Bar.class);
|
||||
* testMethod.notAnnotated(RequestBody.class)).arg(Bar.class);
|
||||
* testMethod.annotPresent(RequestParam.class).arg(Integer.class);
|
||||
* testMethod.annotNotPresent(RequestParam.class)).arg(Integer.class);
|
||||
* testMethod.annot(requestParam().name("c").notRequired()).arg(Integer.class);
|
||||
* </pre>
|
||||
*
|
||||
* <h3>3. Mock Handler Method Invocation</h3>
|
||||
|
|
@ -180,13 +185,17 @@ public class ResolvableMethod {
|
|||
}
|
||||
|
||||
/**
|
||||
* Filter on method arguments that have the given annotation.
|
||||
* @param annotationType the annotation type
|
||||
* @param filter optional filters on the annotation
|
||||
* Filter on method arguments with annotation.
|
||||
* See {@link MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final <A extends Annotation> ArgResolver annotated(Class<A> annotationType, Predicate<A>... filter) {
|
||||
return new ArgResolver().annotated(annotationType, filter);
|
||||
public final ArgResolver annot(Predicate<MethodParameter>... filter) {
|
||||
return new ArgResolver(filter);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final ArgResolver annotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
return new ArgResolver().annotPresent(annotationTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -194,16 +203,8 @@ public class ResolvableMethod {
|
|||
* @param annotationTypes the annotation types
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver notAnnotated(Class<? extends Annotation>... annotationTypes) {
|
||||
return new ArgResolver().notAnnotated(annotationTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on method arguments using customer predicates.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver filtered(Predicate<MethodParameter>... filter) {
|
||||
return new ArgResolver().filtered(filter);
|
||||
public final ArgResolver annotNotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
return new ArgResolver().annotNotPresent(annotationTypes);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -215,11 +216,25 @@ public class ResolvableMethod {
|
|||
private String formatMethod() {
|
||||
return this.method().getName() +
|
||||
Arrays.stream(this.method.getParameters())
|
||||
.map(p -> {
|
||||
Annotation[] annots = p.getAnnotations();
|
||||
return (annots.length != 0 ? Arrays.toString(annots) : "") + " " + p;
|
||||
})
|
||||
.collect(Collectors.joining(",\n\t", "(\n\t", "\n)"));
|
||||
.map(this::formatParameter)
|
||||
.collect(joining(",\n\t", "(\n\t", "\n)"));
|
||||
}
|
||||
|
||||
private String formatParameter(Parameter param) {
|
||||
Annotation[] annot = param.getAnnotations();
|
||||
return annot.length > 0 ?
|
||||
Arrays.stream(annot).map(this::formatAnnotation).collect(joining(",", "[", "]")) + " " + param :
|
||||
param.toString();
|
||||
}
|
||||
|
||||
private String formatAnnotation(Annotation annotation) {
|
||||
Map<String, Object> map = AnnotationUtils.getAnnotationAttributes(annotation);
|
||||
map.forEach((key, value) -> {
|
||||
if (value.equals(ValueConstants.DEFAULT_NONE)) {
|
||||
map.put(key, "NONE");
|
||||
}
|
||||
});
|
||||
return annotation.annotationType().getName() + map;
|
||||
}
|
||||
|
||||
private static ResolvableType toResolvableType(Class<?> type, Class<?>... generics) {
|
||||
|
|
@ -273,25 +288,35 @@ public class ResolvableMethod {
|
|||
}
|
||||
|
||||
/**
|
||||
* Filter on methods with the given annotation type.
|
||||
* @param annotationType the expected annotation type
|
||||
* @param filter optional filters on the actual annotation
|
||||
* Filter on annotated methods.
|
||||
* See {@link MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final <A extends Annotation> Builder annotated(Class<A> annotationType, Predicate<A>... filter) {
|
||||
String message = "annotated=" + annotationType.getName();
|
||||
addFilter(message, m -> {
|
||||
A annot = AnnotatedElementUtils.findMergedAnnotation(m, annotationType);
|
||||
return (annot != null && Arrays.stream(filter).allMatch(f -> f.test(annot)));
|
||||
});
|
||||
public final Builder annot(Predicate<Method>... filters) {
|
||||
this.filters.addAll(Arrays.asList(filters));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on methods annotated with the given annotation type.
|
||||
* @see #annot(Predicate[])
|
||||
* @see MvcAnnotationPredicates
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final Builder annotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
String message = "annotationPresent=" + Arrays.toString(annotationTypes);
|
||||
addFilter(message, method ->
|
||||
Arrays.stream(annotationTypes).allMatch(annotType ->
|
||||
AnnotatedElementUtils.findMergedAnnotation(method, annotType) != null));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on methods not annotated with the given annotation type.
|
||||
*/
|
||||
public final Builder notAnnotated(Class<? extends Annotation>... annotationTypes) {
|
||||
String message = "notAnnotated=" + Arrays.toString(annotationTypes);
|
||||
@SafeVarargs
|
||||
public final Builder annotNotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
String message = "annotationNotPresent=" + Arrays.toString(annotationTypes);
|
||||
addFilter(message, method -> {
|
||||
if (annotationTypes.length != 0) {
|
||||
return Arrays.stream(annotationTypes).noneMatch(annotType ->
|
||||
|
|
@ -334,15 +359,6 @@ public class ResolvableMethod {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add custom filters for matching methods.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final Builder filtered(Predicate<Method>... filters) {
|
||||
this.filters.addAll(Arrays.asList(filters));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a {@code ResolvableMethod} from the provided filters which must
|
||||
* resolve to a unique, single method.
|
||||
|
|
@ -365,7 +381,7 @@ public class ResolvableMethod {
|
|||
|
||||
private String formatMethods(Set<Method> methods) {
|
||||
return "\nMatched:\n" + methods.stream()
|
||||
.map(Method::toGenericString).collect(Collectors.joining(",\n\t", "[\n\t", "\n]"));
|
||||
.map(Method::toGenericString).collect(joining(",\n\t", "[\n\t", "\n]"));
|
||||
}
|
||||
|
||||
public ResolvableMethod mockCall(Consumer<T> invoker) {
|
||||
|
|
@ -440,7 +456,7 @@ public class ResolvableMethod {
|
|||
|
||||
private String formatFilters() {
|
||||
return this.filters.stream().map(Object::toString)
|
||||
.collect(Collectors.joining(",\n\t\t", "[\n\t\t", "\n\t]"));
|
||||
.collect(joining(",\n\t\t", "[\n\t\t", "\n\t]"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -499,18 +515,25 @@ public class ResolvableMethod {
|
|||
this.filters.addAll(Arrays.asList(filter));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Filter on method arguments that have the given annotation.
|
||||
* @param annotationType the annotation type
|
||||
* @param filter optional filters on the annotation
|
||||
* Filter on method arguments with annotations.
|
||||
* See {@link MvcAnnotationPredicates}.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final <A extends Annotation> ArgResolver annotated(Class<A> annotationType, Predicate<A>... filter) {
|
||||
this.filters.add(param -> {
|
||||
A annot = param.getParameterAnnotation(annotationType);
|
||||
return (annot != null && Arrays.stream(filter).allMatch(f -> f.test(annot)));
|
||||
});
|
||||
public final ArgResolver annot(Predicate<MethodParameter>... filters) {
|
||||
this.filters.addAll(Arrays.asList(filters));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on method arguments that have the given annotations.
|
||||
* @param annotationTypes the annotation types
|
||||
* @see #annot(Predicate[])
|
||||
* @see MvcAnnotationPredicates
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver annotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
this.filters.add(param -> Arrays.stream(annotationTypes).allMatch(param::hasParameterAnnotation));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -519,7 +542,7 @@ public class ResolvableMethod {
|
|||
* @param annotationTypes the annotation types
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver notAnnotated(Class<? extends Annotation>... annotationTypes) {
|
||||
public final ArgResolver annotNotPresent(Class<? extends Annotation>... annotationTypes) {
|
||||
this.filters.add(param ->
|
||||
(annotationTypes.length != 0) ?
|
||||
Arrays.stream(annotationTypes).noneMatch(param::hasParameterAnnotation) :
|
||||
|
|
@ -527,15 +550,6 @@ public class ResolvableMethod {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter on method arguments using customer predicates.
|
||||
*/
|
||||
@SafeVarargs
|
||||
public final ArgResolver filtered(Predicate<MethodParameter>... filter) {
|
||||
this.filters.addAll(Arrays.asList(filter));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the argument also matching to the given type.
|
||||
* @param type the expected type
|
||||
|
|
@ -566,8 +580,10 @@ public class ResolvableMethod {
|
|||
*/
|
||||
public final MethodParameter arg() {
|
||||
List<MethodParameter> matches = applyFilters();
|
||||
Assert.state(!matches.isEmpty(), () -> "No matching arg in method\n" + formatMethod());
|
||||
Assert.state(matches.size() == 1, () -> "Multiple matching args in method\n" + formatMethod());
|
||||
Assert.state(!matches.isEmpty(), () ->
|
||||
"No matching arg in method\n" + formatMethod());
|
||||
Assert.state(matches.size() == 1, () ->
|
||||
"Multiple matching args in method\n" + formatMethod() + "\nMatches:\n\t" + matches);
|
||||
return matches.get(0);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -18,7 +18,6 @@ package org.springframework.web.method.annotation;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
|
@ -33,7 +32,10 @@ import org.springframework.web.context.request.NativeWebRequest;
|
|||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.method.ResolvableMethod;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link RequestParamMapMethodArgumentResolver}.
|
||||
|
|
@ -63,16 +65,16 @@ public class RequestParamMapMethodArgumentResolverTests {
|
|||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(MultiValueMap.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class);
|
||||
assertTrue(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(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(Map.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Map.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
|
|
@ -83,7 +85,7 @@ public class RequestParamMapMethodArgumentResolverTests {
|
|||
request.addParameter(name, value);
|
||||
Map<String, String> expected = Collections.singletonMap(name, value);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof Map);
|
||||
|
|
@ -101,17 +103,13 @@ public class RequestParamMapMethodArgumentResolverTests {
|
|||
expected.add(name, value1);
|
||||
expected.add(name, value2);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultiValueMap.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof MultiValueMap);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
private Predicate<RequestParam> name(String name) {
|
||||
return a -> name.equals(a.name());
|
||||
}
|
||||
|
||||
|
||||
public void handle(
|
||||
@RequestParam Map<?, ?> param1,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -20,7 +20,6 @@ import java.util.Arrays;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.junit.Before;
|
||||
|
|
@ -38,7 +37,6 @@ import org.springframework.web.bind.MissingServletRequestParameterException;
|
|||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.ValueConstants;
|
||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
import org.springframework.web.bind.support.DefaultDataBinderFactory;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
|
|
@ -58,6 +56,8 @@ import static org.junit.Assert.assertTrue;
|
|||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.mock;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.requestParam;
|
||||
import static org.springframework.web.method.MvcAnnotationPredicates.requestPart;
|
||||
|
||||
/**
|
||||
* Test fixture with {@link org.springframework.web.method.annotation.RequestParamMethodArgumentResolver}.
|
||||
|
|
@ -87,69 +87,69 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
public void supportsParameter() {
|
||||
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(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(String[].class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
|
||||
assertTrue(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(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile[].class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(Part.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(List.class, Part.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(Part[].class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, name("")).arg(Map.class);
|
||||
param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated().arg(MultipartFile.class);
|
||||
param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(Part.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestPart.class).arg(MultipartFile.class);
|
||||
param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, required(), value("")).arg(String.class);
|
||||
param = this.testMethod.annot(requestParam()).arg(String.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class, required().negate()).arg(String.class);
|
||||
param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(Optional.class, Integer.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
resolver = new RequestParamMethodArgumentResolver(null, false);
|
||||
|
||||
param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
|
||||
param = this.testMethod.annotated(RequestPart.class).arg(MultipartFile.class);
|
||||
param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
|
||||
assertFalse(resolver.supportsParameter(param));
|
||||
}
|
||||
|
||||
|
|
@ -158,7 +158,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
String expected = "foo";
|
||||
request.addParameter("name", expected);
|
||||
|
||||
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 = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
|
|
@ -169,7 +169,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
String[] expected = new String[] {"foo", "bar"};
|
||||
request.addParameter("name", expected);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(String[].class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof String[]);
|
||||
assertArrayEquals("Invalid result", expected, (String[]) result);
|
||||
|
|
@ -182,7 +182,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addFile(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof MultipartFile);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
|
|
@ -198,10 +198,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof List);
|
||||
assertEquals(Arrays.asList(expected1, expected2), result);
|
||||
}
|
||||
|
|
@ -216,8 +215,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addFile(new MockMultipartFile("other", "Hello World 3".getBytes()));
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile[].class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof MultipartFile[]);
|
||||
MultipartFile[] parts = (MultipartFile[]) result;
|
||||
assertEquals(2, parts.length);
|
||||
|
|
@ -234,8 +234,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addPart(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(Part.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof Part);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
|
@ -252,8 +253,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addPart(new MockPart("other", "Hello World 3".getBytes()));
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(List.class, Part.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof List);
|
||||
assertEquals(Arrays.asList(expected1, expected2), result);
|
||||
}
|
||||
|
|
@ -270,8 +272,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addPart(new MockPart("other", "Hello World 3".getBytes()));
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(Part[].class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof Part[]);
|
||||
Part[] parts = (Part[]) result;
|
||||
assertEquals(2, parts.length);
|
||||
|
|
@ -286,14 +289,14 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addFile(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.notAnnotated().arg(MultipartFile.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof MultipartFile);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveMultipartFileListNotAnnotated() throws Exception {
|
||||
public void resolveMultipartFileListNotannot() throws Exception {
|
||||
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
|
||||
MultipartFile expected1 = new MockMultipartFile("multipartFileList", "Hello World 1".getBytes());
|
||||
MultipartFile expected2 = new MockMultipartFile("multipartFileList", "Hello World 2".getBytes());
|
||||
|
|
@ -302,7 +305,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.notAnnotated(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof List);
|
||||
|
|
@ -311,7 +314,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
|
||||
@Test(expected = MultipartException.class)
|
||||
public void isMultipartRequest() throws Exception {
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
|
||||
resolver.resolveArgument(param, null, webRequest, null);
|
||||
fail("Expected exception: request is not a multipart request");
|
||||
}
|
||||
|
|
@ -325,7 +328,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.notAnnotated(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
|
||||
|
||||
Object actual = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(actual instanceof List);
|
||||
|
|
@ -335,7 +338,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
@Test(expected = MultipartException.class)
|
||||
public void noMultipartContent() throws Exception {
|
||||
request.setMethod("POST");
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
|
||||
resolver.resolveArgument(param, null, webRequest, null);
|
||||
fail("Expected exception: no multipart content");
|
||||
}
|
||||
|
|
@ -344,7 +347,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
public void missingMultipartFile() throws Exception {
|
||||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class).arg(MultipartFile.class);
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
|
||||
resolver.resolveArgument(param, null, webRequest, null);
|
||||
fail("Expected exception: no such part found");
|
||||
}
|
||||
|
|
@ -358,7 +361,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addPart(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(Part.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof Part);
|
||||
assertEquals("Invalid result", expected, result);
|
||||
|
|
@ -366,7 +369,7 @@ 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);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertTrue(result instanceof String);
|
||||
assertEquals("Invalid result", "bar", result);
|
||||
|
|
@ -374,7 +377,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
|
||||
@Test(expected = MissingServletRequestParameterException.class)
|
||||
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);
|
||||
resolver.resolveArgument(param, null, webRequest, null);
|
||||
fail("Expected exception");
|
||||
}
|
||||
|
|
@ -389,7 +392,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
|
||||
this.request.addParameter("stringNotAnnot", "");
|
||||
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
|
||||
assertNull(arg);
|
||||
}
|
||||
|
|
@ -404,7 +407,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
|
||||
this.request.addParameter("name", "");
|
||||
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, required().negate()).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
|
||||
Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
|
||||
assertNull(arg);
|
||||
}
|
||||
|
|
@ -412,7 +415,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
@Test
|
||||
public void resolveSimpleTypeParam() throws Exception {
|
||||
request.setParameter("stringNotAnnot", "plainValue");
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
|
||||
assertTrue(result instanceof String);
|
||||
|
|
@ -421,7 +424,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
|
||||
@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);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertNull(result);
|
||||
}
|
||||
|
|
@ -429,7 +432,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
@Test // SPR-10180
|
||||
public void resolveEmptyValueToDefault() throws Exception {
|
||||
this.request.addParameter("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 = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertEquals("bar", result);
|
||||
}
|
||||
|
|
@ -437,7 +440,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
@Test
|
||||
public void resolveEmptyValueWithoutDefault() throws Exception {
|
||||
this.request.addParameter("stringNotAnnot", "");
|
||||
MethodParameter param = this.testMethod.notAnnotated(RequestParam.class).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertEquals("", result);
|
||||
}
|
||||
|
|
@ -445,7 +448,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
@Test
|
||||
public void resolveEmptyValueRequiredWithoutDefault() throws Exception {
|
||||
this.request.addParameter("name", "");
|
||||
MethodParameter param = this.testMethod.annotated(RequestParam.class, required(), value("")).arg(String.class);
|
||||
MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, null);
|
||||
assertEquals("", result);
|
||||
}
|
||||
|
|
@ -457,9 +460,7 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
initializer.setConversionService(new DefaultConversionService());
|
||||
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestParam.class).arg(Optional.class, Integer.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
|
||||
assertEquals(Optional.empty(), result);
|
||||
|
||||
|
|
@ -480,10 +481,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.addFile(expected);
|
||||
webRequest = new ServletWebRequest(request);
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
|
||||
|
||||
assertTrue(result instanceof Optional);
|
||||
assertEquals("Invalid result", expected, ((Optional<?>) result).get());
|
||||
}
|
||||
|
|
@ -497,10 +497,9 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
request.setMethod("POST");
|
||||
request.setContentType("multipart/form-data");
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);
|
||||
|
||||
assertEquals(Optional.empty(), actual);
|
||||
}
|
||||
|
||||
|
|
@ -510,27 +509,12 @@ public class RequestParamMethodArgumentResolverTests {
|
|||
initializer.setConversionService(new DefaultConversionService());
|
||||
WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
|
||||
|
||||
MethodParameter param = this.testMethod
|
||||
.annotated(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
|
||||
MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
|
||||
Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);
|
||||
|
||||
assertEquals(Optional.empty(), actual);
|
||||
}
|
||||
|
||||
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(
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue