additional javadoc and tests
This commit is contained in:
parent
e3462aeef3
commit
9d354192ef
|
|
@ -71,6 +71,9 @@ public class ConversionServiceFactoryBean implements FactoryBean<ConversionServi
|
|||
|
||||
/**
|
||||
* Creates the ConversionService instance returned by this factory bean.
|
||||
* Creates a default conversion service instance by default.
|
||||
* Subclasses may override to customize the ConversionService instance that gets created.
|
||||
* @see ConversionServiceFactory#createDefaultConversionService()
|
||||
*/
|
||||
protected ConversionService createConversionService() {
|
||||
return ConversionServiceFactory.createDefaultConversionService();
|
||||
|
|
|
|||
|
|
@ -60,6 +60,10 @@ public class FormattingConversionServiceFactoryBean implements FactoryBean<Conve
|
|||
|
||||
// subclassing hooks
|
||||
|
||||
/**
|
||||
* Install Formatters and Converters into the new FormattingConversionService using the FormatterRegistry SPI.
|
||||
* Subclasses may override to customize the set of formatters and/or converters that are installed.
|
||||
*/
|
||||
protected void installFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatterForFieldType(Number.class, new NumberFormatter());
|
||||
registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
package org.springframework.context.support;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.converter.ConverterFactory;
|
||||
import org.springframework.core.convert.converter.GenericConverter;
|
||||
|
||||
public class ConversionServiceFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void createDefaultConversionService() {
|
||||
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
|
||||
factory.afterPropertiesSet();
|
||||
ConversionService service = factory.getObject();
|
||||
assertTrue(service.canConvert(String.class, Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createDefaultConversionServiceWithSupplements() {
|
||||
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
|
||||
Set<Object> converters = new HashSet<Object>();
|
||||
converters.add(new Converter<String, Foo>() {
|
||||
public Foo convert(String source) {
|
||||
return new Foo();
|
||||
}
|
||||
});
|
||||
converters.add(new ConverterFactory<String, Bar>() {
|
||||
public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
|
||||
return new Converter<String, T> () {
|
||||
public T convert(String source) {
|
||||
return (T) new Bar();
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
converters.add(new GenericConverter() {
|
||||
public Class<?>[][] getConvertibleTypes() {
|
||||
return new Class[][] { { String.class, Baz.class } };
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void createDefaultConversionServiceWithInvalidSupplements() {
|
||||
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
|
||||
Set<Object> converters = new HashSet<Object>();
|
||||
converters.add("bogus");
|
||||
factory.setConverters(converters);
|
||||
factory.afterPropertiesSet();
|
||||
}
|
||||
|
||||
public static class Foo {
|
||||
|
||||
}
|
||||
|
||||
public static class Bar {
|
||||
|
||||
}
|
||||
|
||||
public static class Baz {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.core.convert.support;
|
||||
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.converter.ConverterRegistry;
|
||||
|
||||
/**
|
||||
* A factory for creating common ConversionService configurations.
|
||||
|
|
@ -31,6 +32,7 @@ public final class ConversionServiceFactory {
|
|||
|
||||
/**
|
||||
* Create a new default ConversionService prototype that can be safely modified.
|
||||
* Callers may cast the returned ConversionService to a {@link ConverterRegistry} to supplement or override the default converters.
|
||||
*/
|
||||
public static ConversionService createDefaultConversionService() {
|
||||
GenericConversionService conversionService = new GenericConversionService();
|
||||
|
|
|
|||
Loading…
Reference in New Issue