/
DirectoryScannerTest.java
552 lines (444 loc) · 18.6 KB
/
DirectoryScannerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package org.codehaus.plexus.util;
/*
* Copyright The Codehaus Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
/**
* Base class for testcases doing tests with files.
*
* @author Dan T. Tran
*/
public class DirectoryScannerTest
extends FileBasedTestCase
{
private static String testDir = getTestDirectory().getPath();
@Test
public void testCrossPlatformIncludesString()
throws IOException, URISyntaxException
{
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir( new File( getTestResourcesDir() + File.separator + "directory-scanner" ).getCanonicalFile() );
String fs;
if ( File.separatorChar == '/' )
{
fs = "\\";
}
else
{
fs = "/";
}
ds.setIncludes( new String[] { "foo" + fs } );
ds.addDefaultExcludes();
ds.scan();
String[] files = ds.getIncludedFiles();
assertEquals( 1, files.length );
}
@Test
public void testCrossPlatformExcludesString()
throws IOException, URISyntaxException
{
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir( new File( getTestResourcesDir() + File.separator + "directory-scanner" ).getCanonicalFile() );
ds.setIncludes( new String[] { "**" } );
String fs;
if ( File.separatorChar == '/' )
{
fs = "\\";
}
else
{
fs = "/";
}
ds.setExcludes( new String[] { "foo" + fs } );
ds.addDefaultExcludes();
ds.scan();
String[] files = ds.getIncludedFiles();
assertEquals( 0, files.length );
}
private String getTestResourcesDir()
throws URISyntaxException
{
ClassLoader cloader = Thread.currentThread().getContextClassLoader();
URL resource = cloader.getResource( "test.txt" );
if ( resource == null )
{
fail( "Cannot locate test-resources directory containing 'test.txt' in the classloader." );
}
File file = new File( new URI( resource.toExternalForm() ).normalize().getPath() );
return file.getParent();
}
private void createTestFiles()
throws IOException
{
FileUtils.mkdir( testDir );
this.createFile( new File( testDir + "/scanner1.dat" ), 0 );
this.createFile( new File( testDir + "/scanner2.dat" ), 0 );
this.createFile( new File( testDir + "/scanner3.dat" ), 0 );
this.createFile( new File( testDir + "/scanner4.dat" ), 0 );
this.createFile( new File( testDir + "/scanner5.dat" ), 0 );
}
@Test
public void testGeneral()
throws IOException
{
this.createTestFiles();
String includes = "scanner1.dat,scanner2.dat,scanner3.dat,scanner4.dat,scanner5.dat";
String excludes = "scanner1.dat,scanner2.dat";
List<File> fileNames = FileUtils.getFiles( new File( testDir ), includes, excludes, false );
assertEquals( "Wrong number of results.", 3, fileNames.size() );
assertTrue( "3 not found.", fileNames.contains( new File( "scanner3.dat" ) ) );
assertTrue( "4 not found.", fileNames.contains( new File( "scanner4.dat" ) ) );
assertTrue( "5 not found.", fileNames.contains( new File( "scanner5.dat" ) ) );
}
@Test
public void testIncludesExcludesWithWhiteSpaces()
throws IOException
{
this.createTestFiles();
String includes = "scanner1.dat,\n \n,scanner2.dat \n\r, scanner3.dat\n, \tscanner4.dat,scanner5.dat\n,";
String excludes = "scanner1.dat,\n \n,scanner2.dat \n\r,,";
List<File> fileNames = FileUtils.getFiles( new File( testDir ), includes, excludes, false );
assertEquals( "Wrong number of results.", 3, fileNames.size() );
assertTrue( "3 not found.", fileNames.contains( new File( "scanner3.dat" ) ) );
assertTrue( "4 not found.", fileNames.contains( new File( "scanner4.dat" ) ) );
assertTrue( "5 not found.", fileNames.contains( new File( "scanner5.dat" ) ) );
}
@Test
public void testFollowSymlinksFalse()
{
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir( new File( "src/test/resources/symlinks/src/" ) );
ds.setFollowSymlinks( false );
ds.scan();
List<String> included = Arrays.asList( ds.getIncludedFiles() );
assertAlwaysIncluded( included );
assertEquals( 9, included.size() );
List<String> includedDirs = Arrays.asList( ds.getIncludedDirectories() );
assertTrue( includedDirs.contains( "" ) ); // w00t !
assertTrue( includedDirs.contains( "aRegularDir" ) );
assertTrue( includedDirs.contains( "symDir" ) );
assertTrue( includedDirs.contains( "symLinkToDirOnTheOutside" ) );
assertTrue( includedDirs.contains( "targetDir" ) );
assertEquals( 5, includedDirs.size() );
}
private void assertAlwaysIncluded( List<String> included )
{
assertTrue( included.contains( "aRegularDir" + File.separator + "aRegularFile.txt" ) );
assertTrue( included.contains( "targetDir" + File.separator + "targetFile.txt" ) );
assertTrue( included.contains( "fileR.txt" ) );
assertTrue( included.contains( "fileW.txt" ) );
assertTrue( included.contains( "fileX.txt" ) );
assertTrue( included.contains( "symR" ) );
assertTrue( included.contains( "symW" ) );
assertTrue( included.contains( "symX" ) );
assertTrue( included.contains( "symLinkToFileOnTheOutside" ) );
}
@Test
public void testFollowSymlinks()
{
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir( new File( "src/test/resources/symlinks/src/" ) );
ds.setFollowSymlinks( true );
ds.scan();
List<String> included = Arrays.asList( ds.getIncludedFiles() );
assertAlwaysIncluded( included );
assertTrue( included.contains( "symDir" + File.separator + "targetFile.txt" ) );
assertTrue( included.contains( "symLinkToDirOnTheOutside" + File.separator + "FileInDirOnTheOutside.txt" ) );
assertEquals( 11, included.size() );
List<String> includedDirs = Arrays.asList( ds.getIncludedDirectories() );
assertTrue( includedDirs.contains( "" ) ); // w00t !
assertTrue( includedDirs.contains( "aRegularDir" ) );
assertTrue( includedDirs.contains( "symDir" ) );
assertTrue( includedDirs.contains( "symLinkToDirOnTheOutside" ) );
assertTrue( includedDirs.contains( "targetDir" ) );
assertEquals( 5, includedDirs.size() );
}
private void createTestDirectories()
throws IOException
{
FileUtils.mkdir( testDir + File.separator + "directoryTest" );
FileUtils.mkdir( testDir + File.separator + "directoryTest" + File.separator + "testDir123" );
FileUtils.mkdir( testDir + File.separator + "directoryTest" + File.separator + "test_dir_123" );
FileUtils.mkdir( testDir + File.separator + "directoryTest" + File.separator + "test-dir-123" );
this.createFile( new File( testDir + File.separator + "directoryTest" + File.separator + "testDir123"
+ File.separator + "file1.dat" ), 0 );
this.createFile( new File( testDir + File.separator + "directoryTest" + File.separator + "test_dir_123"
+ File.separator + "file1.dat" ), 0 );
this.createFile( new File( testDir + File.separator + "directoryTest" + File.separator + "test-dir-123"
+ File.separator + "file1.dat" ), 0 );
}
@Test
public void testDirectoriesWithHyphens()
throws IOException
{
this.createTestDirectories();
DirectoryScanner ds = new DirectoryScanner();
String[] includes = { "**/*.dat" };
String[] excludes = { "" };
ds.setIncludes( includes );
ds.setExcludes( excludes );
ds.setBasedir( new File( testDir + File.separator + "directoryTest" ) );
ds.setCaseSensitive( true );
ds.scan();
String[] files = ds.getIncludedFiles();
assertEquals( "Wrong number of results.", 3, files.length );
}
@Test
public void testAntExcludesOverrideIncludes()
throws IOException
{
printTestHeader();
File dir = new File( testDir, "regex-dir" );
dir.mkdirs();
String[] excludedPaths = { "target/foo.txt" };
createFiles( dir, excludedPaths );
String[] includedPaths = { "src/main/resources/project/target/foo.txt" };
createFiles( dir, includedPaths );
DirectoryScanner ds = new DirectoryScanner();
String[] includes = { "**/target/*" };
String[] excludes = { "target/*" };
// This doesn't work, since excluded patterns refine included ones, meaning they operate on
// the list of paths that passed the included patterns, and can override them.
// String[] includes = {"**src/**/target/**/*" };
// String[] excludes = { "**/target/**/*" };
ds.setIncludes( includes );
ds.setExcludes( excludes );
ds.setBasedir( dir );
ds.scan();
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
}
@Test
public void testAntExcludesOverrideIncludesWithExplicitAntPrefix()
throws IOException
{
printTestHeader();
File dir = new File( testDir, "regex-dir" );
dir.mkdirs();
String[] excludedPaths = { "target/foo.txt" };
createFiles( dir, excludedPaths );
String[] includedPaths = { "src/main/resources/project/target/foo.txt" };
createFiles( dir, includedPaths );
DirectoryScanner ds = new DirectoryScanner();
String[] includes =
{ SelectorUtils.ANT_HANDLER_PREFIX + "**/target/**/*" + SelectorUtils.PATTERN_HANDLER_SUFFIX };
String[] excludes = { SelectorUtils.ANT_HANDLER_PREFIX + "target/**/*" + SelectorUtils.PATTERN_HANDLER_SUFFIX };
// This doesn't work, since excluded patterns refine included ones, meaning they operate on
// the list of paths that passed the included patterns, and can override them.
// String[] includes = {"**src/**/target/**/*" };
// String[] excludes = { "**/target/**/*" };
ds.setIncludes( includes );
ds.setExcludes( excludes );
ds.setBasedir( dir );
ds.scan();
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
}
@Test
public void testRegexIncludeWithExcludedPrefixDirs()
throws IOException
{
printTestHeader();
File dir = new File( testDir, "regex-dir" );
dir.mkdirs();
String[] excludedPaths = { "src/main/foo.txt" };
createFiles( dir, excludedPaths );
String[] includedPaths = { "src/main/resources/project/target/foo.txt" };
createFiles( dir, includedPaths );
String regex = ".+/target.*";
DirectoryScanner ds = new DirectoryScanner();
String includeExpr = SelectorUtils.REGEX_HANDLER_PREFIX + regex + SelectorUtils.PATTERN_HANDLER_SUFFIX;
String[] includes = { includeExpr };
ds.setIncludes( includes );
ds.setBasedir( dir );
ds.scan();
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
}
@Test
public void testRegexExcludeWithNegativeLookahead()
throws IOException
{
printTestHeader();
File dir = new File( testDir, "regex-dir" );
try
{
FileUtils.deleteDirectory( dir );
}
catch ( IOException e )
{
}
dir.mkdirs();
String[] excludedPaths = { "target/foo.txt" };
createFiles( dir, excludedPaths );
String[] includedPaths = { "src/main/resources/project/target/foo.txt" };
createFiles( dir, includedPaths );
String regex = "(?!.*src/).*target.*";
DirectoryScanner ds = new DirectoryScanner();
String excludeExpr = SelectorUtils.REGEX_HANDLER_PREFIX + regex + SelectorUtils.PATTERN_HANDLER_SUFFIX;
String[] excludes = { excludeExpr };
ds.setExcludes( excludes );
ds.setBasedir( dir );
ds.scan();
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
}
@Test
public void testRegexWithSlashInsideCharacterClass()
throws IOException
{
printTestHeader();
File dir = new File( testDir, "regex-dir" );
try
{
FileUtils.deleteDirectory( dir );
}
catch ( IOException e )
{
}
dir.mkdirs();
String[] excludedPaths = { "target/foo.txt", "target/src/main/target/foo.txt" };
createFiles( dir, excludedPaths );
String[] includedPaths = { "module/src/main/target/foo.txt" };
createFiles( dir, includedPaths );
// NOTE: The portion "[^/]" is the interesting part of this pattern.
String regex = "(?!((?!target/)[^/]+/)*src/).*target.*";
DirectoryScanner ds = new DirectoryScanner();
String excludeExpr = SelectorUtils.REGEX_HANDLER_PREFIX + regex + SelectorUtils.PATTERN_HANDLER_SUFFIX;
String[] excludes = { excludeExpr };
ds.setExcludes( excludes );
ds.setBasedir( dir );
ds.scan();
assertInclusionsAndExclusions( ds.getIncludedFiles(), excludedPaths, includedPaths );
}
@Test
public void testIsSymbolicLink()
throws IOException
{
// TODO: Uncomment when PR #25 merged
// if ( !checkTestFilesSymlinks() )
// {
// return;
// }
final File directory = new File( "src/test/resources/symlinks/src" );
DirectoryScanner ds = new DirectoryScanner();
assertTrue( ds.isSymbolicLink( directory, "symR" ) );
assertTrue( ds.isSymbolicLink( directory, "symDir" ) );
assertFalse( ds.isSymbolicLink( directory, "fileR.txt" ) );
assertFalse( ds.isSymbolicLink( directory, "aRegularDir" ) );
}
@Test
public void testIsParentSymbolicLink()
throws IOException
{
// TODO: Uncomment when PR #25 merged
// if ( !checkTestFilesSymlinks() )
// {
// return;
// }
final File directory = new File( "src/test/resources/symlinks/src" );
DirectoryScanner ds = new DirectoryScanner();
assertFalse( ds.isParentSymbolicLink( directory, "symR" ) );
assertFalse( ds.isParentSymbolicLink( directory, "symDir" ) );
assertFalse( ds.isParentSymbolicLink( directory, "fileR.txt" ) );
assertFalse( ds.isParentSymbolicLink( directory, "aRegularDir" ) );
assertFalse( ds.isParentSymbolicLink( new File( directory, "aRegularDir" ), "aRegulatFile.txt" ) );
assertTrue( ds.isParentSymbolicLink( new File( directory, "symDir" ), "targetFile.txt" ) );
assertTrue( ds.isParentSymbolicLink( new File( directory, "symLinkToDirOnTheOutside" ),
"FileInDirOnTheOutside.txt" ) );
}
private void printTestHeader()
{
StackTraceElement ste = new Throwable().getStackTrace()[1];
System.out.println( "Test: " + ste.getMethodName() );
}
private void assertInclusionsAndExclusions( String[] files, String[] excludedPaths, String... includedPaths )
{
Arrays.sort( files );
System.out.println( "Included files: " );
for ( String file : files )
{
System.out.println( file );
}
List<String> failedToExclude = new ArrayList<String>();
for ( String excludedPath : excludedPaths )
{
String alt = excludedPath.replace( '/', '\\' );
System.out.println( "Searching for exclusion as: " + excludedPath + "\nor: " + alt );
if ( Arrays.binarySearch( files, excludedPath ) > -1 || Arrays.binarySearch( files, alt ) > -1 )
{
failedToExclude.add( excludedPath );
}
}
List<String> failedToInclude = new ArrayList<String>();
for ( String includedPath : includedPaths )
{
String alt = includedPath.replace( '/', '\\' );
System.out.println( "Searching for inclusion as: " + includedPath + "\nor: " + alt );
if ( Arrays.binarySearch( files, includedPath ) < 0 && Arrays.binarySearch( files, alt ) < 0 )
{
failedToInclude.add( includedPath );
}
}
StringBuilder buffer = new StringBuilder();
if ( !failedToExclude.isEmpty() )
{
buffer.append( "Should NOT have included:\n" ).append( StringUtils.join( failedToExclude.iterator(),
"\n\t- " ) );
}
if ( !failedToInclude.isEmpty() )
{
if ( buffer.length() > 0 )
{
buffer.append( "\n\n" );
}
buffer.append( "Should have included:\n" ).append( StringUtils.join( failedToInclude.iterator(),
"\n\t- " ) );
}
if ( buffer.length() > 0 )
{
fail( buffer.toString() );
}
}
private void createFiles( File dir, String... paths )
throws IOException
{
for ( String path1 : paths )
{
String path = path1.replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
File file = new File( dir, path );
if ( path.endsWith( File.separator ) )
{
file.mkdirs();
}
else
{
if ( file.getParentFile() != null )
{
file.getParentFile().mkdirs();
}
createFile( file, 0 );
}
}
}
}