From 432c6ebdaec4629c19679b3d8fc1d377bc19181c Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 5 Nov 2012 11:30:25 -0800 Subject: [PATCH] Refactor DateTimeFormatterFactory Refactor DateTimeFormatterFactory into two distinct classes; a general purpose factory and a specialized FactoryBean. These changes are modeled after the existing VelocityEngineFactory and VelocityEngineFactoryBean classes. Issue: SPR-9959 --- .../joda/DateTimeFormatterFactory.java | 73 ++++++++----------- .../joda/DateTimeFormatterFactoryBean.java | 56 ++++++++++++++ ...eTimeFormatAnnotationFormatterFactory.java | 2 +- .../joda/JodaTimeFormatterRegistrar.java | 3 +- .../DateTimeFormatterFactoryBeanTests.java | 63 ++++++++++++++++ .../joda/DateTimeFormatterFactoryTests.java | 73 +++++++------------ 6 files changed, 177 insertions(+), 93 deletions(-) create mode 100644 spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBean.java create mode 100644 spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBeanTests.java diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java index 0dc5fe6f0c0..30ca9615a5c 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactory.java @@ -22,23 +22,25 @@ import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; - -import org.springframework.beans.factory.FactoryBean; import org.springframework.format.annotation.DateTimeFormat.ISO; import org.springframework.util.StringUtils; /** - * {@link FactoryBean} that creates a Joda {@link DateTimeFormatter}. Formatters will be + * Factory that creates a Joda {@link DateTimeFormatter}. Formatters will be * created using the defined {@link #setPattern(String) pattern}, {@link #setIso(ISO) ISO}, * or {@link #setStyle(String) style} (considered in that order). * * @author Phillip Webb * @author Sam Brannen - * @see #getDateTimeFormatter() - * @see #getDateTimeFormatter(DateTimeFormatter) + * @see #createDateTimeFormatter() + * @see #createDateTimeFormatter(DateTimeFormatter) + * @see #setPattern(String) + * @see #setIso(org.springframework.format.annotation.DateTimeFormat.ISO) + * @see #setStyle(String) + * @see DateTimeFormatterFactoryBean * @since 3.2 */ -public class DateTimeFormatterFactory implements FactoryBean { +public class DateTimeFormatterFactory { private ISO iso; @@ -64,33 +66,21 @@ public class DateTimeFormatterFactory implements FactoryBean } - public boolean isSingleton() { - return true; - } - - public Class getObjectType() { - return DateTimeFormatter.class; - } - - public DateTimeFormatter getObject() throws Exception { - return getDateTimeFormatter(); - } - /** - * Get a new {@code DateTimeFormatter} using this factory. If no specific + * Create a new {@code DateTimeFormatter} using this factory. If no specific * {@link #setStyle(String) style}, {@link #setIso(ISO) ISO}, or * {@link #setPattern(String) pattern} have been defined the * {@link DateTimeFormat#mediumDateTime() medium date time format} will be used. * @return a new date time formatter * @see #getObject() - * @see #getDateTimeFormatter(DateTimeFormatter) + * @see #createDateTimeFormatter(DateTimeFormatter) */ - public DateTimeFormatter getDateTimeFormatter() { - return getDateTimeFormatter(DateTimeFormat.mediumDateTime()); + public DateTimeFormatter createDateTimeFormatter() { + return createDateTimeFormatter(DateTimeFormat.mediumDateTime()); } /** - * Get a new {@code DateTimeFormatter} using this factory. If no specific + * Create a new {@code DateTimeFormatter} using this factory. If no specific * {@link #setStyle(String) style}, {@link #setIso(ISO) ISO}, or * {@link #setPattern(String) pattern} have been defined the supplied * {@code fallbackFormatter} will be used. @@ -98,33 +88,31 @@ public class DateTimeFormatterFactory implements FactoryBean * properties have been set (can be {@code null}). * @return a new date time formatter */ - public DateTimeFormatter getDateTimeFormatter(DateTimeFormatter fallbackFormatter) { - DateTimeFormatter dateTimeFormatter = createDateTimeFormatter(); - if(dateTimeFormatter != null && this.timeZone != null) { - dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone)); - } - return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter); - } - - private DateTimeFormatter createDateTimeFormatter() { + public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) { + DateTimeFormatter dateTimeFormatter = null; if (StringUtils.hasLength(pattern)) { - return DateTimeFormat.forPattern(pattern); + dateTimeFormatter = DateTimeFormat.forPattern(pattern); } - if (iso != null && iso != ISO.NONE) { + else if (iso != null && iso != ISO.NONE) { if (iso == ISO.DATE) { - return ISODateTimeFormat.date(); + dateTimeFormatter = ISODateTimeFormat.date(); + } + else if (iso == ISO.TIME) { + dateTimeFormatter = ISODateTimeFormat.time(); } - if (iso == ISO.TIME) { - return ISODateTimeFormat.time(); + else { + dateTimeFormatter = ISODateTimeFormat.dateTime(); } - return ISODateTimeFormat.dateTime(); } - if (StringUtils.hasLength(style)) { - return DateTimeFormat.forStyle(style); + else if (StringUtils.hasLength(style)) { + dateTimeFormatter = DateTimeFormat.forStyle(style); } - return null; - } + if (dateTimeFormatter != null && this.timeZone != null) { + dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone)); + } + return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter); + } /** * Set the {@code TimeZone} to normalize the date values into, if any. @@ -166,5 +154,4 @@ public class DateTimeFormatterFactory implements FactoryBean public void setPattern(String pattern) { this.pattern = pattern; } - } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBean.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBean.java new file mode 100644 index 00000000000..d38ff217976 --- /dev/null +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBean.java @@ -0,0 +1,56 @@ +/* + * 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 org.joda.time.format.DateTimeFormatter; +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.factory.InitializingBean; + +/** + * {@link FactoryBean} that creates a Joda {@link DateTimeFormatter}. See the base class + * {@linkplain DateTimeFormatterFactory} for configuration details. + * + * @author Phillip Webb + * @author Sam Brannen + * @see #setPattern(String) + * @see #setIso(org.springframework.format.annotation.DateTimeFormat.ISO) + * @see #setStyle(String) + * @see DateTimeFormatterFactory + * @since 3.2 + */ +public class DateTimeFormatterFactoryBean extends DateTimeFormatterFactory implements + FactoryBean, InitializingBean { + + private DateTimeFormatter dateTimeFormatter; + + + public void afterPropertiesSet() throws Exception { + this.dateTimeFormatter = createDateTimeFormatter(); + } + + public DateTimeFormatter getObject() throws Exception { + return this.dateTimeFormatter; + } + + public Class getObjectType() { + return DateTimeFormatter.class; + } + + public boolean isSingleton() { + return true; + } +} diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java index a27fcb760ca..dfc0f91262a 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java @@ -119,6 +119,6 @@ public class JodaDateTimeFormatAnnotationFormatterFactory factory.setStyle(resolveEmbeddedValue(annotation.style())); factory.setIso(annotation.iso()); factory.setPattern(resolveEmbeddedValue(annotation.pattern())); - return factory.getDateTimeFormatter(); + return factory.createDateTimeFormatter(); } } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java index 0a192f5d8dc..51d9f9684b1 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java @@ -47,6 +47,7 @@ import org.springframework.format.annotation.DateTimeFormat.ISO; * @see #setUseIsoFormat * @see FormatterRegistrar#registerFormatters * @see org.springframework.format.datetime.DateFormatterRegistrar + * @see DateTimeFormatterFactoryBean */ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { @@ -185,7 +186,7 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { return formatter; } DateTimeFormatter fallbackFormatter = getFallbackFormatter(type); - return factories.get(type).getDateTimeFormatter(fallbackFormatter ); + return factories.get(type).createDateTimeFormatter(fallbackFormatter ); } private DateTimeFormatter getFallbackFormatter(Type type) { diff --git a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBeanTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBeanTests.java new file mode 100644 index 00000000000..54be0f128f6 --- /dev/null +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryBeanTests.java @@ -0,0 +1,63 @@ +/* + * 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.sameInstance; +import static org.junit.Assert.*; + +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.junit.Test; + +/** + * Tests for {@link DateTimeFormatterFactoryBean}. + * + * @author Phillip Webb + * @author Sam Brannen + */ +public class DateTimeFormatterFactoryBeanTests { + + private DateTimeFormatterFactoryBean factory = new DateTimeFormatterFactoryBean(); + + @Test + public void isSingleton() throws Exception { + assertThat(factory.isSingleton(), is(true)); + } + + @Test + @SuppressWarnings("rawtypes") + public void getObjectType() throws Exception { + assertThat(factory.getObjectType(), is(equalTo((Class) DateTimeFormatter.class))); + } + + @Test + public void getObject() throws Exception { + factory.afterPropertiesSet(); + assertThat(factory.getObject(), is(equalTo(DateTimeFormat.mediumDateTime()))); + } + + @Test + public void getObjectIsAlwaysSingleton() throws Exception { + factory.afterPropertiesSet(); + DateTimeFormatter formatter = factory.getObject(); + assertThat(formatter, is(equalTo(DateTimeFormat.mediumDateTime()))); + factory.setStyle("LL"); + assertThat(factory.getObject(), is(sameInstance(formatter))); + } +} diff --git a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java index 8457eb54fe9..4ac9421cf96 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/joda/DateTimeFormatterFactoryTests.java @@ -37,90 +37,67 @@ import org.springframework.format.annotation.DateTimeFormat.ISO; */ public class DateTimeFormatterFactoryTests { + // Potential test timezone, both have daylight savings on October 21st + private static final TimeZone ZURICH = TimeZone.getTimeZone("Europe/Zurich"); + private static final TimeZone NEW_YORK = TimeZone.getTimeZone("America/New_York"); + + // Ensure that we are testing against a timezone other than the default. + private static final TimeZone TEST_TIMEZONE = ZURICH.equals(TimeZone.getDefault()) ? NEW_YORK : ZURICH; + + 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()))); + public void createDateTimeFormatter() throws Exception { + assertThat(factory.createDateTimeFormatter(), is(equalTo(DateTimeFormat.mediumDateTime()))); } @Test - public void shouldCreateFromPattern() throws Exception { + public void createDateTimeFormatterWithPattern() throws Exception { factory = new DateTimeFormatterFactory("yyyyMMddHHmmss"); - DateTimeFormatter formatter = factory.getObject(); + DateTimeFormatter formatter = factory.createDateTimeFormatter(); 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); + public void createDateTimeFormatterWithNullFallback() throws Exception { + DateTimeFormatter formatter = factory.createDateTimeFormatter(null); assertThat(formatter, is(nullValue())); } @Test - public void shouldGetDateTimeFormatterFallback() throws Exception { + public void createDateTimeFormatterWithFallback() throws Exception { DateTimeFormatter fallback = DateTimeFormat.forStyle("LL"); - DateTimeFormatter formatter = factory.getDateTimeFormatter(fallback); + DateTimeFormatter formatter = factory.createDateTimeFormatter(fallback); assertThat(formatter, is(sameInstance(fallback))); } @Test - public void shouldGetDateTimeFormatter() throws Exception { + public void createDateTimeFormatterInOrderOfPropertyPriority() throws Exception { factory.setStyle("SS"); - assertThat(applyLocale(factory.getDateTimeFormatter()).print(dateTime), is("10/21/09 12:10 PM")); + assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("10/21/09 12:10 PM")); factory.setIso(ISO.DATE); - assertThat(applyLocale(factory.getDateTimeFormatter()).print(dateTime), is("2009-10-21")); + assertThat(applyLocale(factory.createDateTimeFormatter()).print(dateTime), is("2009-10-21")); factory.setPattern("yyyyMMddHHmmss"); - assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000")); + assertThat(factory.createDateTimeFormatter().print(dateTime), is("20091021121000")); } @Test - public void shouldGetWithTimeZone() throws Exception { - - TimeZone zurich = TimeZone.getTimeZone("Europe/Zurich"); - TimeZone newYork = TimeZone.getTimeZone("America/New_York"); - - // Ensure that we are testing against a timezone other than the default. - TimeZone testTimeZone; - String offset; - - if (zurich.equals(TimeZone.getDefault())) { - testTimeZone = newYork; - offset = "-0400"; // Daylight savings on October 21st - } - else { - testTimeZone = zurich; - offset = "+0200"; // Daylight savings on October 21st - } - + public void createDateTimeFormatterWithTimeZone() throws Exception { factory.setPattern("yyyyMMddHHmmss Z"); - factory.setTimeZone(testTimeZone); - - DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(testTimeZone); + factory.setTimeZone(TEST_TIMEZONE); + DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TEST_TIMEZONE); DateTime dateTime = new DateTime(2009, 10, 21, 12, 10, 00, 00, dateTimeZone); - - assertThat(factory.getDateTimeFormatter().print(dateTime), is("20091021121000 " + offset)); + String offset = (TEST_TIMEZONE.equals(NEW_YORK) ? "-0400" : "+0200"); + assertThat(factory.createDateTimeFormatter().print(dateTime), is("20091021121000 " + offset)); } private DateTimeFormatter applyLocale(DateTimeFormatter dateTimeFormatter) { return dateTimeFormatter.withLocale(Locale.US); } - }