Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file content matcher to FileMatchers #345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions hamcrest/src/main/java/org/hamcrest/io/FileMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,55 @@
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;

import static org.hamcrest.core.IsEqual.equalTo;

public final class FileMatchers {

/**
* Matcher for matching file content with given file
* @param expected The file has expected content
* @return A FeatureMatcher that takes the content of a file as feature
*/
public static Matcher<File> matchesContentOf(File expected) {
String expectedContent = "";
try {
expectedContent = new String(Files.readAllBytes(expected.toPath()));
} catch (IOException e) {
e.printStackTrace();
}
return new FeatureMatcher<File, String>(equalTo(expectedContent), "A file with content", "content") {
@Override protected String featureValueOf(File actual) {
try {
return new String(Files.readAllBytes(actual.toPath()));
} catch (IOException e) {
e.printStackTrace();
return "Exception: " + e.getMessage();
}
}
};
}

/**
* Matcher for matching file content with given String Matcher
* @param expected The expected content Matcher
* @return A FeatureMatcher that takes the content of a file as feature
*/
public static Matcher<File> aFileWithContent(Matcher<String> expected) {
return new FeatureMatcher<File, String>(expected, "A file with content", "content") {
@Override protected String featureValueOf(File actual) {
try {
return new String(Files.readAllBytes(actual.toPath()));
} catch (IOException e) {
e.printStackTrace();
return "Exception: " + e.getMessage();
}
}
};
}

public static Matcher<File> anExistingDirectory() {
return fileChecker(IS_DIRECTORY, "an existing directory", "is not a directory");
}
Expand Down
27 changes: 27 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/io/FileMatchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.hamcrest.AbstractMatcherTest;
import org.hamcrest.Matcher;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import static org.hamcrest.core.IsEqual.equalTo;
Expand All @@ -13,6 +15,7 @@ public class FileMatchersTest extends AbstractMatcherTest {

private File directory;
private File file;
private File anotherFile;

@Override
protected void setUp() throws IOException {
Expand All @@ -22,6 +25,13 @@ protected void setUp() throws IOException {

file = new File(directory, "myFile");
file.createNewFile();

anotherFile = new File(directory, "myAnotherFile");
anotherFile.createNewFile();

BufferedWriter anotherFileWriter = new BufferedWriter(new FileWriter(anotherFile));
anotherFileWriter.write(("world"));
anotherFileWriter.close();
}

public void testAnExistingDirectory() {
Expand Down Expand Up @@ -88,4 +98,21 @@ protected Matcher<?> createMatcher() {
return FileMatchers.aFileWithSize(1L);
}

public void testFileContentMatcher() {
assertMatches("matches file content with a file", FileMatchers.matchesContentOf(file), file);
assertDoesNotMatch("content of two files with different content won't match", FileMatchers.matchesContentOf(anotherFile), file);
}

public void testFileContentMatcherDescription() {
assertMismatchDescription("content was \"\"", FileMatchers.matchesContentOf(anotherFile), file);
}

public void testAFileWithContent() {
assertMatches("matches file content", FileMatchers.aFileWithContent(equalTo("")), file);
assertDoesNotMatch("doesn't match incorrect content", FileMatchers.aFileWithContent(equalTo("world")), file);
}

public void testAFileWithContentDescription() {
assertMismatchDescription("content was \"\"", FileMatchers.aFileWithContent(equalTo("world")), file);
}
}