Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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