Support for custom global Joda DateTimeFormatters
Added dateFormatter, timeFormatter and dateTimeFormatter properties to JodaTimeFormatterRegistrar allowing for custom global formatting. DateTimeFormatterFactory can be used when configuring with XML. Issue: SPR-7121
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.junit.Test;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
|
||||
/**
|
||||
* Tests for {@link DateTimeFormatterFactory}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class DateTimeFormatterFactoryTests {
|
||||
|
||||
private DateTimeFormatterFactory factory = new DateTimeFormatterFactory();
|
||||
|
||||
private DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00);
|
||||
|
||||
@Test
|
||||
public void shouldDefaultToMediumFormat() throws Exception {
|
||||
assertThat(factory.getObject(), is(equalTo(DateTimeFormat.mediumDateTime())));
|
||||
assertThat(factory.getDateTimeFormatter(), is(equalTo(DateTimeFormat.mediumDateTime())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateFromPattern() throws Exception {
|
||||
factory = new DateTimeFormatterFactory("yyyyMMddHHmmss");
|
||||
DateTimeFormatter formatter = factory.getObject();
|
||||
assertThat(formatter.print(dateTime), is("20091021121000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBeSingleton() throws Exception {
|
||||
assertThat(factory.isSingleton(), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void shouldCreateDateTimeFormatter() throws Exception {
|
||||
assertThat(factory.getObjectType(), is(equalTo((Class)DateTimeFormatter.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetDateTimeFormatterNullFallback() throws Exception {
|
||||
DateTimeFormatter formatter = factory.getDateTimeFormatter(null);
|
||||
assertThat(formatter, is(nullValue()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetDateTimeFormatterFallback() throws Exception {
|
||||
DateTimeFormatter fallback = DateTimeFormat.forStyle("LL");
|
||||
DateTimeFormatter formatter = factory.getDateTimeFormatter(fallback);
|
||||
assertThat(formatter, is(sameInstance(fallback)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetDateTimeFormatter() throws Exception {
|
||||
factory.setStyle("SS");
|
||||
assertThat(applyLocale(factory.getDateTimeFormatter()).print(dateTime), is("10/21/09 12:10 PM"));
|
||||
|
||||
factory.setIso(ISO.DATE);
|
||||
assertThat(applyLocale(factory.getDateTimeFormatter()).print(dateTime), is("2009-10-21"));
|
||||
|
||||
factory.setPattern("yyyyMMddHHmmss");
|
||||
assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldGetWithTimeZone() throws Exception {
|
||||
factory.setPattern("yyyyMMddHHmmss Z");
|
||||
factory.setTimeZone(TimeZone.getTimeZone("-0700"));
|
||||
assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000 -0700"));
|
||||
}
|
||||
|
||||
private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) {
|
||||
return dateTimeFormatter.withLocale(Locale.US);
|
||||
}
|
||||
}
|
||||
@@ -49,15 +49,20 @@ import static org.junit.Assert.*;
|
||||
*/
|
||||
public class JodaTimeFormattingTests {
|
||||
|
||||
private FormattingConversionService conversionService = new FormattingConversionService();
|
||||
private FormattingConversionService conversionService;
|
||||
|
||||
private DataBinder binder;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
setUp(registrar);
|
||||
}
|
||||
|
||||
private void setUp(JodaTimeFormatterRegistrar registrar) {
|
||||
conversionService = new FormattingConversionService();
|
||||
DefaultConversionService.addDefaultConverters(conversionService);
|
||||
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.registerFormatters(conversionService);
|
||||
|
||||
JodaTimeBean bean = new JodaTimeBean();
|
||||
@@ -84,7 +89,7 @@ public class JodaTimeFormattingTests {
|
||||
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("LL", LocaleContextHolder.getLocale()));
|
||||
System.out.println(org.joda.time.format.DateTimeFormat.patternForStyle("FF", LocaleContextHolder.getLocale()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBindLocalDate() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -94,6 +99,30 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("localDate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalDateWithSpecificStyle() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setDateStyle("L");
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("localDate", "October 31, 2009");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("October 31, 2009", binder.getBindingResult().getFieldValue("localDate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalDateWithSpecifcFormatter() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setDateFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMdd"));
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("localDate", "20091031");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("20091031", binder.getBindingResult().getFieldValue("localDate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalDateArray() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -158,6 +187,30 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalTimeWithSpecificStyle() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setTimeStyle("M");
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("localTime", "12:00:00 PM");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalTimeWithSpecificFormatter() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("HHmmss"));
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("localTime", "130000");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("130000", binder.getBindingResult().getFieldValue("localTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindLocalTimeAnnotated() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -205,6 +258,42 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindDateTimeWithSpecificStyle() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setDateTimeStyle("MM");
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("localDateTime", "Oct 31, 2009 12:00:00 PM");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("Oct 31, 2009 12:00:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindDateTimeISO() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setUseIsoFormat(true);
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("dateTime", "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("dateTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindDateTimeWithSpecificFormatter() throws Exception {
|
||||
JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar();
|
||||
registrar.setDateTimeFormatter(org.joda.time.format.DateTimeFormat.forPattern("yyyyMMddHHmmss"));
|
||||
setUp(registrar);
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
propertyValues.add("dateTime", "20091031130000");
|
||||
binder.bind(propertyValues);
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("20091031130000", binder.getBindingResult().getFieldValue("dateTime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindDateTimeAnnotated() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -339,7 +428,7 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("instant"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBindInstantAnnotated() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -357,7 +446,7 @@ public class JodaTimeFormattingTests {
|
||||
assertEquals(0, binder.getBindingResult().getErrorCount());
|
||||
assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("mutableDateTime"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBindMutableDateTimeAnnotated() {
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||
@@ -483,7 +572,7 @@ public class JodaTimeFormattingTests {
|
||||
public void setLocalDateTimeAnnotated(LocalDateTime localDateTimeAnnotated) {
|
||||
this.localDateTimeAnnotated = localDateTimeAnnotated;
|
||||
}
|
||||
|
||||
|
||||
public LocalDateTime getLocalDateTimeAnnotatedLong() {
|
||||
return localDateTimeAnnotatedLong;
|
||||
}
|
||||
@@ -596,7 +685,7 @@ public class JodaTimeFormattingTests {
|
||||
public void setIsoDateTime(DateTime isoDateTime) {
|
||||
this.isoDateTime = isoDateTime;
|
||||
}
|
||||
|
||||
|
||||
public Instant getInstant() {
|
||||
return instant;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user