Search in sources :

Example 16 with WebDataBinderFactory

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

the class PathVariableMethodArgumentResolverTests method resolveArgumentWrappedAsOptional.

@Test
public void resolveArgumentWrappedAsOptional() throws Exception {
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name", "value");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    @SuppressWarnings("unchecked") Optional<String> result = (Optional<String>) resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory);
    assertEquals("PathVariable not resolved correctly", "value", result.get());
    @SuppressWarnings("unchecked") Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(View.PATH_VARIABLES);
    assertNotNull(pathVars);
    assertEquals(1, pathVars.size());
    assertEquals(Optional.of("value"), pathVars.get("name"));
}
Also used : Optional(java.util.Optional) HashMap(java.util.HashMap) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 17 with WebDataBinderFactory

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

the class ModelAttributeMethodProcessorTests method resolveArgumentValidation.

@Test
public void resolveArgumentValidation() throws Exception {
    String name = "attrName";
    Object target = new TestBean();
    this.container.addAttribute(name, target);
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(this.request, target, name)).willReturn(dataBinder);
    this.processor.resolveArgument(this.paramNamedValidModelAttr, this.container, this.request, factory);
    assertTrue(dataBinder.isBindInvoked());
    assertTrue(dataBinder.isValidateInvoked());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 18 with WebDataBinderFactory

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

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

the class ModelAttributeMethodProcessorTests method resolveArgumentBindingDisabled.

@Test
public void resolveArgumentBindingDisabled() throws Exception {
    String name = "noBindAttr";
    Object target = new TestBean();
    this.container.addAttribute(name, target);
    StubRequestDataBinder dataBinder = new StubRequestDataBinder(target, name);
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(this.request, target, name)).willReturn(dataBinder);
    this.processor.resolveArgument(this.paramBindingDisabledAttr, this.container, this.request, factory);
    assertFalse(dataBinder.isBindInvoked());
    assertTrue(dataBinder.isValidateInvoked());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 20 with WebDataBinderFactory

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

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