-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from sonatype-nexus-community/develop
Merge develop to main for release 100
- Loading branch information
Showing
9 changed files
with
145 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
get-metrics/src/main/java/org/sonatype/cs/getmetrics/util/DateCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.sonatype.cs.getmetrics.util; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class DateCheck { | ||
public static boolean checkDateFormat(String period, String date) { | ||
if (!period.toUpperCase().equals("MONTH") && !period.toUpperCase().equals("WEEK")) { | ||
throw new IllegalArgumentException("The period must be either MONTH or WEEK"); | ||
} | ||
if (period.toUpperCase().equals("MONTH")) { | ||
Pattern pattern = Pattern.compile("^\\d{4}-\\d{2}$", Pattern.CASE_INSENSITIVE); | ||
Matcher matcher = pattern.matcher(date); | ||
if (matcher.find()) { | ||
return true; | ||
} else { | ||
throw new IllegalArgumentException( | ||
"iq.api.sm.period is set to MONTH but the dates prodived are not in 2022-01" | ||
+ " format. Perhaps there is a W in there for week format?"); | ||
} | ||
} | ||
Pattern pattern = Pattern.compile("^\\d{4}-W\\d{2}$", Pattern.CASE_INSENSITIVE); | ||
Matcher matcher = pattern.matcher(date); | ||
if (matcher.find()) { | ||
return true; | ||
} else { | ||
throw new IllegalArgumentException( | ||
"iq.api.sm.period is set to WEEK but the dates prodived are not in 2022-W01" | ||
+ " format. Perhaps you've missed out the W?"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
get-metrics/src/test/java/org/sonatype/cs/getmetrics/util/DateCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.sonatype.cs.getmetrics.util; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DateCheckTest { | ||
@Test | ||
void testValidDates() { | ||
Assertions.assertTrue(DateCheck.checkDateFormat("MONTH", "2022-01")); | ||
Assertions.assertTrue(DateCheck.checkDateFormat("WEEK", "2022-W01")); | ||
Assertions.assertTrue(DateCheck.checkDateFormat("month", "2022-01")); | ||
Assertions.assertTrue(DateCheck.checkDateFormat("week", "2022-w01")); | ||
Assertions.assertTrue(DateCheck.checkDateFormat("week", "2022-W01")); | ||
} | ||
|
||
@Test | ||
void testInvalidDates() { | ||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> DateCheck.checkDateFormat("MONTH", "2022-W01"), | ||
"Expected checkDateFormat() to throw, but it didn't"); | ||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> DateCheck.checkDateFormat("WEEK", "2022-01"), | ||
"Expected checkDateFormat() to throw, but it didn't"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters