Skip to content

Commit

Permalink
Fix FeatureMacther: featureValueOf may throw
Browse files Browse the repository at this point in the history
Right now, that may happen in:
- HasToString, if the actual object has a custom toString method which may throw
- FileMatchers.aFileWithCanonicalPath and siblings
  • Loading branch information
alb-i986 committed Aug 23, 2020
1 parent 3f08267 commit 97e8b93
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
18 changes: 12 additions & 6 deletions hamcrest/src/main/java/org/hamcrest/FeatureMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@ public FeatureMatcher(Matcher<? super U> subMatcher, String featureDescription,
* @param actual the target object
* @return the feature to be matched
*/
protected abstract U featureValueOf(T actual);
protected abstract U featureValueOf(T actual) throws Exception;

@Override
protected boolean matchesSafely(T actual, Description mismatch) {
final U featureValue = featureValueOf(actual);
if (!subMatcher.matches(featureValue)) {
mismatch.appendText(featureName).appendText(" ");
subMatcher.describeMismatch(featureValue, mismatch);
try {
final U featureValue = featureValueOf(actual);
if (!subMatcher.matches(featureValue)) {
mismatch.appendText(featureName).appendText(" ");
subMatcher.describeMismatch(featureValue, mismatch);
return false;
}
return true;
} catch (Exception e) { // catches exceptions thrown by featureValueOf(), if any
mismatch.appendText("an exception was thrown: ");
mismatch.appendText(e.toString()); //TODO + stacktrace (?)
return false;
}
return true;
}

@Override
Expand Down
8 changes: 2 additions & 6 deletions hamcrest/src/main/java/org/hamcrest/io/FileMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ public static Matcher<File> aFileNamed(final Matcher<String> expected) {

public static Matcher<File> aFileWithCanonicalPath(final Matcher<String> expected) {
return new FeatureMatcher<File, String>(expected, "A file with canonical path", "path") {
@Override protected String featureValueOf(File actual) {
try {
return actual.getCanonicalPath();
} catch (IOException e) {
return "Exception: " + e.getMessage();
}
@Override protected String featureValueOf(File actual) throws IOException {
return actual.getCanonicalPath();
}
};
}
Expand Down
19 changes: 19 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/FeatureMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public final class FeatureMatcherTest {
assertEquals("was ShouldNotMatch <ShouldNotMatch>", mismatchDescription.toString());
}

@Test public void
canRecoverFromFaultyFeature() {
assertMismatchDescription(
"an exception was thrown: java.lang.UnsupportedOperationException: make the feature fail!",
resultMatcher, new FaultyThingy());
}


public static class Match extends IsEqual<String> {
public Match(String equalArg) { super(equalArg); }
@Override public void describeMismatch(Object item, Description description) {
Expand All @@ -52,6 +60,17 @@ public String getResult() {
}
}

public static class FaultyThingy extends Thingy {
public FaultyThingy() {
super("thingy with failing feature");
}

@Override
public String getResult() {
throw new UnsupportedOperationException("make the feature fail!");
}
}

public static class ShouldNotMatch {
@Override public String toString() { return "ShouldNotMatch"; }
}
Expand Down

0 comments on commit 97e8b93

Please sign in to comment.