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