|
13 | 13 | public final class FileMatchers { |
14 | 14 |
|
15 | 15 | 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"); |
29 | 17 | } |
30 | 18 |
|
31 | 19 | 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"); |
45 | 21 | } |
46 | 22 |
|
47 | 23 | 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"); |
61 | 25 | } |
62 | 26 |
|
63 | 27 | 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"); |
77 | 29 | } |
78 | 30 |
|
79 | 31 | 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"); |
93 | 33 | } |
94 | 34 |
|
95 | 35 | public static Matcher<File> aFileWithSize(long size) { |
@@ -125,4 +65,43 @@ public static Matcher<File> aFileWithAbsolutePath(final Matcher<String> expected |
125 | 65 | @Override protected String featureValueOf(File actual) { return actual.getAbsolutePath(); } |
126 | 66 | }; |
127 | 67 | } |
| 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 | + } |
128 | 107 | } |
0 commit comments