DateTimeFormat annotation supports use as a meta-annotation as well

This commit is contained in:
Juergen Hoeller
2012-10-31 02:22:09 +01:00
committed by unknown
parent ee50d849a1
commit 5b93b14392
3 changed files with 73 additions and 26 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.format.support;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
@@ -106,6 +108,23 @@ public class FormattingConversionServiceTests {
assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
}
@Test
public void testFormatFieldForValueInjectionUsingMetaAnnotations() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
ac.registerBeanDefinition("valueBean", new RootBeanDefinition(MetaValueBean.class, false));
ac.registerBeanDefinition("conversionService", new RootBeanDefinition(FormattingConversionServiceFactoryBean.class));
ac.registerBeanDefinition("ppc", new RootBeanDefinition(PropertyPlaceholderConfigurer.class));
ac.refresh();
System.setProperty("myDate", "10-31-09");
try {
MetaValueBean valueBean = ac.getBean(MetaValueBean.class);
assertEquals(new LocalDate(2009, 10, 31), new LocalDate(valueBean.date));
}
finally {
System.clearProperty("myDate");
}
}
@Test
public void testFormatFieldForAnnotation() throws Exception {
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
@@ -287,6 +306,20 @@ public class FormattingConversionServiceTests {
}
public static class MetaValueBean {
@MyDateAnn
public Date date;
}
@Value("${myDate}")
@org.springframework.format.annotation.DateTimeFormat(pattern="MM-d-yy")
@Retention(RetentionPolicy.RUNTIME)
public static @interface MyDateAnn {
}
public static class Model {
@org.springframework.format.annotation.DateTimeFormat(style="S-")
@@ -310,7 +343,7 @@ public class FormattingConversionServiceTests {
@org.springframework.format.annotation.DateTimeFormat(style="${dateStyle}")
public Date date;
@org.springframework.format.annotation.DateTimeFormat(pattern="${datePattern}")
@MyDatePattern
public List<Date> dates;
public List<Date> getDates() {
@@ -321,7 +354,14 @@ public class FormattingConversionServiceTests {
this.dates = dates;
}
}
@org.springframework.format.annotation.DateTimeFormat(pattern="${datePattern}")
@Retention(RetentionPolicy.RUNTIME)
public static @interface MyDatePattern {
}
public static class NullReturningFormatter implements Formatter<Integer> {
public String print(Integer object, Locale locale) {