Skip to content

Commit a789543

Browse files
committed
Extracted fileChecker
1 parent 609a6b8 commit a789543

File tree

1 file changed

+44
-65
lines changed

1 file changed

+44
-65
lines changed

hamcrest-library/src/main/java/org/hamcrest/io/FileMatchers.java

Lines changed: 44 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,83 +13,23 @@
1313
public final class FileMatchers {
1414

1515
public static Matcher<File> anExistingDirectory() {
16-
return new TypeSafeDiagnosingMatcher<File>() {
17-
public boolean matchesSafely(File actual, Description mismatchDescription) {
18-
final boolean result = actual.isDirectory();
19-
if (!result) {
20-
mismatchDescription.appendText("was a File that either didn't exist, or was not a directory");
21-
}
22-
return result;
23-
}
24-
25-
public void describeTo(Description description) {
26-
description.appendText("a File representing a directory that exists");
27-
}
28-
};
16+
return fileChecker(IS_DIRECTORY, "an existing directory", "is not a directory");
2917
}
3018

3119
public static Matcher<File> anExistingFileOrDirectory() {
32-
return new TypeSafeDiagnosingMatcher<File>() {
33-
public boolean matchesSafely(File actual, Description mismatchDescription) {
34-
final boolean exists = actual.exists();
35-
if (!exists) {
36-
mismatchDescription.appendText("was a File that did not exist");
37-
}
38-
return exists;
39-
}
40-
41-
public void describeTo(Description description) {
42-
description.appendText("a file or directory that exists");
43-
}
44-
};
20+
return fileChecker(EXISTS, "an existing file or directory", "does not exist");
4521
}
4622

4723
public static Matcher<File> anExistingFile() {
48-
return new TypeSafeDiagnosingMatcher<File>() {
49-
public boolean matchesSafely(File actual, Description mismatchDescription) {
50-
final boolean result = actual.isFile();
51-
if (!result) {
52-
mismatchDescription.appendText("was a File that either didn't exist, or was a directory");
53-
}
54-
return result;
55-
}
56-
57-
public void describeTo(Description description) {
58-
description.appendText("a File representing a file that exists");
59-
}
60-
};
24+
return fileChecker(IS_FILE, "an existing File", "is not a file");
6125
}
6226

6327
public static Matcher<File> aReadableFile() {
64-
return new TypeSafeDiagnosingMatcher<File>() {
65-
public boolean matchesSafely(File actual, Description mismatchDescription) {
66-
final boolean result = actual.canRead();
67-
if (!result) {
68-
mismatchDescription.appendText("was a File that could not be read");
69-
}
70-
return result;
71-
}
72-
73-
public void describeTo(Description description) {
74-
description.appendText("a File that can be read");
75-
}
76-
};
28+
return fileChecker(CAN_READ, "a readable File", "cannot be read");
7729
}
7830

7931
public static Matcher<File> aWritableFile() {
80-
return new TypeSafeDiagnosingMatcher<File>() {
81-
public boolean matchesSafely(File actual, Description mismatchDescription) {
82-
final boolean result = actual.canWrite();
83-
if (!result) {
84-
mismatchDescription.appendText("was a File that could not be written to");
85-
}
86-
return result;
87-
}
88-
89-
public void describeTo(Description description) {
90-
description.appendText("a writable File");
91-
}
92-
};
32+
return fileChecker(CAN_WRITE, "a writable File", "cannot be written to");
9333
}
9434

9535
public static Matcher<File> aFileWithSize(long size) {
@@ -125,4 +65,43 @@ public static Matcher<File> aFileWithAbsolutePath(final Matcher<String> expected
12565
@Override protected String featureValueOf(File actual) { return actual.getAbsolutePath(); }
12666
};
12767
}
68+
69+
public static interface FileStatus {
70+
boolean check(File actual);
71+
}
72+
73+
public static final FileStatus CAN_WRITE = new FileStatus() {
74+
@Override public boolean check(File actual) { return actual.canWrite(); }
75+
};
76+
public static final FileStatus CAN_READ = new FileStatus() {
77+
@Override public boolean check(File actual) { return actual.canRead(); }
78+
};
79+
80+
public static final FileStatus IS_FILE = new FileStatus() {
81+
@Override public boolean check(File actual) { return actual.isFile(); }
82+
};
83+
84+
public static final FileStatus IS_DIRECTORY = new FileStatus() {
85+
@Override public boolean check(File actual) { return actual.isDirectory(); }
86+
};
87+
88+
public static final FileStatus EXISTS = new FileStatus() {
89+
@Override public boolean check(File actual) { return actual.exists(); }
90+
};
91+
92+
private static Matcher<File> fileChecker(final FileStatus fileStatus, final String successDescription, final String failureDescription) {
93+
return new TypeSafeDiagnosingMatcher<File>() {
94+
public boolean matchesSafely(File actual, Description mismatchDescription) {
95+
final boolean result = fileStatus.check(actual);
96+
if (!result) {
97+
mismatchDescription.appendText(failureDescription);
98+
}
99+
return result;
100+
}
101+
102+
public void describeTo(Description description) {
103+
description.appendText(successDescription);
104+
}
105+
};
106+
}
128107
}

0 commit comments

Comments
 (0)