From d8ce4438b9ffdb58e2fe0ff0e44dea45ad9730b0 Mon Sep 17 00:00:00 2001 From: Seah Shau Chung Nicholas <97350362+nseah21@users.noreply.github.com> Date: Sat, 30 Dec 2023 18:35:03 +0100 Subject: [PATCH] [#1990] Move TimeUtil ParseException throwing to ArgsParser::parse method (#2075) ArgsParser::parse uses the methods verifyDatesRangeIsCorrect and verifySinceDateIsValid from TimeUtil to throw a ParseException if either of the checks fail. The throwing of an exception in a utility method is a side effect that makes the methods less reusable. Let's move the throwing of ParseException from TimeUtil into ArgsParser. --- .../java/reposense/parser/ArgsParser.java | 13 ++++++-- src/main/java/reposense/util/TimeUtil.java | 29 ----------------- .../java/reposense/parser/ArgsParserTest.java | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/main/java/reposense/parser/ArgsParser.java b/src/main/java/reposense/parser/ArgsParser.java index 05632bfb4d..ff6ea7ae95 100644 --- a/src/main/java/reposense/parser/ArgsParser.java +++ b/src/main/java/reposense/parser/ArgsParser.java @@ -80,6 +80,10 @@ public class ArgsParser { private static final String MESSAGE_INVALID_CONFIG_JSON = "%s Ignoring the report config provided."; private static final String MESSAGE_SINCE_D1_WITH_PERIOD = "You may be using --since d1 with the --period flag. " + "This may result in an incorrect date range being analysed."; + private static final String MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE = + "\"Since Date\" cannot be later than \"Until Date\"."; + private static final String MESSAGE_SINCE_DATE_LATER_THAN_TODAY_DATE = + "\"Since Date\" must not be later than today's date."; private static final Path EMPTY_PATH = Paths.get(""); private static final Path DEFAULT_CONFIG_PATH = Paths.get(System.getProperty("user.dir") + File.separator + "config" + File.separator); @@ -420,8 +424,13 @@ private static void addAnalysisDatesToBuilder(CliArguments.Builder builder, Name ? untilDate : currentDate; - TimeUtil.verifySinceDateIsValid(sinceDate, currentDate); - TimeUtil.verifyDatesRangeIsCorrect(sinceDate, untilDate); + if (sinceDate.compareTo(currentDate) > 0) { + throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_TODAY_DATE); + } + + if (sinceDate.compareTo(untilDate) > 0) { + throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE); + } builder.sinceDate(sinceDate) .isSinceDateProvided(isSinceDateProvided) diff --git a/src/main/java/reposense/util/TimeUtil.java b/src/main/java/reposense/util/TimeUtil.java index 96a71b7e17..83de42414c 100644 --- a/src/main/java/reposense/util/TimeUtil.java +++ b/src/main/java/reposense/util/TimeUtil.java @@ -9,7 +9,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import reposense.parser.ParseException; import reposense.parser.SinceDateArgumentType; import reposense.system.LogsManager; @@ -23,10 +22,6 @@ public class TimeUtil { // "uuuu" is used for year since "yyyy" does not work with ResolverStyle.STRICT private static final DateTimeFormatter CLI_ARGS_DATE_FORMAT = DateTimeFormatter.ofPattern("d/M/uuuu HH:mm:ss"); - private static final String MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE = - "\"Since Date\" cannot be later than \"Until Date\"."; - private static final String MESSAGE_SINCE_DATE_LATER_THAN_TODAY_DATE = - "\"Since Date\" must not be later than today's date."; private static final String EARLIEST_VALID_DATE = "1970-01-01T00:00:00"; private static final String LATEST_VALID_DATE = "2099-12-31T23:59:59"; @@ -168,30 +163,6 @@ public static boolean isEqualToArbitraryFirstDateConverted(LocalDateTime dateTim return dateTime.equals(getArbitraryFirstCommitDateConverted(zoneId)); } - /** - * Verifies that {@code sinceDate} is earlier than {@code untilDate}. - * - * @throws ParseException if {@code sinceDate} supplied is later than {@code untilDate}. - */ - public static void verifyDatesRangeIsCorrect(LocalDateTime sinceDate, LocalDateTime untilDate) - throws ParseException { - if (sinceDate.compareTo(untilDate) > 0) { - throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE); - } - } - - /** - * Verifies that {@code sinceDate} is no later than the date of report generation, given by {@code currentDate}. - * - * @throws ParseException if {@code sinceDate} supplied is later than date of report generation. - */ - public static void verifySinceDateIsValid(LocalDateTime sinceDate, LocalDateTime currentDate) - throws ParseException { - if (sinceDate.compareTo(currentDate) > 0) { - throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_TODAY_DATE); - } - } - /** * Extracts the first substring of {@code date} string that matches the {@code DATE_FORMAT_REGEX}. */ diff --git a/src/test/java/reposense/parser/ArgsParserTest.java b/src/test/java/reposense/parser/ArgsParserTest.java index 11fa3a5714..1ed90dbbc9 100644 --- a/src/test/java/reposense/parser/ArgsParserTest.java +++ b/src/test/java/reposense/parser/ArgsParserTest.java @@ -11,6 +11,7 @@ import java.time.LocalDateTime; import java.time.Month; import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; @@ -606,6 +607,36 @@ public void sinceDate_laterThanUntilDate_throwsParseException() { Assertions.assertThrows(ParseException.class, () -> ArgsParser.parse(translateCommandline(input))); } + @Test + public void sinceDate_laterThanCurrentDate_throwsParseException() { + LocalDateTime tomorrowDateTime = LocalDateTime.now() + .plusDays(1L); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + String tomorrow = tomorrowDateTime.format(formatter); + + + String input = DEFAULT_INPUT_BUILDER.addSinceDate(tomorrow) + .build(); + Assertions.assertThrows(ParseException.class, () -> ArgsParser.parse(translateCommandline(input))); + } + + @Test + public void sinceDate_beforeUntilDateAndLaterThanCurrentDate_throwsParseException() { + LocalDateTime tomorrowDateTime = LocalDateTime.now() + .plusDays(1L); + LocalDateTime dayAfterDateTime = LocalDateTime.now() + .plusDays(2L); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy"); + String tomorrow = tomorrowDateTime.format(formatter); + String dayAfter = dayAfterDateTime.format(formatter); + + String input = DEFAULT_INPUT_BUILDER.addSinceDate(tomorrow) + .addUntilDate(dayAfter) + .build(); + Assertions.assertThrows(ParseException.class, () -> ArgsParser.parse(translateCommandline(input))); + } + @Test public void period_withBothSinceDateAndUntilDate_throwsParseException() { String input = DEFAULT_INPUT_BUILDER.addPeriod("18d")