1b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpackage org.junit.runner.notification; 2b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 3b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.io.PrintWriter; 4b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.io.Serializable; 5b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport java.io.StringWriter; 6b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 7b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotimport org.junit.runner.Description; 8b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 9b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot/** 10b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * A <code>Failure</code> holds a description of the failed test and the 11b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * exception that was thrown while running it. In most cases the {@link org.junit.runner.Description} 12b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * will be of a single test. However, if problems are encountered while constructing the 13b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * test (for example, if a {@link org.junit.BeforeClass} method is not static), it may describe 14b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * something other than a single test. 15b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 16b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabotpublic class Failure implements Serializable { 17b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private static final long serialVersionUID = 1L; 18b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private final Description fDescription; 19b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot private final Throwable fThrownException; 20b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 21b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 22b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Constructs a <code>Failure</code> with the given description and exception. 23b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @param description a {@link org.junit.runner.Description} of the test that failed 24b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @param thrownException the exception that was thrown while running the test 25b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 26b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public Failure(Description description, Throwable thrownException) { 27b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot fThrownException = thrownException; 28b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot fDescription= description; 29b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 30b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 31b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 32b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @return a user-understandable label for the test 33b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 34b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public String getTestHeader() { 35b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return fDescription.getDisplayName(); 36b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 37b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 38b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 39b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @return the raw description of the context of the failure. 40b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 41b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public Description getDescription() { 42b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return fDescription; 43b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 44b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 45b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 46b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @return the exception thrown 47b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 48b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 49b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public Throwable getException() { 50b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return fThrownException; 51b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 52b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 53b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot @Override 54b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public String toString() { 55b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot StringBuffer buffer= new StringBuffer(); 56b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot buffer.append(getTestHeader() + ": "+fThrownException.getMessage()); 57b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return buffer.toString(); 58b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 59b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 60b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 61b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Convenience method 62b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @return the printed form of the exception 63b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 64b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public String getTrace() { 65b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot StringWriter stringWriter= new StringWriter(); 66b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot PrintWriter writer= new PrintWriter(stringWriter); 67b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot getException().printStackTrace(writer); 68b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot StringBuffer buffer= stringWriter.getBuffer(); 69b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return buffer.toString(); 70b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 71b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot 72b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot /** 73b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * Convenience method 74b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot * @return the message of the thrown exception 75b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot */ 76b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot public String getMessage() { 77b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot return getException().getMessage(); 78b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot } 79b3823db9f1192d8c81345740b3e65bd6738ba55bBrett Chabot} 80