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);
}
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]);
}
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));
}
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));
}
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);
}
Aggregations