Search in sources :

Example 6 with WebDataBinder

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

the class ModelFactoryTests method updateModelSessionAttributesSaved.

@Test
public void updateModelSessionAttributesSaved() throws Exception {
    String attributeName = "sessionAttr";
    String attribute = "value";
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(attributeName, attribute);
    WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertEquals(attribute, container.getModel().get(attributeName));
    assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName));
}
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 7 with WebDataBinder

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

the class ModelFactoryTests method updateModelWhenRedirecting.

// SPR-12542
@Test
public void updateModelWhenRedirecting() throws Exception {
    String attributeName = "sessionAttr";
    String attribute = "value";
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(attributeName, attribute);
    String queryParam = "123";
    String queryParamName = "q";
    container.setRedirectModel(new ModelMap(queryParamName, queryParam));
    container.setRedirectModelScenario(true);
    WebDataBinder dataBinder = new WebDataBinder(attribute, attributeName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, attribute, attributeName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertEquals(queryParam, container.getModel().get(queryParamName));
    assertEquals(1, container.getModel().size());
    assertEquals(attribute, this.attributeStore.retrieveAttribute(this.webRequest, attributeName));
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) ModelMap(org.springframework.ui.ModelMap) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 8 with WebDataBinder

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

the class RequestParamMethodArgumentResolverTests method missingRequestParamEmptyValueNotRequired.

@Test
public void missingRequestParamEmptyValueNotRequired() throws Exception {
    WebDataBinder binder = new WebRequestDataBinder(null);
    binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(webRequest, null, "name")).willReturn(binder);
    this.request.addParameter("name", "");
    MethodParameter param = this.testMethod.annot(requestParam().notRequired()).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) StringTrimmerEditor(org.springframework.beans.propertyeditors.StringTrimmerEditor) MethodParameter(org.springframework.core.MethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 9 with WebDataBinder

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

the class InitBinderBindingContextTests method createBinder.

@Test
public void createBinder() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    BindingContext context = createBindingContext("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, null);
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("id", dataBinder.getDisallowedFields()[0]);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) BindingContext(org.springframework.web.reactive.BindingContext) Test(org.junit.Test)

Example 10 with WebDataBinder

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

the class InitBinderBindingContextTests method createBinderWithAttrName.

@Test
public void createBinderWithAttrName() throws Exception {
    ServerWebExchange exchange = MockServerHttpRequest.get("/").toExchange();
    BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = context.createDataBinder(exchange, null, "foo");
    assertNotNull(dataBinder.getDisallowedFields());
    assertEquals("id", dataBinder.getDisallowedFields()[0]);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) WebDataBinder(org.springframework.web.bind.WebDataBinder) BindingContext(org.springframework.web.reactive.BindingContext) 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