1 21 package db4ounit; 22 23 import java.lang.reflect.*; 24 import java.util.*; 25 26 29 public class UnitTestMain { 30 public static void main(String [] args) throws Exception { 31 new UnitTestMain().runTests(args); 32 } 33 34 public final void runTests(String [] args) throws ClassNotFoundException , 35 InstantiationException , IllegalAccessException { 36 TestSuite suite = build(args); 37 TestRunner runner=new TestRunner(suite); 38 runner.run(); 39 } 40 41 protected TestSuiteBuilder builder(Class [] clazzes) { 42 return new ReflectionTestSuiteBuilder(clazzes); 43 } 44 45 private TestSuite build(String [] args) 46 throws ClassNotFoundException , InstantiationException , 47 IllegalAccessException { 48 List plainTestMethods=new ArrayList(); 49 List plainTestClasses=new ArrayList(); 50 for(int idx=0;idx<args.length;idx++) { 51 String testIdentifier = args[idx]; 52 int methodSeparatorIndex = testIdentifier.indexOf('#'); 53 if(methodSeparatorIndex>0) { 54 String className=testIdentifier.substring(0,methodSeparatorIndex); 55 String methodName=testIdentifier.substring(methodSeparatorIndex+1); 56 TestMethod testMethod = testMethod(className, methodName); 57 plainTestMethods.add(testMethod); 58 continue; 59 } 60 Class curClazz=Class.forName(testIdentifier); 61 if(TestCase.class.isAssignableFrom(curClazz)) { 62 plainTestClasses.add(curClazz); 63 } 64 } 65 Class [] plainTestClassesArray = (Class [])plainTestClasses.toArray(new Class [plainTestClasses.size()]); 66 TestSuiteBuilder classBuilder=builder(plainTestClassesArray); 67 Test[] plainTestMethodArray=(Test[])plainTestMethods.toArray(new Test[plainTestMethods.size()]); 68 TestSuite methodSuite=new TestSuite(plainTestMethodArray); 69 return new TestSuite(new Test[]{classBuilder.build(),methodSuite}); 70 } 71 72 private TestMethod testMethod(String className, String methodName) 73 throws ClassNotFoundException , InstantiationException , 74 IllegalAccessException { 75 TestCase test = createTestInstance(className); 76 Method method=null; 77 Method[] methods = test.getClass().getMethods(); 78 for (int methodIdx = 0; methodIdx < methods.length; methodIdx++) { 79 Method curMethod = methods[methodIdx]; 80 if(curMethod.getName().equals(methodName)) { 81 method=curMethod; 82 } 83 } 84 if(method==null) { 85 return null; 86 } 87 return new TestMethod(test,method); 88 } 89 90 protected TestCase createTestInstance(String className) throws ClassNotFoundException , 91 InstantiationException , IllegalAccessException { 92 Class clazz=Class.forName(className); 93 TestCase test=(TestCase)clazz.newInstance(); 94 return test; 95 } 96 } 97 | Popular Tags |