Paul Duffin | cd7c34d | 2016-12-12 16:35:36 +0000 | [diff] [blame] | 1 | package junit.framework; |
| 2 | |
| 3 | import java.io.PrintWriter; |
| 4 | import java.io.StringWriter; |
| 5 | |
| 6 | |
| 7 | /** |
| 8 | * A <code>TestFailure</code> collects a failed test together with |
| 9 | * the caught exception. |
| 10 | * @see TestResult |
| 11 | */ |
| 12 | public class TestFailure extends Object { |
| 13 | protected Test fFailedTest; |
| 14 | protected Throwable fThrownException; |
| 15 | |
| 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 | * Gets the failed test. |
| 26 | */ |
| 27 | public Test failedTest() { |
| 28 | return fFailedTest; |
| 29 | } |
| 30 | /** |
| 31 | * Gets the thrown exception. |
| 32 | */ |
| 33 | public Throwable thrownException() { |
| 34 | return fThrownException; |
| 35 | } |
| 36 | /** |
| 37 | * Returns a short description of the failure. |
| 38 | */ |
| 39 | @Override |
| 40 | public String toString() { |
| 41 | StringBuffer buffer= new StringBuffer(); |
| 42 | buffer.append(fFailedTest+": "+fThrownException.getMessage()); |
| 43 | return buffer.toString(); |
| 44 | } |
| 45 | public String trace() { |
| 46 | StringWriter stringWriter= new StringWriter(); |
| 47 | PrintWriter writer= new PrintWriter(stringWriter); |
| 48 | thrownException().printStackTrace(writer); |
| 49 | StringBuffer buffer= stringWriter.getBuffer(); |
| 50 | return buffer.toString(); |
| 51 | } |
| 52 | public String exceptionMessage() { |
| 53 | return thrownException().getMessage(); |
| 54 | } |
| 55 | public boolean isFailure() { |
| 56 | return thrownException() instanceof AssertionFailedError; |
| 57 | } |
| 58 | } |