Search in sources :

Example 6 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 7 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 8 with DefaultConversionService

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

the class SpringApplication method bindToSpringApplication.

/**
	 * Bind the environment to the {@link SpringApplication}.
	 * @param environment the environment to bind
	 */
protected void bindToSpringApplication(ConfigurableEnvironment environment) {
    PropertiesConfigurationFactory<SpringApplication> binder = new PropertiesConfigurationFactory<>(this);
    ConversionService conversionService = new DefaultConversionService();
    binder.setTargetName("spring.main");
    binder.setConversionService(conversionService);
    binder.setPropertySources(environment.getPropertySources());
    try {
        binder.bindPropertiesToTarget();
    } catch (BindException ex) {
        throw new IllegalStateException("Cannot bind to SpringApplication", ex);
    }
}
Also used : PropertiesConfigurationFactory(org.springframework.boot.bind.PropertiesConfigurationFactory) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConversionService(org.springframework.core.convert.ConversionService) BindException(org.springframework.validation.BindException) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService)

Example 9 with DefaultConversionService

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

the class AbstractPropertyAccessorTests method getPropertyIntermediateMapEntryIsNullWithAutoGrow.

@Test
public void getPropertyIntermediateMapEntryIsNullWithAutoGrow() {
    Foo target = new Foo();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setConversionService(new DefaultConversionService());
    accessor.setAutoGrowNestedPaths(true);
    accessor.setPropertyValue("listOfMaps[0]['luckyNumber']", "9");
    assertEquals("9", target.listOfMaps.get(0).get("luckyNumber"));
}
Also used : DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Test(org.junit.Test)

Example 10 with DefaultConversionService

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

the class AbstractPropertyAccessorTests method setPropertyIntermediateListIsNullWithAutoGrow.

@SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
@Test
public void setPropertyIntermediateListIsNullWithAutoGrow() {
    Foo target = new Foo();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.setConversionService(new DefaultConversionService());
    accessor.setAutoGrowNestedPaths(true);
    Map<String, String> map = new HashMap<>();
    map.put("favoriteNumber", "9");
    accessor.setPropertyValue("list[0]", map);
    assertEquals(map, target.list.get(0));
}
Also used : HashMap(java.util.HashMap) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) 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