Search in sources :

Example 1 with WebRequestDataBinder

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

use of org.springframework.web.bind.support.WebRequestDataBinder 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);
    request.addParameter("name", "");
    MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    Object arg = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertThat(arg).isNull();
}
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.jupiter.api.Test)

Example 3 with WebRequestDataBinder

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

Example 4 with WebRequestDataBinder

use of org.springframework.web.bind.support.WebRequestDataBinder 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.beans.testfixture.beans.TestBean) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory)

Example 5 with WebRequestDataBinder

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

the class ModelAttributeMethodProcessorTests method resolveArgumentViaDefaultConstructor.

@Test
public void resolveArgumentViaDefaultConstructor() 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.jupiter.api.Test)

Aggregations

WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)7 WebRequestDataBinder (org.springframework.web.bind.support.WebRequestDataBinder)7 WebDataBinder (org.springframework.web.bind.WebDataBinder)6 Test (org.junit.jupiter.api.Test)5 MethodParameter (org.springframework.core.MethodParameter)3 StringTrimmerEditor (org.springframework.beans.propertyeditors.StringTrimmerEditor)2 Optional (java.util.Optional)1 Test (org.junit.Test)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1 DefaultFormattingConversionService (org.springframework.format.support.DefaultFormattingConversionService)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1