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