Search in sources :

Example 21 with WebDataBinder

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

the class ExtendedServletRequestDataBinderTests method createBinder.

@Test
public void createBinder() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "nameValue");
    uriTemplateVars.put("age", "25");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    TestBean target = new TestBean();
    WebDataBinder binder = new ExtendedServletRequestDataBinder(target, "");
    ((ServletRequestDataBinder) binder).bind(request);
    assertEquals("nameValue", target.getName());
    assertEquals(25, target.getAge());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) HashMap(java.util.HashMap) TestBean(org.springframework.tests.sample.beans.TestBean) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) Test(org.junit.Test)

Example 22 with WebDataBinder

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

the class ModelAttributeMethodProcessorTests method testGetAttributeFromModel.

private void testGetAttributeFromModel(String expectedAttrName, MethodParameter param) throws Exception {
    Object target = new TestBean();
    this.container.addAttribute(expectedAttrName, target);
    WebDataBinder dataBinder = new WebRequestDataBinder(target);
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(this.request, target, expectedAttrName)).willReturn(dataBinder);
    this.processor.resolveArgument(param, this.container, this.request, factory);
    verify(factory).createBinder(this.request, target, expectedAttrName);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) TestBean(org.springframework.tests.sample.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory)

Example 23 with WebDataBinder

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

the class ModelAttributeMethodProcessorTests method resovleArgumentViaDefaultConstructor.

@Test
public void resovleArgumentViaDefaultConstructor() throws Exception {
    WebDataBinder dataBinder = new WebRequestDataBinder(null);
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(any(), notNull(), eq("attrName"))).willReturn(dataBinder);
    this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory);
    verify(factory).createBinder(any(), notNull(), eq("attrName"));
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 24 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 25 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)

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