Search in sources :

Example 26 with WebDataBinderFactory

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

the class ModelAttributeMethodProcessorTests method resolveArgumentBindingDisabledPreviously.

@Test
public void resolveArgumentBindingDisabledPreviously() throws Exception {
    String name = "attrName";
    Object target = new TestBean();
    this.container.addAttribute(name, target);
    // Declare binding disabled (e.g. via @ModelAttribute method)
    this.container.setBindingDisabled(name);
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(this.request, target, name)).willReturn(dataBinder);
    this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory);
    assertFalse(dataBinder.isBindInvoked());
    assertTrue(dataBinder.isValidateInvoked());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 27 with WebDataBinderFactory

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

the class RequestParamMethodArgumentResolverTests method optionalMultipartFileWithoutMultipartRequest.

@Test
public void optionalMultipartFileWithoutMultipartRequest() 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, 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 28 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);
    assertEquals(Optional.empty(), result);
    this.request.addParameter("name", "123");
    result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.class, result.getClass());
    assertEquals(123, ((Optional) result).get());
}
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 29 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);
    this.request.addParameter("stringNotAnnot", "");
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertNull(arg);
}
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.Test)

Aggregations

WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)29 Test (org.junit.Test)26 WebDataBinder (org.springframework.web.bind.WebDataBinder)15 MethodParameter (org.springframework.core.MethodParameter)7 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)7 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)7 TestBean (org.springframework.tests.sample.beans.TestBean)6 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)6 RequestParam (org.springframework.web.bind.annotation.RequestParam)5 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)5 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)5 Optional (java.util.Optional)2 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 ConversionService (org.springframework.core.convert.ConversionService)1