2020import static org .junit .Assert .assertFalse ;
2121import static org .junit .Assert .assertTrue ;
2222import static org .junit .Assert .fail ;
23+ import static org .junit .Assume .assumeTrue ;
2324
2425import java .io .File ;
2526import java .io .IOException ;
2627import java .net .URI ;
2728import java .net .URISyntaxException ;
2829import java .net .URL ;
30+ import java .nio .file .Files ;
31+ import java .nio .file .Paths ;
2932import java .util .ArrayList ;
3033import java .util .Arrays ;
3134import java .util .List ;
3235
36+ import org .junit .Rule ;
3337import org .junit .Test ;
38+ import org .junit .rules .TestName ;
3439
3540/**
3641 * Base class for testcases doing tests with files.
4045public class DirectoryScannerTest
4146 extends FileBasedTestCase
4247{
48+ @ Rule
49+ public TestName name = new TestName ();
50+
4351 private static String testDir = getTestDirectory ().getPath ();
4452
4553 @ Test
@@ -118,6 +126,47 @@ private void createTestFiles()
118126 this .createFile ( new File ( testDir + "/scanner4.dat" ), 0 );
119127 this .createFile ( new File ( testDir + "/scanner5.dat" ), 0 );
120128 }
129+
130+ /**
131+ * Check if 'src/test/resources/symlinks/src/sym*' test files (start with 'sym') exist and are symlinks.<br>
132+ * On some OS (like Windows 10), the 'git clone' requires to be executed with admin permissions and the
133+ * 'core.symlinks=true' git option.
134+ *
135+ * @return true If files here and symlinks, false otherwise
136+ */
137+ private boolean checkTestFilesSymlinks ()
138+ {
139+ File symlinksDirectory = new File ( "src/test/resources/symlinks/src" );
140+ try
141+ {
142+ List <String > symlinks =
143+ FileUtils .getFileAndDirectoryNames ( symlinksDirectory , "sym*" , null , true , true , true , true );
144+ if ( symlinks .isEmpty () )
145+ {
146+ throw new IOException ( "Symlinks files/directories are not present" );
147+ }
148+ for ( String symLink : symlinks )
149+ {
150+ if ( !Files .isSymbolicLink ( Paths .get ( symLink ) ) )
151+ {
152+ throw new IOException ( String .format ( "Path is not a symlink: %s" , symLink ) );
153+ }
154+ }
155+ return true ;
156+ }
157+ catch ( IOException e )
158+ {
159+ System .err .println ( String .format ( "The unit test '%s.%s' will be skipped, reason: %s" ,
160+ this .getClass ().getSimpleName (), name .getMethodName (),
161+ e .getMessage () ) );
162+ System .out .println ( String .format ( "This test requires symlinks files in '%s' directory." ,
163+ symlinksDirectory .getPath () ) );
164+ System .out .println ( "On some OS (like Windows 10), files are present only if the clone/checkout is done"
165+ + " in administrator mode, and correct (symlinks and not flat file/directory)"
166+ + " if symlinks option are used (for git: git clone -c core.symlinks=true [url])" );
167+ return false ;
168+ }
169+ }
121170
122171 @ Test
123172 public void testGeneral ()
@@ -158,6 +207,8 @@ public void testIncludesExcludesWithWhiteSpaces()
158207 @ Test
159208 public void testFollowSymlinksFalse ()
160209 {
210+ assumeTrue ( checkTestFilesSymlinks () );
211+
161212 DirectoryScanner ds = new DirectoryScanner ();
162213 ds .setBasedir ( new File ( "src/test/resources/symlinks/src/" ) );
163214 ds .setFollowSymlinks ( false );
@@ -190,6 +241,8 @@ private void assertAlwaysIncluded( List<String> included )
190241 @ Test
191242 public void testFollowSymlinks ()
192243 {
244+ assumeTrue ( checkTestFilesSymlinks () );
245+
193246 DirectoryScanner ds = new DirectoryScanner ();
194247 ds .setBasedir ( new File ( "src/test/resources/symlinks/src/" ) );
195248 ds .setFollowSymlinks ( true );
@@ -428,11 +481,7 @@ public void testRegexWithSlashInsideCharacterClass()
428481 public void testIsSymbolicLink ()
429482 throws IOException
430483 {
431- // TODO: Uncomment when PR #25 merged
432- // if ( !checkTestFilesSymlinks() )
433- // {
434- // return;
435- // }
484+ assumeTrue ( checkTestFilesSymlinks () );
436485
437486 final File directory = new File ( "src/test/resources/symlinks/src" );
438487 DirectoryScanner ds = new DirectoryScanner ();
@@ -446,11 +495,7 @@ public void testIsSymbolicLink()
446495 public void testIsParentSymbolicLink ()
447496 throws IOException
448497 {
449- // TODO: Uncomment when PR #25 merged
450- // if ( !checkTestFilesSymlinks() )
451- // {
452- // return;
453- // }
498+ assumeTrue ( checkTestFilesSymlinks () );
454499
455500 final File directory = new File ( "src/test/resources/symlinks/src" );
456501 DirectoryScanner ds = new DirectoryScanner ();
0 commit comments