Search in sources :

Example 26 with WebDataBinder

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

the class ModelFactoryTests method updateModelBindingResult.

@Test
public void updateModelBindingResult() throws Exception {
    String commandName = "attr1";
    Object command = new Object();
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(commandName, command);
    WebDataBinder dataBinder = new WebDataBinder(command, commandName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertEquals(command, container.getModel().get(commandName));
    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
    assertSame(dataBinder.getBindingResult(), container.getModel().get(bindingResultKey));
    assertEquals(2, container.getModel().size());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 27 with WebDataBinder

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

the class InitBinderDataBinderFactoryTests method createBinder.

@Test
public void createBinder() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("id", dataBinder.getDisallowedFields()[0]);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 28 with WebDataBinder

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

the class InitBinderDataBinderFactoryTests method createBinderTypeConversion.

@Test
public void createBinderTypeConversion() throws Exception {
    this.webRequest.getNativeRequest(MockHttpServletRequest.class).setParameter("requestParam", "22");
    this.argumentResolvers.addResolver(new RequestParamMethodArgumentResolver(null, false));
    WebDataBinderFactory factory = createFactory("initBinderTypeConversion", WebDataBinder.class, int.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "foo");
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("requestParam-22", dataBinder.getDisallowedFields()[0]);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 29 with WebDataBinder

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

the class InitBinderDataBinderFactoryTests method createBinderWithAttrName.

@Test
public void createBinderWithAttrName() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "foo");
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("id", dataBinder.getDisallowedFields()[0]);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 30 with WebDataBinder

use of org.springframework.web.bind.WebDataBinder 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

WebDataBinder (org.springframework.web.bind.WebDataBinder)39 Test (org.junit.Test)23 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)15 BindingContext (org.springframework.web.reactive.BindingContext)6 ServerWebExchange (org.springframework.web.server.ServerWebExchange)6 MethodParameter (org.springframework.core.MethodParameter)5 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)5 TestBean (org.springframework.tests.sample.beans.TestBean)4 ServletRequestDataBinder (org.springframework.web.bind.ServletRequestDataBinder)4 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)4 ConversionService (org.springframework.core.convert.ConversionService)3 HashMap (java.util.HashMap)2 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Before (org.junit.Before)1