1 package junitx.framework; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestResult; 6 7 import java.io.PrintWriter ; 8 import java.io.StringWriter ; 9 import java.lang.reflect.Constructor ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.lang.reflect.Method ; 12 import java.lang.reflect.Modifier ; 13 import java.util.Enumeration ; 14 import java.util.List ; 15 import java.util.Vector ; 16 17 21 class TestSuite implements Test { 22 23 protected List fTests = new Vector (10); 24 private String fName; 25 26 29 public TestSuite() { 30 } 31 32 38 public TestSuite(final Class theClass) { 39 fName = theClass.getName(); 40 Constructor constructor = null; 41 try { 42 constructor = getConstructor(theClass); 43 } catch (NoSuchMethodException e) { 44 addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name)")); 45 return; 46 } 47 48 if (!Modifier.isPublic(theClass.getModifiers())) { 49 addTest(warning("Class " + theClass.getName() + " is not public")); 50 return; 51 } 52 53 Class superClass = theClass; 54 Vector names = new Vector (); 55 while (Test.class.isAssignableFrom(superClass)) { 56 Method [] methods = superClass.getDeclaredMethods(); 57 for (int i = 0; i < methods.length; i++) { 58 addTestMethod(methods[i], names, constructor); 59 } 60 superClass = superClass.getSuperclass(); 61 } 62 if (fTests.size() == 0) { 63 addTest(warning("No tests found in " + theClass.getName())); 64 } 65 } 66 67 70 public TestSuite(String name) { 71 fName = name; 72 } 73 74 77 public void addTest(Test test) { 78 fTests.add(test); 79 } 80 81 84 public void addTestSuite(Class testClass) { 85 addTest(new TestSuite(testClass)); 86 } 87 88 private void addTestMethod(Method m, List names, Constructor constructor) { 89 String name = m.getName(); 90 if (names.contains(name)) { 91 return; 92 } 93 94 if (isPublicTestMethod(m)) { 95 names.add(name); 96 97 Object [] args = new Object []{name}; 98 try { 99 addTest((Test) constructor.newInstance(args)); 100 } catch (InstantiationException e) { 101 addTest(warning("Cannot instantiate test case: " + name + " (" + exceptionToString(e) + ")")); 102 } catch (InvocationTargetException e) { 103 addTest(warning("Exception in constructor: " + name + " (" + exceptionToString(e.getTargetException()) + ")")); 104 } catch (IllegalAccessException e) { 105 addTest(warning("Cannot access test case: " + name + " (" + exceptionToString(e) + ")")); 106 } 107 108 } else { if (isTestMethod(m)) { 110 addTest(warning("Test method isn't public: " + m.getName())); 111 } 112 } 113 } 114 115 118 private String exceptionToString(Throwable t) { 119 StringWriter stringWriter = new StringWriter (); 120 PrintWriter writer = new PrintWriter (stringWriter); 121 t.printStackTrace(writer); 122 return stringWriter.toString(); 123 124 } 125 126 129 public int countTestCases() { 130 int count = 0; 131 for (Enumeration e = tests(); e.hasMoreElements();) { 132 Test test = (Test) e.nextElement(); 133 count = count + test.countTestCases(); 134 } 135 return count; 136 } 137 138 142 private Constructor getConstructor(Class theClass) throws NoSuchMethodException { 143 Class [] args = {String .class}; 144 return theClass.getConstructor(args); 145 } 146 147 149 private boolean isPublicTestMethod(Method m) { 150 return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); 151 } 152 153 155 private boolean isTestMethod(Method m) { 156 String name = m.getName(); 157 Class [] parameters = m.getParameterTypes(); 158 Class returnType = m.getReturnType(); 159 return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); 160 } 161 162 165 public void run(TestResult result) { 166 for (Enumeration e = tests(); e.hasMoreElements();) { 167 if (result.shouldStop()) { 168 break; 169 } 170 Test test = (Test) e.nextElement(); 171 runTest(test, result); 172 } 173 } 174 175 public void runTest(Test test, TestResult result) { 176 test.run(result); 177 } 178 179 182 public Test testAt(int index) { 183 return (Test) fTests.get(index); 184 } 185 186 189 public int testCount() { 190 return fTests.size(); 191 } 192 193 196 public Enumeration tests() { 197 return new Vector (fTests).elements(); 199 } 200 201 203 public String toString() { 204 if (getName() != null) { 205 return getName(); 206 } 207 return super.toString(); 208 } 209 210 214 public void setName(String name) { 215 fName = name; 216 } 217 218 223 public String getName() { 224 return fName; 225 } 226 227 230 private Test warning(final String message) { 231 return new TestCase("warning") { 232 protected void runTest() { 233 fail(message); 234 } 235 }; 236 } 237 238 } 239 240 | Popular Tags |