1package junit.framework;
2
3import java.io.PrintWriter;
4import java.io.StringWriter;
5
6
7/**
8 * A {@code TestFailure} collects a failed test together with
9 * the caught exception.
10 *
11 * @see TestResult
12 */
13public class TestFailure {
14    protected Test fFailedTest;
15    protected Throwable fThrownException;
16
17    /**
18     * Constructs a TestFailure with the given test and exception.
19     */
20    public TestFailure(Test failedTest, Throwable thrownException) {
21        fFailedTest = failedTest;
22        fThrownException = thrownException;
23    }
24
25    /**
26     * Gets the failed test.
27     */
28    public Test failedTest() {
29        return fFailedTest;
30    }
31
32    /**
33     * Gets the thrown exception.
34     */
35    public Throwable thrownException() {
36        return fThrownException;
37    }
38
39    /**
40     * Returns a short description of the failure.
41     */
42    @Override
43    public String toString() {
44        return fFailedTest + ": " + fThrownException.getMessage();
45    }
46
47    /**
48     * Returns a String containing the stack trace of the error
49     * thrown by TestFailure.
50     */
51    public String trace() {
52        StringWriter stringWriter = new StringWriter();
53        PrintWriter writer = new PrintWriter(stringWriter);
54        thrownException().printStackTrace(writer);
55        return stringWriter.toString();
56    }
57
58    /**
59     * Returns a String containing the message from the thrown exception.
60     */
61    public String exceptionMessage() {
62        return thrownException().getMessage();
63    }
64
65    /**
66     * Returns {@code true} if the error is considered a failure
67     * (i.e. if it is an instance of {@code AssertionFailedError}),
68     * {@code false} otherwise.
69     */
70    public boolean isFailure() {
71        return thrownException() instanceof AssertionFailedError;
72    }
73}