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