Skip to content

Commit d0b12c6

Browse files
belingueresmichael-o
authored andcommitted
DirectoryScanner traverses directories despite limited inclusions (#63)
This closes #64 and fixes #63
1 parent 59cf121 commit d0b12c6

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public boolean matchPatternStart( String str, boolean isCaseSensitive )
9494
}
9595
else
9696
{
97-
String altStr = source.replace( '\\', '/' );
97+
String altStr = str.replace( '\\', '/' );
9898

9999
return SelectorUtils.matchAntPathPatternStart( this, str, File.separator, isCaseSensitive )
100100
|| SelectorUtils.matchAntPathPatternStart( this, altStr, "/", isCaseSensitive );

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
import java.nio.file.Paths;
3232
import java.util.ArrayList;
3333
import java.util.Arrays;
34+
import java.util.HashSet;
3435
import java.util.List;
36+
import java.util.Set;
3537

38+
import org.junit.Before;
3639
import org.junit.Rule;
3740
import org.junit.Test;
3841
import org.junit.rules.TestName;
@@ -50,6 +53,19 @@ public class DirectoryScannerTest
5053

5154
private static String testDir = getTestDirectory().getPath();
5255

56+
@Before
57+
public void setUp()
58+
{
59+
try
60+
{
61+
FileUtils.deleteDirectory( testDir );
62+
}
63+
catch ( IOException e )
64+
{
65+
fail( "Could not delete directory " + testDir );
66+
}
67+
}
68+
5369
@Test
5470
public void testCrossPlatformIncludesString()
5571
throws IOException, URISyntaxException
@@ -477,6 +493,75 @@ public void testRegexWithSlashInsideCharacterClass()
477493
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
478494
}
479495

496+
/**
497+
* Test that the directory scanning does not enter into not matching directories.
498+
*
499+
* @see <a href="https://github.com/codehaus-plexus/plexus-utils/issues/63">Issue #63</a>
500+
* @throws IOException if occurs an I/O error.
501+
*/
502+
@Test
503+
public void testDoNotScanUnnecesaryDirectories()
504+
throws IOException
505+
{
506+
createTestDirectories();
507+
508+
// create additional directories 'anotherDir1', 'anotherDir2' and 'anotherDir3' with a 'file1.dat' file
509+
FileUtils.mkdir( testDir + File.separator + "directoryTest" + File.separator + "testDir123" + File.separator
510+
+ "anotherDir1" );
511+
FileUtils.mkdir( testDir + File.separator + "directoryTest" + File.separator + "test_dir_123" + File.separator
512+
+ "anotherDir2" );
513+
FileUtils.mkdir( testDir + File.separator + "directoryTest" + File.separator + "test-dir-123" + File.separator
514+
+ "anotherDir3" );
515+
516+
this.createFile( new File( testDir + File.separator + "directoryTest" + File.separator + "testDir123"
517+
+ File.separator + "anotherDir1" + File.separator + "file1.dat" ), 0 );
518+
this.createFile( new File( testDir + File.separator + "directoryTest" + File.separator + "test_dir_123"
519+
+ File.separator + "anotherDir2" + File.separator + "file1.dat" ), 0 );
520+
this.createFile( new File( testDir + File.separator + "directoryTest" + File.separator + "test-dir-123"
521+
+ File.separator + "anotherDir3" + File.separator + "file1.dat" ), 0 );
522+
523+
String[] excludedPaths = {
524+
"directoryTest" + File.separator + "testDir123" + File.separator + "anotherDir1" + File.separator
525+
+ "file1.dat",
526+
"directoryTest" + File.separator + "test_dir_123" + File.separator + "anotherDir2" + File.separator
527+
+ "file1.dat",
528+
"directoryTest" + File.separator + "test-dir-123" + File.separator + "anotherDir3" + File.separator
529+
+ "file1.dat"
530+
};
531+
532+
String[] includedPaths = {
533+
"directoryTest" + File.separator + "testDir123" + File.separator + "file1.dat",
534+
"directoryTest" + File.separator + "test_dir_123" + File.separator + "file1.dat",
535+
"directoryTest" + File.separator + "test-dir-123" + File.separator + "file1.dat"
536+
};
537+
538+
final Set<String> scannedDirSet = new HashSet<String>();
539+
540+
DirectoryScanner ds = new DirectoryScanner()
541+
{
542+
@Override
543+
protected void scandir( File dir, String vpath, boolean fast )
544+
{
545+
scannedDirSet.add( dir.getName() );
546+
super.scandir( dir, vpath, fast );
547+
}
548+
549+
};
550+
551+
// one '*' matches only ONE directory level
552+
String[] includes = { "directoryTest" + File.separator + "*" + File.separator + "file1.dat" };
553+
ds.setIncludes( includes );
554+
ds.setBasedir( new File( testDir ) );
555+
ds.scan();
556+
557+
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
558+
559+
Set<String> expectedScannedDirSet =
560+
new HashSet<String>( Arrays.asList( "io", "directoryTest", "testDir123", "test_dir_123", "test-dir-123" ) );
561+
562+
assertEquals( expectedScannedDirSet, scannedDirSet );
563+
}
564+
480565
@Test
481566
public void testIsSymbolicLink()
482567
throws IOException

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import static org.junit.Assert.assertFalse;
1920
import static org.junit.Assert.assertTrue;
2021

2122
import org.junit.Test;
@@ -32,4 +33,23 @@ public void testMatchPath()
3233
MatchPattern mp = MatchPattern.fromString( "ABC*" );
3334
assertTrue( mp.matchPath( "ABCD", true ) );
3435
}
36+
37+
/**
38+
* @see <a href="https://github.com/codehaus-plexus/plexus-utils/issues/63">Issue #63</a>
39+
*/
40+
@Test
41+
public void testMatchPatternStart()
42+
{
43+
MatchPattern mp = MatchPattern.fromString( "ABC*" );
44+
45+
assertTrue( mp.matchPatternStart( "ABCD", true ) );
46+
assertFalse( mp.matchPatternStart( "AbCD", true ) );
47+
48+
assertTrue( mp.matchPatternStart( "ABCD", false ) );
49+
assertTrue( mp.matchPatternStart( "AbCD", false ) );
50+
51+
assertFalse( mp.matchPatternStart( "XXXX", true ) );
52+
assertFalse( mp.matchPatternStart( "XXXX", false ) );
53+
}
54+
3555
}

0 commit comments

Comments
 (0)