Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4 changes to make external usage easier #278

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/codehaus/plexus/util/MatchPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ private MatchPattern(String source, String separator) {
}
}

/**
* Gets the source pattern for this match pattern.
* @return the source string without Ant or Regex pattern markers.
*/
public String getSource() {
return regexPattern == null ? source : regexPattern;
}

public boolean matchPath(String str, boolean isCaseSensitive) {
if (regexPattern != null) {
return str.matches(regexPattern);
Expand Down Expand Up @@ -99,7 +107,7 @@ public boolean startsWith(String string) {
return source.startsWith(string);
}

static String[] tokenizePathToString(String path, String separator) {
public static String[] tokenizePathToString(String path, String separator) {
List<String> ret = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(path, separator);
while (st.hasMoreTokens()) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/codehaus/plexus/util/MatchPatterns.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ private MatchPatterns(MatchPattern[] patterns) {
this.patterns = patterns;
}

/**
* Gets a list of enclosed MatchPattern sources.
* @return A list of enclosed MatchPattern sources.
*/
public List<String> getSources() {
List<String> sources = new ArrayList<>();
for (MatchPattern pattern : patterns) {
sources.add(pattern.getSource());
}
return sources;
}

/**
* <p>Checks these MatchPatterns against a specified string.</p>
*
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/org/codehaus/plexus/util/SelectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public static boolean matchPatternStart(String pattern, String str, boolean isCa
}
}

static boolean isAntPrefixedPattern(String pattern) {
return pattern.length() > (ANT_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length() + 1)
public static boolean isAntPrefixedPattern(String pattern) {
return pattern.length() > (ANT_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length())
&& pattern.startsWith(ANT_HANDLER_PREFIX)
&& pattern.endsWith(PATTERN_HANDLER_SUFFIX);
}
Expand Down Expand Up @@ -253,8 +253,8 @@ private static String toOSRelatedPath(String pattern, String separator) {
return pattern;
}

static boolean isRegexPrefixedPattern(String pattern) {
return pattern.length() > (REGEX_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length() + 1)
public static boolean isRegexPrefixedPattern(String pattern) {
return pattern.length() > (REGEX_HANDLER_PREFIX.length() + PATTERN_HANDLER_SUFFIX.length())
&& pattern.startsWith(REGEX_HANDLER_PREFIX)
&& pattern.endsWith(PATTERN_HANDLER_SUFFIX);
}
Expand Down Expand Up @@ -708,4 +708,22 @@ public static String removeWhitespace(String input) {
}
return result.toString();
}

/**
* Extract the pattern without the Regex or Ant prefix. In the case of Ant style matches ensure
* that the path uses specified separator.
* @param pattern the pattern to extract from.
* @param separator the system file name separator in the pattern.
* @return The pattern without the Regex or Ant prefix.
*/
public static String extractPattern(final String pattern, final String separator) {
if (isRegexPrefixedPattern(pattern)) {
return pattern.substring(REGEX_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length());
} else {
String localPattern = isAntPrefixedPattern(pattern)
? pattern.substring(ANT_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length())
: pattern;
return toOSRelatedPath(localPattern, separator);
}
}
}
32 changes: 32 additions & 0 deletions src/test/java/org/codehaus/plexus/util/MatchPatternTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +31,20 @@
* @since 3.4.0
*/
public class MatchPatternTest {

/**
* <p>testGetSource</p>
*/
@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());
}

/**
* <p>testMatchPath.</p>
*
Expand Down Expand Up @@ -58,4 +74,20 @@ public void testMatchPatternStart() {
assertFalse(mp.matchPatternStart("XXXX", true));
assertFalse(mp.matchPatternStart("XXXX", false));
}

/**
* <p>testTokenizePathToString.</p>
*/
@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);
}
}
15 changes: 15 additions & 0 deletions src/test/java/org/codehaus/plexus/util/MatchPatternsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +33,17 @@
* @since 3.4.0
*/
public class MatchPatternsTest {
/**
* <p>testGetSource</p>
*/
@Test
public void testGetSources() {
List<String> expected = Arrays.asList("ABC**", "some/ABC*", "[ABC].*");
MatchPatterns from = MatchPatterns.from("ABC**", "%ant[some/ABC*]", "%regex[[ABC].*]");
List<String> actual = from.getSources();
assertEquals(expected, actual);
}

/**
* <p>testMatches.</p>
*
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,6 +31,41 @@
* @since 3.4.0
*/
public class SelectorUtilsTest {
/**
* <p>testExtractPattern.</p>
*/
@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*]", "\\"));
}

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

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

/**
* <p>testMatchPath_DefaultFileSeparator.</p>
*/
Expand Down