Skip to content

Commit

Permalink
Polish PatternMatchUtils[Tests]
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 27, 2023
1 parent d6c9ed2 commit 215f9d8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
public abstract class PatternMatchUtils {

/**
* Match a String against the given pattern, supporting the following simple
* pattern styles: {@code xxx*}, {@code *xxx}, {@code *xxx*}, and {@code xxx*yyy}
* matches (with an arbitrary number of pattern parts), as well as direct equality.
* Match a String against the given pattern, supporting direct equality as
* well as the following simple pattern styles: {@code xxx*}, {@code *xxx},
* {@code *xxx*}, and {@code xxx*yyy} (with an arbitrary number of pattern parts).
* <p>Returns {@code false} if the supplied String or pattern is {@code null}.
* @param pattern the pattern to match against
* @param str the String to match
* @return whether the String matches the given pattern
Expand Down Expand Up @@ -73,14 +74,16 @@ public static boolean simpleMatch(@Nullable String pattern, @Nullable String str
}

/**
* Match a String against the given patterns, supporting the following simple
* pattern styles: {@code xxx*}, {@code *xxx}, {@code *xxx*}, and {@code xxx*yyy}
* matches (with an arbitrary number of pattern parts), as well as direct equality.
* Match a String against the given patterns, supporting direct equality as
* well as the following simple pattern styles: {@code xxx*}, {@code *xxx},
* {@code *xxx*}, and {@code xxx*yyy} (with an arbitrary number of pattern parts).
* <p>Returns {@code false} if the supplied String is {@code null} or if the
* supplied patterns array is {@code null} or empty.
* @param patterns the patterns to match against
* @param str the String to match
* @return whether the String matches any of the given patterns
*/
public static boolean simpleMatch(@Nullable String[] patterns, String str) {
public static boolean simpleMatch(@Nullable String[] patterns, @Nullable String str) {
if (patterns != null) {
for (String pattern : patterns) {
if (simpleMatch(pattern, str)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,86 +21,119 @@
import static org.assertj.core.api.Assertions.assertThat;

/**
* Unit tests for {@link PatternMatchUtils}.
*
* @author Juergen Hoeller
* @author Johan Gorter
* @author Sam Brannen
*/
class PatternMatchUtilsTests {

@Test
void nullAndEmptyValues() {
assertDoesNotMatch((String) null, null);
assertDoesNotMatch((String) null, "");
assertDoesNotMatch("123", null);

assertDoesNotMatch((String[]) null, null);
assertDoesNotMatch((String[]) null, "");
assertDoesNotMatch(new String[] {}, null);
}

@Test
void trivial() {
assertThat(PatternMatchUtils.simpleMatch((String) null, "")).isFalse();
assertThat(PatternMatchUtils.simpleMatch("1", null)).isFalse();
doTest("*", "123", true);
doTest("123", "123", true);
assertMatches("", "");
assertMatches("123", "123");
assertMatches("*", "123");

assertMatches(new String[] { "" }, "");
assertMatches(new String[] { "123" }, "123");
assertMatches(new String[] { "*" }, "123");

assertMatches(new String[] { null, "" }, "");
assertMatches(new String[] { null, "123" }, "123");
assertMatches(new String[] { null, "*" }, "123");
}

@Test
void startsWith() {
doTest("get*", "getMe", true);
doTest("get*", "setMe", false);
assertMatches("get*", "getMe");
assertDoesNotMatch("get*", "setMe");
}

@Test
void endsWith() {
doTest("*Test", "getMeTest", true);
doTest("*Test", "setMe", false);
assertMatches("*Test", "getMeTest");
assertDoesNotMatch("*Test", "setMe");
}

@Test
void between() {
doTest("*stuff*", "getMeTest", false);
doTest("*stuff*", "getstuffTest", true);
doTest("*stuff*", "stuffTest", true);
doTest("*stuff*", "getstuff", true);
doTest("*stuff*", "stuff", true);
assertDoesNotMatch("*stuff*", "getMeTest");
assertMatches("*stuff*", "getstuffTest");
assertMatches("*stuff*", "stuffTest");
assertMatches("*stuff*", "getstuff");
assertMatches("*stuff*", "stuff");
}

@Test
void startsEnds() {
doTest("on*Event", "onMyEvent", true);
doTest("on*Event", "onEvent", true);
doTest("3*3", "3", false);
doTest("3*3", "33", true);
assertMatches("on*Event", "onMyEvent");
assertMatches("on*Event", "onEvent");
assertDoesNotMatch("3*3", "3");
assertMatches("3*3", "33");
}

@Test
void startsEndsBetween() {
doTest("12*45*78", "12345678", true);
doTest("12*45*78", "123456789", false);
doTest("12*45*78", "012345678", false);
doTest("12*45*78", "124578", true);
doTest("12*45*78", "1245457878", true);
doTest("3*3*3", "33", false);
doTest("3*3*3", "333", true);
assertMatches("12*45*78", "12345678");
assertDoesNotMatch("12*45*78", "123456789");
assertDoesNotMatch("12*45*78", "012345678");
assertMatches("12*45*78", "124578");
assertMatches("12*45*78", "1245457878");
assertDoesNotMatch("3*3*3", "33");
assertMatches("3*3*3", "333");
}

@Test
void ridiculous() {
doTest("*1*2*3*", "0011002001010030020201030", true);
doTest("1*2*3*4", "10300204", false);
doTest("1*2*3*3", "10300203", false);
doTest("*1*2*3*", "123", true);
doTest("*1*2*3*", "132", false);
assertMatches("*1*2*3*", "0011002001010030020201030");
assertDoesNotMatch("1*2*3*4", "10300204");
assertDoesNotMatch("1*2*3*3", "10300203");
assertMatches("*1*2*3*", "123");
assertDoesNotMatch("*1*2*3*", "132");
}

@Test
void patternVariants() {
doTest("*a", "*", false);
doTest("*a", "a", true);
doTest("*a", "b", false);
doTest("*a", "aa", true);
doTest("*a", "ba", true);
doTest("*a", "ab", false);
doTest("**a", "*", false);
doTest("**a", "a", true);
doTest("**a", "b", false);
doTest("**a", "aa", true);
doTest("**a", "ba", true);
doTest("**a", "ab", false);
assertDoesNotMatch("*a", "*");
assertMatches("*a", "a");
assertDoesNotMatch("*a", "b");
assertMatches("*a", "aa");
assertMatches("*a", "ba");
assertDoesNotMatch("*a", "ab");
assertDoesNotMatch("**a", "*");
assertMatches("**a", "a");
assertDoesNotMatch("**a", "b");
assertMatches("**a", "aa");
assertMatches("**a", "ba");
assertDoesNotMatch("**a", "ab");
}

private void assertMatches(String pattern, String str) {
assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isTrue();
}

private void assertDoesNotMatch(String pattern, String str) {
assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isFalse();
}

private void assertMatches(String[] patterns, String str) {
assertThat(PatternMatchUtils.simpleMatch(patterns, str)).isTrue();
}

private void doTest(String pattern, String str, boolean shouldMatch) {
assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isEqualTo(shouldMatch);
private void assertDoesNotMatch(String[] patterns, String str) {
assertThat(PatternMatchUtils.simpleMatch(patterns, str)).isFalse();
}

}

0 comments on commit 215f9d8

Please sign in to comment.