Search in sources :

Example 61 with MethodParameter

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

the class RequestParamMethodArgumentResolverTests method missingOptionalMultipartFile.

@Test
public void missingOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.empty(), actual);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(org.springframework.web.bind.annotation.RequestParam) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 62 with MethodParameter

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

the class RequestParamMethodArgumentResolverTests method resolveStringArray.

@Test
public void resolveStringArray() throws Exception {
    String[] expected = new String[] { "foo", "bar" };
    request.addParameter("name", expected);
    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);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 63 with MethodParameter

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

the class RequestParamMethodArgumentResolverTests method resolvePart.

@Test
public void resolvePart() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart expected = new MockPart("pfile", "Hello World".getBytes());
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletWebRequest(request);
    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);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 64 with MethodParameter

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

the class RequestParamMethodArgumentResolverTests method resolveEmptyValueRequiredWithoutDefault.

@Test
public void resolveEmptyValueRequiredWithoutDefault() throws Exception {
    this.request.addParameter("name", "");
    MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertEquals("", result);
}
Also used : MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 65 with MethodParameter

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

the class RequestParamMethodArgumentResolverTests method supportsParameter.

@Test
public void supportsParameter() {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) 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