From 5a7deb28f72e65a8caabc7e0f92deeb868aa28df Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Mon, 22 Jan 2024 10:21:54 +0800 Subject: [PATCH] Throw IllegalArgumentException if parse time placeholder error (#15514) (cherry picked from commit 5fa31e036d77698f3732a42664bfac3e7edcb8e4) --- .../task/api/parser/TimePlaceholderUtils.java | 26 +++++------ .../plugin/task/api/utils/ParameterUtils.java | 1 - .../api/parser/TimePlaceholderUtilsTest.java | 46 +++++++++++-------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java index c4b0fc84ef..42d95083f1 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtils.java @@ -338,10 +338,10 @@ public class TimePlaceholderUtils { */ 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); } @@ -354,8 +354,9 @@ public class TimePlaceholderUtils { */ 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); @@ -366,19 +367,16 @@ public class TimePlaceholderUtils { 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 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 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; } /** diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtils.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtils.java index 11d1bb113c..ea334125d5 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtils.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/main/java/org/apache/dolphinscheduler/plugin/task/api/utils/ParameterUtils.java @@ -301,7 +301,6 @@ public class ParameterUtils { continue; } String value = TimePlaceholderUtils.getPlaceHolderTime(key, date); - assert value != null; matcher.appendReplacement(newValue, value); } diff --git a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java index 448f45fd55..b1b992e857 100644 --- a/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java +++ b/dolphinscheduler-task-plugin/dolphinscheduler-task-api/src/test/java/org/apache/dolphinscheduler/plugin/task/api/parser/TimePlaceholderUtilsTest.java @@ -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; @@ -30,9 +32,9 @@ import org.junit.jupiter.api.Test; 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 timeParams = BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA, date, null); + private Map timeParams = BusinessTimeUtils.getBusinessTime(CommandType.COMPLEMENT_DATA, date, null); @Test public void timePlaceHolderForThisDay() { @@ -40,10 +42,10 @@ public class TimePlaceholderUtilsTest { 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 @@ -51,10 +53,10 @@ public class TimePlaceholderUtilsTest { 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 @@ -64,20 +66,20 @@ public class TimePlaceholderUtilsTest { 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 @@ -86,10 +88,10 @@ public class TimePlaceholderUtilsTest { 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 @@ -98,10 +100,10 @@ public class TimePlaceholderUtilsTest { 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 @@ -110,10 +112,10 @@ public class TimePlaceholderUtilsTest { 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 @@ -122,9 +124,17 @@ public class TimePlaceholderUtilsTest { 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()); } }