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