Search in sources :

Example 21 with DefaultConversionService

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

the class ServletModelAttributeMethodProcessorTests method setup.

@Before
public void setup() throws Exception {
    processor = new ServletModelAttributeMethodProcessor(false);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    binderFactory = new ServletRequestDataBinderFactory(null, initializer);
    mavContainer = new ModelAndViewContainer();
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request);
    Method method = getClass().getDeclaredMethod("modelAttribute", TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
    testBeanModelAttr = new MethodParameter(method, 0);
    testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
    testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Before(org.junit.Before)

Example 22 with DefaultConversionService

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

the class RequestParamMethodArgumentResolverTests method optionalMultipartFileWithoutMultipartRequest.

@Test
public void optionalMultipartFileWithoutMultipartRequest() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    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 23 with DefaultConversionService

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

the class RequestParamMethodArgumentResolverTests method resolveOptionalParamValue.

@Test
@SuppressWarnings("rawtypes")
public void resolveOptionalParamValue() throws Exception {
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    WebDataBinderFactory binderFactory = new DefaultDataBinderFactory(initializer);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    Object result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.empty(), result);
    this.request.addParameter("name", "123");
    result = resolver.resolveArgument(param, null, webRequest, binderFactory);
    assertEquals(Optional.class, result.getClass());
    assertEquals(123, ((Optional) result).get());
}
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 24 with DefaultConversionService

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

the class SecurityPropertiesTests method init.

@Before
public void init() {
    this.binder.setIgnoreUnknownFields(false);
    this.binder.setConversionService(new DefaultConversionService());
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Before(org.junit.Before)

Example 25 with DefaultConversionService

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

the class ConfigurationPropertiesBindingPostProcessor method getDefaultConversionService.

private ConversionService getDefaultConversionService() {
    if (this.defaultConversionService == null) {
        DefaultConversionService conversionService = new DefaultConversionService();
        this.applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
        for (Converter<?, ?> converter : this.converters) {
            conversionService.addConverter(converter);
        }
        for (GenericConverter genericConverter : this.genericConverters) {
            conversionService.addConverter(genericConverter);
        }
        this.defaultConversionService = conversionService;
    }
    return this.defaultConversionService;
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService)

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