From 1f55ae1e6418b34c745d472903bfb835eb4c392b Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Mon, 4 Nov 2024 13:40:20 +0000 Subject: [PATCH] added tests, fixed one bug --- .../codehaus/plexus/util/MatchPattern.java | 2 +- .../plexus/util/MatchPatternTest.java | 32 +++++++++++++++++ .../plexus/util/MatchPatternsTest.java | 15 ++++++++ .../plexus/util/SelectorUtilsTest.java | 36 +++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/codehaus/plexus/util/MatchPattern.java b/src/main/java/org/codehaus/plexus/util/MatchPattern.java index 4c276f3b..93381c51 100644 --- a/src/main/java/org/codehaus/plexus/util/MatchPattern.java +++ b/src/main/java/org/codehaus/plexus/util/MatchPattern.java @@ -63,7 +63,7 @@ private MatchPattern(String source, String separator) { * @return the source string without Ant or Regex pattern markers. */ public String getSource() { - return source; + return regexPattern == null ? source : regexPattern; } public boolean matchPath(String str, boolean isCaseSensitive) { diff --git a/src/test/java/org/codehaus/plexus/util/MatchPatternTest.java b/src/test/java/org/codehaus/plexus/util/MatchPatternTest.java index 7dbef6b3..809b8ad4 100644 --- a/src/test/java/org/codehaus/plexus/util/MatchPatternTest.java +++ b/src/test/java/org/codehaus/plexus/util/MatchPatternTest.java @@ -18,6 +18,8 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -29,6 +31,20 @@ * @since 3.4.0 */ public class MatchPatternTest { + + /** + *

testGetSource

+ */ + @Test + public void testGetSource() { + MatchPattern mp = MatchPattern.fromString("ABC*"); + assertEquals("ABC*", mp.getSource()); + mp = MatchPattern.fromString("%ant[some/ABC*]"); + assertEquals("some/ABC*", mp.getSource()); + mp = MatchPattern.fromString("%regex[[ABC].*]"); + assertEquals("[ABC].*", mp.getSource()); + } + /** *

testMatchPath.

* @@ -58,4 +74,20 @@ public void testMatchPatternStart() { assertFalse(mp.matchPatternStart("XXXX", true)); assertFalse(mp.matchPatternStart("XXXX", false)); } + + /** + *

testTokenizePathToString.

+ */ + @Test + public void testTokenizePathToString() { + String[] expected = {"hello", "world"}; + String[] actual = MatchPattern.tokenizePathToString("hello/world", "/"); + assertArrayEquals(expected, actual); + + actual = MatchPattern.tokenizePathToString("/hello/world", "/"); + assertArrayEquals(expected, actual); + + actual = MatchPattern.tokenizePathToString("/hello/world/", "/"); + assertArrayEquals(expected, actual); + } } diff --git a/src/test/java/org/codehaus/plexus/util/MatchPatternsTest.java b/src/test/java/org/codehaus/plexus/util/MatchPatternsTest.java index 47074f8f..b31b5b2f 100644 --- a/src/test/java/org/codehaus/plexus/util/MatchPatternsTest.java +++ b/src/test/java/org/codehaus/plexus/util/MatchPatternsTest.java @@ -16,8 +16,12 @@ * limitations under the License. */ +import java.util.Arrays; +import java.util.List; + import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -29,6 +33,17 @@ * @since 3.4.0 */ public class MatchPatternsTest { + /** + *

testGetSource

+ */ + @Test + public void testGetSources() { + List expected = Arrays.asList("ABC**", "some/ABC*", "[ABC].*"); + MatchPatterns from = MatchPatterns.from("ABC**", "%ant[some/ABC*]", "%regex[[ABC].*]"); + List actual = from.getSources(); + assertEquals(expected, actual); + } + /** *

testMatches.

* diff --git a/src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java b/src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java index 21a10e2f..d48759de 100644 --- a/src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java +++ b/src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -30,6 +31,41 @@ * @since 3.4.0 */ public class SelectorUtilsTest { + /** + *

testExtractPattern.

+ */ + @Test + public void testExtractPattern() { + assertEquals("[A-Z].*", SelectorUtils.extractPattern("%regex[[A-Z].*]", "/")); + assertEquals("ABC*", SelectorUtils.extractPattern("%ant[ABC*]", "/")); + assertEquals("some/ABC*", SelectorUtils.extractPattern("%ant[some/ABC*]", "/")); + assertEquals("some\\ABC*", SelectorUtils.extractPattern("%ant[some\\ABC*]", "\\")); + assertEquals("some/ABC*", SelectorUtils.extractPattern("%ant[some\\ABC*]", "/")); + assertEquals("some\\ABC*", SelectorUtils.extractPattern("%ant[some/ABC*]", "\\")); + } + + /** + *

testIsAntPrefixedPattern.

+ */ + @Test + public void testIsAntPrefixedPattern() { + assertFalse(SelectorUtils.isAntPrefixedPattern("%ant[A]")); // single char not allowed + assertTrue(SelectorUtils.isAntPrefixedPattern("%ant[AB]")); + assertFalse(SelectorUtils.isAntPrefixedPattern("%ant[]")); + assertFalse(SelectorUtils.isAntPrefixedPattern("*")); + } + + /** + *

testIsRegexPrefixedPattern.

+ */ + @Test + public void testIsRegexPrefixedPattern() { + assertFalse(SelectorUtils.isRegexPrefixedPattern("%regex[A]")); // single char not allowed + assertTrue(SelectorUtils.isRegexPrefixedPattern("%regex[.*]")); + assertFalse(SelectorUtils.isRegexPrefixedPattern("%regex[]")); + assertFalse(SelectorUtils.isRegexPrefixedPattern("*")); + } + /** *

testMatchPath_DefaultFileSeparator.

*/