Skip to content

Commit 407a93c

Browse files
fishermansmichael-o
authored andcommitted
SelectorUtils.matchPath(): inconsistent behaviour on POSIX-like and Windows for single Wildcard pattern (#191)
This fixes #191 and closes #192
1 parent 8dec931 commit 407a93c

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

src/main/java/org/codehaus/plexus/util/SelectorUtils.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,32 @@ public static boolean matchPath( String pattern, String str, String separator, b
253253
{
254254
if ( isRegexPrefixedPattern( pattern ) )
255255
{
256-
pattern =
256+
String localPattern =
257257
pattern.substring( REGEX_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length() );
258258

259-
return str.matches( pattern );
259+
return str.matches( localPattern );
260260
}
261261
else
262262
{
263-
if ( isAntPrefixedPattern( pattern ) )
264-
{
265-
pattern = pattern.substring( ANT_HANDLER_PREFIX.length(),
266-
pattern.length() - PATTERN_HANDLER_SUFFIX.length() );
267-
}
263+
String localPattern = isAntPrefixedPattern( pattern )
264+
? pattern.substring( ANT_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length() )
265+
: pattern;
266+
final String osRelatedPath = toOSRelatedPath( str, separator );
267+
final String osRelatedPattern = toOSRelatedPath( localPattern, separator );
268+
return matchAntPathPattern( osRelatedPattern, osRelatedPath, separator, isCaseSensitive );
269+
}
270+
}
268271

269-
return matchAntPathPattern( pattern, str, separator, isCaseSensitive );
272+
private static String toOSRelatedPath( String pattern, String separator )
273+
{
274+
if ( "/".equals( separator ) )
275+
{
276+
return pattern.replace( "\\", separator );
277+
}
278+
if ( "\\".equals( separator ) ) {
279+
return pattern.replace( "/", separator );
270280
}
281+
return pattern;
271282
}
272283

273284
static boolean isRegexPrefixedPattern( String pattern )

src/test/java/org/codehaus/plexus/util/SelectorUtilsTest.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* <p>SelectorUtilsTest class.</p>
2828
*
2929
* @author herve
30-
* @version $Id: $Id
3130
* @since 3.4.0
3231
*/
3332
public class SelectorUtilsTest
@@ -92,4 +91,55 @@ public void testMatchPath_WindowsFileSeparator()
9291
// Pattern and target don't start with file separator
9392
assertTrue( SelectorUtils.matchPath( "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false ) );
9493
}
94+
95+
@Test
96+
public void testPatternMatchSingleWildcardPosix()
97+
{
98+
assertFalse(SelectorUtils.matchPath(
99+
"/com/test/*",
100+
"/com/test/test/hallo"));
101+
}
102+
103+
104+
@Test
105+
public void testPatternMatchDoubleWildcardCaseInsensitivePosix()
106+
{
107+
assertTrue(SelectorUtils.matchPath(
108+
"/com/test/**",
109+
"/com/test/test/hallo"));
110+
}
111+
112+
113+
@Test
114+
public void testPatternMatchDoubleWildcardPosix()
115+
{
116+
assertTrue(SelectorUtils.matchPath(
117+
"/com/test/**",
118+
"/com/test/test/hallo"));
119+
}
120+
121+
122+
@Test
123+
public void testPatternMatchSingleWildcardWindows()
124+
{
125+
assertFalse(SelectorUtils.matchPath(
126+
"D:\\com\\test\\*",
127+
"D:\\com\\test\\test\\hallo"));
128+
129+
assertFalse(SelectorUtils.matchPath(
130+
"D:/com/test/*",
131+
"D:/com/test/test/hallo"));
132+
}
133+
134+
@Test
135+
public void testPatternMatchDoubleWildcardWindows()
136+
{
137+
assertTrue(SelectorUtils.matchPath(
138+
"D:\\com\\test\\**",
139+
"D:\\com\\test\\test\\hallo"));
140+
141+
assertTrue(SelectorUtils.matchPath(
142+
"D:\\com\\test\\**",
143+
"D:/com/test/test/hallo"));
144+
}
95145
}

0 commit comments

Comments
 (0)