Search in sources :

Example 6 with GenericConverter

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));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericConverter(org.springframework.core.convert.converter.GenericConverter) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionService(org.springframework.core.convert.ConversionService) Converter(org.springframework.core.convert.converter.Converter) GenericConverter(org.springframework.core.convert.converter.GenericConverter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with GenericConverter

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());
    }
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) Test(org.junit.Test)

Aggregations

GenericConverter (org.springframework.core.convert.converter.GenericConverter)7 ConditionalGenericConverter (org.springframework.core.convert.converter.ConditionalGenericConverter)4 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ConversionService (org.springframework.core.convert.ConversionService)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 Converter (org.springframework.core.convert.converter.Converter)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1