Search in sources :

Example 81 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class MessageWriterResultHandlerTests method unsupportedReturnType.

// SPR-13135
@Test
public void unsupportedReturnType() throws Exception {
    ByteArrayOutputStream body = new ByteArrayOutputStream();
    MethodParameter type = on(TestController.class).resolveReturnType(OutputStream.class);
    HttpMessageWriter<?> writer = new EncoderHttpMessageWriter<>(new ByteBufferEncoder());
    Mono<Void> mono = initResultHandler(writer).writeBody(body, type, this.exchange);
    StepVerifier.create(mono).expectError(IllegalStateException.class).verify();
}
Also used : ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 82 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method supportsWithDefaultResolution.

@Test
public void supportsWithDefaultResolution() throws Exception {
    ModelAttributeMethodArgumentResolver resolver = new ModelAttributeMethodArgumentResolver(new ReactiveAdapterRegistry(), true);
    MethodParameter param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(String.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, String.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) Mono(reactor.core.publisher.Mono) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 83 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method supports.

@Test
public void supports() throws Exception {
    ModelAttributeMethodArgumentResolver resolver = new ModelAttributeMethodArgumentResolver(new ReactiveAdapterRegistry(), false);
    MethodParameter param = this.testMethod.annotPresent(ModelAttribute.class).arg(Foo.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) Mono(reactor.core.publisher.Mono) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 84 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method testValidationError.

private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor) throws URISyntaxException {
    ServerWebExchange exchange = postForm("age=invalid");
    Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
    mono = valueMonoExtractor.apply(mono);
    StepVerifier.create(mono).consumeErrorWith(ex -> {
        assertTrue(ex instanceof WebExchangeBindException);
        WebExchangeBindException bindException = (WebExchangeBindException) ex;
        assertEquals(1, bindException.getErrorCount());
        assertTrue(bindException.hasFieldErrors("age"));
    }).verify();
}
Also used : StepVerifier(reactor.test.StepVerifier) URISyntaxException(java.net.URISyntaxException) BindingResult(org.springframework.validation.BindingResult) BindingContext(org.springframework.web.reactive.BindingContext) Function(java.util.function.Function) ServerWebExchange(org.springframework.web.server.ServerWebExchange) Assert.assertSame(org.junit.Assert.assertSame) Single(rx.Single) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) Duration(java.time.Duration) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Before(org.junit.Before) Validated(org.springframework.validation.annotation.Validated) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) Assert.assertNotNull(org.junit.Assert.assertNotNull) MediaType(org.springframework.http.MediaType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) ResolvableMethod(org.springframework.web.method.ResolvableMethod) RxReactiveStreams(rx.RxReactiveStreams) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) Assert.assertFalse(org.junit.Assert.assertFalse) Assert.assertEquals(org.junit.Assert.assertEquals) ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException)

Example 85 with MethodParameter

use of org.springframework.core.MethodParameter in project spring-framework by spring-projects.

the class ModelAttributeMethodArgumentResolverTests method createAndBindToMono.

@Test
public void createAndBindToMono() throws Exception {
    MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Mono.class, Foo.class);
    testBindFoo(parameter, mono -> {
        assertTrue(mono.getClass().getName(), mono instanceof Mono);
        Object value = ((Mono<?>) mono).block(Duration.ofSeconds(5));
        assertEquals(Foo.class, value.getClass());
        return (Foo) value;
    });
}
Also used : Mono(reactor.core.publisher.Mono) ModelAttribute(org.springframework.web.bind.annotation.ModelAttribute) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Aggregations

MethodParameter (org.springframework.core.MethodParameter)322 Test (org.junit.Test)251 Method (java.lang.reflect.Method)65 ArrayList (java.util.ArrayList)35 RequestParam (org.springframework.web.bind.annotation.RequestParam)30 HandlerMethod (org.springframework.web.method.HandlerMethod)28 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)27 Before (org.junit.Before)25 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)25 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)23 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)23 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)23 ResolvableType (org.springframework.core.ResolvableType)21 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)21 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)21 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)20 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)20 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)20 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)20 Mono (reactor.core.publisher.Mono)19