1 20 package org.apache.cactus.internal; 21 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Modifier ; 28 import java.util.Enumeration ; 29 import java.util.Vector ; 30 31 import org.apache.cactus.ServletTestCase; 32 33 import junit.framework.Test; 34 import junit.framework.TestCase; 35 import junit.framework.TestResult; 36 37 44 public abstract class AbstractTestSuite implements Test 45 { 46 49 private Vector tests = new Vector (10); 50 51 54 private String name; 55 56 59 public AbstractTestSuite() 60 { 61 } 62 63 66 public AbstractTestSuite(final Class theClass) 67 { 68 setName(theClass.getName()); 69 Constructor constructor; 70 try 71 { 72 constructor = getTestConstructor(theClass); 74 } 75 catch (NoSuchMethodException e) 76 { 77 addTest(warning("Class " + theClass.getName() 78 + " has no public constructor TestCase(String name)")); 79 return; 80 } 81 82 if (!Modifier.isPublic(theClass.getModifiers())) 83 { 84 addTest(warning("Class " + theClass.getName() + " is not public")); 85 return; 86 } 87 88 Class superClass = theClass; 89 Vector names = new Vector (); 90 while (Test.class.isAssignableFrom(superClass)) 91 { 92 Method [] methods = superClass.getDeclaredMethods(); 93 for (int i = 0; i < methods.length; i++) 94 { 95 addTestMethod(methods[i], names, constructor); 96 } 97 superClass = superClass.getSuperclass(); 98 } 99 if (this.tests.size() == 0) 100 { 101 addTest(warning("No tests found in " + theClass.getName())); 102 } 103 } 104 105 108 public AbstractTestSuite(String theName) 109 { 110 setName(theName); 111 } 112 113 116 protected void addTest(Test theTest) 117 { 118 this.tests.addElement(theTest); 119 } 120 121 124 protected void addTestSuite(Class theTestClass) 125 { 126 addTest(createTestSuite(theTestClass)); 127 } 128 129 132 private void addTestMethod(Method theMethod, Vector theNames, 133 Constructor theConstructor) 134 { 135 String name = theMethod.getName(); 136 if (theNames.contains(name)) 137 { 138 return; 139 } 140 if (isPublicTestMethod(theMethod)) 141 { 142 theNames.addElement(name); 143 144 try 145 { 146 Object constructorInstance; 148 if (theConstructor.getParameterTypes().length == 0) 149 { 150 constructorInstance = theConstructor.newInstance( 151 new Object [0]); 152 if (constructorInstance instanceof TestCase) 153 { 154 ((TestCase) constructorInstance).setName(name); 155 } 156 } 157 else 158 { 159 constructorInstance = theConstructor.newInstance( 160 new Object [] {name}); 161 } 162 addTest(new ServletTestCase(name, (Test) constructorInstance)); 163 } 164 catch (InstantiationException e) 165 { 166 addTest(warning("Cannot instantiate test case: " + name 167 + " (" + exceptionToString(e) + ")")); 168 } 169 catch (InvocationTargetException e) 170 { 171 addTest(warning("Exception in constructor: " + name + " (" 172 + exceptionToString(e.getTargetException()) + ")")); 173 } 174 catch (IllegalAccessException e) 175 { 176 addTest(warning("Cannot access test case: " + name + " (" 177 + exceptionToString(e) + ")")); 178 } 179 } 180 else 181 { 182 if (isTestMethod(theMethod)) 184 { 185 addTest(warning("Test method isn't public: " 186 + theMethod.getName())); 187 } 188 } 189 } 190 191 194 private String exceptionToString(Throwable theThrowable) 195 { 196 StringWriter stringWriter = new StringWriter (); 197 PrintWriter writer = new PrintWriter (stringWriter); 198 theThrowable.printStackTrace(writer); 199 return stringWriter.toString(); 200 } 201 202 205 public int countTestCases() 206 { 207 int count = 0; 208 for (Enumeration e = tests(); e.hasMoreElements();) 209 { 210 Test test = (Test) e.nextElement(); 211 count = count + test.countTestCases(); 212 } 213 return count; 214 } 215 216 219 private boolean isPublicTestMethod(Method theMethod) 220 { 221 return isTestMethod(theMethod) 222 && Modifier.isPublic(theMethod.getModifiers()); 223 } 224 225 228 private boolean isTestMethod(Method theMethod) 229 { 230 String name = theMethod.getName(); 231 Class [] parameters = theMethod.getParameterTypes(); 232 Class returnType = theMethod.getReturnType(); 233 return parameters.length == 0 234 && name.startsWith("test") 235 && returnType.equals(Void.TYPE); 236 } 237 238 241 public void run(TestResult theResult) 242 { 243 for (Enumeration e = tests(); e.hasMoreElements();) 244 { 245 if (theResult.shouldStop()) 246 { 247 break; 248 } 249 Test test = (Test) e.nextElement(); 250 runTest(test, theResult); 251 } 252 } 253 254 257 protected void runTest(Test theTest, TestResult theResult) 258 { 259 theTest.run(theResult); 260 } 261 262 265 protected Test testAt(int theIndex) 266 { 267 return (Test) this.tests.elementAt(theIndex); 268 } 269 270 279 protected static Constructor getTestConstructor(Class theClass) 280 throws NoSuchMethodException 281 { 282 Constructor result; 283 try 284 { 285 result = theClass.getConstructor(new Class [] {String .class}); 286 } 287 catch (NoSuchMethodException e) 288 { 289 result = theClass.getConstructor(new Class [0]); 290 } 291 return result; 292 } 293 294 297 protected int testCount() 298 { 299 return this.tests.size(); 300 } 301 302 305 protected Enumeration tests() 306 { 307 return this.tests.elements(); 308 } 309 310 313 public String toString() 314 { 315 if (getName() != null) 316 { 317 return getName(); 318 } 319 return super.toString(); 320 } 321 322 325 protected void setName(String theName) 326 { 327 this.name = theName; 328 } 329 330 333 protected String getName() 334 { 335 return this.name; 336 } 337 338 341 private static Test warning(final String theMessage) 342 { 343 return new TestCase("warning") 344 { 345 protected void runTest() 346 { 347 fail(theMessage); 348 } 349 }; 350 } 351 352 358 protected abstract Test createTestSuite(Class theTestClass); 359 360 366 protected abstract Test createCactusTestCase(String theName, Test theTest); 367 } 368 | Popular Tags |