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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* TestSuite.java -- JUnit test suite
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.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
/**
* A collection of JUnit tests.
*/
public class TestSuite
implements Test, Testlet
{
/**
* The name of the test.
*/
private String fName;
/**
* The tests in this test suite.
*/
private Vector fTests;
/**
* Creates a new test suite.
*/
public TestSuite()
{
fTests = new Vector();
}
/**
* Creates a new test suite that loads its tests from the specified class.
*
* @param theClass the class to load the tests from
*/
public TestSuite(Class theClass)
{
this();
fName = theClass.getName();
Class clazz = theClass;
List names = new ArrayList();
while (Test.class.isAssignableFrom(clazz))
{
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++)
{
addTestMethod(methods[i], names, theClass);
}
clazz = clazz.getSuperclass();
}
}
/**
* Creates a new TestSuite with the specified name.
*
* @param name the name of the test suite
*/
public TestSuite(String name)
{
setName(name);
}
/**
* Creates a new TestSuite with the specified name that loads the tests from
* the specified class.
*
* @param theClass the class to load the tests from
* @param name the name of the test suite
*/
public TestSuite(Class theClass, String name)
{
this(theClass);
setName(name);
}
/**
* Adds the specified method to the test suite if appropriate.
*
* @param method the method to check
* @param names the list of names of already added methods
* @param theClass the test class
*/
private void addTestMethod(Method method, List names, Class theClass)
{
String name = method.getName();
if (! names.contains(name))
{
if (isPublicTestMethod(method))
{
names.add(name);
addTest(createTest(theClass, name));
}
}
}
/**
* Checks if the specified method is a test method and is public.
*
* @param method the method to check
*
* @return <code>true</code> if the method is a public test method,
* <code>false</code> otherwise
*/
private boolean isPublicTestMethod(Method method)
{
return isTestMethod(method) && Modifier.isPublic(method.getModifiers());
}
/**
* Checks if the specified method is a test method.
*
* @param method the method to check
*
* @return <code>true</code> if the method is a test method,
* <code>false</code> otherwise
*/
private boolean isTestMethod(Method method)
{
String name = method.getName();
Class[] params = method.getParameterTypes();
Class ret = method.getReturnType();
return params.length == 0 && name.startsWith("test")
&& ret.equals(Void.TYPE);
}
/**
* Creates a test for the specified test class and with the specified
* name.
*
* @param theClass the test class
* @param name the test name
*
* @return the test instance
*/
public static Test createTest(Class theClass, String name)
{
Constructor constructor = null;
Test test = null;
try
{
constructor = getTestConstructor(theClass);
}
catch (NoSuchMethodException ex)
{
test = null;
}
if (constructor != null)
{
Object o = null;
try
{
if (constructor.getParameterTypes().length == 0)
{
o = constructor.newInstance(new Object[0]);
if (o instanceof TestCase)
((TestCase) o).setName(name);
}
else
{
o = constructor.newInstance(new Object[]{ name });
}
}
catch (InstantiationException ex)
{
test = null;
}
catch (InvocationTargetException ex)
{
test = null;
}
catch (IllegalAccessException ex)
{
test = null;
}
test = (Test) o;
}
return test;
}
/**
* Returns the constructor for the specified test class.
*
* @param theClass the test class
*
* @return the constructor for the specified test class
*
* @throws NoSuchMethodException if no suitable constructor exists
*/
public static Constructor getTestConstructor(Class theClass)
throws NoSuchMethodException
{
Class[] args = { String.class };
try
{
return theClass.getConstructor(args);
}
catch (NoSuchMethodException ex)
{
// Search for a no-arg constructor below.
}
return theClass.getConstructor(new Class[0]);
}
/**
* Returns the number of tests in this test suite.
*
* @return the number of tests in this test suite
*/
public int countTestCases()
{
int count = 0;
for (Iterator i = fTests.iterator(); i.hasNext();)
{
Test test = (Test) i.next();
count += test.countTestCases();
}
return count;
}
/**
* Runs the test cases from this test suite.
*
* @param result the test results
*/
public void run(TestResult result)
{
for (Iterator i = fTests.iterator(); i.hasNext();)
{
if (! result.shouldStop())
{
Test test = (Test) i.next();
runTest(test, result);
}
}
}
/**
* Runs a single test.
*
* @param test the test to run
* @param result the test results
*/
public void runTest(Test test, TestResult result)
{
test.run(result);
}
/**
* Adds a single test to the test suite.
*
* @param test the test to add
*/
public void addTest(Test test)
{
fTests.add(test);
}
/**
* Adds tests from the specified class to the test suite.
*
* @param testClass the class from which to load tests to add
*/
public void addTestSuite(Class testClass)
{
fTests.add(new TestSuite(testClass));
}
/**
* Sets the name for this test.
*
* @param name the name to set
*/
public void setName(String name)
{
fName = name;
}
/**
* Returns the name of this test.
*
* @return the name of this test
*/
public String getName()
{
return fName;
}
/**
* This implements the Mauve Testlet interface. This implementation
* runs all tests in this testsuite that are runnable by Mauve.
*
* @param harness the Mauve test harness
*/
public void test(TestHarness harness)
{
for (Iterator i = fTests.iterator(); i.hasNext();)
{
Test test = (Test) i.next();
if (test instanceof TestCase)
((TestCase) test).testSingle(harness);
else if (test instanceof Testlet)
((Testlet) test).test(harness);
}
}
}
|