use of org.springframework.core.convert.converter.GenericConverter in project spring-framework by spring-projects.
the class ConversionServiceFactoryBeanTests method createDefaultConversionServiceWithSupplements.
@Test
public void createDefaultConversionServiceWithSupplements() {
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
Set<Object> converters = new HashSet<>();
converters.add(new Converter<String, Foo>() {
@Override
public Foo convert(String source) {
return new Foo();
}
});
converters.add(new ConverterFactory<String, Bar>() {
@Override
public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
return new Converter<String, T>() {
@SuppressWarnings("unchecked")
@Override
public T convert(String source) {
return (T) new Bar();
}
};
}
});
converters.add(new GenericConverter() {
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, Baz.class));
}
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return new Baz();
}
});
factory.setConverters(converters);
factory.afterPropertiesSet();
ConversionService service = factory.getObject();
assertTrue(service.canConvert(String.class, Integer.class));
assertTrue(service.canConvert(String.class, Foo.class));
assertTrue(service.canConvert(String.class, Bar.class));
assertTrue(service.canConvert(String.class, Baz.class));
}
use of org.springframework.core.convert.converter.GenericConverter in project spring-framework by spring-projects.
the class GenericConversionServiceTests method shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter.
@Test
public void shouldNotSupportNullConvertibleTypesFromNonConditionalGenericConverter() {
GenericConverter converter = new NonConditionalGenericConverter();
try {
conversionService.addConverter(converter);
fail("Did not throw IllegalStateException");
} catch (IllegalStateException ex) {
assertEquals("Only conditional converters may return null convertible types", ex.getMessage());
}
}
Aggregations