21
21
import java .net .URI ;
22
22
import java .net .URISyntaxException ;
23
23
import java .net .URL ;
24
+ import java .nio .file .Files ;
25
+ import java .nio .file .Paths ;
24
26
import java .util .ArrayList ;
25
27
import java .util .Arrays ;
26
28
import java .util .List ;
33
35
public class DirectoryScannerTest
34
36
extends FileBasedTestCase
35
37
{
38
+
36
39
private static String testDir = getTestDirectory ().getPath ();
37
40
38
41
public void testCrossPlatformIncludesString ()
@@ -110,6 +113,44 @@ private void createTestFiles()
110
113
this .createFile ( new File ( testDir + "/scanner5.dat" ), 0 );
111
114
}
112
115
116
+ /**
117
+ * Check if 'src/test/resources/symlinks/src/sym*' test files (start with 'sym') exist and are symlinks.<br>
118
+ * On some OS (like Windows 10), the 'git clone' requires to be executed with admin permissions and the
119
+ * 'core.symlinks=true' git option.
120
+ *
121
+ * @return true If files here and symlinks, false otherwise
122
+ */
123
+ private boolean checkTestFilesSymlinks ()
124
+ {
125
+ File symlinksDirectory = new File ( "src/test/resources/symlinks/src" );
126
+ try
127
+ {
128
+ List <String > symlinks =
129
+ FileUtils .getFileAndDirectoryNames ( symlinksDirectory , "sym*" , null , true , true , true , true );
130
+ if ( symlinks .isEmpty () )
131
+ {
132
+ throw new IOException ( "Symlinks files/directories are not present" );
133
+ }
134
+ for ( String symLink : symlinks )
135
+ {
136
+ if ( !Files .isSymbolicLink ( Paths .get ( symLink ) ) )
137
+ {
138
+ throw new IOException ( String .format ( "Path is not a symlink: %s" , symLink ) );
139
+ }
140
+ }
141
+ return true ;
142
+ }
143
+ catch ( IOException e )
144
+ {
145
+ System .err .println ( String .format ( "The unit test '%s.%s' will be skipped, reason: %s" ,
146
+ this .getClass ().getSimpleName (), this .getName (), e .getMessage () ) );
147
+ System .out .println ( String .format ( "This test requires symlinks files in '%s' directory." ,
148
+ symlinksDirectory .getPath () ) );
149
+ System .out .println ( "On some OS (like Windows 10), files are present only if the clone/checkout is done in administrator mode, and correct (symlinks and not flat file/directory) if symlinks option are used (for git: git clone -c core.symlinks=true [url])" );
150
+ return false ;
151
+ }
152
+ }
153
+
113
154
public void testGeneral ()
114
155
throws IOException
115
156
{
@@ -146,6 +187,10 @@ public void testIncludesExcludesWithWhiteSpaces()
146
187
147
188
public void testFollowSymlinksFalse ()
148
189
{
190
+ if ( !checkTestFilesSymlinks () )
191
+ {
192
+ return ;
193
+ }
149
194
DirectoryScanner ds = new DirectoryScanner ();
150
195
ds .setBasedir ( new File ( "src/test/resources/symlinks/src/" ) );
151
196
ds .setFollowSymlinks ( false );
@@ -177,6 +222,10 @@ private void assertAlwaysIncluded( List<String> included )
177
222
178
223
public void testFollowSymlinks ()
179
224
{
225
+ if ( !checkTestFilesSymlinks () )
226
+ {
227
+ return ;
228
+ }
180
229
DirectoryScanner ds = new DirectoryScanner ();
181
230
ds .setBasedir ( new File ( "src/test/resources/symlinks/src/" ) );
182
231
ds .setFollowSymlinks ( true );
@@ -446,8 +495,7 @@ private void assertInclusionsAndExclusions( String[] files, String[] excludedPat
446
495
StringBuilder buffer = new StringBuilder ();
447
496
if ( !failedToExclude .isEmpty () )
448
497
{
449
- buffer .append ( "Should NOT have included:\n " ).append (
450
- StringUtils .join ( failedToExclude .iterator (),
498
+ buffer .append ( "Should NOT have included:\n " ).append ( StringUtils .join ( failedToExclude .iterator (),
451
499
"\n \t - " ) );
452
500
}
453
501
@@ -458,8 +506,8 @@ private void assertInclusionsAndExclusions( String[] files, String[] excludedPat
458
506
buffer .append ( "\n \n " );
459
507
}
460
508
461
- buffer .append ( "Should have included:\n " )
462
- . append ( StringUtils . join ( failedToInclude . iterator (), "\n \t - " ) );
509
+ buffer .append ( "Should have included:\n " ). append ( StringUtils . join ( failedToInclude . iterator (),
510
+ "\n \t - " ) );
463
511
}
464
512
465
513
if ( buffer .length () > 0 )
0 commit comments