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 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/* TestCase.java -- A JUnit test case
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 gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
/**
* A JUnit test case.
*/
public abstract class TestCase
extends Assert
implements Test, Testlet
{
/**
* The name of the test case.
*/
private String fName;
/**
* Creates a new TestCase.
*/
public TestCase()
{
fName = null;
}
/**
* Creates a test case with a name.
*
* @param name the name of the test case
*/
public TestCase(String name)
{
fName = name;
}
/**
* Returns the number of test cases executed by this test.
*
* @return the number of test cases executed by this test
*/
public int countTestCases()
{
return 1;
}
/**
* Creates a default TestResult object.
*
* @return a default TestResult object
*/
protected TestResult createResult()
{
return new TestResult();
}
/**
* Creates a TestResult and runs a test by calling
* {@link #run(TestResult)}.
*
* @return the test results after running the test
*/
public TestResult run()
{
TestResult result = createResult();
run(result);
return result;
}
/**
* Runs the test and collects the result in the specified TestResult
* object.
*
* @param result the TestResult object to collect the results in
*/
public void run(TestResult result)
{
result.run(this);
}
/**
* Runs the bare test sequence. This calls {@link #setUp()}, executes
* the test by calling {@link #runTest}, and finally finishes with
* {@link #tearDown()}.
*
* @throws Throwable if a failure or error occured
*/
public void runBare()
throws Throwable
{
Throwable t = null;
setUp();
try
{
runTest();
}
catch (Throwable ex)
{
t = ex;
}
finally
{
try
{
tearDown();
}
catch (Throwable ex2)
{
if (t == null)
t = ex2;
}
}
if (t != null)
throw t;
}
/**
* Actually runs the test. The default implementation tries to find
* a method with the name specified in the constructor of this class.
* If such a method is found, it is invoked.
*
* @throws Throwable for test errors or failures
*/
protected void runTest()
throws Throwable
{
Method method = null;
try
{
method = getClass().getMethod(fName, (Class[]) null);
}
catch (NoSuchMethodException ex)
{
fail("Method " + fName + " not found");
}
if (! Modifier.isPublic(method.getModifiers()))
fail("Method " + fName + " must be public");
try
{
method.invoke(this, new Object[0]);
}
catch (InvocationTargetException ex)
{
ex.fillInStackTrace();
throw ex.getTargetException();
}
catch (IllegalAccessException ex)
{
ex.fillInStackTrace();
throw ex;
}
}
/**
* Prepares the test. This is called before {@link #runTest()}.
*
* @throws Exception if anything goes wrong
*/
protected void setUp()
throws Exception
{
// This is a hook with nothing to do itself.
}
/**
* Finishes the test. This is called after {@link #runTest()}.
*
* @throws Exception if anything goes wrong
*/
protected void tearDown()
throws Exception
{
// This is a hook with nothing to do itself.
}
/**
* Returns the name of the test case.
*
* @return the name of the test case
*/
public String getName()
{
return fName;
}
/**
* Sets the name of the test case.
*
* @param name the name to set
*/
public void setName(String name)
{
fName = name;
}
/**
* Returns a string representation of this test case.
*
* @return a string representation of this TestCase
*/
public String toString()
{
StringBuffer str = new StringBuffer();
str.append(getName());
str.append('(');
str.append(getClass().getName());
str.append(')');
return str.toString();
}
/**
* Implementation of Mauve's Testlet interface. This makes JUnit TestCases
* automatically executable by the Mauve test harness.
*/
public void test(TestHarness harness)
{
// Fetch the actual JUnit testsuite.
Test test = getTest();
// This is normally an instance of TestSuite.
if (test instanceof TestSuite)
{
TestSuite suite = (TestSuite) test;
suite.test(harness);
}
}
/**
* Performs a single test.
*
* @param harness the test harness to use
*/
void testSingle(TestHarness harness)
{
TestCase.harness = harness;
try
{
runBare();
}
catch (Throwable ex)
{
harness.fail(ex.getMessage());
harness.debug(ex);
}
TestCase.harness = null;
}
/**
* Fetches the test suite to be run. This tries to call a static
* suite() method on this class, and, if that fails, create a new TestSuite
* with this class as parameter, which will collect all test*() methods.
*
* @return the testsuite for this class or null, if no test suite could be
* created
*/
private Test getTest()
{
Class clazz = getClass();
Method suiteMethod = null;
Test test = null;
try
{
clazz.getMethod("suite", new Class[0]);
}
catch (Exception ex)
{
test = new TestSuite(clazz);
}
if (test == null && suiteMethod != null) // suite() method found.
{
try
{
test = (Test) suiteMethod.invoke(null, (Object[]) new Class[0]);
}
catch (InvocationTargetException ex)
{
test = null;
}
catch (IllegalAccessException ex)
{
test = null;
}
}
return test;
}
}
|