Skip to content

Commit 609a6b8

Browse files
committed
Converting to feature matchers
1 parent e227510 commit 609a6b8

File tree

1 file changed

+15
-61
lines changed

1 file changed

+15
-61
lines changed

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

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.hamcrest.io;
22

33
import org.hamcrest.Description;
4+
import org.hamcrest.FeatureMatcher;
45
import org.hamcrest.Matcher;
56
import org.hamcrest.TypeSafeDiagnosingMatcher;
67

@@ -95,80 +96,33 @@ public static Matcher<File> aFileWithSize(long size) {
9596
return aFileWithSize(equalTo(size));
9697
}
9798

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(); }
113102
};
114103
}
115104

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(); }
131108
};
132109
}
133110

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) {
137114
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();
145116
} 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();
148118
}
149119
}
150-
151-
public void describeTo(Description description) {
152-
description.appendText("a File whose canonical path is ").appendDescriptionOf(path);
153-
}
154120
};
155121
}
156122

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(); }
172126
};
173127
}
174128
}

0 commit comments

Comments
 (0)