diff --git a/src/test/java/emissary/test/util/RegularExpressionTestUtil.java b/src/test/java/emissary/test/util/RegularExpressionTestUtil.java index 3391c766a5..3dd41f3d99 100644 --- a/src/test/java/emissary/test/util/RegularExpressionTestUtil.java +++ b/src/test/java/emissary/test/util/RegularExpressionTestUtil.java @@ -1,11 +1,13 @@ package emissary.test.util; +import org.apache.commons.collections4.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.annotation.Nullable; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -26,9 +28,9 @@ public class RegularExpressionTestUtil { * @param shouldMatch - Optional. The list of strings that should match the regular expression. * @param shouldNotMatch - Optional. The list of strings that should not match the regular expression. */ - public static void testRegexPattern(String regexPatternString, List shouldMatch, List shouldNotMatch) { + public static void testRegexPattern(String regexPatternString, @Nullable List shouldMatch, @Nullable List shouldNotMatch) { - if (regexPatternString.isBlank()) { + if (regexPatternString == null || regexPatternString.isBlank()) { fail("The test lacks required parameters."); } @@ -40,29 +42,34 @@ public static void testRegexPattern(String regexPatternString, List shou /** * A method to test a list of values that should and should not match a particular regular expression. One of the two - * lists may be empty, but not both. + * lists may be empty or null, but not both. * * @param patternUnderTest - Required. The pre-compiled pattern used to test against the provided values. Must not be * null. * @param shouldMatch - Optional. The list of strings that should match the regular expression. * @param shouldNotMatch - Optional. The list of strings that should not match the regular expression. */ - public static void testRegexPattern(Pattern patternUnderTest, List shouldMatch, List shouldNotMatch) { + public static void testRegexPattern(Pattern patternUnderTest, @Nullable List shouldMatch, @Nullable List shouldNotMatch) { int fineGrainTestCount = 0; - if (patternUnderTest == null || (shouldMatch.isEmpty() && shouldNotMatch.isEmpty())) { + if (patternUnderTest == null || (CollectionUtils.isEmpty(shouldMatch) && CollectionUtils.isEmpty(shouldNotMatch))) { fail("The test lacks required parameters."); } - for (String matchMe : shouldMatch) { - Matcher mm = patternUnderTest.matcher(matchMe); - assertTrue(mm.find(), String.format(" -- Pattern SHOULD match the regex [%s], but did not: %s", patternUnderTest.pattern(), matchMe)); - fineGrainTestCount++; + + if (!CollectionUtils.isEmpty(shouldMatch)) { + for (String matchMe : shouldMatch) { + Matcher mm = patternUnderTest.matcher(matchMe); + assertTrue(mm.find(), String.format(" -- Pattern SHOULD match the regex [%s], but did not: %s", patternUnderTest.pattern(), matchMe)); + fineGrainTestCount++; + } } - for (String dontMatchMe : shouldNotMatch) { - Matcher dmm = patternUnderTest.matcher(dontMatchMe); - assertFalse(dmm.find(), - String.format(" -- Pattern SHOULD NOT match the regex[%s], but did: %s", patternUnderTest.pattern(), dontMatchMe)); - fineGrainTestCount++; + if (!CollectionUtils.isEmpty(shouldNotMatch)) { + for (String dontMatchMe : shouldNotMatch) { + Matcher dmm = patternUnderTest.matcher(dontMatchMe); + assertFalse(dmm.find(), + String.format(" -- Pattern SHOULD NOT match the regex[%s], but did: %s", patternUnderTest.pattern(), dontMatchMe)); + fineGrainTestCount++; + } } if (fineGrainTestCount == 0) { fail("Didn't test anything.");