Search in sources :

Example 11 with DefaultConversionService

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

the class RelaxedDataBinderTests method testBindNestedReadOnlyListCommaSeparated.

@Test(expected = NotWritablePropertyException.class)
public void testBindNestedReadOnlyListCommaSeparated() throws Exception {
    TargetWithReadOnlyNestedList target = new TargetWithReadOnlyNestedList();
    this.conversionService = new DefaultConversionService();
    bind(target, "nested: bar,foo");
    assertThat(target.getNested().toString()).isEqualTo("[bar, foo]");
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 12 with DefaultConversionService

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

the class RelaxedDataBinderTests method testBindNestedReadOnlyCollectionIndexed.

@Test
public void testBindNestedReadOnlyCollectionIndexed() throws Exception {
    TargetWithReadOnlyNestedCollection target = new TargetWithReadOnlyNestedCollection();
    this.conversionService = new DefaultConversionService();
    bind(target, "nested[0]: bar\nnested[1]:foo");
    assertThat(target.getNested().toString()).isEqualTo("[bar, foo]");
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 13 with DefaultConversionService

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

the class DefaultListableBeanFactoryTests method testCustomConverter.

@Test
public void testCustomConverter() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    GenericConversionService conversionService = new DefaultConversionService();
    conversionService.addConverter(new Converter<String, Float>() {

        @Override
        public Float convert(String source) {
            try {
                NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
                return nf.parse(source).floatValue();
            } catch (ParseException ex) {
                throw new IllegalArgumentException(ex);
            }
        }
    });
    lbf.setConversionService(conversionService);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", "1,1");
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPropertyValues(pvs);
    lbf.registerBeanDefinition("testBean", bd);
    TestBean testBean = (TestBean) lbf.getBean("testBean");
    assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ParseException(java.text.ParseException) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) NumberFormat(java.text.NumberFormat) Test(org.junit.Test)

Example 14 with DefaultConversionService

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

the class HeaderMethodArgumentResolverTests method resolveOptionalHeaderAsEmpty.

@Test
public void resolveOptionalHeaderAsEmpty() throws Exception {
    GenericApplicationContext cxt = new GenericApplicationContext();
    cxt.refresh();
    HeaderMethodArgumentResolver resolver = new HeaderMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
    Message<String> message = MessageBuilder.withPayload("foo").build();
    Object result = resolver.resolveArgument(paramOptional, message);
    assertEquals(Optional.empty(), result);
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 15 with DefaultConversionService

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

the class PropertySourcesPlaceholderConfigurerTests method optionalPropertyWithValue.

@Test
public void optionalPropertyWithValue() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setConversionService(new DefaultConversionService());
    bf.registerBeanDefinition("testBean", genericBeanDefinition(OptionalTestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MockEnvironment env = new MockEnvironment();
    env.setProperty("my.name", "myValue");
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setEnvironment(env);
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(OptionalTestBean.class).getName(), equalTo(Optional.of("myValue")));
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Aggregations

DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)39 Test (org.junit.Test)31 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