1. package junit.framework;
  2. import java.io.PrintWriter;
  3. import java.io.StringWriter;
  4. /**
  5. * A <code>TestFailure</code> collects a failed test together with
  6. * the caught exception.
  7. * @see TestResult
  8. */
  9. public class TestFailure extends Object {
  10. protected Test fFailedTest;
  11. protected Throwable fThrownException;
  12. /**
  13. * Constructs a TestFailure with the given test and exception.
  14. */
  15. public TestFailure(Test failedTest, Throwable thrownException) {
  16. fFailedTest= failedTest;
  17. fThrownException= thrownException;
  18. }
  19. /**
  20. * Gets the failed test.
  21. */
  22. public Test failedTest() {
  23. return fFailedTest;
  24. }
  25. /**
  26. * Gets the thrown exception.
  27. */
  28. public Throwable thrownException() {
  29. return fThrownException;
  30. }
  31. /**
  32. * Returns a short description of the failure.
  33. */
  34. public String toString() {
  35. StringBuffer buffer= new StringBuffer();
  36. buffer.append(fFailedTest+": "+fThrownException.getMessage());
  37. return buffer.toString();
  38. }
  39. public String trace() {
  40. StringWriter stringWriter= new StringWriter();
  41. PrintWriter writer= new PrintWriter(stringWriter);
  42. thrownException().printStackTrace(writer);
  43. StringBuffer buffer= stringWriter.getBuffer();
  44. return buffer.toString();
  45. }
  46. public String exceptionMessage() {
  47. return thrownException().getMessage();
  48. }
  49. public boolean isFailure() {
  50. return thrownException() instanceof AssertionFailedError;
  51. }
  52. }