Search in sources :

Example 1 with WebDataBinderFactory

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

the class AbstractRequestAttributesArgumentResolverTests method resolveOptional.

@Test
public void resolveOptional() throws Exception {
    WebDataBinder dataBinder = new WebRequestDataBinder(null);
    dataBinder.setConversionService(new DefaultConversionService());
    WebDataBinderFactory factory = mock(WebDataBinderFactory.class);
    given(factory.createBinder(this.webRequest, null, "foo")).willReturn(dataBinder);
    MethodParameter param = initMethodParameter(3);
    Object actual = testResolveArgument(param, factory);
    assertThat(actual).isNotNull();
    assertThat(actual.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional<?>) actual).isPresent()).isFalse();
    Foo foo = new Foo();
    this.webRequest.setAttribute("foo", foo, getScope());
    actual = testResolveArgument(param, factory);
    assertThat(actual).isNotNull();
    assertThat(actual.getClass()).isEqualTo(Optional.class);
    assertThat(((Optional<?>) actual).isPresent()).isTrue();
    assertThat(((Optional<?>) actual).get()).isSameAs(foo);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 2 with WebDataBinderFactory

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

the class InitBinderDataBinderFactoryTests method createBinderWithAttrNameNoMatch.

@Test
public void createBinderWithAttrNameNoMatch() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, "invalidName");
    assertThat(dataBinder.getDisallowedFields()).isNull();
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 3 with WebDataBinderFactory

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

the class InitBinderDataBinderFactoryTests method createBinderNullAttrName.

@Test
public void createBinderNullAttrName() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinderWithAttributeName", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
    assertThat(dataBinder.getDisallowedFields()).isNull();
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 4 with WebDataBinderFactory

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

the class InitBinderDataBinderFactoryTests method createBinderWithGlobalInitialization.

@Test
public void createBinderWithGlobalInitialization() throws Exception {
    ConversionService conversionService = new DefaultFormattingConversionService();
    bindingInitializer.setConversionService(conversionService);
    WebDataBinderFactory factory = createFactory("initBinder", WebDataBinder.class);
    WebDataBinder dataBinder = factory.createBinder(this.webRequest, null, null);
    assertThat(dataBinder.getConversionService()).isSameAs(conversionService);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) ConversionService(org.springframework.core.convert.ConversionService) DefaultFormattingConversionService(org.springframework.format.support.DefaultFormattingConversionService) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Example 5 with WebDataBinderFactory

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

the class InitBinderDataBinderFactoryTests method returnValueNotExpected.

@Test
public void returnValueNotExpected() throws Exception {
    WebDataBinderFactory factory = createFactory("initBinderReturnValue", WebDataBinder.class);
    assertThatIllegalStateException().isThrownBy(() -> factory.createBinder(this.webRequest, null, "invalidName"));
}
Also used : WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

Aggregations

WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)36 Test (org.junit.jupiter.api.Test)32 WebDataBinder (org.springframework.web.bind.WebDataBinder)16 MethodParameter (org.springframework.core.MethodParameter)12 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)12 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)12 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)11 RequestParam (org.springframework.web.bind.annotation.RequestParam)10 Optional (java.util.Optional)8 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)7 TestBean (org.springframework.beans.testfixture.beans.TestBean)6 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)5 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)3 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)2 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1