Skip to content

Commit

Permalink
Fixes from testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ldhardy committed Jan 7, 2025
1 parent c635a75 commit 93a6c65
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/test/java/emissary/test/util/RegularExpressionTestUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<String> shouldMatch, List<String> shouldNotMatch) {
public static void testRegexPattern(String regexPatternString, @Nullable List<String> shouldMatch, @Nullable List<String> shouldNotMatch) {

if (regexPatternString.isBlank()) {
if (regexPatternString == null || regexPatternString.isBlank()) {
fail("The test lacks required parameters.");
}

Expand All @@ -40,29 +42,34 @@ public static void testRegexPattern(String regexPatternString, List<String> 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<String> shouldMatch, List<String> shouldNotMatch) {
public static void testRegexPattern(Pattern patternUnderTest, @Nullable List<String> shouldMatch, @Nullable List<String> 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.");
Expand Down

0 comments on commit 93a6c65

Please sign in to comment.