Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1990] Move TimeUtil ParseException throwing to ArgsParser::parse method #2075

Merged
merged 9 commits into from
Dec 30, 2023
13 changes: 11 additions & 2 deletions src/main/java/reposense/parser/ArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
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);
Expand Down Expand Up @@ -420,8 +424,13 @@
? untilDate
: currentDate;

TimeUtil.verifySinceDateIsValid(sinceDate, currentDate);
TimeUtil.verifyDatesRangeIsCorrect(sinceDate, untilDate);
if (sinceDate.compareTo(untilDate) > 0) {
throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_UNTIL_DATE);
}

if (sinceDate.compareTo(currentDate) > 0) {
throw new ParseException(MESSAGE_SINCE_DATE_LATER_THAN_TODAY_DATE);

Check warning on line 432 in src/main/java/reposense/parser/ArgsParser.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/reposense/parser/ArgsParser.java#L432

Added line #L432 was not covered by tests
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some test coverage for the two new if blocks added?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @MarcusTXK, I've added the tests for ArgParser. The PR is ready for review.


builder.sinceDate(sinceDate)
.isSinceDateProvided(isSinceDateProvided)
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/reposense/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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";
Expand Down Expand Up @@ -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}.
*/
Expand Down
Loading