use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.
the class DefaultListableBeanFactoryTests method customConverter.
@Test
void customConverter() {
GenericConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(String.class, Float.class, 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");
assertThat(testBean.getMyFloat().floatValue() == 1.1f).isTrue();
}
use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.
the class BeanWrapperEnumTests method testStandardEnumSetWithMultipleValues.
@Test
public void testStandardEnumSetWithMultipleValues() {
GenericBean<?> gb = new GenericBean<>();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setConversionService(new DefaultConversionService());
assertThat(gb.getStandardEnumSet()).isNull();
bw.setPropertyValue("standardEnumSet", new String[] { "VALUE_1", "VALUE_2" });
assertThat(gb.getStandardEnumSet().size()).isEqualTo(2);
assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_1)).isTrue();
assertThat(gb.getStandardEnumSet().contains(CustomEnum.VALUE_2)).isTrue();
}
use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.
the class BeanWrapperEnumTests method testStandardEnumMapWithMultipleValues.
@Test
public void testStandardEnumMapWithMultipleValues() {
GenericBean<?> gb = new GenericBean<>();
BeanWrapper bw = new BeanWrapperImpl(gb);
bw.setConversionService(new DefaultConversionService());
assertThat(gb.getStandardEnumMap()).isNull();
Map<String, Integer> map = new LinkedHashMap<>();
map.put("VALUE_1", 1);
map.put("VALUE_2", 2);
bw.setPropertyValue("standardEnumMap", map);
assertThat(gb.getStandardEnumMap().size()).isEqualTo(2);
assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_1)).isEqualTo(1);
assertThat(gb.getStandardEnumMap().get(CustomEnum.VALUE_2)).isEqualTo(2);
}
use of org.springframework.core.convert.support.DefaultConversionService in project spring-framework by spring-projects.
the class DataBinderTests method testOptionalProperty.
@Test
void testOptionalProperty() {
OptionalHolder bean = new OptionalHolder();
DataBinder binder = new DataBinder(bean);
binder.setConversionService(new DefaultConversionService());
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("id", "1");
pvs.add("name", null);
binder.bind(pvs);
assertThat(bean.getId()).isEqualTo("1");
assertThat(bean.getName()).isEmpty();
pvs = new MutablePropertyValues();
pvs.add("id", "2");
pvs.add("name", "myName");
binder.bind(pvs);
assertThat(bean.getId()).isEqualTo("2");
assertThat(bean.getName().get()).isEqualTo("myName");
}
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);
assertThat(resolver.resolveArgument(paramOptional, mavContainer, webRequest, binderFactory)).isEqualTo(Optional.empty());
}
Aggregations