aboutsummaryrefslogtreecommitdiff
path: root/src/junit/framework/TestSuite.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/junit/framework/TestSuite.java')
-rw-r--r--src/junit/framework/TestSuite.java106
1 files changed, 60 insertions, 46 deletions
diff --git a/src/junit/framework/TestSuite.java b/src/junit/framework/TestSuite.java
index 5a96bc2..336efd1 100644
--- a/src/junit/framework/TestSuite.java
+++ b/src/junit/framework/TestSuite.java
@@ -6,11 +6,13 @@ 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.Enumeration;
+import java.util.List;
import java.util.Vector;
/**
- * A <code>TestSuite</code> is a <code>Composite</code> of Tests.
+ * <p>A <code>TestSuite</code> is a <code>Composite</code> of Tests.
* It runs a collection of test cases. Here is an example using
* the dynamic test definition.
* <pre>
@@ -18,20 +20,25 @@ import java.util.Vector;
* suite.addTest(new MathTest("testAdd"));
* suite.addTest(new MathTest("testDivideByZero"));
* </pre>
- * Alternatively, a TestSuite can extract the tests to be run automatically.
+ * </p>
+ *
+ * <p>Alternatively, a TestSuite can extract the tests to be run automatically.
* To do so you pass the class of your TestCase class to the
* TestSuite constructor.
* <pre>
* TestSuite suite= new TestSuite(MathTest.class);
* </pre>
- * This constructor creates a suite with all the methods
- * starting with "test" that take no arguments.
- * <p>
- * A final option is to do the same for a large array of test classes.
+ * </p>
+ *
+ * <p>This constructor creates a suite with all the methods
+ * starting with "test" that take no arguments.</p>
+ *
+ * <p>A final option is to do the same for a large array of test classes.
* <pre>
* Class[] testClasses = { MathTest.class, AnotherTest.class }
* TestSuite suite= new TestSuite(testClasses);
* </pre>
+ * </p>
*
* @see Test
*/
@@ -41,8 +48,8 @@ public class TestSuite implements Test {
* ...as the moon sets over the early morning Merlin, Oregon
* mountains, our intrepid adventurers type...
*/
- static public Test createTest(Class theClass, String name) {
- Constructor constructor;
+ static public Test createTest(Class<?> theClass, String name) {
+ Constructor<?> constructor;
try {
constructor= getTestConstructor(theClass);
} catch (NoSuchMethodException e) {
@@ -71,10 +78,9 @@ public class TestSuite implements Test {
* Gets a constructor which takes a single String as
* its argument or a no arg constructor.
*/
- public static Constructor getTestConstructor(Class theClass) throws NoSuchMethodException {
- Class[] args= { String.class };
+ public static Constructor<?> getTestConstructor(Class<?> theClass) throws NoSuchMethodException {
try {
- return theClass.getConstructor(args);
+ return theClass.getConstructor(String.class);
} catch (NoSuchMethodException e) {
// fall through
}
@@ -86,6 +92,7 @@ public class TestSuite implements Test {
*/
public static Test warning(final String message) {
return new TestCase("warning") {
+ @Override
protected void runTest() {
fail(message);
}
@@ -100,11 +107,11 @@ public class TestSuite implements Test {
PrintWriter writer= new PrintWriter(stringWriter);
t.printStackTrace(writer);
return stringWriter.toString();
-
}
+
private String fName;
- private Vector fTests= new Vector(10);
+ private Vector<Test> fTests= new Vector<Test>(10); // Cannot convert this to List because it is used directly by some test runners
/**
* Constructs an empty TestSuite.
@@ -115,10 +122,14 @@ public class TestSuite implements Test {
/**
* Constructs a TestSuite from the given class. Adds all the methods
* starting with "test" as test cases to the suite.
- * Parts of this method was written at 2337 meters in the Hueffihuette,
+ * Parts of this method were written at 2337 meters in the Hueffihuette,
* Kanton Uri
*/
- public TestSuite(final Class theClass) {
+ public TestSuite(final Class<?> theClass) {
+ addTestsFromTestCase(theClass);
+ }
+
+ private void addTestsFromTestCase(final Class<?> theClass) {
fName= theClass.getName();
try {
getTestConstructor(theClass); // Avoid generating multiple error messages
@@ -132,13 +143,11 @@ public class TestSuite implements Test {
return;
}
- Class superClass= theClass;
- Vector names= new Vector();
+ Class<?> superClass= theClass;
+ List<String> names= new ArrayList<String>();
while (Test.class.isAssignableFrom(superClass)) {
- Method[] methods= superClass.getDeclaredMethods();
- for (int i= 0; i < methods.length; i++) {
- addTestMethod(methods[i], names, theClass);
- }
+ for (Method each : superClass.getDeclaredMethods())
+ addTestMethod(each, names, theClass);
superClass= superClass.getSuperclass();
}
if (fTests.size() == 0)
@@ -149,7 +158,7 @@ public class TestSuite implements Test {
* Constructs a TestSuite from the given class with the given name.
* @see TestSuite#TestSuite(Class)
*/
- public TestSuite(Class theClass, String name) {
+ public TestSuite(Class<? extends TestCase> theClass, String name) {
this(theClass);
setName(name);
}
@@ -163,18 +172,25 @@ public class TestSuite implements Test {
/**
* Constructs a TestSuite from the given array of classes.
- * @param classes
+ * @param classes {@link TestCase}s
*/
- public TestSuite (Class[] classes) {
- for (int i= 0; i < classes.length; i++)
- addTest(new TestSuite(classes[i]));
+ public TestSuite (Class<?>... classes) {
+ for (Class<?> each : classes)
+ addTest(testCaseForClass(each));
+ }
+
+ private Test testCaseForClass(Class<?> each) {
+ if (TestCase.class.isAssignableFrom(each))
+ return new TestSuite(each.asSubclass(TestCase.class));
+ else
+ return warning(each.getCanonicalName() + " does not extend TestCase");
}
/**
* Constructs a TestSuite from the given array of classes with the given name.
* @see TestSuite#TestSuite(Class[])
*/
- public TestSuite(Class[] classes, String name) {
+ public TestSuite(Class<? extends TestCase>[] classes, String name) {
this(classes);
setName(name);
}
@@ -183,13 +199,13 @@ public class TestSuite implements Test {
* Adds a test to the suite.
*/
public void addTest(Test test) {
- fTests.addElement(test);
+ fTests.add(test);
}
/**
* Adds the tests from the given class to the suite
*/
- public void addTestSuite(Class testClass) {
+ public void addTestSuite(Class<? extends TestCase> testClass) {
addTest(new TestSuite(testClass));
}
@@ -198,10 +214,8 @@ public class TestSuite implements Test {
*/
public int countTestCases() {
int count= 0;
- for (Enumeration e= tests(); e.hasMoreElements(); ) {
- Test test= (Test)e.nextElement();
- count= count + test.countTestCases();
- }
+ for (Test each : fTests)
+ count+= each.countTestCases();
return count;
}
@@ -218,11 +232,10 @@ public class TestSuite implements Test {
* Runs the tests and collects their result in a TestResult.
*/
public void run(TestResult result) {
- for (Enumeration e= tests(); e.hasMoreElements(); ) {
+ for (Test each : fTests) {
if (result.shouldStop() )
break;
- Test test= (Test)e.nextElement();
- runTest(test, result);
+ runTest(each, result);
}
}
@@ -232,7 +245,7 @@ public class TestSuite implements Test {
/**
* Sets the name of the suite.
- * @param name The name to set
+ * @param name the name to set
*/
public void setName(String name) {
fName= name;
@@ -242,7 +255,7 @@ public class TestSuite implements Test {
* Returns the test at the given index
*/
public Test testAt(int index) {
- return (Test)fTests.elementAt(index);
+ return fTests.get(index);
}
/**
@@ -255,28 +268,29 @@ public class TestSuite implements Test {
/**
* Returns the tests as an enumeration
*/
- public Enumeration tests() {
+ public Enumeration<Test> tests() {
return fTests.elements();
}
/**
*/
+ @Override
public String toString() {
if (getName() != null)
return getName();
return super.toString();
}
- private void addTestMethod(Method m, Vector names, Class theClass) {
+ private void addTestMethod(Method m, List<String> names, Class<?> theClass) {
String name= m.getName();
if (names.contains(name))
return;
if (! isPublicTestMethod(m)) {
if (isTestMethod(m))
- addTest(warning("Test method isn't public: "+m.getName()));
+ addTest(warning("Test method isn't public: "+ m.getName() + "(" + theClass.getCanonicalName() + ")"));
return;
}
- names.addElement(name);
+ names.add(name);
addTest(createTest(theClass, name));
}
@@ -285,9 +299,9 @@ public class TestSuite implements Test {
}
private boolean isTestMethod(Method m) {
- String name= m.getName();
- Class[] parameters= m.getParameterTypes();
- Class returnType= m.getReturnType();
- return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE);
+ return
+ m.getParameterTypes().length == 0 &&
+ m.getName().startsWith("test") &&
+ m.getReturnType().equals(Void.TYPE);
}
} \ No newline at end of file