When specifying the templateUpdateDelay configuration setting with a String (with Properties), the time unit is required, unless the value is 0.
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/4bffe528 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/4bffe528 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/4bffe528 Branch: refs/heads/3 Commit: 4bffe5284a14303e4e60a87044108d30dd4fc12c Parents: 45ebfe3 Author: ddekany <[email protected]> Authored: Tue Feb 21 00:10:54 2017 +0100 Committer: ddekany <[email protected]> Committed: Tue Feb 21 00:10:54 2017 +0100 ---------------------------------------------------------------------- .../apache/freemarker/core/Configuration.java | 39 +++++++++++++------- .../freemarker/core/ast/Configurable.java | 11 ++++-- .../freemarker/core/util/_StringUtil.java | 4 ++ src/manual/en_US/FM3-CHANGE-LOG.txt | 4 +- .../freemarker/core/ConfigurationTest.java | 17 ++++++++- 5 files changed, 56 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4bffe528/src/main/java/org/apache/freemarker/core/Configuration.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/Configuration.java b/src/main/java/org/apache/freemarker/core/Configuration.java index 679d074..77ef138 100644 --- a/src/main/java/org/apache/freemarker/core/Configuration.java +++ b/src/main/java/org/apache/freemarker/core/Configuration.java @@ -2418,25 +2418,38 @@ public class Configuration extends Configurable implements Cloneable, ParserConf } } else if (TEMPLATE_UPDATE_DELAY_KEY_SNAKE_CASE.equals(name) || TEMPLATE_UPDATE_DELAY_KEY_CAMEL_CASE.equals(name)) { - long multipier; - String valueWithoutUnit; - if (value.endsWith("ms")) { + final String valueWithoutUnit; + final String unit; + int numberEnd = 0; + while (numberEnd < value.length() && !Character.isAlphabetic(value.charAt(numberEnd))) { + numberEnd++; + } + valueWithoutUnit = value.substring(0, numberEnd).trim(); + unit = value.substring(numberEnd).trim(); + + final long multipier; + if (unit.equals("ms")) { multipier = 1; - valueWithoutUnit = rightTrim(value.substring(0, value.length() - 2)); - } else if (value.endsWith("s")) { + } else if (unit.equals("s")) { multipier = 1000; - valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1)); - } else if (value.endsWith("m")) { + } else if (unit.equals("m")) { multipier = 1000 * 60; - valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1)); - } else if (value.endsWith("h")) { + } else if (unit.equals("h")) { multipier = 1000 * 60 * 60; - valueWithoutUnit = rightTrim(value.substring(0, value.length() - 1)); + } else if (!unit.isEmpty()) { + throw invalidSettingValueException(name, value, + "Unrecognized time unit " + _StringUtil.jQuote(unit) + ". Valid units are: ms, s, m, h"); } else { - multipier = 1000; // Default is seconds for backward compatibility - valueWithoutUnit = value; + multipier = 0; } - setTemplateUpdateDelayMilliseconds(Integer.parseInt(valueWithoutUnit) * multipier); + + int parsedValue = Integer.parseInt(valueWithoutUnit); + if (multipier == 0 && parsedValue != 0) { + throw invalidSettingValueException(name, value, + "Time unit must be specified for a non-0 value (examples: 500 ms, 3 s, 2 m, 1 h)."); + } + + setTemplateUpdateDelayMilliseconds(parsedValue * multipier); } else if (TAG_SYNTAX_KEY_SNAKE_CASE.equals(name) || TAG_SYNTAX_KEY_CAMEL_CASE.equals(name)) { if ("auto_detect".equals(value) || "autoDetect".equals(value)) { setTagSyntax(AUTO_DETECT_TAG_SYNTAX); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4bffe528/src/main/java/org/apache/freemarker/core/ast/Configurable.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/Configurable.java b/src/main/java/org/apache/freemarker/core/ast/Configurable.java index ff290c9..d1a86f3 100644 --- a/src/main/java/org/apache/freemarker/core/ast/Configurable.java +++ b/src/main/java/org/apache/freemarker/core/ast/Configurable.java @@ -2349,13 +2349,18 @@ public class Configurable { /** * @since 2.3.21 */ - protected TemplateException settingValueAssignmentException(String name, String value, Throwable cause) { + protected final TemplateException settingValueAssignmentException(String name, String value, Throwable cause) { return new SettingValueAssignmentException(getEnvironment(), name, value, cause); } + + protected final TemplateException invalidSettingValueException(String name, String value) { + return invalidSettingValueException(name, value, null); + } - protected TemplateException invalidSettingValueException(String name, String value) { + protected final TemplateException invalidSettingValueException(String name, String value, String reason) { return new _MiscTemplateException(getEnvironment(), - "Invalid value for setting ", new _DelayedJQuote(name), ": ", new _DelayedJQuote(value)); + "Invalid value for setting ", new _DelayedJQuote(name), ": ", new _DelayedJQuote(value), + (reason != null ? ": " : null), (reason != null ? reason : null)); } /** http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4bffe528/src/main/java/org/apache/freemarker/core/util/_StringUtil.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/util/_StringUtil.java b/src/main/java/org/apache/freemarker/core/util/_StringUtil.java index 0458b83..a6aa715 100644 --- a/src/main/java/org/apache/freemarker/core/util/_StringUtil.java +++ b/src/main/java/org/apache/freemarker/core/util/_StringUtil.java @@ -1637,6 +1637,10 @@ public class _StringUtil { return sb.toString(); } + public static boolean isASCIIDigit(char c) { + return c >= '0' && c <= '9'; + } + public static boolean isUpperUSASCII(char c) { return c >= 'A' && c <= 'Z'; } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4bffe528/src/manual/en_US/FM3-CHANGE-LOG.txt ---------------------------------------------------------------------- diff --git a/src/manual/en_US/FM3-CHANGE-LOG.txt b/src/manual/en_US/FM3-CHANGE-LOG.txt index 6c4f610..8af1138 100644 --- a/src/manual/en_US/FM3-CHANGE-LOG.txt +++ b/src/manual/en_US/FM3-CHANGE-LOG.txt @@ -109,4 +109,6 @@ the FreeMarer 3 changelog here: - Removed parameterless DefaultObjectWrapper and BeansWrapper constructors. Now specifying the incomplatibleImprovement version is required. - Removed the static default Configuration instance. (It's not possible to create a template with null Configuration - constructor argument anymore.) \ No newline at end of file + constructor argument anymore.) + - When specifying the templateUpdateDelay configuration setting with a String (with Properties), the time unit is + required, unless the value is 0. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/4bffe528/src/test/java/org/apache/freemarker/core/ConfigurationTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/freemarker/core/ConfigurationTest.java b/src/test/java/org/apache/freemarker/core/ConfigurationTest.java index 7ebcef5..2b4be56 100644 --- a/src/test/java/org/apache/freemarker/core/ConfigurationTest.java +++ b/src/test/java/org/apache/freemarker/core/ConfigurationTest.java @@ -19,6 +19,7 @@ package org.apache.freemarker.core; +import static org.apache.freemarker.test.hamcerst.Matchers.containsStringIgnoringCase; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.containsString; @@ -1150,8 +1151,20 @@ public class ConfigurationTest extends TestCase { cfg.setTemplateUpdateDelayMilliseconds(100); assertEquals(100L, cfg.getTemplateUpdateDelayMilliseconds()); - cfg.setSetting(Configuration.TEMPLATE_UPDATE_DELAY_KEY, "5"); - assertEquals(5000L, cfg.getTemplateUpdateDelayMilliseconds()); + try { + cfg.setSetting(Configuration.TEMPLATE_UPDATE_DELAY_KEY, "5"); + assertEquals(5000L, cfg.getTemplateUpdateDelayMilliseconds()); + } catch (SettingValueAssignmentException e) { + assertThat(e.getCause().getMessage(), containsStringIgnoringCase("unit must be specified")); + } + cfg.setSetting(Configuration.TEMPLATE_UPDATE_DELAY_KEY, "0"); + assertEquals(0L, cfg.getTemplateUpdateDelayMilliseconds()); + try { + cfg.setSetting(Configuration.TEMPLATE_UPDATE_DELAY_KEY, "5 foo"); + assertEquals(5000L, cfg.getTemplateUpdateDelayMilliseconds()); + } catch (SettingValueAssignmentException e) { + assertThat(e.getCause().getMessage(), containsStringIgnoringCase("\"foo\"")); + } cfg.setSetting(Configuration.TEMPLATE_UPDATE_DELAY_KEY, "3 ms"); assertEquals(3L, cfg.getTemplateUpdateDelayMilliseconds());
