1 16 package org.apache.commons.collections; 17 18 import java.lang.reflect.Constructor ; 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Modifier ; 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.List ; 25 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 138 public class BulkTest extends TestCase implements Cloneable { 139 140 141 149 150 156 String verboseName; 157 158 159 165 public BulkTest(String name) { 166 super(name); 167 this.verboseName = getClass().getName(); 168 } 169 170 171 176 public Object clone() { 177 try { 178 return super.clone(); 179 } catch (CloneNotSupportedException e) { 180 throw new Error (); } 182 } 183 184 185 218 public String [] ignoredTests() { 219 return null; 220 } 221 222 223 228 public String toString() { 229 return getName() + "(" + verboseName + ") "; 230 } 231 232 233 248 public static TestSuite makeSuite(Class c) { 249 if (Modifier.isAbstract(c.getModifiers())) { 250 throw new IllegalArgumentException ("Class must not be abstract."); 251 } 252 if (!BulkTest.class.isAssignableFrom(c)) { 253 throw new IllegalArgumentException ("Class must extend BulkTest."); 254 } 255 return new BulkTestSuiteMaker(c).make(); 256 } 257 258 } 259 260 261 class BulkTestSuiteMaker { 265 266 267 private Class startingClass; 268 269 270 private List ignored; 271 272 273 private TestSuite result; 274 275 279 private String prefix; 280 281 286 public BulkTestSuiteMaker(Class startingClass) { 287 this.startingClass = startingClass; 288 } 289 290 295 public TestSuite make() { 296 this.result = new TestSuite(); 297 this.prefix = getBaseName(startingClass); 298 result.setName(prefix); 299 300 BulkTest bulk = makeFirstTestCase(startingClass); 301 ignored = new ArrayList (); 302 String [] s = bulk.ignoredTests(); 303 if (s != null) { 304 ignored.addAll(Arrays.asList(s)); 305 } 306 make(bulk); 307 return result; 308 } 309 310 317 void make(BulkTest bulk) { 318 Class c = bulk.getClass(); 319 Method [] all = c.getMethods(); 320 for (int i = 0; i < all.length; i++) { 321 if (isTest(all[i])) addTest(bulk, all[i]); 322 if (isBulk(all[i])) addBulk(bulk, all[i]); 323 } 324 } 325 326 334 void addTest(BulkTest bulk, Method m) { 335 BulkTest bulk2 = (BulkTest)bulk.clone(); 336 bulk2.setName(m.getName()); 337 bulk2.verboseName = prefix + "." + m.getName(); 338 if (ignored.contains(bulk2.verboseName)) return; 339 result.addTest(bulk2); 340 } 341 342 351 void addBulk(BulkTest bulk, Method m) { 352 String verboseName = prefix + "." + m.getName(); 353 if (ignored.contains(verboseName)) return; 354 355 BulkTest bulk2; 356 try { 357 bulk2 = (BulkTest)m.invoke(bulk, null); 358 if (bulk2 == null) return; 359 } catch (InvocationTargetException ex) { 360 ex.getTargetException().printStackTrace(); 361 throw new Error (); } catch (IllegalAccessException ex) { 363 ex.printStackTrace(); 364 throw new Error (); } 366 367 String oldPrefix = prefix; 369 TestSuite oldResult = result; 370 371 prefix = prefix + "." + m.getName(); 372 result = new TestSuite(); 373 result.setName(m.getName()); 374 375 make(bulk2); 376 377 oldResult.addTest(result); 378 379 prefix = oldPrefix; 381 result = oldResult; 382 } 383 384 390 private static String getBaseName(Class c) { 391 String name = c.getName(); 392 int p = name.lastIndexOf('.'); 393 if (p > 0) { 394 name = name.substring(p + 1); 395 } 396 return name; 397 } 398 399 400 403 private static Constructor getTestCaseConstructor(Class c) { 404 try { 405 return c.getConstructor(new Class [] { String .class }); 406 } catch (NoSuchMethodException e) { 407 throw new IllegalArgumentException (c + " must provide " + 408 "a (String) constructor"); 409 } 410 } 411 412 private static BulkTest makeTestCase(Class c, Method m) { 413 Constructor con = getTestCaseConstructor(c); 414 try { 415 return (BulkTest)con.newInstance(new String [] { m.getName() }); 416 } catch (InvocationTargetException e) { 417 e.printStackTrace(); 418 throw new RuntimeException (); } catch (IllegalAccessException e) { 420 throw new Error (); } catch (InstantiationException e) { 422 throw new RuntimeException (); } 424 } 425 426 private static BulkTest makeFirstTestCase(Class c) { 427 Method [] all = c.getMethods(); 428 for (int i = 0; i < all.length; i++) { 429 if (isTest(all[i])) return makeTestCase(c, all[i]); 430 } 431 throw new IllegalArgumentException (c.getName() + " must provide " 432 + " at least one test method."); 433 } 434 435 438 private static boolean isTest(Method m) { 439 if (!m.getName().startsWith("test")) return false; 440 if (m.getReturnType() != Void.TYPE) return false; 441 if (m.getParameterTypes().length != 0) return false; 442 int mods = m.getModifiers(); 443 if (Modifier.isStatic(mods)) return false; 444 if (Modifier.isAbstract(mods)) return false; 445 return true; 446 } 447 448 451 private static boolean isBulk(Method m) { 452 if (!m.getName().startsWith("bulkTest")) return false; 453 if (m.getReturnType() != BulkTest.class) return false; 454 if (m.getParameterTypes().length != 0) return false; 455 int mods = m.getModifiers(); 456 if (Modifier.isStatic(mods)) return false; 457 if (Modifier.isAbstract(mods)) return false; 458 return true; 459 } 460 461 } 462 | Popular Tags |