20
20
import static org .junit .Assert .assertFalse ;
21
21
import static org .junit .Assert .assertTrue ;
22
22
import static org .junit .Assert .fail ;
23
+ import static org .junit .Assume .assumeTrue ;
23
24
24
25
import java .io .File ;
25
26
import java .io .IOException ;
26
27
import java .net .URI ;
27
28
import java .net .URISyntaxException ;
28
29
import java .net .URL ;
30
+ import java .nio .file .Files ;
31
+ import java .nio .file .Paths ;
29
32
import java .util .ArrayList ;
30
33
import java .util .Arrays ;
31
34
import java .util .List ;
32
35
36
+ import org .junit .Rule ;
33
37
import org .junit .Test ;
38
+ import org .junit .rules .TestName ;
34
39
35
40
/**
36
41
* Base class for testcases doing tests with files.
40
45
public class DirectoryScannerTest
41
46
extends FileBasedTestCase
42
47
{
48
+ @ Rule
49
+ public TestName name = new TestName ();
50
+
43
51
private static String testDir = getTestDirectory ().getPath ();
44
52
45
53
@ Test
@@ -118,6 +126,47 @@ private void createTestFiles()
118
126
this .createFile ( new File ( testDir + "/scanner4.dat" ), 0 );
119
127
this .createFile ( new File ( testDir + "/scanner5.dat" ), 0 );
120
128
}
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
+ }
121
170
122
171
@ Test
123
172
public void testGeneral ()
@@ -158,6 +207,8 @@ public void testIncludesExcludesWithWhiteSpaces()
158
207
@ Test
159
208
public void testFollowSymlinksFalse ()
160
209
{
210
+ assumeTrue ( checkTestFilesSymlinks () );
211
+
161
212
DirectoryScanner ds = new DirectoryScanner ();
162
213
ds .setBasedir ( new File ( "src/test/resources/symlinks/src/" ) );
163
214
ds .setFollowSymlinks ( false );
@@ -190,6 +241,8 @@ private void assertAlwaysIncluded( List<String> included )
190
241
@ Test
191
242
public void testFollowSymlinks ()
192
243
{
244
+ assumeTrue ( checkTestFilesSymlinks () );
245
+
193
246
DirectoryScanner ds = new DirectoryScanner ();
194
247
ds .setBasedir ( new File ( "src/test/resources/symlinks/src/" ) );
195
248
ds .setFollowSymlinks ( true );
@@ -428,11 +481,7 @@ public void testRegexWithSlashInsideCharacterClass()
428
481
public void testIsSymbolicLink ()
429
482
throws IOException
430
483
{
431
- // TODO: Uncomment when PR #25 merged
432
- // if ( !checkTestFilesSymlinks() )
433
- // {
434
- // return;
435
- // }
484
+ assumeTrue ( checkTestFilesSymlinks () );
436
485
437
486
final File directory = new File ( "src/test/resources/symlinks/src" );
438
487
DirectoryScanner ds = new DirectoryScanner ();
@@ -446,11 +495,7 @@ public void testIsSymbolicLink()
446
495
public void testIsParentSymbolicLink ()
447
496
throws IOException
448
497
{
449
- // TODO: Uncomment when PR #25 merged
450
- // if ( !checkTestFilesSymlinks() )
451
- // {
452
- // return;
453
- // }
498
+ assumeTrue ( checkTestFilesSymlinks () );
454
499
455
500
final File directory = new File ( "src/test/resources/symlinks/src" );
456
501
DirectoryScanner ds = new DirectoryScanner ();
0 commit comments