SPR-7912 Add tests for FormattingConversionServiceFactoryBean, update reference docs, and remove mvc:formatters

This commit is contained in:
Rossen Stoyanchev
2011-01-27 11:26:19 +00:00
parent 149348c907
commit abff2b959b
7 changed files with 350 additions and 192 deletions

View File

@@ -38,25 +38,6 @@
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="formatters">
<xsd:annotation>
<xsd:documentation><![CDATA[
Registers custom Formatter and AnnotationFormatterFactory types with the FormattingConversionService.
Specifying custom formatters does not cancel the ones already built-in.
]]></xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="beans:bean" minOccurs="1" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation><![CDATA[
The Formatter or the AnnotationFormatterFactory bean definition.
]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:all>
<xsd:attribute name="conversion-service" type="xsd:string">
<xsd:annotation>

View File

@@ -19,34 +19,16 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.text.ParseException;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Formatter;
import org.springframework.format.Parser;
import org.springframework.format.Printer;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver;
@@ -85,21 +67,6 @@ public class AnnotationDrivenBeanDefinitionParserTests {
verifyMessageConverters(appContext.getBean(AnnotationMethodHandlerExceptionResolver.class));
}
@Test
public void testFormatters() throws Exception {
FormattingConversionService conversionService = appContext.getBean(FormattingConversionService.class);
assertNotNull(conversionService);
TestBean testBean = conversionService.convert("5", TestBean.class);
assertEquals(TestBeanFormatter.class.getSimpleName() + " should have been used.", 5, testBean.getField());
assertEquals("5", conversionService.convert(testBean, String.class));
TypeDescriptor intTypeDescriptor = new TypeDescriptor(TestBean.class.getDeclaredField("anotherField"));
Object actual = conversionService.convert(">>5<<", TypeDescriptor.valueOf(String.class), intTypeDescriptor);
assertEquals(TestBeanAnnotationFormatterFactory.class.getSimpleName() + " should have been used", 5, actual);
actual = conversionService.convert(5, intTypeDescriptor, TypeDescriptor.valueOf(String.class));
assertEquals(">>5<<", actual);
}
private void verifyMessageConverters(Object bean) {
assertNotNull(bean);
@@ -124,102 +91,4 @@ public class AnnotationDrivenBeanDefinitionParserTests {
}
@SuppressWarnings("unused")
private static class TestWebArgumentResolver implements WebArgumentResolver {
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
throw new IllegalStateException("Not expected to be invoked");
}
}
@SuppressWarnings("unused")
private static class AnotherTestWebArgumentResolver implements WebArgumentResolver {
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
throw new IllegalStateException("Not expected to be invoked");
}
}
private static class TestBeanFormatter implements Formatter<TestBean> {
public String print(TestBean object, Locale locale) {
return String.valueOf(object.getField());
}
public TestBean parse(String text, Locale locale) throws ParseException {
TestBean object = new TestBean();
object.setField(Integer.parseInt(text));
return object;
}
}
private static class TestBeanAnnotationFormatterFactory implements AnnotationFormatterFactory<SpecialIntFormat> {
private final Set<Class<?>> fieldTypes = new HashSet<Class<?>>(1);
@SuppressWarnings("unused")
public TestBeanAnnotationFormatterFactory() {
fieldTypes.add(Integer.class);
}
public Set<Class<?>> getFieldTypes() {
return fieldTypes;
}
public Printer<?> getPrinter(SpecialIntFormat annotation, Class<?> fieldType) {
return new Printer<Integer>() {
public String print(Integer object, Locale locale) {
return ">>" + object.toString() + "<<";
}
};
}
public Parser<?> getParser(SpecialIntFormat annotation, Class<?> fieldType) {
return new Parser<Integer>() {
public Integer parse(String text, Locale locale) throws ParseException {
if (!text.startsWith(">>") || !text.endsWith("<<") || (text.length() < 5)) {
throw new ParseException(text + " is not in the expected format '>>intValue<<'", 0);
}
return Integer.parseInt(text.substring(2,3));
}
};
}
}
private static class TestBean {
private int field;
@SpecialIntFormat
private int anotherField;
public int getField() {
return field;
}
public void setField(int field) {
this.field = field;
}
@SuppressWarnings("unused")
public int getAnotherField() {
return anotherField;
}
@SuppressWarnings("unused")
public void setAnotherField(int anotherField) {
this.anotherField = anotherField;
}
}
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
private @interface SpecialIntFormat {
}
}

View File

@@ -10,10 +10,6 @@
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
</mvc:message-converters>
<mvc:formatters>
<bean class="org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParserTests$TestBeanFormatter"/>
<bean class="org.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParserTests$TestBeanAnnotationFormatterFactory"/>
</mvc:formatters>
</mvc:annotation-driven>
<bean id="messageCodesResolver"