carbidecpp22devenv/plugins/org.eclipse.test.source_3.5.0.r20080925/src/org.junit4_4.5.0.v20090423/junitsrc/org/junit/runner/notification/Failure.java
author cawthron
Fri, 04 Dec 2009 11:49:54 -0600
changeset 636 3ef299ba838f
permissions -rw-r--r--
add files for RCL_2_2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
636
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     1
package org.junit.runner.notification;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     2
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     3
import java.io.PrintWriter;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     4
import java.io.StringWriter;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     5
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     6
import org.junit.runner.Description;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     7
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     8
/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
     9
 * A <code>Failure</code> holds a description of the failed test and the
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    10
 * exception that was thrown while running it. In most cases the {@link org.junit.runner.Description}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    11
 * will be of a single test. However, if problems are encountered while constructing the
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    12
 * test (for example, if a {@link org.junit.BeforeClass} method is not static), it may describe
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    13
 * something other than a single test.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    14
 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    15
public class Failure {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    16
	private final Description fDescription;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    17
	private final Throwable fThrownException;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    18
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    19
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    20
	 * Constructs a <code>Failure</code> with the given description and exception.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    21
	 * @param description a {@link org.junit.runner.Description} of the test that failed
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    22
	 * @param thrownException the exception that was thrown while running the test
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    23
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    24
	public Failure(Description description, Throwable thrownException) {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    25
		fThrownException = thrownException;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    26
		fDescription= description;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    27
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    28
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    29
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    30
	 * @return a user-understandable label for the test
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    31
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    32
	public String getTestHeader() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    33
		return fDescription.getDisplayName();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    34
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    35
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    36
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    37
	 * @return the raw description of the context of the failure.
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    38
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    39
	public Description getDescription() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    40
		return fDescription;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    41
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    42
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    43
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    44
	 * @return the exception thrown
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    45
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    46
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    47
	public Throwable getException() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    48
	    return fThrownException;
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    49
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    50
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    51
	@Override
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    52
	public String toString() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    53
	    StringBuffer buffer= new StringBuffer();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    54
	    buffer.append(getTestHeader() + ": "+fThrownException.getMessage());
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    55
	    return buffer.toString();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    56
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    57
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    58
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    59
	 * Convenience method
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    60
	 * @return the printed form of the exception
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    61
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    62
	public String getTrace() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    63
		StringWriter stringWriter= new StringWriter();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    64
		PrintWriter writer= new PrintWriter(stringWriter);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    65
		getException().printStackTrace(writer);
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    66
		StringBuffer buffer= stringWriter.getBuffer();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    67
		return buffer.toString();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    68
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    69
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    70
	/**
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    71
	 * Convenience method
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    72
	 * @return the message of the thrown exception
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    73
	 */
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    74
	public String getMessage() {
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    75
		return getException().getMessage();
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    76
	}
3ef299ba838f add files for RCL_2_2
cawthron
parents:
diff changeset
    77
}