Search in sources :

Example 31 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method missingOptionalParamList.

@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamList() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, List.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(result).isEqualTo(Optional.empty());
    result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional) result).isPresent()).isFalse();
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) 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.jupiter.api.Test)

Example 32 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueConvertedToNull.

// SPR-10578
@Test
public void missingRequestParamEmptyValueConvertedToNull() throws Exception {
    WebDataBinder binder = new WebRequestDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(webRequest, null, "stringNotAnnot")).willReturn(binder);
    request.addParameter("stringNotAnnot", "");
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(arg).isNull();
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) RequestParam(org.springframework.web.bind.annotation.RequestParam) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) MethodParameter(org.springframework.core.MethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 33 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.

@Test
public void resolveOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    boolean condition = result instanceof Optional;
    assertThat(condition).isTrue();
    assertThat(((Optional<?>) result).get()).as("Invalid result").isEqualTo(expected);
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 34 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveOptionalParamValue.

@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(result).isEqualTo(Optional.empty());
    request.addParameter("name", "123");
    result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional) result).get()).isEqualTo(123);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) 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.jupiter.api.Test)

Example 35 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method missingOptionalParamArray.

@Test
@SuppressWarnings("rawtypes")
public void missingOptionalParamArray() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer[].class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(result).isEqualTo(Optional.empty());
    result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(result.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional) result).isPresent()).isFalse();
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) 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.jupiter.api.Test)

Aggregations

WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)36 Test (org.junit.jupiter.api.Test)32 WebDataBinder (org.springframework.web.bind.WebDataBinder)16 MethodParameter (org.springframework.core.MethodParameter)12 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)12 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)12 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)11 RequestParam (org.springframework.web.bind.annotation.RequestParam)10 Optional (java.util.Optional)8 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)5 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1