From 3ceb612467ee3eef452599794c8b45d738dba5d3 Mon Sep 17 00:00:00 2001 From: Kirs Date: Tue, 1 Dec 2020 14:34:03 +0800 Subject: [PATCH] [FIX#4033] $[] conflicts with mysql keywords (#4111) * [FIX#4033] $[] conflicts with mysql keywords We currently only use this symbol for dates, so I filtered out the number type. this close #4033 * test * fix error --- .../common/utils/ParameterUtils.java | 57 +++++++++--- .../common/utils/StringUtils.java | 4 + .../placeholder/TimePlaceholderUtils.java | 87 ++++++++++++++----- .../common/utils/ParameterUtilsTest.java | 77 +++++++++------- .../placeholder/TimePlaceholderUtilsTest.java | 81 +++++++++-------- pom.xml | 1 + 6 files changed, 204 insertions(+), 103 deletions(-) diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java index 39ec04afc..1227b4e04 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/ParameterUtils.java @@ -32,6 +32,8 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,6 +45,10 @@ public class ParameterUtils { private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class); + private static final String DATE_PARSE_PATTERN = "\\$\\[([^\\]]+)]"; + + private static final String DATE_START_PATTERN = "^[0-9]"; + private ParameterUtils() { throw new UnsupportedOperationException("Construct ParameterUtils"); } @@ -51,7 +57,7 @@ public class ParameterUtils { * convert parameters place holders * * @param parameterString parameter - * @param parameterMap parameter map + * @param parameterMap parameter map * @return convert parameters place holders */ public static String convertParameterPlaceholders(String parameterString, Map parameterMap) { @@ -62,7 +68,7 @@ public class ParameterUtils { //Get current time, schedule execute time String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME); - Date cronTime = null; + Date cronTime; if (StringUtils.isNotEmpty(cronTimeStr)) { cronTime = DateUtils.parse(cronTimeStr, Constants.PARAMETER_FORMAT_TIME); @@ -75,7 +81,7 @@ public class ParameterUtils { // replace time $[...] form, eg. $[yyyyMMdd] if (cronTime != null) { - parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true); + return dateTemplateParse(parameterString, cronTime); } return parameterString; @@ -86,7 +92,7 @@ public class ParameterUtils { * convert parameters place holders * * @param parameterString parameter - * @param parameterMap parameter map + * @param parameterMap parameter map * @return convert parameters place holders */ public static String convertParameterPlaceholders2(String parameterString, Map parameterMap) { @@ -109,8 +115,7 @@ public class ParameterUtils { // replace time $[...] form, eg. $[yyyyMMdd] if (cronTime != null) { - parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true); - + return dateTemplateParse(parameterString, cronTime); } return parameterString; } @@ -118,10 +123,10 @@ public class ParameterUtils { /** * set in parameter * - * @param index index - * @param stmt preparedstatement + * @param index index + * @param stmt preparedstatement * @param dataType data type - * @param value value + * @param value value * @throws Exception errors */ public static void setInParameter(int index, PreparedStatement stmt, DataType dataType, String value) throws Exception { @@ -149,10 +154,10 @@ public class ParameterUtils { /** * curing user define parameters * - * @param globalParamMap global param map + * @param globalParamMap global param map * @param globalParamList global param list - * @param commandType command type - * @param scheduleTime schedule time + * @param commandType command type + * @param scheduleTime schedule time * @return curing user define parameters */ public static String curingGlobalParams(Map globalParamMap, List globalParamList, @@ -169,7 +174,7 @@ public class ParameterUtils { Map allParamMap = new HashMap<>(); //If it is a complement, a complement time needs to be passed in, according to the task type Map timeParams = BusinessTimeUtils - .getBusinessTime(commandType, scheduleTime); + .getBusinessTime(commandType, scheduleTime); if (timeParams != null) { allParamMap.putAll(timeParams); @@ -248,4 +253,30 @@ public class ParameterUtils { } return map; } + + private static String dateTemplateParse(String templateStr, Date date) { + if (templateStr == null) { + return null; + } + Pattern pattern = Pattern.compile(DATE_PARSE_PATTERN); + + StringBuffer newValue = new StringBuffer(templateStr.length()); + + Matcher matcher = pattern.matcher(templateStr); + + while (matcher.find()) { + String key = matcher.group(1); + if (Pattern.matches(DATE_START_PATTERN, key)) { + continue; + } + String value = TimePlaceholderUtils.getPlaceHolderTime(key, date); + assert value != null; + matcher.appendReplacement(newValue, value); + } + + matcher.appendTail(newValue); + + return newValue.toString(); + } + } diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java index bedfa85f5..256f19905 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/StringUtils.java @@ -51,4 +51,8 @@ public class StringUtils { return src.replaceAll("[\n|\r|\t]", "_"); } } + + public static String trim(String str) { + return str == null ? null : str.trim(); + } } diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java index 35cb01839..0094390c7 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtils.java @@ -14,18 +14,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.dolphinscheduler.common.utils.placeholder; +import static org.apache.dolphinscheduler.common.Constants.ADD_CHAR; +import static org.apache.dolphinscheduler.common.Constants.ADD_STRING; +import static org.apache.dolphinscheduler.common.Constants.DIVISION_CHAR; +import static org.apache.dolphinscheduler.common.Constants.DIVISION_STRING; +import static org.apache.dolphinscheduler.common.Constants.LEFT_BRACE_CHAR; +import static org.apache.dolphinscheduler.common.Constants.LEFT_BRACE_STRING; +import static org.apache.dolphinscheduler.common.Constants.MULTIPLY_CHAR; +import static org.apache.dolphinscheduler.common.Constants.MULTIPLY_STRING; +import static org.apache.dolphinscheduler.common.Constants.N; +import static org.apache.dolphinscheduler.common.Constants.P; +import static org.apache.dolphinscheduler.common.Constants.RIGHT_BRACE_CHAR; +import static org.apache.dolphinscheduler.common.Constants.SUBTRACT_CHAR; +import static org.apache.dolphinscheduler.common.Constants.SUBTRACT_STRING; + import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.utils.DateUtils; -import org.apache.commons.lang.StringUtils; +import org.apache.dolphinscheduler.common.utils.StringUtils; + +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Stack; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; - -import static org.apache.dolphinscheduler.common.Constants.*; - /** * time place holder utils */ @@ -46,8 +65,8 @@ public class TimePlaceholderUtils { * Replaces all placeholders of format {@code ${name}} with the value returned * from the supplied {@link PropertyPlaceholderHelper.PlaceholderResolver}. * - * @param value the value containing the placeholders to be replaced - * @param date custom date + * @param value the value containing the placeholders to be replaced + * @param date custom date * @param ignoreUnresolvablePlaceholders ignore unresolvable placeholders * @return the supplied value with placeholders replaced inline */ @@ -59,11 +78,11 @@ public class TimePlaceholderUtils { return helper.replacePlaceholders(value, new TimePlaceholderResolver(value, date)); } - /** * Creates a new {@code PropertyPlaceholderHelper} that uses the supplied prefix and suffix. + * * @param ignoreUnresolvablePlaceholders indicates whether unresolvable placeholders should - * be ignored ({@code true}) or cause an exception ({@code false}) + * be ignored ({@code true}) or cause an exception ({@code false}) */ private static PropertyPlaceholderHelper getPropertyPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) { return new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, null, ignoreUnresolvablePlaceholders); @@ -89,7 +108,7 @@ public class TimePlaceholderUtils { * Change the sign in the expression to P (positive) N (negative) * * @param expression - * @return eg. "-3+-6*(+8)-(-5) -> S3+S6*(P8)-(S5)" + * @return eg. "-3+-6*(+8)-(-5) -> S3+S6*(P8)-(S5)" */ private static String convert(String expression) { char[] arr = expression.toCharArray(); @@ -262,7 +281,7 @@ public class TimePlaceholderUtils { * Placeholder replacement resolver */ private static class TimePlaceholderResolver implements - PropertyPlaceholderHelper.PlaceholderResolver { + PropertyPlaceholderHelper.PlaceholderResolver { private final String value; @@ -278,12 +297,28 @@ public class TimePlaceholderUtils { try { return calculateTime(placeholderName, date); } catch (Exception ex) { - logger.error("resolve placeholder '{}' in [ {} ]" , placeholderName, value, ex); + logger.error("resolve placeholder '{}' in [ {} ]", placeholderName, value, ex); return null; } } } + /** + * return the formatted date according to the corresponding date format + * + * @param expression date expression + * @param date date + * @return reformat date + */ + public static String getPlaceHolderTime(String expression, Date date) { + if (StringUtils.isBlank(expression)) { + return null; + } + if (null == date) { + return null; + } + return calculateTime(expression, date); + } /** * calculate time @@ -320,9 +355,10 @@ public class TimePlaceholderUtils { /** * calculate time expresstion + * * @param expression expresstion - * @param date date - * @return map with date, date format + * @param date date + * @return map with date, date format */ public static Map.Entry calcTimeExpression(String expression, Date date) { Map.Entry resultEntry; @@ -346,8 +382,9 @@ public class TimePlaceholderUtils { /** * get first day of month + * * @param expression expresstion - * @param date date + * @param date date * @return first day of month */ public static Map.Entry calcMonthBegin(String expression, Date date) { @@ -369,8 +406,9 @@ public class TimePlaceholderUtils { /** * get last day of month + * * @param expression expresstion - * @param date date + * @param date date * @return last day of month */ public static Map.Entry calcMonthEnd(String expression, Date date) { @@ -392,8 +430,9 @@ public class TimePlaceholderUtils { /** * get first day of week + * * @param expression expresstion - * @param date date + * @param date date * @return monday */ public static Map.Entry calcWeekStart(String expression, Date date) { @@ -414,8 +453,9 @@ public class TimePlaceholderUtils { /** * get last day of week + * * @param expression expresstion - * @param date date + * @param date date * @return last day of week */ public static Map.Entry calcWeekEnd(String expression, Date date) { @@ -437,8 +477,9 @@ public class TimePlaceholderUtils { /** * calc months expression + * * @param expression expresstion - * @param date date + * @param date date * @return calc months */ public static Map.Entry calcMonths(String expression, Date date) { @@ -461,7 +502,7 @@ public class TimePlaceholderUtils { * calculate time expression * * @param expression expresstion - * @param date date + * @param date date * @return calculate time expression with date,format */ public static Map.Entry calcMinutes(String expression, Date date) { @@ -471,7 +512,7 @@ public class TimePlaceholderUtils { if (Character.isDigit(expression.charAt(index + 1))) { String addMinuteExpr = expression.substring(index + 1); Date targetDate = org.apache.commons.lang.time.DateUtils - .addMinutes(date, calcMinutes(addMinuteExpr)); + .addMinutes(date, calcMinutes(addMinuteExpr)); String dateFormat = expression.substring(0, index); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); @@ -482,7 +523,7 @@ public class TimePlaceholderUtils { if (Character.isDigit(expression.charAt(index + 1))) { String addMinuteExpr = expression.substring(index + 1); Date targetDate = org.apache.commons.lang.time.DateUtils - .addMinutes(date, 0 - calcMinutes(addMinuteExpr)); + .addMinutes(date, 0 - calcMinutes(addMinuteExpr)); String dateFormat = expression.substring(0, index); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); @@ -512,7 +553,7 @@ public class TimePlaceholderUtils { } else { calcExpression = String.format("60*24*(%s)%s", minuteExpression.substring(0, index), - minuteExpression.substring(index)); + minuteExpression.substring(index)); } return calculate(calcExpression); diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java index 8627226b8..796a5faa4 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/ParameterUtilsTest.java @@ -14,25 +14,30 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.dolphinscheduler.common.utils; -import org.apache.commons.lang.time.DateUtils; +import static org.apache.dolphinscheduler.common.utils.placeholder.TimePlaceholderUtils.replacePlaceholders; + +import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.DataType; import org.apache.dolphinscheduler.common.enums.Direct; import org.apache.dolphinscheduler.common.process.Property; import org.apache.dolphinscheduler.common.utils.placeholder.PlaceholderUtils; + +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.*; - -import static org.apache.dolphinscheduler.common.Constants.PARAMETER_FORMAT_TIME; -import static org.apache.dolphinscheduler.common.utils.placeholder.TimePlaceholderUtils.replacePlaceholders; - - public class ParameterUtilsTest { public static final Logger logger = LoggerFactory.getLogger(ParameterUtilsTest.class); @@ -40,13 +45,13 @@ public class ParameterUtilsTest { * Test convertParameterPlaceholders */ @Test - public void testConvertParameterPlaceholders() throws Exception { + public void testConvertParameterPlaceholders() throws ParseException { // parameterString,parameterMap is null Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, null)); // parameterString is null,parameterMap is not null - Map parameterMap = new HashMap(); - parameterMap.put("testParameter","testParameter"); + Map parameterMap = new HashMap(); + parameterMap.put("testParameter", "testParameter"); Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap)); // parameterString、parameterMap is not null @@ -54,60 +59,72 @@ public class ParameterUtilsTest { Assert.assertEquals(parameterString, ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap)); //replace variable ${} form - parameterMap.put("testParameter2","${testParameter}"); - Assert.assertEquals(parameterString,PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true)); + parameterMap.put("testParameter2", "${testParameter}"); + Assert.assertEquals(parameterString, PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true)); // replace time $[...] form, eg. $[yyyyMMdd] Date cronTime = new Date(); Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true)); // replace time $[...] form, eg. $[yyyyMMdd] - Date cronTimeStr = DateUtils.parseDate("20191220145900", new String[]{PARAMETER_FORMAT_TIME}); + Date cronTimeStr = DateUtils.stringToDate("2019-02-02 00:00:00"); Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTimeStr, true)); } + @Test + public void testConvertParameterPlaceholders2() { + String parameterString = + "${user} is userName, '$[1]' '$[add_months(yyyyMMdd,12*2)]' '$[add_months(yyyyMMdd,-12*2)]' '$[add_months(yyyyMMdd,3)]' '$[add_months(yyyyMMdd,-4)]' " + + "'$[yyyyMMdd+7*2]' '$[yyyyMMdd-7*2]' '$[yyyyMMdd+3]' '$[0]' '$[yyyyMMdd-3]' '$[HHmmss+2/24]' '$[HHmmss-1/24]' '$[HHmmss+3/24/60]' '$[HHmmss-2/24/60]' '$[3]'"; + Map parameterMap = new HashMap<>(); + parameterMap.put("user", "Kris"); + parameterMap.put(Constants.PARAMETER_DATETIME, "20201201123000"); + parameterString = ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap); + Assert.assertEquals("Kris is userName, '$[1]' '20221201' '20181201' '20210301' '20200801' '20201215' '20201117' '20201204' '$[0]' '20201128' '143000' '113000' '123300' '122800' '$[3]'", + parameterString); + } + /** * Test curingGlobalParams */ @Test - public void testCuringGlobalParams() throws Exception { + public void testCuringGlobalParams() { //define globalMap Map globalParamMap = new HashMap<>(); - globalParamMap.put("globalParams1","Params1"); + globalParamMap.put("globalParams1", "Params1"); //define globalParamList List globalParamList = new ArrayList<>(); //define scheduleTime - Date scheduleTime = DateUtils.parseDate("20191220145900", new String[]{PARAMETER_FORMAT_TIME}); + Date scheduleTime = DateUtils.stringToDate("2019-12-20 00:00:00"); //test globalParamList is null String result = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime); Assert.assertNull(result); - Assert.assertNull(ParameterUtils.curingGlobalParams(null,null,CommandType.START_CURRENT_TASK_PROCESS,null)); - Assert.assertNull(ParameterUtils.curingGlobalParams(globalParamMap,null,CommandType.START_CURRENT_TASK_PROCESS,scheduleTime)); + Assert.assertNull(ParameterUtils.curingGlobalParams(null, null, CommandType.START_CURRENT_TASK_PROCESS, null)); + Assert.assertNull(ParameterUtils.curingGlobalParams(globalParamMap, null, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime)); //test globalParamList is not null - Property property=new Property("testGlobalParam", Direct.IN, DataType.VARCHAR,"testGlobalParam"); + Property property = new Property("testGlobalParam", Direct.IN, DataType.VARCHAR, "testGlobalParam"); globalParamList.add(property); - String result2 = ParameterUtils.curingGlobalParams(null,globalParamList,CommandType.START_CURRENT_TASK_PROCESS,scheduleTime); + String result2 = ParameterUtils.curingGlobalParams(null, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime); Assert.assertEquals(result2, JSONUtils.toJsonString(globalParamList)); - String result3 = ParameterUtils.curingGlobalParams(globalParamMap,globalParamList,CommandType.START_CURRENT_TASK_PROCESS,null); + String result3 = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, null); Assert.assertEquals(result3, JSONUtils.toJsonString(globalParamList)); String result4 = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime); Assert.assertEquals(result4, JSONUtils.toJsonString(globalParamList)); //test var $ startsWith - globalParamMap.put("bizDate","${system.biz.date}"); - globalParamMap.put("b1zCurdate","${system.biz.curdate}"); + globalParamMap.put("bizDate", "${system.biz.date}"); + globalParamMap.put("b1zCurdate", "${system.biz.curdate}"); - - Property property2=new Property("testParamList1", Direct.IN, DataType.VARCHAR,"testParamList"); - Property property3=new Property("testParamList2", Direct.IN, DataType.VARCHAR,"{testParamList1}"); - Property property4=new Property("testParamList3", Direct.IN, DataType.VARCHAR,"${b1zCurdate}"); + Property property2 = new Property("testParamList1", Direct.IN, DataType.VARCHAR, "testParamList"); + Property property3 = new Property("testParamList2", Direct.IN, DataType.VARCHAR, "{testParamList1}"); + Property property4 = new Property("testParamList3", Direct.IN, DataType.VARCHAR, "${b1zCurdate}"); globalParamList.add(property2); globalParamList.add(property3); @@ -123,9 +140,9 @@ public class ParameterUtilsTest { @Test public void testHandleEscapes() throws Exception { Assert.assertNull(ParameterUtils.handleEscapes(null)); - Assert.assertEquals("",ParameterUtils.handleEscapes("")); - Assert.assertEquals("test Parameter",ParameterUtils.handleEscapes("test Parameter")); - Assert.assertEquals("////%test////%Parameter",ParameterUtils.handleEscapes("%test%Parameter")); + Assert.assertEquals("", ParameterUtils.handleEscapes("")); + Assert.assertEquals("test Parameter", ParameterUtils.handleEscapes("test Parameter")); + Assert.assertEquals("////%test////%Parameter", ParameterUtils.handleEscapes("%test%Parameter")); } } diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java index d204dfd4d..68f206d28 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/placeholder/TimePlaceholderUtilsTest.java @@ -14,55 +14,62 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.dolphinscheduler.common.utils.placeholder; import org.apache.dolphinscheduler.common.utils.DateUtils; + +import java.util.Date; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import java.util.Date; - public class TimePlaceholderUtilsTest { - Date date = null; + private Date date; @Before - public void init(){ - date = DateUtils.parse("20170101010101","yyyyMMddHHmmss"); + public void init() { + date = DateUtils.parse("20170101010101", "yyyyMMddHHmmss"); } -// @Test -// public void replacePlaceholdersT() { -// Assert.assertEquals("2017test12017:***2016-12-31,20170102,20170130,20161227,20161231", TimePlaceholderUtils.replacePlaceholders("$[yyyy]test1$[yyyy:***]$[yyyy-MM-dd-1],$[month_begin(yyyyMMdd, 1)],$[month_end(yyyyMMdd, -1)],$[week_begin(yyyyMMdd, 1)],$[week_end(yyyyMMdd, -1)]", -// date, true)); -// -// Assert.assertEquals("1483200061,1483290061,1485709261,1482771661,1483113600,1483203661", TimePlaceholderUtils.replacePlaceholders("$[timestamp(yyyyMMdd00mmss)]," -// + "$[timestamp(month_begin(yyyyMMddHHmmss, 1))]," -// + "$[timestamp(month_end(yyyyMMddHHmmss, -1))]," -// + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))]," -// + "$[timestamp(week_end(yyyyMMdd000000, -1))]," -// + "$[timestamp(yyyyMMddHHmmss)]", -// date, true)); -// } -// -// -// -// @Test -// public void calcMinutesT() { -// Assert.assertEquals("Sun Jan 01 01:01:01 CST 2017=yyyy", TimePlaceholderUtils.calcMinutes("yyyy", date).toString()); -// Assert.assertEquals("Sun Jan 08 01:01:01 CST 2017=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd+7*1", date).toString()); -// Assert.assertEquals("Sun Dec 25 01:01:01 CST 2016=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd-7*1", date).toString()); -// Assert.assertEquals("Mon Jan 02 01:01:01 CST 2017=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd+1", date).toString()); -// Assert.assertEquals("Sat Dec 31 01:01:01 CST 2016=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd-1", date).toString()); -// Assert.assertEquals("Sun Jan 01 02:01:01 CST 2017=yyyyMMddHH", TimePlaceholderUtils.calcMinutes("yyyyMMddHH+1/24", date).toString()); -// Assert.assertEquals("Sun Jan 01 00:01:01 CST 2017=yyyyMMddHH", TimePlaceholderUtils.calcMinutes("yyyyMMddHH-1/24", date).toString()); -// } -// -// @Test -// public void calcMonthsT() { -// Assert.assertEquals("Mon Jan 01 01:01:01 CST 2018=yyyyMMdd", TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,12*1)", date).toString()); -// Assert.assertEquals("Fri Jan 01 01:01:01 CST 2016=yyyyMMdd", TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,-12*1)", date).toString()); -// } + @Test + public void replacePlaceholdersT() { + Assert.assertEquals("2017test12017:***2016-12-31,20170102,20170130,20161227,20161231", TimePlaceholderUtils + .replacePlaceholders("$[yyyy]test1$[yyyy:***]$[yyyy-MM-dd-1],$[month_begin(yyyyMMdd, 1)],$[month_end(yyyyMMdd, -1)],$[week_begin(yyyyMMdd, 1)],$[week_end(yyyyMMdd, -1)]", + date, true)); + + Assert.assertEquals("1483200061,1483290061,1485709261,1482771661,1483113600,1483203661", TimePlaceholderUtils.replacePlaceholders("$[timestamp(yyyyMMdd00mmss)]," + + "$[timestamp(month_begin(yyyyMMddHHmmss, 1))]," + + "$[timestamp(month_end(yyyyMMddHHmmss, -1))]," + + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))]," + + "$[timestamp(week_end(yyyyMMdd000000, -1))]," + + "$[timestamp(yyyyMMddHHmmss)]", + date, true)); + } + + @Test + public void calcMinutesT() { + Assert.assertEquals("Sun Jan 01 01:01:01 CST 2017=yyyy", TimePlaceholderUtils.calcMinutes("yyyy", date).toString()); + Assert.assertEquals("Sun Jan 08 01:01:01 CST 2017=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd+7*1", date).toString()); + Assert.assertEquals("Sun Dec 25 01:01:01 CST 2016=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd-7*1", date).toString()); + Assert.assertEquals("Mon Jan 02 01:01:01 CST 2017=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd+1", date).toString()); + Assert.assertEquals("Sat Dec 31 01:01:01 CST 2016=yyyyMMdd", TimePlaceholderUtils.calcMinutes("yyyyMMdd-1", date).toString()); + Assert.assertEquals("Sun Jan 01 02:01:01 CST 2017=yyyyMMddHH", TimePlaceholderUtils.calcMinutes("yyyyMMddHH+1/24", date).toString()); + Assert.assertEquals("Sun Jan 01 00:01:01 CST 2017=yyyyMMddHH", TimePlaceholderUtils.calcMinutes("yyyyMMddHH-1/24", date).toString()); + } + + @Test + public void calcMonthsT() { + Assert.assertEquals("Mon Jan 01 01:01:01 CST 2018=yyyyMMdd", TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,12*1)", date).toString()); + Assert.assertEquals("Fri Jan 01 01:01:01 CST 2016=yyyyMMdd", TimePlaceholderUtils.calcMonths("add_months(yyyyMMdd,-12*1)", date).toString()); + } + + @Test + public void testGetPlaceHolderTime() { + + Assert.assertEquals("20170101", TimePlaceholderUtils.getPlaceHolderTime("yyyyMMdd", date)); + } } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8621bedb1..b9a990ac9 100644 --- a/pom.xml +++ b/pom.xml @@ -780,6 +780,7 @@ **/common/utils/LoggerUtilsTest.java **/common/utils/OSUtilsTest.java **/common/utils/ParameterUtilsTest.java + **/common/utils/TimePlaceholderUtilsTest.java **/common/utils/PreconditionsTest.java **/common/utils/PropertyUtilsTest.java **/common/utils/SchemaUtilsTest.java