提交 d6545417 编写于 作者: K Keith Donald

iso date time format tests

上级 8de34c6f
...@@ -42,7 +42,7 @@ public final class JodaTimeContextHolder { ...@@ -42,7 +42,7 @@ public final class JodaTimeContextHolder {
* @param context the current JodaTimeContext, or <code>null</code> to clear * @param context the current JodaTimeContext, or <code>null</code> to clear
* the thread-bound context * the thread-bound context
*/ */
public static void setLocaleContext(JodaTimeContext context) { public static void setJodaTimeContext(JodaTimeContext context) {
jodaTimeContextHolder.set(context); jodaTimeContextHolder.set(context);
} }
......
...@@ -103,6 +103,7 @@ public class JodaTimeFormattingConfigurer { ...@@ -103,6 +103,7 @@ public class JodaTimeFormattingConfigurer {
formatterRegistry.addFormatterForFieldType(Date.class, new MillisecondInstantPrinter(jodaDateTimeFormatter), dateTimeParser); formatterRegistry.addFormatterForFieldType(Date.class, new MillisecondInstantPrinter(jodaDateTimeFormatter), dateTimeParser);
formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
formatterRegistry.addFormatterForFieldAnnotation(new ISODateTimeFormatAnnotationFormatterFactory());
} }
// internal helpers // internal helpers
......
...@@ -7,6 +7,7 @@ import java.util.Date; ...@@ -7,6 +7,7 @@ import java.util.Date;
import java.util.Locale; import java.util.Locale;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime; import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime; import org.joda.time.LocalTime;
...@@ -16,6 +17,7 @@ import org.junit.Test; ...@@ -16,6 +17,7 @@ import org.junit.Test;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.ISODateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.Style; import org.springframework.format.annotation.DateTimeFormat.Style;
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer; import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
import org.springframework.format.support.FormattingConversionService; import org.springframework.format.support.FormattingConversionService;
...@@ -36,11 +38,15 @@ public class JodaTimeFormattingTests { ...@@ -36,11 +38,15 @@ public class JodaTimeFormattingTests {
binder.setConversionService(conversionService); binder.setConversionService(conversionService);
LocaleContextHolder.setLocale(Locale.US); LocaleContextHolder.setLocale(Locale.US);
JodaTimeContext context = new JodaTimeContext();
context.setTimeZone(DateTimeZone.forID("-05:00"));
JodaTimeContextHolder.setJodaTimeContext(context);
} }
@After @After
public void tearDown() { public void tearDown() {
LocaleContextHolder.setLocale(null); LocaleContextHolder.setLocale(null);
JodaTimeContextHolder.setJodaTimeContext(null);
} }
@Test @Test
...@@ -120,7 +126,6 @@ public class JodaTimeFormattingTests { ...@@ -120,7 +126,6 @@ public class JodaTimeFormattingTests {
MutablePropertyValues propertyValues = new MutablePropertyValues(); MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("dateTimeAnnotated", "Oct 31, 2009 12:00 PM"); propertyValues.addPropertyValue("dateTimeAnnotated", "Oct 31, 2009 12:00 PM");
binder.bind(propertyValues); binder.bind(propertyValues);
System.out.println(binder.getBindingResult());
assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated")); assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated"));
} }
...@@ -179,6 +184,33 @@ public class JodaTimeFormattingTests { ...@@ -179,6 +184,33 @@ public class JodaTimeFormattingTests {
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated")); assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
} }
@Test
public void testBindISODate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("isoDate", "2009-10-31");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate"));
}
@Test
public void testBindISOTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("isoTime", "12:00:00.000-05:00");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("12:00:00.000", binder.getBindingResult().getFieldValue("isoTime"));
}
@Test
public void testBindISODateTime() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.addPropertyValue("isoDateTime", "2009-10-31T12:00:00.000Z");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime"));
}
public static class JodaTimeBean { public static class JodaTimeBean {
private LocalDate localDate; private LocalDate localDate;
...@@ -216,6 +248,15 @@ public class JodaTimeFormattingTests { ...@@ -216,6 +248,15 @@ public class JodaTimeFormattingTests {
@DateTimeFormat(dateStyle = Style.SHORT) @DateTimeFormat(dateStyle = Style.SHORT)
private Long millisAnnotated; private Long millisAnnotated;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE)
private LocalDate isoDate;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.TIME)
private LocalTime isoTime;
@ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE_TIME)
private DateTime isoDateTime;
public LocalDate getLocalDate() { public LocalDate getLocalDate() {
return localDate; return localDate;
} }
...@@ -328,5 +369,29 @@ public class JodaTimeFormattingTests { ...@@ -328,5 +369,29 @@ public class JodaTimeFormattingTests {
this.millisAnnotated = millisAnnotated; this.millisAnnotated = millisAnnotated;
} }
public LocalDate getIsoDate() {
return isoDate;
}
public void setIsoDate(LocalDate isoDate) {
this.isoDate = isoDate;
}
public LocalTime getIsoTime() {
return isoTime;
}
public void setIsoTime(LocalTime isoTime) {
this.isoTime = isoTime;
}
public DateTime getIsoDateTime() {
return isoDateTime;
}
public void setIsoDateTime(DateTime isoDateTime) {
this.isoDateTime = isoDateTime;
}
} }
} }
...@@ -326,7 +326,7 @@ public class MappingTests { ...@@ -326,7 +326,7 @@ public class MappingTests {
// field to multiple fields // field to multiple fields
.addAssemblerMapping("activationDateTime", new Converter<Map<String, String>, DateTime>() { .addAssemblerMapping("activationDateTime", new Converter<Map<String, String>, DateTime>() {
public DateTime convert(Map<String, String> source) { public DateTime convert(Map<String, String> source) {
MutableDateTime dateTime = new MutableDateTime(DateTimeZone.forID("-04:00")); MutableDateTime dateTime = new MutableDateTime(DateTimeZone.forID("-05:00"));
dateTime.setYear(Integer.parseInt(source.get("year"))); dateTime.setYear(Integer.parseInt(source.get("year")));
dateTime.setMonthOfYear(Integer.parseInt(source.get("month"))); dateTime.setMonthOfYear(Integer.parseInt(source.get("month")));
dateTime.setDayOfMonth(Integer.parseInt(source.get("day"))); dateTime.setDayOfMonth(Integer.parseInt(source.get("day")));
...@@ -344,7 +344,7 @@ public class MappingTests { ...@@ -344,7 +344,7 @@ public class MappingTests {
source.put("activationDateTime.hour", "12"); source.put("activationDateTime.hour", "12");
source.put("activationDateTime.minute", "0"); source.put("activationDateTime.minute", "0");
Account account = mapper.map(source, new Account()); Account account = mapper.map(source, new Account());
assertEquals(ISODateTimeFormat.dateTime().withOffsetParsed().parseDateTime("2009-10-12T12:00:00.000-04:00"), account assertEquals(ISODateTimeFormat.dateTime().withOffsetParsed().parseDateTime("2009-10-12T12:00:00.000-05:00"), account
.getActivationDateTime()); .getActivationDateTime());
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册