1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
/* TestResult.java -- Collects test results
Copyright (C) 2006 Roman Kennke (kennke@aicas.com)
This file is part of Mauve.
Mauve is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Mauve is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mauve; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/
// Tags: not-a-test
package junit.framework;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
/**
* Collects the results of a test run.
*/
public class TestResult
{
/**
* The errors from the test run.
*/
protected List fErrors;
/**
* The failures from the test run.
*/
protected List fFailures;
/**
* The test listeners.
*/
protected List fListeners;
/**
* The number of tests that have been run.
*/
protected int fRunTests;
/**
* Indicates if the test run should stop.
*/
private boolean fStop;
/**
* Creates a new TestResult object.
*/
public TestResult()
{
fErrors = new ArrayList();
fFailures = new ArrayList();
fListeners = new ArrayList();
fRunTests = 0;
fStop = false;
}
/**
* Runs the specified TestCase.
*
* @param test the test case to run
*/
protected void run(final TestCase test)
{
startTest(test);
Protectable protectable = new Protectable()
{
public void protect()
throws Throwable
{
test.runBare();
}
};
runProtected(test, protectable);
endTest(test);
}
/**
* Runs a test in a protected environment.
*
* @param test the test to run
* @param p the protectable
*/
public void runProtected(final TestCase test, Protectable p)
{
try
{
p.protect();
}
catch (AssertionFailedError e)
{
addFailure(test, e);
}
catch (ThreadDeath e)
{
throw e;
}
catch (Throwable e)
{
addError(test, e);
}
}
/**
* Starts the specified test. This counts the tests and informs
* interested listeners.
*
* @param test the test to start
*/
public void startTest(Test test)
{
final int count = test.countTestCases();
synchronized (this)
{
fRunTests += count;
}
for (Iterator i = cloneListeners().iterator(); i.hasNext();)
{
TestListener l = (TestListener) i.next();
l.startTest(test);
}
}
/**
* Ends the specified test. This informs interested listeners.
*
* @param test the test to end
*/
public void endTest(Test test)
{
for (Iterator i = cloneListeners().iterator(); i.hasNext();)
{
TestListener l = (TestListener) i.next();
l.endTest(test);
}
}
/**
* Adds a failure to the test result.
*
* @param test the failed test
* @param failure the test failure
*/
public synchronized void addFailure(Test test, AssertionFailedError failure)
{
fFailures.add(new TestFailure(test, failure));
for (Iterator i = cloneListeners().iterator(); i.hasNext();)
{
TestListener l = (TestListener) i.next();
l.addFailure(test, failure);
}
}
/**
* Adds an error to the test result.
*
* @param test the err'ed test
* @param failure the test error
*/
public synchronized void addError(Test test, Throwable failure)
{
fErrors.add(new TestFailure(test, failure));
for (Iterator i = cloneListeners().iterator(); i.hasNext();)
{
TestListener l = (TestListener) i.next();
l.addError(test, failure);
}
}
/**
* Adds a test listener.
*
* @param l the listener to add
*/
public synchronized void addListener(TestListener l)
{
fListeners.add(l);
}
/**
* Removes a test listener.
*
* @param l the listener to be removed
*/
public synchronized void removeListener(TestListener l)
{
fListeners.remove(l);
}
/**
* Returns the number of errors.
*
* @return the number of errors
*/
public synchronized int errorCount()
{
return fErrors.size();
}
/**
* Returns the errors in this test result.
*
* @return the errors in this test result
*/
public synchronized Enumeration errors()
{
return Collections.enumeration(fErrors);
}
/**
* Returns the number of failures.
*
* @return the number of failures
*/
public synchronized int failureCount()
{
return fFailures.size();
}
/**
* Returns the failures in this test result.
*
* @return the failures in this test result
*/
public synchronized Enumeration failures()
{
return Collections.enumeration(fFailures);
}
/**
* Returns the number of tests that have been run.
*
* @return the number of tests that have been run
*/
public synchronized int runCount()
{
return fRunTests;
}
/**
* Returns <code>true</code> when the test should stop, <code>false</code>
* otherwise.
*
* @return <code>true</code> when the test should stop, <code>false</code>
* otherwise
*/
public synchronized boolean shouldStop()
{
return fStop;
}
/**
* Stops the test.
*/
public synchronized void stop()
{
fStop = true;
}
/**
* Returns <code>true</code> when the test had no errors and no failures,
* <code>false</code> otherwise.
*
* @return <code>true</code> when the test had no errors and no failures,
* <code>false</code> otherwise
*/
public synchronized boolean wasSuccessful()
{
return failureCount() == 0 && errorCount() == 0;
}
/**
* Returns a cloned listener list.
*
* @return a cloned listener list
*/
private synchronized List cloneListeners()
{
List copy = new ArrayList();
copy.addAll(fListeners);
return copy;
}
}
|