99import java .nio .file .Files ;
1010import java .nio .file .Path ;
1111import java .util .function .Predicate ;
12+ import java .util .stream .Collectors ;
1213
1314import static org .hamcrest .TypeSafeDiagnosingMatcher .matcher ;
1415import 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
0 commit comments