3 import java.lang.reflect.*;
5 import junit.framework.*;
8 * An implementation of a TestCollector that loads
9 * all classes on the class path and tests whether
10 * it is assignable from Test or provides a static suite method.
13 public class LoadingTestCollector extends ClassPathTestCollector {
15 TestCaseClassLoader fLoader;
17 public LoadingTestCollector() {
18 fLoader= new TestCaseClassLoader();
21 protected boolean isTestClass(String classFileName) {
23 if (classFileName.endsWith(".class")) {
24 Class testClass= classFromFile(classFileName);
25 return (testClass != null) && isTestClass(testClass);
28 catch (ClassNotFoundException expected) {
30 catch (NoClassDefFoundError notFatal) {
35 Class classFromFile(String classFileName) throws ClassNotFoundException {
36 String className= classNameFromFile(classFileName);
37 if (!fLoader.isExcluded(className))
38 return fLoader.loadClass(className, false);
42 boolean isTestClass(Class testClass) {
43 if (hasSuiteMethod(testClass))
45 if (Test.class.isAssignableFrom(testClass) &&
46 Modifier.isPublic(testClass.getModifiers()) &&
47 hasPublicConstructor(testClass))
52 boolean hasSuiteMethod(Class testClass) {
54 testClass.getMethod(BaseTestRunner.SUITE_METHODNAME, new Class[0]);
55 } catch(Exception e) {
61 boolean hasPublicConstructor(Class testClass) {
63 TestSuite.getTestConstructor(testClass);
64 } catch(NoSuchMethodException e) {