14
14
15
15
import static java .lang .String .format ;
16
16
import static java .nio .file .Files .readAllBytes ;
17
+ import static java .util .Comparator .comparing ;
17
18
import static java .util .Objects .requireNonNull ;
18
19
import static java .util .stream .Collectors .toList ;
19
20
import static org .assertj .core .error .ShouldBeAbsolutePath .shouldBeAbsolutePath ;
25
26
import static org .assertj .core .error .ShouldBeRelativePath .shouldBeRelativePath ;
26
27
import static org .assertj .core .error .ShouldBeWritable .shouldBeWritable ;
27
28
import static org .assertj .core .error .ShouldContain .directoryShouldContain ;
29
+ import static org .assertj .core .error .ShouldContainRecursively .directoryShouldContainRecursively ;
28
30
import static org .assertj .core .error .ShouldExist .shouldExist ;
29
31
import static org .assertj .core .error .ShouldHaveBinaryContent .shouldHaveBinaryContent ;
30
32
import static org .assertj .core .error .ShouldHaveContent .shouldHaveContent ;
48
50
import java .io .UncheckedIOException ;
49
51
import java .nio .charset .Charset ;
50
52
import java .nio .charset .MalformedInputException ;
53
+ import java .nio .file .Path ;
51
54
import java .nio .file .PathMatcher ;
52
55
import java .security .MessageDigest ;
53
56
import java .security .NoSuchAlgorithmException ;
54
57
import java .util .List ;
55
58
import java .util .function .Predicate ;
59
+ import java .util .stream .Stream ;
56
60
57
61
import org .assertj .core .api .AssertionInfo ;
58
62
import org .assertj .core .util .VisibleForTesting ;
@@ -475,9 +479,21 @@ public void assertIsDirectoryContaining(AssertionInfo info, File actual, Predica
475
479
}
476
480
477
481
public void assertIsDirectoryContaining (AssertionInfo info , File actual , String syntaxAndPattern ) {
478
- requireNonNull (syntaxAndPattern , "The syntax and pattern to build PathMatcher should not be null" );
479
- Predicate <File > pathMatcher = pathMatcher (info , actual , syntaxAndPattern );
480
- assertIsDirectoryContaining (info , actual , pathMatcher , format ("the '%s' pattern" , syntaxAndPattern ));
482
+ 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 ));
485
+ }
486
+
487
+ public void assertIsDirectoryRecursivelyContaining (AssertionInfo info , File actual , String syntaxAndPattern ) {
488
+ 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 ));
492
+ }
493
+
494
+ public void assertIsDirectoryRecursivelyContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
495
+ requireNonNull (filter , "The files filter should not be null" );
496
+ assertIsDirectoryRecursivelyContaining (info , actual , filter , "the given filter" );
481
497
}
482
498
483
499
public void assertIsDirectoryNotContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
@@ -486,9 +502,9 @@ public void assertIsDirectoryNotContaining(AssertionInfo info, File actual, Pred
486
502
}
487
503
488
504
public void assertIsDirectoryNotContaining (AssertionInfo info , File actual , String syntaxAndPattern ) {
489
- requireNonNull (syntaxAndPattern , "The syntax and pattern to build PathMatcher should not be null" );
490
- Predicate <File > pathMatcher = pathMatcher (info , actual , syntaxAndPattern );
491
- assertIsDirectoryNotContaining (info , actual , pathMatcher , format ("the '%s' pattern" , syntaxAndPattern ));
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 ));
492
508
}
493
509
494
510
public static List <String > toFileNames (List <File > files ) {
@@ -497,6 +513,12 @@ public static List<String> toFileNames(List<File> files) {
497
513
.collect (toList ());
498
514
}
499
515
516
+ public static List <String > toAbsolutePaths (List <File > files ) {
517
+ return files .stream ()
518
+ .map (File ::getAbsolutePath )
519
+ .collect (toList ());
520
+ }
521
+
500
522
// non public section
501
523
502
524
private List <File > filterDirectory (AssertionInfo info , File actual , Predicate <File > filter ) {
@@ -529,7 +551,46 @@ private List<String> directoryContentDescription(AssertionInfo info, File actual
529
551
return toFileNames (directoryContent (info , actual ));
530
552
}
531
553
532
- private Predicate <File > pathMatcher (AssertionInfo info , File actual , String syntaxAndPattern ) {
554
+ // BEGIN - recursively assertion private methods
555
+ private boolean isDirectoryRecursivelyContaining (AssertionInfo info , File actual , Predicate <File > filter ) {
556
+ assertIsDirectory (info , actual );
557
+ try (Stream <File > fileStream = createRecursiveStreamOfFile (actual )) {
558
+ return fileStream .anyMatch (filter );
559
+ }
560
+ }
561
+
562
+ private List <File > directoryRecursiveContent (File actual ) {
563
+ try (Stream <File > fileStream = createRecursiveStreamOfFile (actual )) {
564
+ return fileStream .sorted (comparing (File ::getAbsolutePath ))
565
+ .collect (toList ());
566
+ }
567
+ }
568
+
569
+ private Stream <File > createRecursiveStreamOfFile (File directory ) {
570
+ Path path = directory .toPath ();
571
+ try {
572
+ return java .nio .file .Files .walk (path )
573
+ .filter (p -> !p .equals (path ))
574
+ .map (Path ::toFile );
575
+ } catch (IOException e ) {
576
+ throw new UncheckedIOException (format ("Unable to walk recursively the directory :<%s>" , path ), e );
577
+ }
578
+ }
579
+
580
+ private void assertIsDirectoryRecursivelyContaining (AssertionInfo info , File actual , Predicate <File > filter ,
581
+ String filterPresentation ) {
582
+ if (!isDirectoryRecursivelyContaining (info , actual , filter )) {
583
+ throw failures .failure (info , directoryShouldContainRecursively (actual , directoryRecursiveContentDescription (actual ),
584
+ filterPresentation ));
585
+ }
586
+ }
587
+
588
+ private List <String > directoryRecursiveContentDescription (File actual ) {
589
+ return toAbsolutePaths (directoryRecursiveContent (actual ));
590
+ }
591
+ // END - recursively assertion private methods
592
+
593
+ private static Predicate <File > fileMatcher (AssertionInfo info , File actual , String syntaxAndPattern ) {
533
594
assertNotNull (info , actual );
534
595
PathMatcher pathMatcher = actual .toPath ().getFileSystem ().getPathMatcher (syntaxAndPattern );
535
596
return file -> pathMatcher .matches (file .toPath ());
0 commit comments