Skip to content

Commit 97e8b93

Browse files
committed
Fix FeatureMacther: featureValueOf may throw
Right now, that may happen in: - HasToString, if the actual object has a custom toString method which may throw - FileMatchers.aFileWithCanonicalPath and siblings
1 parent 3f08267 commit 97e8b93

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

hamcrest/src/main/java/org/hamcrest/FeatureMatcher.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,23 @@ public FeatureMatcher(Matcher<? super U> subMatcher, String featureDescription,
3333
* @param actual the target object
3434
* @return the feature to be matched
3535
*/
36-
protected abstract U featureValueOf(T actual);
36+
protected abstract U featureValueOf(T actual) throws Exception;
3737

3838
@Override
3939
protected boolean matchesSafely(T actual, Description mismatch) {
40-
final U featureValue = featureValueOf(actual);
41-
if (!subMatcher.matches(featureValue)) {
42-
mismatch.appendText(featureName).appendText(" ");
43-
subMatcher.describeMismatch(featureValue, mismatch);
40+
try {
41+
final U featureValue = featureValueOf(actual);
42+
if (!subMatcher.matches(featureValue)) {
43+
mismatch.appendText(featureName).appendText(" ");
44+
subMatcher.describeMismatch(featureValue, mismatch);
45+
return false;
46+
}
47+
return true;
48+
} catch (Exception e) { // catches exceptions thrown by featureValueOf(), if any
49+
mismatch.appendText("an exception was thrown: ");
50+
mismatch.appendText(e.toString()); //TODO + stacktrace (?)
4451
return false;
4552
}
46-
return true;
4753
}
4854

4955
@Override

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,8 @@ public static Matcher<File> aFileNamed(final Matcher<String> expected) {
5050

5151
public static Matcher<File> aFileWithCanonicalPath(final Matcher<String> expected) {
5252
return new FeatureMatcher<File, String>(expected, "A file with canonical path", "path") {
53-
@Override protected String featureValueOf(File actual) {
54-
try {
55-
return actual.getCanonicalPath();
56-
} catch (IOException e) {
57-
return "Exception: " + e.getMessage();
58-
}
53+
@Override protected String featureValueOf(File actual) throws IOException {
54+
return actual.getCanonicalPath();
5955
}
6056
};
6157
}

hamcrest/src/test/java/org/hamcrest/FeatureMatcherTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public final class FeatureMatcherTest {
3333
assertEquals("was ShouldNotMatch <ShouldNotMatch>", mismatchDescription.toString());
3434
}
3535

36+
@Test public void
37+
canRecoverFromFaultyFeature() {
38+
assertMismatchDescription(
39+
"an exception was thrown: java.lang.UnsupportedOperationException: make the feature fail!",
40+
resultMatcher, new FaultyThingy());
41+
}
42+
43+
3644
public static class Match extends IsEqual<String> {
3745
public Match(String equalArg) { super(equalArg); }
3846
@Override public void describeMismatch(Object item, Description description) {
@@ -52,6 +60,17 @@ public String getResult() {
5260
}
5361
}
5462

63+
public static class FaultyThingy extends Thingy {
64+
public FaultyThingy() {
65+
super("thingy with failing feature");
66+
}
67+
68+
@Override
69+
public String getResult() {
70+
throw new UnsupportedOperationException("make the feature fail!");
71+
}
72+
}
73+
5574
public static class ShouldNotMatch {
5675
@Override public String toString() { return "ShouldNotMatch"; }
5776
}

0 commit comments

Comments
 (0)