carbidecpp20devenv/plugins/org.eclipse.test.source_3.3.0.v20080507/src/org.junit4_4.3.1/junitsrc/org/junit/runner/notification/Failure.java
author Deepak Modgil <Deepak.Modgil@Nokia.com>
Fri, 03 Apr 2009 17:08:57 +0100
changeset 0 20e4ed35fd3f
permissions -rw-r--r--
DP tools release version Revision: 200912

package org.junit.runner.notification;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.junit.runner.Description;

/**
 * A <code>Failure</code> holds a description of the failed test and the 
 * exception that was thrown while running it. In most cases the {@link org.junit.runner.Description}
 * will be of a single test. However, if problems are encountered while constructing the
 * test (for example, if a {@link org.junit.BeforeClass} method is not static), it may describe
 * something other than a single test.
 */
public class Failure {
	private final Description fDescription;
	private Throwable fThrownException;

	/**
	 * Constructs a <code>Failure</code> with the given description and exception.
	 * @param description a {@link org.junit.runner.Description} of the test that failed
	 * @param thrownException the exception that was thrown while running the test
	 */
	public Failure(Description description, Throwable thrownException) {
		fThrownException = thrownException;
		fDescription= description;
	}

	/**
	 * @return a user-understandable label for the test
	 */
	public String getTestHeader() {
		return fDescription.getDisplayName();
	}

	/**
	 * @return the raw description of the context of the failure.
	 */
	public Description getDescription() {
		return fDescription;
	}

	/**
	 * @return the exception thrown
	 */

	public Throwable getException() {
	    return fThrownException;
	}

	@Override
	public String toString() {
	    StringBuffer buffer= new StringBuffer();
	    buffer.append(getTestHeader() + ": "+fThrownException.getMessage());
	    return buffer.toString();
	}

	/**
	 * Convenience method
	 * @return the printed form of the exception
	 */
	public String getTrace() {
		StringWriter stringWriter= new StringWriter();
		PrintWriter writer= new PrintWriter(stringWriter);
		getException().printStackTrace(writer);
		StringBuffer buffer= stringWriter.getBuffer();
		return buffer.toString();
	}

	/**
	 * Convenience method
	 * @return the message of the thrown exception
	 */
	public String getMessage() {
		return getException().getMessage();
	}
}