Polish tests
This commit is contained in:
parent
b0de99bc8c
commit
9ee4f6ee30
|
|
@ -18,10 +18,10 @@ package org.springframework.web.reactive.result;
|
|||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.bouncycastle.util.Arrays;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.core.MethodIntrospector;
|
||||
import org.springframework.core.MethodParameter;
|
||||
|
|
@ -101,24 +101,23 @@ public class ResolvableMethod {
|
|||
return this;
|
||||
}
|
||||
|
||||
// Resolve methods
|
||||
|
||||
public Method resolve() {
|
||||
// String comparison (ResolvableType's with different providers)
|
||||
String expectedReturnType = getReturnType();
|
||||
|
||||
Set<Method> methods = MethodIntrospector.selectMethods(this.targetClass,
|
||||
(ReflectionUtils.MethodFilter) method -> {
|
||||
if (this.methodName != null && !this.methodName.equals(method.getName())) {
|
||||
return false;
|
||||
}
|
||||
if (getReturnType() != null) {
|
||||
// String comparison (ResolvableType's with different providers)
|
||||
String actual = ResolvableType.forMethodReturnType(method).toString();
|
||||
if (!actual.equals(getReturnType()) && !Object.class.equals(method.getDeclaringClass())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(this.argumentTypes)) {
|
||||
if (!Arrays.areEqual(this.argumentTypes, method.getParameterTypes())) {
|
||||
if (!Arrays.equals(this.argumentTypes, method.getParameterTypes())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -145,6 +144,41 @@ public class ResolvableMethod {
|
|||
return new MethodParameter(method, -1);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final MethodParameter resolveParam(Predicate<MethodParameter>... predicates) {
|
||||
return resolveParam(null, predicates);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public final MethodParameter resolveParam(ResolvableType type,
|
||||
Predicate<MethodParameter>... predicates) {
|
||||
|
||||
List<MethodParameter> matches = new ArrayList<>();
|
||||
|
||||
Method method = resolve();
|
||||
for (int i = 0; i < method.getParameterCount(); i++) {
|
||||
MethodParameter param = new MethodParameter(method, i);
|
||||
if (type != null) {
|
||||
if (!ResolvableType.forMethodParameter(param).toString().equals(type.toString())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(predicates)) {
|
||||
if (Arrays.stream(predicates).filter(p -> !p.test(param)).findFirst().isPresent()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
matches.add(param);
|
||||
}
|
||||
|
||||
Assert.isTrue(!matches.isEmpty(), "No matching method argument: " + this);
|
||||
Assert.isTrue(matches.size() == 1, "Multiple matching method arguments: " + matches);
|
||||
|
||||
return matches.get(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public class InvocableHandlerMethodTests {
|
|||
|
||||
@Test
|
||||
public void invokeMethodWithValue() throws Exception {
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg", String.class);
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg");
|
||||
addResolver(hm, Mono.just("value1"));
|
||||
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
|
||||
|
||||
|
|
@ -92,7 +92,7 @@ public class InvocableHandlerMethodTests {
|
|||
|
||||
@Test
|
||||
public void noMatchingResolver() throws Exception {
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg", String.class);
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg");
|
||||
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
|
||||
|
||||
TestSubscriber.subscribe(mono)
|
||||
|
|
@ -103,7 +103,7 @@ public class InvocableHandlerMethodTests {
|
|||
|
||||
@Test
|
||||
public void resolverThrowsException() throws Exception {
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg", String.class);
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg");
|
||||
addResolver(hm, Mono.error(new IllegalStateException("boo")));
|
||||
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
|
||||
|
||||
|
|
@ -115,7 +115,7 @@ public class InvocableHandlerMethodTests {
|
|||
|
||||
@Test
|
||||
public void resolverWithErrorSignal() throws Exception {
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg", String.class);
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg");
|
||||
addResolver(hm, Mono.error(new IllegalStateException("boo")));
|
||||
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
|
||||
|
||||
|
|
@ -127,7 +127,7 @@ public class InvocableHandlerMethodTests {
|
|||
|
||||
@Test
|
||||
public void illegalArgumentExceptionIsWrappedWithInvocationDetails() throws Exception {
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg", String.class);
|
||||
InvocableHandlerMethod hm = handlerMethod("singleArg");
|
||||
addResolver(hm, Mono.just(1));
|
||||
Mono<HandlerResult> mono = hm.invokeForRequest(this.exchange, this.model);
|
||||
|
||||
|
|
@ -149,8 +149,8 @@ public class InvocableHandlerMethodTests {
|
|||
}
|
||||
|
||||
|
||||
private InvocableHandlerMethod handlerMethod(String name, Class<?>... args) throws Exception {
|
||||
Method method = ResolvableMethod.on(TestController.class).name(name).argumentTypes(args).resolve();
|
||||
private InvocableHandlerMethod handlerMethod(String name) throws Exception {
|
||||
Method method = ResolvableMethod.on(TestController.class).name(name).resolve();
|
||||
return new InvocableHandlerMethod(new HandlerMethod(new TestController(), method));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@
|
|||
*/
|
||||
package org.springframework.web.reactive.result.method.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
|
|
@ -37,10 +36,8 @@ import reactor.core.test.TestSubscriber;
|
|||
import rx.Observable;
|
||||
import rx.Single;
|
||||
|
||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.codec.Decoder;
|
||||
import org.springframework.core.codec.StringDecoder;
|
||||
import org.springframework.core.convert.support.MonoToCompletableFutureConverter;
|
||||
|
|
@ -57,23 +54,24 @@ import org.springframework.http.converter.reactive.HttpMessageConverter;
|
|||
import org.springframework.http.server.reactive.MockServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.MockServerHttpResponse;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.reactive.result.ResolvableMethod;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.ServerWebInputException;
|
||||
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
|
||||
import org.springframework.web.server.adapter.DefaultServerWebExchange;
|
||||
import org.springframework.web.server.session.DefaultWebSessionManager;
|
||||
import org.springframework.web.server.session.MockWebSessionManager;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.springframework.core.ResolvableType.forClass;
|
||||
import static org.springframework.core.ResolvableType.forClassWithGenerics;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RequestBodyArgumentResolver}.
|
||||
|
|
@ -81,23 +79,20 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class RequestBodyArgumentResolverTests {
|
||||
|
||||
private RequestBodyArgumentResolver resolver;
|
||||
private RequestBodyArgumentResolver resolver = resolver(new JacksonJsonDecoder());
|
||||
|
||||
private ServerWebExchange exchange;
|
||||
|
||||
private MockServerHttpRequest request;
|
||||
|
||||
private ModelMap model;
|
||||
private ResolvableMethod testMethod = ResolvableMethod.on(this.getClass()).name("handle");
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.resolver = resolver(new JacksonJsonDecoder());
|
||||
this.request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path"));
|
||||
MockServerHttpResponse response = new MockServerHttpResponse();
|
||||
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
|
||||
this.exchange = new DefaultServerWebExchange(this.request, response, sessionManager);
|
||||
this.model = new ExtendedModelMap();
|
||||
this.exchange = new DefaultServerWebExchange(this.request, response, new MockWebSessionManager());
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -105,15 +100,21 @@ public class RequestBodyArgumentResolverTests {
|
|||
public void supports() throws Exception {
|
||||
RequestBodyArgumentResolver resolver = resolver(new StringDecoder());
|
||||
|
||||
assertTrue(resolver.supportsParameter(parameter("monoTestBean")));
|
||||
assertFalse(resolver.supportsParameter(parameter("paramWithoutAnnotation")));
|
||||
ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
assertTrue(resolver.supportsParameter(param));
|
||||
|
||||
MethodParameter parameter = this.testMethod.resolveParam(p -> !p.hasParameterAnnotations());
|
||||
assertFalse(resolver.supportsParameter(parameter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingContentType() throws Exception {
|
||||
String body = "{\"bar\":\"BARBAR\",\"foo\":\"FOOFOO\"}";
|
||||
this.request.writeWith(Flux.just(dataBuffer(body)));
|
||||
Mono<Object> result = this.resolver.resolveArgument(parameter("monoTestBean"), this.model, this.exchange);
|
||||
ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Mono<Object> result = this.resolver.resolveArgument(param, new ExtendedModelMap(), this.exchange);
|
||||
|
||||
TestSubscriber.subscribe(result)
|
||||
.assertError(UnsupportedMediaTypeStatusException.class);
|
||||
|
|
@ -122,28 +123,41 @@ public class RequestBodyArgumentResolverTests {
|
|||
@Test @SuppressWarnings("unchecked")
|
||||
public void monoTestBean() throws Exception {
|
||||
String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}";
|
||||
Mono<TestBean> mono = (Mono<TestBean>) resolveValue("monoTestBean", Mono.class, body);
|
||||
ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Mono<TestBean> mono = (Mono<TestBean>) resolveValue(param, Mono.class, body);
|
||||
|
||||
assertEquals(new TestBean("f1", "b1"), mono.block());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void fluxTestBean() throws Exception {
|
||||
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]";
|
||||
Flux<TestBean> flux = (Flux<TestBean>) resolveValue("fluxTestBean", Flux.class, body);
|
||||
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), flux.collectList().block());
|
||||
ResolvableType type = forClassWithGenerics(Flux.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Flux<TestBean> flux = (Flux<TestBean>) resolveValue(param, Flux.class, body);
|
||||
|
||||
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")),
|
||||
flux.collectList().block());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void singleTestBean() throws Exception {
|
||||
String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}";
|
||||
Single<TestBean> single = (Single<TestBean>) resolveValue("singleTestBean", Single.class, body);
|
||||
ResolvableType type = forClassWithGenerics(Single.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Single<TestBean> single = (Single<TestBean>) resolveValue(param, Single.class, body);
|
||||
|
||||
assertEquals(new TestBean("f1", "b1"), single.toBlocking().value());
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void observableTestBean() throws Exception {
|
||||
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]";
|
||||
Observable<?> observable = (Observable<?>) resolveValue("observableTestBean", Observable.class, body);
|
||||
ResolvableType type = forClassWithGenerics(Observable.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Observable<?> observable = (Observable<?>) resolveValue(param, Observable.class, body);
|
||||
|
||||
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")),
|
||||
observable.toList().toBlocking().first());
|
||||
}
|
||||
|
|
@ -151,13 +165,21 @@ public class RequestBodyArgumentResolverTests {
|
|||
@Test @SuppressWarnings("unchecked")
|
||||
public void futureTestBean() throws Exception {
|
||||
String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}";
|
||||
assertEquals(new TestBean("f1", "b1"), resolveValue("futureTestBean", CompletableFuture.class, body).get());
|
||||
ResolvableType type = forClassWithGenerics(CompletableFuture.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
CompletableFuture future = resolveValue(param, CompletableFuture.class, body);
|
||||
|
||||
assertEquals(new TestBean("f1", "b1"), future.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBean() throws Exception {
|
||||
String body = "{\"bar\":\"b1\",\"foo\":\"f1\"}";
|
||||
assertEquals(new TestBean("f1", "b1"), resolveValue("testBean", TestBean.class, body));
|
||||
MethodParameter param = this.testMethod.resolveParam(
|
||||
forClass(TestBean.class), p -> p.hasParameterAnnotation(RequestBody.class));
|
||||
TestBean value = resolveValue(param, TestBean.class, body);
|
||||
|
||||
assertEquals(new TestBean("f1", "b1"), value);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -166,47 +188,65 @@ public class RequestBodyArgumentResolverTests {
|
|||
Map<String, String> map = new HashMap<>();
|
||||
map.put("foo", "f1");
|
||||
map.put("bar", "b1");
|
||||
assertEquals(map, resolveValue("map", Map.class, body));
|
||||
ResolvableType type = forClassWithGenerics(Map.class, String.class, String.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Map actual = resolveValue(param, Map.class, body);
|
||||
|
||||
assertEquals(map, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void list() throws Exception {
|
||||
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]";
|
||||
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")),
|
||||
resolveValue("list", List.class, body));
|
||||
ResolvableType type = forClassWithGenerics(List.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
List<?> list = resolveValue(param, List.class, body);
|
||||
|
||||
assertEquals(Arrays.asList(new TestBean("f1", "b1"), new TestBean("f2", "b2")), list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void array() throws Exception {
|
||||
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]";
|
||||
assertArrayEquals(new TestBean[] {new TestBean("f1", "b1"), new TestBean("f2", "b2")},
|
||||
resolveValue("array", TestBean[].class, body));
|
||||
ResolvableType type = forClass(TestBean[].class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
TestBean[] value = resolveValue(param, TestBean[].class, body);
|
||||
|
||||
assertArrayEquals(new TestBean[] {new TestBean("f1", "b1"), new TestBean("f2", "b2")}, value);
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void validateMonoTestBean() throws Exception {
|
||||
String body = "{\"bar\":\"b1\"}";
|
||||
Mono<TestBean> mono = (Mono<TestBean>) resolveValue("monoTestBean", Mono.class, body);
|
||||
TestSubscriber.subscribe(mono).assertNoValues().assertError(ServerWebInputException.class);
|
||||
ResolvableType type = forClassWithGenerics(Mono.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Mono<TestBean> mono = resolveValue(param, Mono.class, body);
|
||||
|
||||
TestSubscriber.subscribe(mono)
|
||||
.assertNoValues()
|
||||
.assertError(ServerWebInputException.class);
|
||||
}
|
||||
|
||||
@Test @SuppressWarnings("unchecked")
|
||||
public void validateFluxTestBean() throws Exception {
|
||||
String body = "[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\"}]";
|
||||
Flux<TestBean> flux = (Flux<TestBean>) resolveValue("fluxTestBean", Flux.class, body);
|
||||
ResolvableType type = forClassWithGenerics(Flux.class, TestBean.class);
|
||||
MethodParameter param = this.testMethod.resolveParam(type);
|
||||
Flux<TestBean> flux = resolveValue(param, Flux.class, body);
|
||||
|
||||
TestSubscriber.subscribe(flux).assertValues(new TestBean("f1", "b1"))
|
||||
TestSubscriber.subscribe(flux)
|
||||
.assertValues(new TestBean("f1", "b1"))
|
||||
.assertError(ServerWebInputException.class);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T resolveValue(String paramName, Class<T> valueType, String body) {
|
||||
private <T> T resolveValue(MethodParameter param, Class<T> valueType, String body) {
|
||||
|
||||
this.request.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
this.request.writeWith(Flux.just(dataBuffer(body)));
|
||||
|
||||
Mono<Object> result = this.resolver.resolveArgument(parameter(paramName), this.model, this.exchange);
|
||||
Mono<Object> result = this.resolver.resolveArgument(param, new ExtendedModelMap(), this.exchange);
|
||||
Object value = result.block(Duration.ofSeconds(5));
|
||||
|
||||
assertNotNull(value);
|
||||
|
|
@ -228,20 +268,6 @@ public class RequestBodyArgumentResolverTests {
|
|||
return new RequestBodyArgumentResolver(converters, service, new TestBeanValidator());
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConfusingArgumentToVarargsMethod")
|
||||
private MethodParameter parameter(String name) {
|
||||
ParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
|
||||
Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
|
||||
String[] names = nameDiscoverer.getParameterNames(method);
|
||||
for (int i=0; i < names.length; i++) {
|
||||
if (name.equals(names[i])) {
|
||||
return new SynthesizingMethodParameter(method, i);
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Invalid parameter name '" + name + "'. Actual parameters: " +
|
||||
Arrays.toString(names));
|
||||
}
|
||||
|
||||
private DataBuffer dataBuffer(String body) {
|
||||
byte[] bytes = body.getBytes(Charset.forName("UTF-8"));
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||
|
|
@ -266,16 +292,17 @@ public class RequestBodyArgumentResolverTests {
|
|||
|
||||
|
||||
@XmlRootElement
|
||||
static class TestBean {
|
||||
private static class TestBean {
|
||||
|
||||
private String foo;
|
||||
|
||||
private String bar;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public TestBean() {
|
||||
}
|
||||
|
||||
public TestBean(String foo, String bar) {
|
||||
TestBean(String foo, String bar) {
|
||||
this.foo = foo;
|
||||
this.bar = bar;
|
||||
}
|
||||
|
|
@ -319,7 +346,7 @@ public class RequestBodyArgumentResolverTests {
|
|||
}
|
||||
}
|
||||
|
||||
static class TestBeanValidator implements Validator {
|
||||
private static class TestBeanValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
|
|
|
|||
|
|
@ -79,6 +79,8 @@ public class ViewResolutionResultHandlerTests {
|
|||
|
||||
private ServerWebExchange exchange;
|
||||
|
||||
private ModelMap model = new ExtendedModelMap();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
|
@ -102,7 +104,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
testSupports(ResolvableType.forClass(TestBean.class), true);
|
||||
testSupports(ResolvableType.forClass(Integer.class), false);
|
||||
|
||||
testSupports(resolvableMethod().annotated(ModelAttribute.class), true);
|
||||
testSupports(ResolvableMethod.on(TestController.class).annotated(ModelAttribute.class), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -151,7 +153,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
String responseBody = "account: {id=123, testBean=TestBean[name=Joe]}";
|
||||
testHandle("/account", returnType, returnValue, responseBody, resolver);
|
||||
|
||||
testHandle("/account", resolvableMethod().annotated(ModelAttribute.class),
|
||||
testHandle("/account", ResolvableMethod.on(TestController.class).annotated(ModelAttribute.class),
|
||||
99L, "account: {id=123, num=99}", resolver);
|
||||
}
|
||||
|
||||
|
|
@ -194,8 +196,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
public void unresolvedViewName() throws Exception {
|
||||
String returnValue = "account";
|
||||
ResolvableType type = ResolvableType.forClass(String.class);
|
||||
ExtendedModelMap model = new ExtendedModelMap();
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), returnValue, returnType(type), model);
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), returnValue, returnType(type), this.model);
|
||||
|
||||
this.request.setUri(new URI("/path"));
|
||||
Mono<Void> mono = createResultHandler().handleResult(this.exchange, handlerResult);
|
||||
|
|
@ -207,8 +208,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
public void contentNegotiation() throws Exception {
|
||||
TestBean value = new TestBean("Joe");
|
||||
ResolvableType type = ResolvableType.forClass(TestBean.class);
|
||||
ExtendedModelMap model = new ExtendedModelMap();
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType(type), model);
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType(type), this.model);
|
||||
|
||||
this.request.getHeaders().setAccept(Collections.singletonList(APPLICATION_JSON));
|
||||
this.request.setUri(new URI("/account"));
|
||||
|
|
@ -227,8 +227,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
public void contentNegotiationWith406() throws Exception {
|
||||
TestBean value = new TestBean("Joe");
|
||||
ResolvableType type = ResolvableType.forClass(TestBean.class);
|
||||
ExtendedModelMap model = new ExtendedModelMap();
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType(type), model);
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType(type), this.model);
|
||||
|
||||
this.request.getHeaders().setAccept(Collections.singletonList(APPLICATION_JSON));
|
||||
this.request.setUri(new URI("/account"));
|
||||
|
|
@ -240,23 +239,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
|
||||
|
||||
private MethodParameter returnType(ResolvableType type) {
|
||||
return resolvableMethod().returning(type).resolveReturnType();
|
||||
}
|
||||
|
||||
private ResolvableMethod resolvableMethod() {
|
||||
return ResolvableMethod.on(TestController.class);
|
||||
}
|
||||
|
||||
private void testSupports(ResolvableType type, boolean result) {
|
||||
testSupports(resolvableMethod().returning(type), result);
|
||||
}
|
||||
|
||||
private void testSupports(ResolvableMethod resolvableMethod, boolean result) {
|
||||
ViewResolutionResultHandler resultHandler = createResultHandler(mock(ViewResolver.class));
|
||||
MethodParameter returnType = resolvableMethod.resolveReturnType();
|
||||
ExtendedModelMap model = new ExtendedModelMap();
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), null, returnType, model);
|
||||
assertEquals(result, resultHandler.supports(handlerResult));
|
||||
return ResolvableMethod.on(TestController.class).returning(type).resolveReturnType();
|
||||
}
|
||||
|
||||
private ViewResolutionResultHandler createResultHandler(ViewResolver... resolvers) {
|
||||
|
|
@ -264,7 +247,6 @@ public class ViewResolutionResultHandlerTests {
|
|||
}
|
||||
|
||||
private ViewResolutionResultHandler createResultHandler(List<View> defaultViews, ViewResolver... resolvers) {
|
||||
|
||||
FormattingConversionService service = new DefaultFormattingConversionService();
|
||||
service.addConverter(new MonoToCompletableFutureConverter());
|
||||
service.addConverter(new ReactorToRxJava1Converter());
|
||||
|
|
@ -275,10 +257,22 @@ public class ViewResolutionResultHandlerTests {
|
|||
return handler;
|
||||
}
|
||||
|
||||
private void testSupports(ResolvableType type, boolean result) {
|
||||
testSupports(ResolvableMethod.on(TestController.class).returning(type), result);
|
||||
}
|
||||
|
||||
private void testSupports(ResolvableMethod resolvableMethod, boolean result) {
|
||||
ViewResolutionResultHandler resultHandler = createResultHandler(mock(ViewResolver.class));
|
||||
MethodParameter returnType = resolvableMethod.resolveReturnType();
|
||||
HandlerResult handlerResult = new HandlerResult(new Object(), null, returnType, this.model);
|
||||
assertEquals(result, resultHandler.supports(handlerResult));
|
||||
}
|
||||
|
||||
private void testHandle(String path, ResolvableType returnType, Object returnValue,
|
||||
String responseBody, ViewResolver... resolvers) throws URISyntaxException {
|
||||
|
||||
testHandle(path, resolvableMethod().returning(returnType), returnValue, responseBody, resolvers);
|
||||
testHandle(path, ResolvableMethod.on(TestController.class).returning(returnType),
|
||||
returnValue, responseBody, resolvers);
|
||||
}
|
||||
|
||||
private void testHandle(String path, ResolvableMethod resolvableMethod, Object returnValue,
|
||||
|
|
@ -306,11 +300,11 @@ public class ViewResolutionResultHandlerTests {
|
|||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
|
||||
|
||||
public TestViewResolver(String... viewNames) {
|
||||
TestViewResolver(String... viewNames) {
|
||||
Arrays.stream(viewNames).forEach(name -> this.views.put(name, new TestView(name)));
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
|
|
@ -327,19 +321,19 @@ public class ViewResolutionResultHandlerTests {
|
|||
|
||||
}
|
||||
|
||||
public static final class TestView implements View {
|
||||
private static final class TestView implements View {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final List<MediaType> mediaTypes;
|
||||
|
||||
|
||||
public TestView(String name) {
|
||||
TestView(String name) {
|
||||
this.name = name;
|
||||
this.mediaTypes = Collections.singletonList(MediaType.TEXT_HTML);
|
||||
}
|
||||
|
||||
public TestView(String name, MediaType... mediaTypes) {
|
||||
TestView(String name, MediaType... mediaTypes) {
|
||||
this.name = name;
|
||||
this.mediaTypes = Arrays.asList(mediaTypes);
|
||||
}
|
||||
|
|
@ -371,7 +365,7 @@ public class ViewResolutionResultHandlerTests {
|
|||
|
||||
private final String name;
|
||||
|
||||
public TestBean(String name) {
|
||||
TestBean(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue