Skip to content

Commit

Permalink
Throw IllegalArgumentException if parse time placeholder error
Browse files Browse the repository at this point in the history
(cherry picked from commit 5fa31e0)
  • Loading branch information
ruanwenjun committed Jan 20, 2024
1 parent 308e4fb commit 26544c7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ public String resolvePlaceholder(String placeholderName) {
*/
public static String getPlaceHolderTime(String expression, Date date) {
if (StringUtils.isBlank(expression)) {
return null;
throw new IllegalArgumentException("expression is null");
}
if (null == date) {
return null;
throw new IllegalArgumentException("date is null");
}
return calculateTime(expression, date);
}
Expand All @@ -354,8 +354,9 @@ public static String getPlaceHolderTime(String expression, Date date) {
*/
private static String calculateTime(String expression, Date date) {
// After N years: $[add_months(yyyyMMdd,12*N)], the first N months: $[add_months(yyyyMMdd,-N)], etc
String value;

if (date == null) {
throw new IllegalArgumentException("Cannot parse the expression: " + expression + ", date is null");
}
try {
if (expression.startsWith(TIMESTAMP)) {
String timeExpression = expression.substring(TIMESTAMP.length() + 1, expression.length() - 1);
Expand All @@ -366,19 +367,16 @@ private static String calculateTime(String expression, Date date) {

Date timestamp = DateUtils.parse(dateStr, PARAMETER_FORMAT_TIME);

value = String.valueOf(timestamp.getTime() / 1000);
} else if (expression.startsWith(YEAR_WEEK)) {
value = calculateYearWeek(expression, date);
} else {
Map.Entry<Date, String> entry = calcTimeExpression(expression, date);
value = DateUtils.format(entry.getKey(), entry.getValue());
return String.valueOf(timestamp.getTime() / 1000);
}
if (expression.startsWith(YEAR_WEEK)) {
return calculateYearWeek(expression, date);
}
Map.Entry<Date, String> entry = calcTimeExpression(expression, date);
return DateUtils.format(entry.getKey(), entry.getValue());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw e;
throw new IllegalArgumentException("Unsupported placeholder expression: " + expression, e);
}

return value;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ private static String dateTemplateParse(String templateStr, Date date) {
continue;
}
String value = TimePlaceholderUtils.getPlaceHolderTime(key, date);
assert value != null;
matcher.appendReplacement(newValue, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.dolphinscheduler.plugin.task.api.parser;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.apache.dolphinscheduler.common.enums.CommandType;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.common.utils.placeholder.BusinessTimeUtils;
Expand All @@ -30,31 +32,31 @@

public class TimePlaceholderUtilsTest {

Date date = DateUtils.parse("2022-08-26 00:00:00", "yyyy-MM-dd HH:mm:ss");
private Date date = DateUtils.parse("2022-08-26 00:00:00", "yyyy-MM-dd HH:mm:ss");

Map<String, String> timeParams = BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA, date, null);
private Map<String, String> timeParams = BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA, date, null);

@Test
public void timePlaceHolderForThisDay() {
String thisDay = "$[this_day(yyyy-MM-dd)]";
String thisDate = "$[this_day(yyyyMMdd)]";

String thisDayTime = ParameterUtils.convertParameterPlaceholders(thisDay, timeParams);
Assertions.assertEquals(thisDayTime, "2022-08-26");
assertEquals(thisDayTime, "2022-08-26");

String thisDateTime = ParameterUtils.convertParameterPlaceholders(thisDate, timeParams);
Assertions.assertEquals(thisDateTime, "20220826");
assertEquals(thisDateTime, "20220826");
}

@Test
public void timePlaceHolderForLastDay() {
String lastDay = "$[last_day(yyyy-MM-dd)]";
String lastDate = "$[last_day(yyyyMMdd)]";
String lastDayTime = ParameterUtils.convertParameterPlaceholders(lastDay, timeParams);
Assertions.assertEquals(lastDayTime, "2022-08-25");
assertEquals(lastDayTime, "2022-08-25");

String lastDateTime = ParameterUtils.convertParameterPlaceholders(lastDate, timeParams);
Assertions.assertEquals(lastDateTime, "20220825");
assertEquals(lastDateTime, "20220825");
}

@Test
Expand All @@ -64,20 +66,20 @@ public void timePlaceHolderForYearWeekDay() {
String yearWeekDay = "$[year_week(yyyyMMdd)]";

String yearWeekDateTime = ParameterUtils.convertParameterPlaceholders(yearWeekDate, timeParams);
Assertions.assertEquals(yearWeekDateTime, "2022-34");
assertEquals(yearWeekDateTime, "2022-34");

String yearWeekDayTime = ParameterUtils.convertParameterPlaceholders(yearWeekDay, timeParams);
Assertions.assertEquals(yearWeekDayTime, "202234");
assertEquals(yearWeekDayTime, "202234");

// Start the week on Friday
String yearWeekDateAny = "$[year_week(yyyy-MM-dd,5)]";
String yearWeekDayAny = "$[year_week(yyyyMMdd,5)]";

String yearWeekDateAnyTime = ParameterUtils.convertParameterPlaceholders(yearWeekDateAny, timeParams);
Assertions.assertEquals(yearWeekDateAnyTime, "2022-35");
assertEquals(yearWeekDateAnyTime, "2022-35");

String yearWeekDayAnyTime = ParameterUtils.convertParameterPlaceholders(yearWeekDayAny, timeParams);
Assertions.assertEquals(yearWeekDayAnyTime, "202235");
assertEquals(yearWeekDayAnyTime, "202235");
}

@Test
Expand All @@ -86,10 +88,10 @@ public void timePlaceHolderForMonthFirstDay() {
String monthFirstDay = "$[month_first_day(yyyyMMdd,-1)]";

String monthFirstDateTime = ParameterUtils.convertParameterPlaceholders(monthFirstDate, timeParams);
Assertions.assertEquals(monthFirstDateTime, "2022-07-01");
assertEquals(monthFirstDateTime, "2022-07-01");

String monthFirstDayTime = ParameterUtils.convertParameterPlaceholders(monthFirstDay, timeParams);
Assertions.assertEquals(monthFirstDayTime, "20220701");
assertEquals(monthFirstDayTime, "20220701");
}

@Test
Expand All @@ -98,10 +100,10 @@ public void timePlaceHolderForMonthLastDay() {
String monthLastDay = "$[month_last_day(yyyyMMdd,-1)]";

String monthLastDateTime = ParameterUtils.convertParameterPlaceholders(monthLastDate, timeParams);
Assertions.assertEquals(monthLastDateTime, "2022-07-31");
assertEquals(monthLastDateTime, "2022-07-31");

String monthLastDayTime = ParameterUtils.convertParameterPlaceholders(monthLastDay, timeParams);
Assertions.assertEquals(monthLastDayTime, "20220731");
assertEquals(monthLastDayTime, "20220731");
}

@Test
Expand All @@ -110,10 +112,10 @@ public void timePlaceHolderForWeekFirstDay() {
String weekFirstDay = "$[week_first_day(yyyyMMdd,0)]";

String weekFirstDateTime = ParameterUtils.convertParameterPlaceholders(weekFirstDate, timeParams);
Assertions.assertEquals(weekFirstDateTime, "2022-08-22");
assertEquals(weekFirstDateTime, "2022-08-22");

String weekFirstDayTime = ParameterUtils.convertParameterPlaceholders(weekFirstDay, timeParams);
Assertions.assertEquals(weekFirstDayTime, "20220822");
assertEquals(weekFirstDayTime, "20220822");
}

@Test
Expand All @@ -122,9 +124,17 @@ public void timePlaceHolderForWeekLastDay() {
String weekLastDay = "$[week_last_day(yyyyMMdd,0)]";

String weekLastDateTime = ParameterUtils.convertParameterPlaceholders(weekLastDate, timeParams);
Assertions.assertEquals(weekLastDateTime, "2022-08-28");
assertEquals(weekLastDateTime, "2022-08-28");

String weekLastDayTime = ParameterUtils.convertParameterPlaceholders(weekLastDay, timeParams);
Assertions.assertEquals(weekLastDayTime, "20220828");
assertEquals(weekLastDayTime, "20220828");
}

@Test
void getPlaceHolderTime() {
IllegalArgumentException illegalArgumentException = Assertions.assertThrows(IllegalArgumentException.class,
() -> TimePlaceholderUtils.getPlaceHolderTime("$[week_last_day(yyyy-MM-dd,0) - 1]", new Date()));
assertEquals("Unsupported placeholder expression: $[week_last_day(yyyy-MM-dd,0) - 1]",
illegalArgumentException.getMessage());
}
}

0 comments on commit 26544c7

Please sign in to comment.