Search in sources :

Example 6 with WebDataBinderFactory

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

the class ModelAttributeMethodProcessorTests method resolveArgumentBindException.

@Test(expected = BindException.class)
public void resolveArgumentBindException() throws Exception {
    String name = "testBean";
    Object target = new TestBean();
    this.container.getModel().addAttribute(target);
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
    dataBinder.getBindingResult().reject("error");
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.request, target, name)).willReturn(dataBinder);
    this.processor.resolveArgument(this.paramNonSimpleType, this.container, this.request, binderFactory);
    verify(binderFactory).createBinder(this.request, target, name);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 7 with WebDataBinderFactory

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

the class ModelAttributeMethodProcessorTests method resolveArgumentOrdering.

// SPR-9378
@Test
public void resolveArgumentOrdering() throws Exception {
    String name = "testBean";
    Object testBean = new TestBean(name);
    this.container.addAttribute(name, testBean);
    this.container.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, testBean);
    Object anotherTestBean = new TestBean();
    this.container.addAttribute("anotherTestBean", anotherTestBean);
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(testBean, name);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.request, testBean, name)).willReturn(dataBinder);
    this.processor.resolveArgument(this.paramModelAttr, this.container, this.request, binderFactory);
    Object[] values = this.container.getModel().values().toArray();
    assertSame("Resolved attribute should be updated to be last", testBean, values[1]);
    assertSame("BindingResult of resolved attr should be last", dataBinder.getBindingResult(), values[2]);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 8 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory 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 9 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory 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 10 with WebDataBinderFactory

use of org.springframework.web.bind.support.WebDataBinderFactory 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)

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