From de5152eb97a446d55899f3e917ff53620c44ef92 Mon Sep 17 00:00:00 2001 From: ddekany Date: Mon, 31 Aug 2015 21:49:06 +0200 Subject: [PATCH] TemplateDateFormat.isTimeZoneBound support + tests --- .../java/freemarker/core/Environment.java | 7 +- .../core/ISOLikeTemplateDateFormat.java | 5 + .../core/JavaTemplateDateFormat.java | 5 + .../freemarker/core/TemplateDateFormat.java | 4 +- .../java/freemarker/core/DateFormatTest.java | 92 +++++++++++++++++- .../EpochMillisTemplateDateFormatFactory.java | 7 +- ...dTZSensitiveTemplateDateFormatFactory.java | 96 +++++++++++++++++++ .../freemarker/core/NumberFormatTest.java | 2 +- 8 files changed, 209 insertions(+), 9 deletions(-) create mode 100644 src/test/java/freemarker/core/LocAndTZSensitiveTemplateDateFormatFactory.java diff --git a/src/main/java/freemarker/core/Environment.java b/src/main/java/freemarker/core/Environment.java index bf435db9..51f9c00f 100644 --- a/src/main/java/freemarker/core/Environment.java +++ b/src/main/java/freemarker/core/Environment.java @@ -845,7 +845,10 @@ public void setTimeZone(TimeZone timeZone) { if (!timeZone.equals(prevTimeZone)) { if (cachedTempDateFormatArray != null) { for (int i = 0; i < CACHED_TDFS_SQL_D_T_TZ_OFFS; i++) { - cachedTempDateFormatArray[i] = null; + TemplateDateFormat f = cachedTempDateFormatArray[i]; + if (f != null && f.isTimeZoneBound()) { + cachedTempDateFormatArray[i] = null; + } } } if (cachedTempDateFormatsByFmtStrArray != null) { @@ -1444,7 +1447,7 @@ private TemplateDateFormat getTemplateDateFormat( } return format; } catch (InvalidFormatStringException e) { - throw new _TemplateModelException(e.getCause(), + throw new _TemplateModelException(e, (formatStringCfgSettingName == null ? (Object) "Malformed date/time format string: " : new Object[] { diff --git a/src/main/java/freemarker/core/ISOLikeTemplateDateFormat.java b/src/main/java/freemarker/core/ISOLikeTemplateDateFormat.java index a9edb46c..a18b789a 100644 --- a/src/main/java/freemarker/core/ISOLikeTemplateDateFormat.java +++ b/src/main/java/freemarker/core/ISOLikeTemplateDateFormat.java @@ -242,6 +242,11 @@ public final boolean isLocaleBound() { return false; } + @Override + public boolean isTimeZoneBound() { + return true; + } + /** * Always returns {@code null} (there's no markup format). */ diff --git a/src/main/java/freemarker/core/JavaTemplateDateFormat.java b/src/main/java/freemarker/core/JavaTemplateDateFormat.java index 8dd24981..d764c392 100644 --- a/src/main/java/freemarker/core/JavaTemplateDateFormat.java +++ b/src/main/java/freemarker/core/JavaTemplateDateFormat.java @@ -57,6 +57,11 @@ public boolean isLocaleBound() { return true; } + @Override + public boolean isTimeZoneBound() { + return true; + } + /** * Always returns {@code null} (there's no markup format). */ diff --git a/src/main/java/freemarker/core/TemplateDateFormat.java b/src/main/java/freemarker/core/TemplateDateFormat.java index 0e1bcd7b..b7f51f67 100644 --- a/src/main/java/freemarker/core/TemplateDateFormat.java +++ b/src/main/java/freemarker/core/TemplateDateFormat.java @@ -101,9 +101,7 @@ public boolean format(TemplateDateModel d /** * Tells if this formatter should be re-created if the time zone changes. Currently always {@code true}. */ - public final boolean isTimeZoneBound() { - return true; - } + public abstract boolean isTimeZoneBound(); /** * Utility method to extract the {@link Date} from an {@link TemplateDateModel}, and throw diff --git a/src/test/java/freemarker/core/DateFormatTest.java b/src/test/java/freemarker/core/DateFormatTest.java index a6ad0eaf..a84971fa 100644 --- a/src/test/java/freemarker/core/DateFormatTest.java +++ b/src/test/java/freemarker/core/DateFormatTest.java @@ -15,8 +15,12 @@ */ package freemarker.core; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + import java.util.Date; import java.util.Locale; +import java.util.TimeZone; import org.junit.Before; import org.junit.Test; @@ -36,9 +40,11 @@ public void setup() { Configuration cfg = getConfiguration(); cfg.setIncompatibleImprovements(Configuration.VERSION_2_3_24); cfg.setLocale(Locale.US); + cfg.setTimeZone(TimeZone.getTimeZone("GMT+01:00")); cfg.setCustomDateFormats(ImmutableMap.of( - "epoch", EpochMillisTemplateDateFormatFactory.INSTANCE)); + "epoch", EpochMillisTemplateDateFormatFactory.INSTANCE, + "loc", LocAndTZSensitiveTemplateDateFormatFactory.INSTANCE)); } @Test @@ -54,6 +60,59 @@ public void testCustomFormat() throws Exception { + "${d} ${d?string} <#setting locale='de_DE'>${d}", "123456789 123456789 123456789"); } + + @Test + public void testLocaleChange() throws Exception { + addToDataModel("d", new Date(123456789)); + assertOutput( + "${d?string.@loc} ${d?string.@loc} " + + "<#setting locale='de_DE'>" + + "${d?string.@loc} ${d?string.@loc} " + + "<#setting locale='en_US'>" + + "${d?string.@loc} ${d?string.@loc}", + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00 " + + "123456789@de_DE:GMT+01:00 123456789@de_DE:GMT+01:00 " + + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00"); + + getConfiguration().setDateTimeFormat("@loc"); + assertOutput( + "<#assign d = d?datetime>" + + "${d} ${d?string} " + + "<#setting locale='de_DE'>" + + "${d} ${d?string} " + + "<#setting locale='en_US'>" + + "${d} ${d?string}", + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00 " + + "123456789@de_DE:GMT+01:00 123456789@de_DE:GMT+01:00 " + + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00"); + } + + @Test + public void testTimeZoneChange() throws Exception { + addToDataModel("d", new Date(123456789)); + getConfiguration().setDateTimeFormat("iso"); + assertOutput( + "${d?string.@loc} ${d?string.@loc} ${d?datetime?isoLocal} " + + "<#setting timeZone='GMT+02:00'>" + + "${d?string.@loc} ${d?string.@loc} ${d?datetime?isoLocal} " + + "<#setting timeZone='GMT+01:00'>" + + "${d?string.@loc} ${d?string.@loc} ${d?datetime?isoLocal}", + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00 1970-01-02T11:17:36+01:00 " + + "123456789@en_US:GMT+02:00 123456789@en_US:GMT+02:00 1970-01-02T12:17:36+02:00 " + + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00 1970-01-02T11:17:36+01:00"); + + getConfiguration().setDateTimeFormat("@loc"); + assertOutput( + "<#assign d = d?datetime>" + + "${d} ${d?string} " + + "<#setting timeZone='GMT+02:00'>" + + "${d} ${d?string} " + + "<#setting timeZone='GMT+01:00'>" + + "${d} ${d?string}", + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00 " + + "123456789@en_US:GMT+02:00 123456789@en_US:GMT+02:00 " + + "123456789@en_US:GMT+01:00 123456789@en_US:GMT+01:00"); + } @Test public void testWrongFormatStrings() throws Exception { @@ -64,6 +123,37 @@ public void testWrongFormatStrings() throws Exception { assertErrorContains("${.now?string('x2')}", "\"x2\"", "'x'"); } + @Test + public void testUnknownCustomFormat() throws Exception { + { + getConfiguration().setDateTimeFormat("@noSuchFormat"); + Throwable exc = assertErrorContains( + "${.now}", + "\"@noSuchFormat\"", "\"noSuchFormat\"", "\"datetime_format\""); + assertThat(exc.getCause(), instanceOf(UndefinedCustomFormatException.class)); + + } + { + getConfiguration().setDateFormat("@noSuchFormatD"); + assertErrorContains( + "${.now?date}", + "\"@noSuchFormatD\"", "\"noSuchFormatD\"", "\"date_format\""); + } + { + getConfiguration().setTimeFormat("@noSuchFormatT"); + assertErrorContains( + "${.now?time}", + "\"@noSuchFormatT\"", "\"noSuchFormatT\"", "\"time_format\""); + } + + { + getConfiguration().setDateTimeFormat(""); + Throwable exc = assertErrorContains("${.now?string('@noSuchFormat2')}", + "\"@noSuchFormat2\"", "\"noSuchFormat2\""); + assertThat(exc.getCause(), instanceOf(UndefinedCustomFormatException.class)); + } + } + @Test public void testNullInNumberModel() throws Exception { addToDataModel("n", new MutableTemplateDateModel()); diff --git a/src/test/java/freemarker/core/EpochMillisTemplateDateFormatFactory.java b/src/test/java/freemarker/core/EpochMillisTemplateDateFormatFactory.java index f80aa2b5..92feda56 100644 --- a/src/test/java/freemarker/core/EpochMillisTemplateDateFormatFactory.java +++ b/src/test/java/freemarker/core/EpochMillisTemplateDateFormatFactory.java @@ -58,6 +58,11 @@ public boolean isLocaleBound() { return false; } + @Override + public boolean isTimeZoneBound() { + return false; + } + @Override public MO format(TemplateDateModel dateModel, MarkupOutputFormat outputFormat) throws UnformattableNumberException, TemplateModelException { @@ -77,8 +82,6 @@ public Date parse(String s) throws ParseException { public String getDescription() { return "millis since the epoch"; } - - } diff --git a/src/test/java/freemarker/core/LocAndTZSensitiveTemplateDateFormatFactory.java b/src/test/java/freemarker/core/LocAndTZSensitiveTemplateDateFormatFactory.java new file mode 100644 index 00000000..ff4b2234 --- /dev/null +++ b/src/test/java/freemarker/core/LocAndTZSensitiveTemplateDateFormatFactory.java @@ -0,0 +1,96 @@ +/* + * Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky + * + * 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 freemarker.core; + +import java.text.ParseException; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +import org.apache.commons.lang.NotImplementedException; + +import freemarker.template.TemplateDateModel; +import freemarker.template.TemplateModelException; + +public class LocAndTZSensitiveTemplateDateFormatFactory extends TemplateDateFormatFactory { + + public static final LocAndTZSensitiveTemplateDateFormatFactory INSTANCE = new LocAndTZSensitiveTemplateDateFormatFactory(); + + private LocAndTZSensitiveTemplateDateFormatFactory() { + // Defined to decrease visibility + } + + @Override + public TemplateDateFormat get(int dateType, boolean zonelessInput, String params, Locale locale, TimeZone timeZone, + Environment env) throws TemplateModelException, UnknownDateTypeFormattingUnsupportedException, + InvalidFormatParametersException { + TemplateNumberFormatUtil.checkHasNoParameters(params); + return new LocAndTZSensitiveTemplateDateFormat(locale, timeZone); + } + + private static class LocAndTZSensitiveTemplateDateFormat extends TemplateDateFormat { + + private final Locale locale; + private final TimeZone timeZone; + + public LocAndTZSensitiveTemplateDateFormat(Locale locale, TimeZone timeZone) { + this.locale = locale; + this.timeZone = timeZone; + } + + @Override + public String format(TemplateDateModel dateModel) + throws UnformattableDateException, TemplateModelException { + return String.valueOf(getNonNullDate(dateModel).getTime() + "@" + locale + ":" + timeZone.getID()); + } + + @Override + public boolean isLocaleBound() { + return true; + } + + @Override + public boolean isTimeZoneBound() { + return true; + } + + @Override + public MO format(TemplateDateModel dateModel, + MarkupOutputFormat outputFormat) throws UnformattableNumberException, TemplateModelException { + throw new NotImplementedException(); + } + + @Override + public Date parse(String s) throws ParseException { + try { + int atIdx = s.indexOf("@"); + if (atIdx == -1) { + throw new ParseException("Missing @", 0); + } + return new Date(Long.parseLong(s.substring(0, atIdx))); + } catch (NumberFormatException e) { + throw new ParseException("Malformed long", 0); + } + } + + @Override + public String getDescription() { + return "millis since the epoch"; + } + + } + +} diff --git a/src/test/java/freemarker/core/NumberFormatTest.java b/src/test/java/freemarker/core/NumberFormatTest.java index 968c14c3..18fd9ed0 100644 --- a/src/test/java/freemarker/core/NumberFormatTest.java +++ b/src/test/java/freemarker/core/NumberFormatTest.java @@ -52,7 +52,7 @@ public void setup() { } @Test - public void testUnknownNumberFormat() throws Exception { + public void testUnknownCustomFormat() throws Exception { { getConfiguration().setNumberFormat("@noSuchFormat"); Throwable exc = assertErrorContains("${1}", "\"@noSuchFormat\"", "\"noSuchFormat\"");