|
1 | 1 | package org.hamcrest.io; |
2 | 2 |
|
3 | 3 | import org.hamcrest.Description; |
| 4 | +import org.hamcrest.FeatureMatcher; |
4 | 5 | import org.hamcrest.Matcher; |
5 | 6 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
6 | 7 |
|
@@ -95,80 +96,33 @@ public static Matcher<File> aFileWithSize(long size) { |
95 | 96 | return aFileWithSize(equalTo(size)); |
96 | 97 | } |
97 | 98 |
|
98 | | - public static Matcher<File> aFileWithSize(final Matcher<Long> size) { |
99 | | - return new TypeSafeDiagnosingMatcher<File>() { |
100 | | - public boolean matchesSafely(File actual, Description mismatchDescription) { |
101 | | - final long length = actual.length(); |
102 | | - final boolean result = size.matches(length); |
103 | | - if (!result) { |
104 | | - mismatchDescription.appendText("was a File whose size "); |
105 | | - size.describeMismatch(length, mismatchDescription); |
106 | | - } |
107 | | - return result; |
108 | | - } |
109 | | - |
110 | | - public void describeTo(Description description) { |
111 | | - description.appendText("a File whose size is ").appendDescriptionOf(size); |
112 | | - } |
| 99 | + public static Matcher<File> aFileWithSize(final Matcher<Long> expected) { |
| 100 | + return new FeatureMatcher<File, Long>(expected, "A file with size", "size") { |
| 101 | + @Override protected Long featureValueOf(File actual) { return actual.length(); } |
113 | 102 | }; |
114 | 103 | } |
115 | 104 |
|
116 | | - public static Matcher<File> aFileNamed(final Matcher<String> name) { |
117 | | - return new TypeSafeDiagnosingMatcher<File>() { |
118 | | - public boolean matchesSafely(File actual, Description mismatchDescription) { |
119 | | - final String actualName = actual.getName(); |
120 | | - final boolean result = name.matches(actualName); |
121 | | - if (!result) { |
122 | | - mismatchDescription.appendText("was a File whose name "); |
123 | | - name.describeMismatch(actualName, mismatchDescription); |
124 | | - } |
125 | | - return result; |
126 | | - } |
127 | | - |
128 | | - public void describeTo(Description description) { |
129 | | - description.appendText("a File whose name is ").appendDescriptionOf(name); |
130 | | - } |
| 105 | + public static Matcher<File> aFileNamed(final Matcher<String> expected) { |
| 106 | + return new FeatureMatcher<File, String>(expected, "A file with name", "name") { |
| 107 | + @Override protected String featureValueOf(File actual) { return actual.getName(); } |
131 | 108 | }; |
132 | 109 | } |
133 | 110 |
|
134 | | - public static Matcher<File> aFileWithCanonicalPath(final Matcher<String> path) { |
135 | | - return new TypeSafeDiagnosingMatcher<File>() { |
136 | | - public boolean matchesSafely(File actual, Description mismatchDescription) { |
| 111 | + public static Matcher<File> aFileWithCanonicalPath(final Matcher<String> expected) { |
| 112 | + return new FeatureMatcher<File, String>(expected, "A file with canonical path", "path") { |
| 113 | + @Override protected String featureValueOf(File actual) { |
137 | 114 | try { |
138 | | - String canonicalPath = actual.getCanonicalPath(); |
139 | | - final boolean result = path.matches(canonicalPath); |
140 | | - if (!result) { |
141 | | - mismatchDescription.appendText("was a File whose canonical path "); |
142 | | - path.describeMismatch(canonicalPath, mismatchDescription); |
143 | | - } |
144 | | - return result; |
| 115 | + return actual.getCanonicalPath(); |
145 | 116 | } catch (IOException e) { |
146 | | - mismatchDescription.appendText("was a File whose canonical path was underivable (exception: ").appendValue(e).appendText(")"); |
147 | | - return false; |
| 117 | + return "Exception: " + e.getMessage(); |
148 | 118 | } |
149 | 119 | } |
150 | | - |
151 | | - public void describeTo(Description description) { |
152 | | - description.appendText("a File whose canonical path is ").appendDescriptionOf(path); |
153 | | - } |
154 | 120 | }; |
155 | 121 | } |
156 | 122 |
|
157 | | - public static Matcher<File> aFileWithAbsolutePath(final Matcher<String> path) { |
158 | | - return new TypeSafeDiagnosingMatcher<File>() { |
159 | | - public boolean matchesSafely(File actual, Description mismatchDescription) { |
160 | | - final String absolute = actual.getAbsolutePath(); |
161 | | - final boolean result = path.matches(absolute); |
162 | | - if (!result) { |
163 | | - mismatchDescription.appendText("was a File whose absolute path "); |
164 | | - path.describeMismatch(absolute, mismatchDescription); |
165 | | - } |
166 | | - return result; |
167 | | - } |
168 | | - |
169 | | - public void describeTo(Description description) { |
170 | | - description.appendText("a File whose absolute path is ").appendDescriptionOf(path); |
171 | | - } |
| 123 | + public static Matcher<File> aFileWithAbsolutePath(final Matcher<String> expected) { |
| 124 | + return new FeatureMatcher<File, String>(expected, "A file with absolute path", "path") { |
| 125 | + @Override protected String featureValueOf(File actual) { return actual.getAbsolutePath(); } |
172 | 126 | }; |
173 | 127 | } |
174 | 128 | } |
0 commit comments