1 54 55 package junitx.util; 56 57 import java.lang.reflect.InvocationTargetException ; 58 import java.lang.reflect.Method ; 59 import java.lang.reflect.Modifier ; 60 import java.util.List ; 61 62 import junit.framework.Test; 63 import junit.framework.TestCase; 64 import junit.framework.TestSuite; 65 66 70 abstract class AbstractSuiteBuilder { 71 72 private TestFilter filter; 73 74 public AbstractSuiteBuilder(TestFilter filter) { 75 this.filter = filter; 76 } 77 78 81 public void setFilter(TestFilter filter) { 82 this.filter = filter; 83 } 84 85 88 protected boolean isTestClass(String path) { 89 boolean temp = path.endsWith(".class") && (path.indexOf("$") < 0); 90 if (filter == null) { 91 return temp; 92 } else { 93 return temp && filter.include(path); 94 } 95 } 96 97 101 protected void merge(List classenames, TestSuite suite) throws ClassNotFoundException , IllegalAccessException , InvocationTargetException { 102 for (int i = 0; i < classenames.size(); i++) { 103 String classname = (String ) classenames.get(i); 104 Class cls = Class.forName(classname); 105 106 if (junit.framework.TestCase.class.isAssignableFrom(cls) && ((this.filter == null) || (this.filter.include(cls)))) { 107 110 try { 111 Method suiteMethod = cls.getMethod("suite", new Class [0]); 112 if (!Modifier.isPublic(suiteMethod.getModifiers())) { 113 suite.addTest(warning("Method 'suite' should be public (class " + cls.getName() + ")")); 114 } else if (!Modifier.isStatic(suiteMethod.getModifiers())) { 115 suite.addTest(warning("Method 'suite' should be static (class " + cls.getName() + ")")); 116 } else if (!Test.class.isAssignableFrom(suiteMethod.getReturnType())) { 117 suite.addTest(warning("Method 'suite' should have a Test return type (class " + cls.getName() + ")")); 118 } else if (suiteMethod.getParameterTypes().length != 0) { 119 suite.addTest(warning("Method 'suite' should have no arguments (class " + cls.getName() + ")")); 120 } else { 121 Test test = (Test) suiteMethod.invoke(null, new Class [0]); 122 suite.addTest(test); 123 } 124 } catch (NoSuchMethodException e) { 125 suite.addTest(new TestSuite(cls)); 126 } 127 } 128 } 129 } 130 131 134 private static Test warning(final String message) { 135 return new TestCase("warning") { 136 protected void runTest() { 137 fail(message); 138 } 139 }; 140 } 141 142 } | Popular Tags |