Browse Source

additional javadoc and tests

pull/23217/head
Keith Donald 15 years ago
parent
commit
9d354192ef
  1. 3
      org.springframework.context/src/main/java/org/springframework/context/support/ConversionServiceFactoryBean.java
  2. 4
      org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java
  3. 80
      org.springframework.context/src/test/java/org/springframework/context/support/ConversionServiceFactoryBeanTests.java
  4. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java

3
org.springframework.context/src/main/java/org/springframework/context/support/ConversionServiceFactoryBean.java

@ -71,6 +71,9 @@ public class ConversionServiceFactoryBean implements FactoryBean<ConversionServi @@ -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();

4
org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java

@ -60,6 +60,10 @@ public class FormattingConversionServiceFactoryBean implements FactoryBean<Conve @@ -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());

80
org.springframework.context/src/test/java/org/springframework/context/support/ConversionServiceFactoryBeanTests.java

@ -0,0 +1,80 @@ @@ -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 {
}
}

2
org.springframework.core/src/main/java/org/springframework/core/convert/support/ConversionServiceFactory.java

@ -17,6 +17,7 @@ @@ -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 { @@ -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…
Cancel
Save