Skip to content

Commit 48ed152

Browse files
committed
Path and File assertions refactoring
* Pull up `DirectoryStream.Filter` parameter * Pull up `FileFilter` parameter * Move file name string conversion to `File` error messages * Move path string conversion to `Path` error messages
1 parent 87eecea commit 48ed152

15 files changed

+109
-107
lines changed

src/main/java/org/assertj/core/error/ShouldContain.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
*/
1313
package org.assertj.core.error;
1414

15+
import static java.util.stream.Collectors.toList;
16+
import static org.assertj.core.error.GroupTypeDescription.getGroupTypeDescription;
17+
import static org.assertj.core.util.Strings.escapePercent;
18+
1519
import java.io.File;
1620
import java.nio.file.Path;
1721
import java.util.List;
1822

1923
import org.assertj.core.internal.ComparisonStrategy;
2024
import org.assertj.core.internal.StandardComparisonStrategy;
21-
import static org.assertj.core.error.GroupTypeDescription.getGroupTypeDescription;
22-
import static org.assertj.core.util.Strings.escapePercent;
2325

2426
/**
2527
* Creates an error message indicating that an assertion that verifies a group of elements contains a given set of values failed.
2628
* A group of elements can be a collection, an array or a {@code String}.<br>
27-
* It also mention the {@link ComparisonStrategy} used.
29+
* It also mentions the {@link ComparisonStrategy} used.
2830
*
2931
* @author Alex Ruiz
3032
* @author Joel Costigliola
@@ -70,12 +72,24 @@ public static ErrorMessageFactory shouldContain(Object actual, Object expected,
7072
return shouldContain(actual, expected, notFound, StandardComparisonStrategy.instance());
7173
}
7274

73-
public static ErrorMessageFactory directoryShouldContain(File actual, List<String> directoryContent, String filterDescription) {
74-
return new ShouldContain(actual, directoryContent, filterDescription);
75+
public static ErrorMessageFactory directoryShouldContain(File actual, List<File> directoryContent, String filterDescription) {
76+
return new ShouldContain(actual, toFileNames(directoryContent), filterDescription);
77+
}
78+
79+
private static List<String> toFileNames(List<File> files) {
80+
return files.stream()
81+
.map(File::getName)
82+
.collect(toList());
83+
}
84+
85+
public static ErrorMessageFactory directoryShouldContain(Path actual, List<Path> directoryContent, String filterDescription) {
86+
return new ShouldContain(actual, toPathNames(directoryContent), filterDescription);
7587
}
7688

77-
public static ErrorMessageFactory directoryShouldContain(Path actual, List<String> directoryContent, String filterDescription) {
78-
return new ShouldContain(actual, directoryContent, filterDescription);
89+
private static List<String> toPathNames(List<Path> files) {
90+
return files.stream()
91+
.map(Path::toString)
92+
.collect(toList());
7993
}
8094

8195
private ShouldContain(Object actual, Object expected, Object notFound, ComparisonStrategy comparisonStrategy,

src/main/java/org/assertj/core/error/ShouldNotContain.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.assertj.core.error;
1414

15+
import static java.util.stream.Collectors.toList;
16+
1517
import java.io.File;
1618
import java.nio.file.Path;
1719
import java.util.List;
@@ -56,14 +58,24 @@ private ShouldNotContain(Object actual, Object expected, Object found, Compariso
5658
super("%nExpecting%n %s%nnot to contain%n %s%nbut found%n %s%n%s", actual, expected, found, comparisonStrategy);
5759
}
5860

59-
public static ErrorMessageFactory directoryShouldNotContain(File actual, List<String> matchingContent,
60-
String filterDescription) {
61-
return new ShouldNotContain(actual, matchingContent, filterDescription);
61+
public static ErrorMessageFactory directoryShouldNotContain(File actual, List<File> matchingContent, String filterDescription) {
62+
return new ShouldNotContain(actual, toFileNames(matchingContent), filterDescription);
63+
}
64+
65+
private static List<String> toFileNames(List<File> files) {
66+
return files.stream()
67+
.map(File::getName)
68+
.collect(toList());
69+
}
70+
71+
public static ErrorMessageFactory directoryShouldNotContain(Path actual, List<Path> matchingContent, String filterDescription) {
72+
return new ShouldNotContain(actual, toPathNames(matchingContent), filterDescription);
6273
}
6374

64-
public static ErrorMessageFactory directoryShouldNotContain(Path actual, List<String> matchingContent,
65-
String filterDescription) {
66-
return new ShouldNotContain(actual, matchingContent, filterDescription);
75+
private static List<String> toPathNames(List<Path> files) {
76+
return files.stream()
77+
.map(Path::toString)
78+
.collect(toList());
6779
}
6880

6981
private ShouldNotContain(Object actual, List<String> matchingContent, String filterDescription) {

src/main/java/org/assertj/core/internal/Files.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import static org.assertj.core.util.Preconditions.checkArgument;
4646

4747
import java.io.File;
48+
import java.io.FileFilter;
4849
import java.io.IOException;
4950
import java.io.InputStream;
5051
import java.io.UncheckedIOException;
@@ -75,7 +76,7 @@ public class Files {
7576

7677
private static final String UNABLE_TO_COMPARE_FILE_CONTENTS = "Unable to compare contents of files:<%s> and:<%s>";
7778
private static final Files INSTANCE = new Files();
78-
private static final Predicate<File> ANY = any -> true;
79+
private static final FileFilter ANY = any -> true;
7980

8081
/**
8182
* Returns the singleton instance of this class.
@@ -475,20 +476,19 @@ public void assertIsNotEmptyDirectory(AssertionInfo info, File actual) {
475476

476477
public void assertIsDirectoryContaining(AssertionInfo info, File actual, Predicate<File> filter) {
477478
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");
479480
}
480481

481482
public void assertIsDirectoryContaining(AssertionInfo info, File actual, String syntaxAndPattern) {
482483
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));
485486
}
486487

487488
public void assertIsDirectoryRecursivelyContaining(AssertionInfo info, File actual, String syntaxAndPattern) {
488489
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));
492492
}
493493

494494
public void assertIsDirectoryRecursivelyContaining(AssertionInfo info, File actual, Predicate<File> filter) {
@@ -498,27 +498,20 @@ public void assertIsDirectoryRecursivelyContaining(AssertionInfo info, File actu
498498

499499
public void assertIsDirectoryNotContaining(AssertionInfo info, File actual, Predicate<File> filter) {
500500
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");
502502
}
503503

504504
public void assertIsDirectoryNotContaining(AssertionInfo info, File actual, String syntaxAndPattern) {
505505
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));
508508
}
509509

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
518511

519-
private List<File> filterDirectory(AssertionInfo info, File actual, Predicate<File> filter) {
512+
private List<File> filterDirectory(AssertionInfo info, File actual, FileFilter filter) {
520513
assertIsDirectory(info, actual);
521-
File[] items = actual.listFiles(filter::test);
514+
File[] items = actual.listFiles(filter);
522515
requireNonNull(items, "Directory listing should not be null");
523516
return list(items);
524517
}
@@ -527,25 +520,20 @@ private List<File> directoryContent(AssertionInfo info, File actual) {
527520
return filterDirectory(info, actual, ANY);
528521
}
529522

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) {
531524
List<File> matchingFiles = filterDirectory(info, actual, filter);
532525
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));
534527
}
535528
}
536529

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) {
539531
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));
542534
}
543535
}
544536

545-
private List<String> directoryContentDescription(AssertionInfo info, File actual) {
546-
return toFileNames(directoryContent(info, actual));
547-
}
548-
549537
private boolean isDirectoryRecursivelyContaining(AssertionInfo info, File actual, Predicate<File> filter) {
550538
assertIsDirectory(info, actual);
551539
try (Stream<File> actualContent = recursiveContentOf(actual)) {
@@ -578,10 +566,10 @@ private void assertIsDirectoryRecursivelyContaining(AssertionInfo info, File act
578566
}
579567
}
580568

581-
private static Predicate<File> fileMatcher(AssertionInfo info, File actual, String syntaxAndPattern) {
569+
private static FileFilter fileFilter(AssertionInfo info, File actual, String syntaxAndPattern) {
582570
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());
585573
}
586574

587575
private static void assertNotNull(AssertionInfo info, File actual) {

src/main/java/org/assertj/core/internal/NioFilesWrapper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
import java.io.IOException;
1616
import java.io.InputStream;
1717
import java.nio.file.DirectoryStream;
18+
import java.nio.file.DirectoryStream.Filter;
1819
import java.nio.file.Files;
19-
import java.nio.file.LinkOption;
2020
import java.nio.file.OpenOption;
2121
import java.nio.file.Path;
22-
import java.util.function.Predicate;
2322

2423
/**
2524
* Wrapper for <code>{@link java.nio.file.Files}</code> to test methods throwing {@link IOException}.
@@ -38,8 +37,8 @@ public InputStream newInputStream(Path path, OpenOption... options) throws IOExc
3837
return Files.newInputStream(path, options);
3938
}
4039

41-
public DirectoryStream<Path> newDirectoryStream(Path path, Predicate<Path> matcher) throws IOException {
42-
return Files.newDirectoryStream(path, matcher::test);
40+
public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException {
41+
return Files.newDirectoryStream(dir, filter);
4342
}
4443

4544
public long size(Path path) throws IOException {

src/main/java/org/assertj/core/internal/Paths.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import java.io.UncheckedIOException;
5656
import java.nio.charset.Charset;
5757
import java.nio.file.DirectoryStream;
58+
import java.nio.file.DirectoryStream.Filter;
5859
import java.nio.file.Files;
5960
import java.nio.file.LinkOption;
6061
import java.nio.file.Path;
@@ -80,7 +81,7 @@ public class Paths {
8081
private static final String UNABLE_TO_COMPARE_PATH_CONTENTS = "Unable to compare contents of paths:<%s> and:<%s>";
8182

8283
private static final Paths INSTANCE = new Paths();
83-
private static final Predicate<Path> ANY = any -> true;
84+
private static final Filter<Path> ANY = any -> true;
8485

8586
@VisibleForTesting
8687
Diff diff = new Diff();
@@ -314,7 +315,7 @@ public void assertHasDigest(AssertionInfo info, Path actual, String algorithm, S
314315

315316
public void assertIsDirectoryContaining(AssertionInfo info, Path actual, Predicate<Path> filter) {
316317
requireNonNull(filter, "The paths filter should not be null");
317-
assertIsDirectoryContaining(info, actual, filter, "the given filter");
318+
assertIsDirectoryContaining(info, actual, filter::test, "the given filter");
318319
}
319320

320321
public void assertIsDirectoryContaining(AssertionInfo info, Path actual, String syntaxAndPattern) {
@@ -337,7 +338,7 @@ public void assertIsDirectoryRecursivelyContaining(AssertionInfo info, Path actu
337338

338339
public void assertIsDirectoryNotContaining(AssertionInfo info, Path actual, Predicate<Path> filter) {
339340
requireNonNull(filter, "The paths filter should not be null");
340-
assertIsDirectoryNotContaining(info, actual, filter, "the given filter");
341+
assertIsDirectoryNotContaining(info, actual, filter::test, "the given filter");
341342
}
342343

343344
public void assertIsDirectoryNotContaining(AssertionInfo info, Path actual, String syntaxAndPattern) {
@@ -374,15 +375,9 @@ public void assertIsNotEmptyFile(AssertionInfo info, Path actual) {
374375
}
375376
}
376377

377-
public static List<String> toPathNames(List<Path> files) {
378-
return files.stream()
379-
.map(Path::toString)
380-
.collect(toList());
381-
}
382-
383-
// non public section
378+
// non-public section
384379

385-
private List<Path> filterDirectory(AssertionInfo info, Path actual, Predicate<Path> filter) {
380+
private List<Path> filterDirectory(AssertionInfo info, Path actual, Filter<Path> filter) {
386381
assertIsDirectory(info, actual);
387382
try (DirectoryStream<Path> stream = nioFilesWrapper.newDirectoryStream(actual, filter)) {
388383
return stream(stream.spliterator(), false).collect(toList());
@@ -395,10 +390,10 @@ private List<Path> directoryContent(AssertionInfo info, Path actual) {
395390
return filterDirectory(info, actual, ANY);
396391
}
397392

398-
private void assertIsDirectoryContaining(AssertionInfo info, Path actual, Predicate<Path> filter, String filterPresentation) {
393+
private void assertIsDirectoryContaining(AssertionInfo info, Path actual, Filter<Path> filter, String filterPresentation) {
399394
List<Path> matchingFiles = filterDirectory(info, actual, filter);
400395
if (matchingFiles.isEmpty()) {
401-
throw failures.failure(info, directoryShouldContain(actual, directoryContentDescription(info, actual), filterPresentation));
396+
throw failures.failure(info, directoryShouldContain(actual, directoryContent(info, actual), filterPresentation));
402397
}
403398
}
404399

@@ -430,18 +425,13 @@ private void assertIsDirectoryRecursivelyContaining(AssertionInfo info, Path act
430425
}
431426
}
432427

433-
private void assertIsDirectoryNotContaining(AssertionInfo info, Path actual, Predicate<Path> filter,
434-
String filterPresentation) {
428+
private void assertIsDirectoryNotContaining(AssertionInfo info, Path actual, Filter<Path> filter, String filterPresentation) {
435429
List<Path> matchingPaths = filterDirectory(info, actual, filter);
436430
if (matchingPaths.size() > 0) {
437-
throw failures.failure(info, directoryShouldNotContain(actual, toPathNames(matchingPaths), filterPresentation));
431+
throw failures.failure(info, directoryShouldNotContain(actual, matchingPaths, filterPresentation));
438432
}
439433
}
440434

441-
private List<String> directoryContentDescription(AssertionInfo info, Path actual) {
442-
return toPathNames(directoryContent(info, actual));
443-
}
444-
445435
private PathMatcher pathMatcher(AssertionInfo info, Path actual, String syntaxAndPattern) {
446436
assertNotNull(info, actual);
447437
return actual.getFileSystem().getPathMatcher(syntaxAndPattern);

src/test/java/org/assertj/core/error/ShouldContain_create_Test.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
import java.io.File;
2727
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.util.List;
2830
import java.util.Map;
2931

3032
import org.assertj.core.data.MapEntry;
@@ -35,10 +37,6 @@
3537
import org.junit.jupiter.api.Test;
3638

3739
/**
38-
* Tests for
39-
* <code>{@link ShouldContain#create(org.assertj.core.description.Description, org.assertj.core.presentation.Representation)}</code>
40-
* .
41-
*
4240
* @author Alex Ruiz
4341
* @author Yvonne Wang
4442
* @author Joel Costigliola
@@ -295,7 +293,8 @@ void should_create_error_message_for_file_directory() {
295293
// GIVEN
296294
File directory = mock(File.class);
297295
given(directory.getAbsolutePath()).willReturn("root");
298-
ErrorMessageFactory factory = directoryShouldContain(directory, list("foo.txt", "bar.txt"), "glob:**.java");
296+
List<File> directoryContent = list(new File("root", "foo.txt"), new File("root", "bar.txt"));
297+
ErrorMessageFactory factory = directoryShouldContain(directory, directoryContent, "glob:**.java");
299298
// WHEN
300299
String message = factory.create(new TextDescription("Test"));
301300
// THEN
@@ -312,7 +311,8 @@ void should_create_error_message_for_file_directory_escaping_percent() {
312311
// GIVEN
313312
File directory = mock(File.class);
314313
given(directory.getAbsolutePath()).willReturn("root%dir");
315-
ErrorMessageFactory factory = directoryShouldContain(directory, list("foo%1.txt", "bar%2.txt"), "glob:**%Test.java");
314+
List<File> directoryContent = list(new File("root%dir", "foo%1.txt"), new File("root%dir", "bar%2.txt"));
315+
ErrorMessageFactory factory = directoryShouldContain(directory, directoryContent, "glob:**%Test.java");
316316
// WHEN
317317
String message = factory.create(new TextDescription("Test"));
318318
// THEN
@@ -327,9 +327,9 @@ void should_create_error_message_for_file_directory_escaping_percent() {
327327
@Test
328328
void should_create_error_message_for_path_directory() {
329329
// GIVEN
330-
Path directory = mock(Path.class);
331-
given(directory.toString()).willReturn("root");
332-
ErrorMessageFactory factory = directoryShouldContain(directory, list("foo.txt", "bar.txt"), "glob:**.java");
330+
Path directory = Paths.get("root");
331+
List<Path> directoryContent = list(directory.resolve("foo.txt"), directory.resolve("bar.txt"));
332+
ErrorMessageFactory factory = directoryShouldContain(directory, directoryContent, "glob:**.java");
333333
// WHEN
334334
String message = factory.create(new TextDescription("Test"));
335335
// THEN
@@ -338,7 +338,8 @@ void should_create_error_message_for_path_directory() {
338338
" root%n" +
339339
"to contain at least one file matching glob:**.java but there was none.%n" +
340340
"The directory content was:%n" +
341-
" [foo.txt, bar.txt]"));
341+
" [%s, %s]",
342+
directory.resolve("foo.txt"), directory.resolve("bar.txt")));
342343
}
343344

344345
}

0 commit comments

Comments
 (0)