未验证 提交 3ceb6124 编写于 作者: K Kirs 提交者: GitHub

[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
上级 3e411d07
...@@ -32,6 +32,8 @@ import java.util.Iterator; ...@@ -32,6 +32,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -43,6 +45,10 @@ public class ParameterUtils { ...@@ -43,6 +45,10 @@ public class ParameterUtils {
private static final Logger logger = LoggerFactory.getLogger(ParameterUtils.class); 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() { private ParameterUtils() {
throw new UnsupportedOperationException("Construct ParameterUtils"); throw new UnsupportedOperationException("Construct ParameterUtils");
} }
...@@ -51,7 +57,7 @@ public class ParameterUtils { ...@@ -51,7 +57,7 @@ public class ParameterUtils {
* convert parameters place holders * convert parameters place holders
* *
* @param parameterString parameter * @param parameterString parameter
* @param parameterMap parameter map * @param parameterMap parameter map
* @return convert parameters place holders * @return convert parameters place holders
*/ */
public static String convertParameterPlaceholders(String parameterString, Map<String, String> parameterMap) { public static String convertParameterPlaceholders(String parameterString, Map<String, String> parameterMap) {
...@@ -62,7 +68,7 @@ public class ParameterUtils { ...@@ -62,7 +68,7 @@ public class ParameterUtils {
//Get current time, schedule execute time //Get current time, schedule execute time
String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME); String cronTimeStr = parameterMap.get(Constants.PARAMETER_DATETIME);
Date cronTime = null; Date cronTime;
if (StringUtils.isNotEmpty(cronTimeStr)) { if (StringUtils.isNotEmpty(cronTimeStr)) {
cronTime = DateUtils.parse(cronTimeStr, Constants.PARAMETER_FORMAT_TIME); cronTime = DateUtils.parse(cronTimeStr, Constants.PARAMETER_FORMAT_TIME);
...@@ -75,7 +81,7 @@ public class ParameterUtils { ...@@ -75,7 +81,7 @@ public class ParameterUtils {
// replace time $[...] form, eg. $[yyyyMMdd] // replace time $[...] form, eg. $[yyyyMMdd]
if (cronTime != null) { if (cronTime != null) {
parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true); return dateTemplateParse(parameterString, cronTime);
} }
return parameterString; return parameterString;
...@@ -86,7 +92,7 @@ public class ParameterUtils { ...@@ -86,7 +92,7 @@ public class ParameterUtils {
* convert parameters place holders * convert parameters place holders
* *
* @param parameterString parameter * @param parameterString parameter
* @param parameterMap parameter map * @param parameterMap parameter map
* @return convert parameters place holders * @return convert parameters place holders
*/ */
public static String convertParameterPlaceholders2(String parameterString, Map<String, String> parameterMap) { public static String convertParameterPlaceholders2(String parameterString, Map<String, String> parameterMap) {
...@@ -109,8 +115,7 @@ public class ParameterUtils { ...@@ -109,8 +115,7 @@ public class ParameterUtils {
// replace time $[...] form, eg. $[yyyyMMdd] // replace time $[...] form, eg. $[yyyyMMdd]
if (cronTime != null) { if (cronTime != null) {
parameterString = TimePlaceholderUtils.replacePlaceholders(parameterString, cronTime, true); return dateTemplateParse(parameterString, cronTime);
} }
return parameterString; return parameterString;
} }
...@@ -118,10 +123,10 @@ public class ParameterUtils { ...@@ -118,10 +123,10 @@ public class ParameterUtils {
/** /**
* set in parameter * set in parameter
* *
* @param index index * @param index index
* @param stmt preparedstatement * @param stmt preparedstatement
* @param dataType data type * @param dataType data type
* @param value value * @param value value
* @throws Exception errors * @throws Exception errors
*/ */
public static void setInParameter(int index, PreparedStatement stmt, DataType dataType, String value) throws Exception { public static void setInParameter(int index, PreparedStatement stmt, DataType dataType, String value) throws Exception {
...@@ -149,10 +154,10 @@ public class ParameterUtils { ...@@ -149,10 +154,10 @@ public class ParameterUtils {
/** /**
* curing user define parameters * curing user define parameters
* *
* @param globalParamMap global param map * @param globalParamMap global param map
* @param globalParamList global param list * @param globalParamList global param list
* @param commandType command type * @param commandType command type
* @param scheduleTime schedule time * @param scheduleTime schedule time
* @return curing user define parameters * @return curing user define parameters
*/ */
public static String curingGlobalParams(Map<String, String> globalParamMap, List<Property> globalParamList, public static String curingGlobalParams(Map<String, String> globalParamMap, List<Property> globalParamList,
...@@ -169,7 +174,7 @@ public class ParameterUtils { ...@@ -169,7 +174,7 @@ public class ParameterUtils {
Map<String, String> allParamMap = new HashMap<>(); Map<String, String> allParamMap = new HashMap<>();
//If it is a complement, a complement time needs to be passed in, according to the task type //If it is a complement, a complement time needs to be passed in, according to the task type
Map<String, String> timeParams = BusinessTimeUtils Map<String, String> timeParams = BusinessTimeUtils
.getBusinessTime(commandType, scheduleTime); .getBusinessTime(commandType, scheduleTime);
if (timeParams != null) { if (timeParams != null) {
allParamMap.putAll(timeParams); allParamMap.putAll(timeParams);
...@@ -248,4 +253,30 @@ public class ParameterUtils { ...@@ -248,4 +253,30 @@ public class ParameterUtils {
} }
return map; 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();
}
} }
...@@ -51,4 +51,8 @@ public class StringUtils { ...@@ -51,4 +51,8 @@ public class StringUtils {
return src.replaceAll("[\n|\r|\t]", "_"); return src.replaceAll("[\n|\r|\t]", "_");
} }
} }
public static String trim(String str) {
return str == null ? null : str.trim();
}
} }
...@@ -14,17 +14,36 @@ ...@@ -14,17 +14,36 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.common.utils.placeholder; 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.Constants;
import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils; import org.apache.dolphinscheduler.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*; 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 static org.apache.dolphinscheduler.common.Constants.*; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* time place holder utils * time place holder utils
...@@ -46,8 +65,8 @@ public class TimePlaceholderUtils { ...@@ -46,8 +65,8 @@ public class TimePlaceholderUtils {
* Replaces all placeholders of format {@code ${name}} with the value returned * Replaces all placeholders of format {@code ${name}} with the value returned
* from the supplied {@link PropertyPlaceholderHelper.PlaceholderResolver}. * from the supplied {@link PropertyPlaceholderHelper.PlaceholderResolver}.
* *
* @param value the value containing the placeholders to be replaced * @param value the value containing the placeholders to be replaced
* @param date custom date * @param date custom date
* @param ignoreUnresolvablePlaceholders ignore unresolvable placeholders * @param ignoreUnresolvablePlaceholders ignore unresolvable placeholders
* @return the supplied value with placeholders replaced inline * @return the supplied value with placeholders replaced inline
*/ */
...@@ -59,11 +78,11 @@ public class TimePlaceholderUtils { ...@@ -59,11 +78,11 @@ public class TimePlaceholderUtils {
return helper.replacePlaceholders(value, new TimePlaceholderResolver(value, date)); return helper.replacePlaceholders(value, new TimePlaceholderResolver(value, date));
} }
/** /**
* Creates a new {@code PropertyPlaceholderHelper} that uses the supplied prefix and suffix. * Creates a new {@code PropertyPlaceholderHelper} that uses the supplied prefix and suffix.
*
* @param ignoreUnresolvablePlaceholders indicates whether unresolvable placeholders should * @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) { private static PropertyPlaceholderHelper getPropertyPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
return new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, null, ignoreUnresolvablePlaceholders); return new PropertyPlaceholderHelper(PLACEHOLDER_PREFIX, PLACEHOLDER_SUFFIX, null, ignoreUnresolvablePlaceholders);
...@@ -89,7 +108,7 @@ public class TimePlaceholderUtils { ...@@ -89,7 +108,7 @@ public class TimePlaceholderUtils {
* Change the sign in the expression to P (positive) N (negative) * Change the sign in the expression to P (positive) N (negative)
* *
* @param expression * @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) { private static String convert(String expression) {
char[] arr = expression.toCharArray(); char[] arr = expression.toCharArray();
...@@ -262,7 +281,7 @@ public class TimePlaceholderUtils { ...@@ -262,7 +281,7 @@ public class TimePlaceholderUtils {
* Placeholder replacement resolver * Placeholder replacement resolver
*/ */
private static class TimePlaceholderResolver implements private static class TimePlaceholderResolver implements
PropertyPlaceholderHelper.PlaceholderResolver { PropertyPlaceholderHelper.PlaceholderResolver {
private final String value; private final String value;
...@@ -278,12 +297,28 @@ public class TimePlaceholderUtils { ...@@ -278,12 +297,28 @@ public class TimePlaceholderUtils {
try { try {
return calculateTime(placeholderName, date); return calculateTime(placeholderName, date);
} catch (Exception ex) { } catch (Exception ex) {
logger.error("resolve placeholder '{}' in [ {} ]" , placeholderName, value, ex); logger.error("resolve placeholder '{}' in [ {} ]", placeholderName, value, ex);
return null; 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 * calculate time
...@@ -320,9 +355,10 @@ public class TimePlaceholderUtils { ...@@ -320,9 +355,10 @@ public class TimePlaceholderUtils {
/** /**
* calculate time expresstion * calculate time expresstion
*
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return map with date, date format * @return map with date, date format
*/ */
public static Map.Entry<Date, String> calcTimeExpression(String expression, Date date) { public static Map.Entry<Date, String> calcTimeExpression(String expression, Date date) {
Map.Entry<Date, String> resultEntry; Map.Entry<Date, String> resultEntry;
...@@ -346,8 +382,9 @@ public class TimePlaceholderUtils { ...@@ -346,8 +382,9 @@ public class TimePlaceholderUtils {
/** /**
* get first day of month * get first day of month
*
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return first day of month * @return first day of month
*/ */
public static Map.Entry<Date, String> calcMonthBegin(String expression, Date date) { public static Map.Entry<Date, String> calcMonthBegin(String expression, Date date) {
...@@ -369,8 +406,9 @@ public class TimePlaceholderUtils { ...@@ -369,8 +406,9 @@ public class TimePlaceholderUtils {
/** /**
* get last day of month * get last day of month
*
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return last day of month * @return last day of month
*/ */
public static Map.Entry<Date, String> calcMonthEnd(String expression, Date date) { public static Map.Entry<Date, String> calcMonthEnd(String expression, Date date) {
...@@ -392,8 +430,9 @@ public class TimePlaceholderUtils { ...@@ -392,8 +430,9 @@ public class TimePlaceholderUtils {
/** /**
* get first day of week * get first day of week
*
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return monday * @return monday
*/ */
public static Map.Entry<Date, String> calcWeekStart(String expression, Date date) { public static Map.Entry<Date, String> calcWeekStart(String expression, Date date) {
...@@ -414,8 +453,9 @@ public class TimePlaceholderUtils { ...@@ -414,8 +453,9 @@ public class TimePlaceholderUtils {
/** /**
* get last day of week * get last day of week
*
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return last day of week * @return last day of week
*/ */
public static Map.Entry<Date, String> calcWeekEnd(String expression, Date date) { public static Map.Entry<Date, String> calcWeekEnd(String expression, Date date) {
...@@ -437,8 +477,9 @@ public class TimePlaceholderUtils { ...@@ -437,8 +477,9 @@ public class TimePlaceholderUtils {
/** /**
* calc months expression * calc months expression
*
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return calc months * @return calc months
*/ */
public static Map.Entry<Date, String> calcMonths(String expression, Date date) { public static Map.Entry<Date, String> calcMonths(String expression, Date date) {
...@@ -461,7 +502,7 @@ public class TimePlaceholderUtils { ...@@ -461,7 +502,7 @@ public class TimePlaceholderUtils {
* calculate time expression * calculate time expression
* *
* @param expression expresstion * @param expression expresstion
* @param date date * @param date date
* @return calculate time expression with date,format * @return calculate time expression with date,format
*/ */
public static Map.Entry<Date, String> calcMinutes(String expression, Date date) { public static Map.Entry<Date, String> calcMinutes(String expression, Date date) {
...@@ -471,7 +512,7 @@ public class TimePlaceholderUtils { ...@@ -471,7 +512,7 @@ public class TimePlaceholderUtils {
if (Character.isDigit(expression.charAt(index + 1))) { if (Character.isDigit(expression.charAt(index + 1))) {
String addMinuteExpr = expression.substring(index + 1); String addMinuteExpr = expression.substring(index + 1);
Date targetDate = org.apache.commons.lang.time.DateUtils Date targetDate = org.apache.commons.lang.time.DateUtils
.addMinutes(date, calcMinutes(addMinuteExpr)); .addMinutes(date, calcMinutes(addMinuteExpr));
String dateFormat = expression.substring(0, index); String dateFormat = expression.substring(0, index);
return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat);
...@@ -482,7 +523,7 @@ public class TimePlaceholderUtils { ...@@ -482,7 +523,7 @@ public class TimePlaceholderUtils {
if (Character.isDigit(expression.charAt(index + 1))) { if (Character.isDigit(expression.charAt(index + 1))) {
String addMinuteExpr = expression.substring(index + 1); String addMinuteExpr = expression.substring(index + 1);
Date targetDate = org.apache.commons.lang.time.DateUtils Date targetDate = org.apache.commons.lang.time.DateUtils
.addMinutes(date, 0 - calcMinutes(addMinuteExpr)); .addMinutes(date, 0 - calcMinutes(addMinuteExpr));
String dateFormat = expression.substring(0, index); String dateFormat = expression.substring(0, index);
return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat); return new AbstractMap.SimpleImmutableEntry<>(targetDate, dateFormat);
...@@ -512,7 +553,7 @@ public class TimePlaceholderUtils { ...@@ -512,7 +553,7 @@ public class TimePlaceholderUtils {
} else { } else {
calcExpression = String.format("60*24*(%s)%s", minuteExpression.substring(0, index), calcExpression = String.format("60*24*(%s)%s", minuteExpression.substring(0, index),
minuteExpression.substring(index)); minuteExpression.substring(index));
} }
return calculate(calcExpression); return calculate(calcExpression);
......
...@@ -14,25 +14,30 @@ ...@@ -14,25 +14,30 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.common.utils; 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.CommandType;
import org.apache.dolphinscheduler.common.enums.DataType; import org.apache.dolphinscheduler.common.enums.DataType;
import org.apache.dolphinscheduler.common.enums.Direct; import org.apache.dolphinscheduler.common.enums.Direct;
import org.apache.dolphinscheduler.common.process.Property; import org.apache.dolphinscheduler.common.process.Property;
import org.apache.dolphinscheduler.common.utils.placeholder.PlaceholderUtils; 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.Assert;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 class ParameterUtilsTest {
public static final Logger logger = LoggerFactory.getLogger(ParameterUtilsTest.class); public static final Logger logger = LoggerFactory.getLogger(ParameterUtilsTest.class);
...@@ -40,13 +45,13 @@ public class ParameterUtilsTest { ...@@ -40,13 +45,13 @@ public class ParameterUtilsTest {
* Test convertParameterPlaceholders * Test convertParameterPlaceholders
*/ */
@Test @Test
public void testConvertParameterPlaceholders() throws Exception { public void testConvertParameterPlaceholders() throws ParseException {
// parameterString,parameterMap is null // parameterString,parameterMap is null
Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, null)); Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, null));
// parameterString is null,parameterMap is not null // parameterString is null,parameterMap is not null
Map<String, String> parameterMap = new HashMap<String,String>(); Map<String, String> parameterMap = new HashMap<String, String>();
parameterMap.put("testParameter","testParameter"); parameterMap.put("testParameter", "testParameter");
Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap)); Assert.assertNull(ParameterUtils.convertParameterPlaceholders(null, parameterMap));
// parameterString、parameterMap is not null // parameterString、parameterMap is not null
...@@ -54,60 +59,72 @@ public class ParameterUtilsTest { ...@@ -54,60 +59,72 @@ public class ParameterUtilsTest {
Assert.assertEquals(parameterString, ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap)); Assert.assertEquals(parameterString, ParameterUtils.convertParameterPlaceholders(parameterString, parameterMap));
//replace variable ${} form //replace variable ${} form
parameterMap.put("testParameter2","${testParameter}"); parameterMap.put("testParameter2", "${testParameter}");
Assert.assertEquals(parameterString,PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true)); Assert.assertEquals(parameterString, PlaceholderUtils.replacePlaceholders(parameterString, parameterMap, true));
// replace time $[...] form, eg. $[yyyyMMdd] // replace time $[...] form, eg. $[yyyyMMdd]
Date cronTime = new Date(); Date cronTime = new Date();
Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true)); Assert.assertEquals(parameterString, replacePlaceholders(parameterString, cronTime, true));
// replace time $[...] form, eg. $[yyyyMMdd] // 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)); 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<String, String> 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 curingGlobalParams
*/ */
@Test @Test
public void testCuringGlobalParams() throws Exception { public void testCuringGlobalParams() {
//define globalMap //define globalMap
Map<String, String> globalParamMap = new HashMap<>(); Map<String, String> globalParamMap = new HashMap<>();
globalParamMap.put("globalParams1","Params1"); globalParamMap.put("globalParams1", "Params1");
//define globalParamList //define globalParamList
List<Property> globalParamList = new ArrayList<>(); List<Property> globalParamList = new ArrayList<>();
//define scheduleTime //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 //test globalParamList is null
String result = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime); String result = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime);
Assert.assertNull(result); Assert.assertNull(result);
Assert.assertNull(ParameterUtils.curingGlobalParams(null,null,CommandType.START_CURRENT_TASK_PROCESS,null)); 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(globalParamMap, null, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime));
//test globalParamList is not null //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); 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)); 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)); Assert.assertEquals(result3, JSONUtils.toJsonString(globalParamList));
String result4 = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime); String result4 = ParameterUtils.curingGlobalParams(globalParamMap, globalParamList, CommandType.START_CURRENT_TASK_PROCESS, scheduleTime);
Assert.assertEquals(result4, JSONUtils.toJsonString(globalParamList)); Assert.assertEquals(result4, JSONUtils.toJsonString(globalParamList));
//test var $ startsWith //test var $ startsWith
globalParamMap.put("bizDate","${system.biz.date}"); globalParamMap.put("bizDate", "${system.biz.date}");
globalParamMap.put("b1zCurdate","${system.biz.curdate}"); globalParamMap.put("b1zCurdate", "${system.biz.curdate}");
Property property2=new Property("testParamList1", Direct.IN, DataType.VARCHAR,"testParamList"); Property property2 = new Property("testParamList1", Direct.IN, DataType.VARCHAR, "testParamList");
Property property3=new Property("testParamList2", Direct.IN, DataType.VARCHAR,"{testParamList1}"); Property property3 = new Property("testParamList2", Direct.IN, DataType.VARCHAR, "{testParamList1}");
Property property4=new Property("testParamList3", Direct.IN, DataType.VARCHAR,"${b1zCurdate}"); Property property4 = new Property("testParamList3", Direct.IN, DataType.VARCHAR, "${b1zCurdate}");
globalParamList.add(property2); globalParamList.add(property2);
globalParamList.add(property3); globalParamList.add(property3);
...@@ -123,9 +140,9 @@ public class ParameterUtilsTest { ...@@ -123,9 +140,9 @@ public class ParameterUtilsTest {
@Test @Test
public void testHandleEscapes() throws Exception { public void testHandleEscapes() throws Exception {
Assert.assertNull(ParameterUtils.handleEscapes(null)); Assert.assertNull(ParameterUtils.handleEscapes(null));
Assert.assertEquals("",ParameterUtils.handleEscapes("")); Assert.assertEquals("", ParameterUtils.handleEscapes(""));
Assert.assertEquals("test Parameter",ParameterUtils.handleEscapes("test Parameter")); Assert.assertEquals("test Parameter", ParameterUtils.handleEscapes("test Parameter"));
Assert.assertEquals("////%test////%Parameter",ParameterUtils.handleEscapes("%test%Parameter")); Assert.assertEquals("////%test////%Parameter", ParameterUtils.handleEscapes("%test%Parameter"));
} }
} }
...@@ -14,55 +14,62 @@ ...@@ -14,55 +14,62 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.common.utils.placeholder; package org.apache.dolphinscheduler.common.utils.placeholder;
import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.DateUtils;
import java.util.Date;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.util.Date;
public class TimePlaceholderUtilsTest { public class TimePlaceholderUtilsTest {
Date date = null; private Date date;
@Before @Before
public void init(){ public void init() {
date = DateUtils.parse("20170101010101","yyyyMMddHHmmss"); date = DateUtils.parse("20170101010101", "yyyyMMddHHmmss");
} }
// @Test @Test
// public void replacePlaceholdersT() { 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)]", Assert.assertEquals("2017test12017:***2016-12-31,20170102,20170130,20161227,20161231", TimePlaceholderUtils
// date, true)); .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))]," Assert.assertEquals("1483200061,1483290061,1485709261,1482771661,1483113600,1483203661", TimePlaceholderUtils.replacePlaceholders("$[timestamp(yyyyMMdd00mmss)],"
// + "$[timestamp(month_end(yyyyMMddHHmmss, -1))]," + "$[timestamp(month_begin(yyyyMMddHHmmss, 1))],"
// + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))]," + "$[timestamp(month_end(yyyyMMddHHmmss, -1))],"
// + "$[timestamp(week_end(yyyyMMdd000000, -1))]," + "$[timestamp(week_begin(yyyyMMddHHmmss, 1))],"
// + "$[timestamp(yyyyMMddHHmmss)]", + "$[timestamp(week_end(yyyyMMdd000000, -1))],"
// date, true)); + "$[timestamp(yyyyMMddHHmmss)]",
// } date, true));
// }
//
// @Test
// @Test public void calcMinutesT() {
// public void calcMinutesT() { Assert.assertEquals("Sun Jan 01 01:01:01 CST 2017=yyyy", TimePlaceholderUtils.calcMinutes("yyyy", date).toString());
// 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 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("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("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("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 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());
// Assert.assertEquals("Sun Jan 01 00:01:01 CST 2017=yyyyMMddHH", TimePlaceholderUtils.calcMinutes("yyyyMMddHH-1/24", date).toString()); }
// }
// @Test
// @Test public void calcMonthsT() {
// 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("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());
// 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
...@@ -780,6 +780,7 @@ ...@@ -780,6 +780,7 @@
<include>**/common/utils/LoggerUtilsTest.java</include> <include>**/common/utils/LoggerUtilsTest.java</include>
<include>**/common/utils/OSUtilsTest.java</include> <include>**/common/utils/OSUtilsTest.java</include>
<include>**/common/utils/ParameterUtilsTest.java</include> <include>**/common/utils/ParameterUtilsTest.java</include>
<include>**/common/utils/TimePlaceholderUtilsTest.java</include>
<include>**/common/utils/PreconditionsTest.java</include> <include>**/common/utils/PreconditionsTest.java</include>
<include>**/common/utils/PropertyUtilsTest.java</include> <include>**/common/utils/PropertyUtilsTest.java</include>
<include>**/common/utils/SchemaUtilsTest.java</include> <include>**/common/utils/SchemaUtilsTest.java</include>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册