Search in sources :

Example 16 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method missingOptionalMultipartFile.

@Test
public void missingOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object actual = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.empty(), actual);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) RequestParam(org.springframework.web.bind.annotation.RequestParam) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 17 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService 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);
    assertNotNull(actual);
    assertEquals(Optional.class, actual.getClass());
    assertFalse(((Optional) actual).isPresent());
    Foo foo = new Foo();
    this.webRequest.setAttribute("foo", foo, getScope());
    actual = testResolveArgument(param, factory);
    assertNotNull(actual);
    assertEquals(Optional.class, actual.getClass());
    assertTrue(((Optional) actual).isPresent());
    assertSame(foo, ((Optional) actual).get());
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) WebRequestDataBinder(org.springframework.web.bind.support.WebRequestDataBinder) 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.Test)

Example 18 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolveOptionalMultipartFile.

@Test
public void resolveOptionalMultipartFile() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes());
    request.addFile(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertTrue(result instanceof Optional);
    assertEquals("Invalid result", expected, ((Optional<?>) result).get());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) RequestParam(org.springframework.web.bind.annotation.RequestParam) Optional(java.util.Optional) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) MethodParameter(org.springframework.core.MethodParameter) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 19 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.

the class PathVariableMethodArgumentResolverTests method wrapEmptyWithOptional.

@Test
public void wrapEmptyWithOptional() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    assertEquals(Optional.empty(), resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory));
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) DefaultDataBinderFactory(org.springframework.web.bind.support.DefaultDataBinderFactory) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.Test)

Example 20 with DefaultConversionService

use of org.springframework.core.convert.support.DefaultConversionService 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)

Aggregations

DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)37 Test (org.junit.Test)30 MethodParameter (org.springframework.core.MethodParameter)7 ConfigurableWebBindingInitializer (org.springframework.web.bind.support.ConfigurableWebBindingInitializer)7 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)7 DefaultDataBinderFactory (org.springframework.web.bind.support.DefaultDataBinderFactory)6 Before (org.junit.Before)5 ConversionService (org.springframework.core.convert.ConversionService)5 RequestParam (org.springframework.web.bind.annotation.RequestParam)4 Method (java.lang.reflect.Method)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)3 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 DefaultParameterNameDiscoverer (org.springframework.core.DefaultParameterNameDiscoverer)2 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)2 MockEnvironment (org.springframework.mock.env.MockEnvironment)2 GenericBean (org.springframework.tests.sample.beans.GenericBean)2 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)2