Skip to content

Commit

Permalink
added tests, fixed one bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudenw authored and slachiewicz committed Nov 5, 2024
1 parent 2b68c66 commit 1f55ae1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/plexus/util/MatchPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
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() {
assertFalse(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() {
assertFalse(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

0 comments on commit 1f55ae1

Please sign in to comment.