Skip to content

Commit 138c036

Browse files
committed
1 parent 3896620 commit 138c036

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,23 +251,38 @@ public static boolean matchPath( String pattern, String str, boolean isCaseSensi
251251

252252
public static boolean matchPath( String pattern, String str, String separator, boolean isCaseSensitive )
253253
{
254-
if ( isRegexPrefixedPattern( pattern ) )
254+
/* 2022-03-30: Fixed: Caller parameter in a public method should be handled immutable. */
255+
String localPattern = pattern;
256+
if ( isRegexPrefixedPattern( localPattern ) )
255257
{
256-
pattern =
257-
pattern.substring( REGEX_HANDLER_PREFIX.length(), pattern.length() - PATTERN_HANDLER_SUFFIX.length() );
258+
localPattern =
259+
localPattern.substring( REGEX_HANDLER_PREFIX.length(), localPattern.length() - PATTERN_HANDLER_SUFFIX.length() );
258260

259-
return str.matches( pattern );
261+
return str.matches( localPattern );
260262
}
261263
else
262264
{
263-
if ( isAntPrefixedPattern( pattern ) )
265+
if ( isAntPrefixedPattern( localPattern ) )
264266
{
265-
pattern = pattern.substring( ANT_HANDLER_PREFIX.length(),
266-
pattern.length() - PATTERN_HANDLER_SUFFIX.length() );
267+
localPattern = localPattern.substring( ANT_HANDLER_PREFIX.length(),
268+
localPattern.length() - PATTERN_HANDLER_SUFFIX.length() );
267269
}
270+
final String osRelatedPath = toOSRelatedPath( str, separator );
271+
final String osRelatedPattern = toOSRelatedPath( localPattern, separator );
272+
return matchAntPathPattern( osRelatedPattern, osRelatedPath, separator, isCaseSensitive );
273+
}
274+
}
268275

269-
return matchAntPathPattern( pattern, str, separator, isCaseSensitive );
276+
private static String toOSRelatedPath( String pattern, String separator )
277+
{
278+
if ( "/".equals( separator ) )
279+
{
280+
return pattern.replace( "\\", separator );
281+
}
282+
if ( "\\".equals( separator ) ) {
283+
return pattern.replace( "/", separator );
270284
}
285+
return pattern;
271286
}
272287

273288
static boolean isRegexPrefixedPattern( String pattern )

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,55 @@ public void testMatchPath_WindowsFileSeparator()
9292
// Pattern and target don't start with file separator
9393
assertTrue( SelectorUtils.matchPath( "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false ) );
9494
}
95+
96+
@Test
97+
public void testPatternMatchSingleWildCardLinux()
98+
{
99+
assertFalse(SelectorUtils.matchPath(
100+
"/com/test/*",
101+
"/com/test/test/hallo"));
102+
}
103+
104+
105+
@Test
106+
public void testPatternMatchDoubleWildCardCaseInLinux()
107+
{
108+
assertTrue(SelectorUtils.matchPath(
109+
"/com/test/**",
110+
"/com/test/test/hallo"));
111+
}
112+
113+
114+
@Test
115+
public void testPatternMatchDoubleWildCardLinux()
116+
{
117+
assertTrue(SelectorUtils.matchPath(
118+
"/com/test/**",
119+
"/com/test/test/hallo"));
120+
}
121+
122+
123+
@Test
124+
public void testPatternMatchSingleWildCardWindows()
125+
{
126+
assertFalse(SelectorUtils.matchPath(
127+
"D:\\com\\test\\*",
128+
"D:\\com\\test\\test\\hallo"));
129+
130+
assertFalse(SelectorUtils.matchPath(
131+
"D:/com/test/*",
132+
"D:/com/test/test/hallo"));
133+
}
134+
135+
@Test
136+
public void testPatternMatchDoubleWildCardWindows()
137+
{
138+
assertTrue(SelectorUtils.matchPath(
139+
"D:\\com\\test\\**",
140+
"D:\\com\\test\\test\\hallo"));
141+
142+
assertTrue(SelectorUtils.matchPath(
143+
"D:\\com\\test\\**",
144+
"D:/com/test/test/hallo"));
145+
}
95146
}

0 commit comments

Comments
 (0)