45
45
import static org .assertj .core .util .Preconditions .checkArgument ;
46
46
47
47
import java .io .File ;
48
+ import java .io .FileFilter ;
48
49
import java .io .IOException ;
49
50
import java .io .InputStream ;
50
51
import java .io .UncheckedIOException ;
@@ -75,7 +76,7 @@ public class Files {
75
76
76
77
private static final String UNABLE_TO_COMPARE_FILE_CONTENTS = "Unable to compare contents of files:<%s> and:<%s>" ;
77
78
private static final Files INSTANCE = new Files ();
78
- private static final Predicate < File > ANY = any -> true ;
79
+ private static final FileFilter ANY = any -> true ;
79
80
80
81
/**
81
82
* Returns the singleton instance of this class.
@@ -475,20 +476,19 @@ public void assertIsNotEmptyDirectory(AssertionInfo info, File actual) {
475
476
476
477
public void assertIsDirectoryContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
477
478
requireNonNull (filter , "The files filter should not be null" );
478
- assertIsDirectoryContaining (info , actual , filter , "the given filter" );
479
+ assertIsDirectoryContaining (info , actual , filter :: test , "the given filter" );
479
480
}
480
481
481
482
public void assertIsDirectoryContaining (AssertionInfo info , File actual , String syntaxAndPattern ) {
482
483
requireNonNull (syntaxAndPattern , "The syntax and pattern should not be null" );
483
- Predicate < File > fileMatcher = fileMatcher (info , actual , syntaxAndPattern );
484
- assertIsDirectoryContaining (info , actual , fileMatcher , format ("the '%s' pattern" , syntaxAndPattern ));
484
+ FileFilter filter = fileFilter (info , actual , syntaxAndPattern );
485
+ assertIsDirectoryContaining (info , actual , filter , format ("the '%s' pattern" , syntaxAndPattern ));
485
486
}
486
487
487
488
public void assertIsDirectoryRecursivelyContaining (AssertionInfo info , File actual , String syntaxAndPattern ) {
488
489
requireNonNull (syntaxAndPattern , "The syntax and pattern should not be null" );
489
- Predicate <File > fileMatcher = fileMatcher (info , actual , syntaxAndPattern );
490
- assertIsDirectoryRecursivelyContaining (info , actual , fileMatcher ,
491
- format ("the '%s' pattern" , syntaxAndPattern ));
490
+ FileFilter filter = fileFilter (info , actual , syntaxAndPattern );
491
+ assertIsDirectoryRecursivelyContaining (info , actual , filter ::accept , format ("the '%s' pattern" , syntaxAndPattern ));
492
492
}
493
493
494
494
public void assertIsDirectoryRecursivelyContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
@@ -498,27 +498,20 @@ public void assertIsDirectoryRecursivelyContaining(AssertionInfo info, File actu
498
498
499
499
public void assertIsDirectoryNotContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
500
500
requireNonNull (filter , "The files filter should not be null" );
501
- assertIsDirectoryNotContaining (info , actual , filter , "the given filter" );
501
+ assertIsDirectoryNotContaining (info , actual , filter :: test , "the given filter" );
502
502
}
503
503
504
504
public void assertIsDirectoryNotContaining (AssertionInfo info , File actual , String syntaxAndPattern ) {
505
505
requireNonNull (syntaxAndPattern , "The syntax and pattern should not be null" );
506
- Predicate < File > fileMatcher = fileMatcher (info , actual , syntaxAndPattern );
507
- assertIsDirectoryNotContaining (info , actual , fileMatcher , format ("the '%s' pattern" , syntaxAndPattern ));
506
+ FileFilter filter = fileFilter (info , actual , syntaxAndPattern );
507
+ assertIsDirectoryNotContaining (info , actual , filter , format ("the '%s' pattern" , syntaxAndPattern ));
508
508
}
509
509
510
- @ VisibleForTesting
511
- public static List <String > toFileNames (List <File > files ) {
512
- return files .stream ()
513
- .map (File ::getName )
514
- .collect (toList ());
515
- }
516
-
517
- // non public section
510
+ // non-public section
518
511
519
- private List <File > filterDirectory (AssertionInfo info , File actual , Predicate < File > filter ) {
512
+ private List <File > filterDirectory (AssertionInfo info , File actual , FileFilter filter ) {
520
513
assertIsDirectory (info , actual );
521
- File [] items = actual .listFiles (filter :: test );
514
+ File [] items = actual .listFiles (filter );
522
515
requireNonNull (items , "Directory listing should not be null" );
523
516
return list (items );
524
517
}
@@ -527,25 +520,20 @@ private List<File> directoryContent(AssertionInfo info, File actual) {
527
520
return filterDirectory (info , actual , ANY );
528
521
}
529
522
530
- private void assertIsDirectoryContaining (AssertionInfo info , File actual , Predicate < File > filter , String filterPresentation ) {
523
+ private void assertIsDirectoryContaining (AssertionInfo info , File actual , FileFilter filter , String filterPresentation ) {
531
524
List <File > matchingFiles = filterDirectory (info , actual , filter );
532
525
if (matchingFiles .isEmpty ()) {
533
- throw failures .failure (info , directoryShouldContain (actual , directoryContentDescription (info , actual ), filterPresentation ));
526
+ throw failures .failure (info , directoryShouldContain (actual , directoryContent (info , actual ), filterPresentation ));
534
527
}
535
528
}
536
529
537
- private void assertIsDirectoryNotContaining (AssertionInfo info , File actual , Predicate <File > filter ,
538
- String filterPresentation ) {
530
+ private void assertIsDirectoryNotContaining (AssertionInfo info , File actual , FileFilter filter , String filterPresentation ) {
539
531
List <File > matchingFiles = filterDirectory (info , actual , filter );
540
- if (matchingFiles .size () > 0 ) {
541
- throw failures .failure (info , directoryShouldNotContain (actual , toFileNames ( matchingFiles ) , filterPresentation ));
532
+ if (! matchingFiles .isEmpty () ) {
533
+ throw failures .failure (info , directoryShouldNotContain (actual , matchingFiles , filterPresentation ));
542
534
}
543
535
}
544
536
545
- private List <String > directoryContentDescription (AssertionInfo info , File actual ) {
546
- return toFileNames (directoryContent (info , actual ));
547
- }
548
-
549
537
private boolean isDirectoryRecursivelyContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
550
538
assertIsDirectory (info , actual );
551
539
try (Stream <File > actualContent = recursiveContentOf (actual )) {
@@ -578,10 +566,10 @@ private void assertIsDirectoryRecursivelyContaining(AssertionInfo info, File act
578
566
}
579
567
}
580
568
581
- private static Predicate < File > fileMatcher (AssertionInfo info , File actual , String syntaxAndPattern ) {
569
+ private static FileFilter fileFilter (AssertionInfo info , File actual , String syntaxAndPattern ) {
582
570
assertNotNull (info , actual );
583
- PathMatcher pathMatcher = actual .toPath ().getFileSystem ().getPathMatcher (syntaxAndPattern );
584
- return file -> pathMatcher .matches (file .toPath ());
571
+ PathMatcher matcher = actual .toPath ().getFileSystem ().getPathMatcher (syntaxAndPattern );
572
+ return file -> matcher .matches (file .toPath ());
585
573
}
586
574
587
575
private static void assertNotNull (AssertionInfo info , File actual ) {
0 commit comments