View Javadoc

1   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2   // This file is copied from JUnit with no modification.
3   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4   package junit.framework;
5   
6   import java.util.Enumeration;
7   import java.util.Vector;
8   
9   /**
10   * A <code>TestResult</code> collects the results of executing
11   * a test case. It is an instance of the Collecting Parameter pattern.
12   * The test framework distinguishes between <i>failures</i> and <i>errors</i>.
13   * A failure is anticipated and checked for with assertions. Errors are
14   * unanticipated problems like an <code>ArrayIndexOutOfBoundsException</code>.
15   *
16   * @see Test
17   */
18  public class TestResult extends Object {
19  	protected Vector fFailures;
20  	protected Vector fErrors;
21  	protected Vector fListeners;
22  	protected int fRunTests;
23  	private boolean fStop;
24  	
25  	public TestResult() {
26  		fFailures= new Vector();
27  		fErrors= new Vector();
28  		fListeners= new Vector();
29  		fRunTests= 0;
30  		fStop= false;
31  	}
32  	/**
33  	 * Adds an error to the list of errors. The passed in exception
34  	 * caused the error.
35  	 */
36  	public synchronized void addError(Test test, Throwable t) {
37  		fErrors.addElement(new TestFailure(test, t));
38  		for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
39  			((TestListener)e.nextElement()).addError(test, t);
40  		}
41  	}
42  	/**
43  	 * Adds a failure to the list of failures. The passed in exception
44  	 * caused the failure.
45  	 */
46  	public synchronized void addFailure(Test test, AssertionFailedError t) {
47  		fFailures.addElement(new TestFailure(test, t));
48  		for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
49  			((TestListener)e.nextElement()).addFailure(test, t);
50  		}
51  	}
52  	/**
53  	 * Registers a TestListener
54  	 */
55  	public synchronized void addListener(TestListener listener) {
56  		fListeners.addElement(listener);
57  	}
58  	/**
59  	 * Unregisters a TestListener
60  	 */
61  	public synchronized void removeListener(TestListener listener) {
62  		fListeners.removeElement(listener);
63  	}
64  	/**
65  	 * Returns a copy of the listeners.
66  	 */
67  	private synchronized Vector cloneListeners() {
68  		Vector clone = new Vector();
69  		for (int i = 0; i < fListeners.size(); i++) {
70  			clone.addElement(fListeners.elementAt(i));
71  		}
72  		return clone;
73  	}
74  	/**
75  	 * Informs the result that a test was completed.
76  	 */
77  	public void endTest(Test test) {
78  		for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
79  			((TestListener)e.nextElement()).endTest(test);
80  		}
81  	}
82  	/**
83  	 * Gets the number of detected errors.
84  	 */
85  	public synchronized int errorCount() {
86  		return fErrors.size();
87  	}
88  	/**
89  	 * Returns an Enumeration for the errors
90  	 */
91  	public synchronized Enumeration errors() {
92  		return fErrors.elements();
93  	}
94  	/**
95  	 * Gets the number of detected failures.
96  	 */
97  	public synchronized int failureCount() {
98  		return fFailures.size();
99  	}
100 	/**
101 	 * Returns an Enumeration for the failures
102 	 */
103 	public synchronized Enumeration failures() {
104 		return fFailures.elements();
105 	}
106 	/**
107 	 * Runs a TestCase.
108 	 */
109 	protected void run(final TestCase test) {
110 		startTest(test);
111 		Protectable p= new Protectable() {
112 			public void protect() throws Throwable {
113 				test.runBare();
114 			}
115 		};
116 		runProtected(test, p);
117 
118 		endTest(test);
119 	}
120 	/**
121 	 * Gets the number of run tests.
122 	 */
123 	public synchronized int runCount() {
124 		return fRunTests;
125 	}
126 	/**
127 	 * Runs a TestCase.
128 	 */
129 	public void runProtected(final Test test, Protectable p) {
130 		try {
131 			p.protect();
132 		} 
133 		catch (AssertionFailedError e) {
134 			addFailure(test, e);
135 		}
136 		catch (Throwable e) {
137 			addError(test, e);
138 		}
139 	}
140 	/**
141 	 * Checks whether the test run should stop
142 	 */
143 	public synchronized boolean shouldStop() {
144 		return fStop;
145 	}
146 	/**
147 	 * Informs the result that a test will be started.
148 	 */
149 	public void startTest(Test test) {
150 		final int count= test.countTestCases();
151 		synchronized(this) {
152 			fRunTests+= count;
153 		}
154 		for (Enumeration e= cloneListeners().elements(); e.hasMoreElements(); ) {
155 			((TestListener)e.nextElement()).startTest(test);
156 		}
157 	}
158 	/**
159 	 * Marks that the test run should stop.
160 	 */
161 	public synchronized void stop() {
162 		fStop= true;
163 	}
164 	/**
165 	 * Returns whether the entire test was successful or not.
166 	 */
167 	public synchronized boolean wasSuccessful() {
168 		return failureCount() == 0 && errorCount() == 0;
169 	}
170 }