Skip to content

Commit 028841a

Browse files
committed
Added file content matchers to PathMatchers.
These correspond to the recently added FileMatchers matchers.
1 parent 102b116 commit 028841a

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

hamcrest/src/main/java/org/hamcrest/io/PathMatchers.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
1111
import java.util.function.Predicate;
12+
import java.util.stream.Collectors;
1213

1314
import static org.hamcrest.TypeSafeDiagnosingMatcher.matcher;
1415
import static org.hamcrest.core.IsEqual.equalTo;
@@ -207,6 +208,39 @@ public static Matcher<Path> hasFileSystem(final Matcher<FileSystem> expected) {
207208
};
208209
}
209210

211+
/**
212+
* Matcher for matching file content with given file path
213+
*
214+
* Note: line endings in the file are preserved in their platform dependent form,
215+
* so both files must contain the same line endings to match.
216+
*
217+
* @param expected The file with the expected content
218+
* @return A FeatureMatcher that takes the content of a file path as feature
219+
*/
220+
public static Matcher<Path> matchesContentOf(Path expected) {
221+
String expectedContent = toUncheckedEx(()->Files.readString(expected));
222+
return new FeatureMatcher<Path, String>(equalTo(expectedContent), "A file with content", "content") {
223+
@Override protected String featureValueOf(Path actual) { return toUncheckedEx(()->Files.readString(actual));}
224+
};
225+
}
226+
227+
/**
228+
* Matcher for matching file content with given String Matcher
229+
*
230+
* Note: line endings in the file are converted to '\n' to avoid platform dependent test results
231+
*
232+
* @param expected The expected content Matcher
233+
* @return A FeatureMatcher that takes the content of a file as feature
234+
*/
235+
public static Matcher<Path> hasContent(Matcher<String> expected) {
236+
return new FeatureMatcher<Path, String>(expected, "A file with content", "content") {
237+
@Override protected String featureValueOf(Path actual) {
238+
// use collector to avoid platform dependent line endings
239+
return toUncheckedEx(()->Files.lines(actual).collect(Collectors.joining("\n")));
240+
}
241+
};
242+
}
243+
210244
// Possible additions:
211245
// - hasParent(Matcher<Path>)
212246
// - hasRoot(Matcher<Path>)
@@ -221,7 +255,6 @@ public static Matcher<Path> hasFileSystem(final Matcher<FileSystem> expected) {
221255
// - hasFileAttribute(String, Matcher<Object>)
222256
// - hasProvider(Matcher<FileSystemProvider>)
223257

224-
// - hasContent(Matcher<String>)
225258
// - containsStrings(String...)
226259

227260
// Workaround for JDK 8 not supporting Files.isHidden(Path) for directories

hamcrest/src/test/java/org/hamcrest/io/PathMatchersTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ public class PathMatchersTest extends AbstractMatcherTest {
2323
@TempDir Path tempDir;
2424
private Path directory;
2525
private Path file;
26+
private Path anotherFile;
2627
private Path symbolicLink;
2728

2829
@BeforeEach
2930
protected void setUp() throws IOException {
3031
directory = Files.createDirectory(tempDir.resolve("myDir"));
3132
file = directory.resolve("myFile");
33+
anotherFile = directory.resolve("myAnotherFile");
3234
Files.createFile(file);
35+
Files.writeString(anotherFile, "world");
3336
Files.createFile(directory.resolve("mydirFile")); // Makes sure myDir is not empty.
3437
if (!OS.WINDOWS.isCurrentOs()) { // Can't do symbolic links on Windows unless admin privileges are available.
3538
symbolicLink = tempDir.resolve("mySymbolicLink");
@@ -193,7 +196,29 @@ public void testHasFileSystem() {
193196
// assertDoesNotMatch("doesn't match incorrect file system",PathMatchers.hasFileSystem(equalTo(Paths.get("foo").getFileSystem())), file);
194197
}
195198

196-
@Override
199+
@Test
200+
public void testFileContentMatcher() {
201+
assertMatches("matches file content with a file", PathMatchers.matchesContentOf(file), file);
202+
assertDoesNotMatch("content of two files with different content won't match", PathMatchers.matchesContentOf(anotherFile), file);
203+
}
204+
205+
@Test
206+
public void testFileContentMatcherDescription() {
207+
assertMismatchDescription("content was \"\"", PathMatchers.matchesContentOf(anotherFile), file);
208+
}
209+
210+
@Test
211+
public void testAFileWithContent() {
212+
assertMatches("matches file content", PathMatchers.hasContent(equalTo("")), file);
213+
assertDoesNotMatch("doesn't match incorrect content", PathMatchers.hasContent(equalTo("world")), file);
214+
}
215+
216+
@Test
217+
public void testAFileWithContentDescription() {
218+
assertMismatchDescription("content was \"\"", PathMatchers.hasContent(equalTo("world")), file);
219+
}
220+
221+
@Override
197222
protected Matcher<?> createMatcher() {
198223
return PathMatchers.hasSizeEqualTo(1L);
199224
// return PathMatchers.isSymbolicLink();

0 commit comments

Comments
 (0)