| Home >> All >> junit >> extensions >> [ abbot Javadoc ] |
Source code: junit/extensions/abbot/TestHelper.java
1 package junit.extensions.abbot; 2 3 import abbot.Log; 4 import junit.framework.*; 5 6 /** Provides automatic test suite generation given command-line arguments. 7 * Also allows for a single test to be run if it is specified. 8 */ 9 10 public class TestHelper { 11 private TestHelper() { } 12 13 public static Test generateSuite(Class[] classes) { 14 TestSuite suite = new TestSuite(); 15 for (int i=0;i < classes.length;i++) { 16 try { 17 java.lang.reflect.Method suiteMethod = 18 classes[i].getMethod("suite", null); 19 suite.addTest((Test)suiteMethod.invoke(null, null)); 20 } 21 catch(Exception exc) { 22 suite.addTest(new TestSuite(classes[i])); 23 } 24 } 25 return suite; 26 } 27 28 public static void runTests(String[] args, Class testClass) { 29 args = Log.init(args); 30 try { 31 Test test; 32 if (args.length == 1 && args[0].startsWith("test")) { 33 test = (Test)testClass.getConstructor(new Class[]{ 34 String.class 35 }).newInstance(new Object[] { args[0] }); 36 } 37 else { 38 try { 39 test = (Test)testClass.getMethod("suite", null). 40 invoke(null, null); 41 } 42 catch(Exception exc) { 43 test = new TestSuite(testClass); 44 } 45 } 46 junit.textui.TestRunner runner = new junit.textui.TestRunner(); 47 try { 48 TestResult r = runner.doRun(test, false); 49 if (!r.wasSuccessful()) 50 System.exit(-1); 51 System.exit(0); 52 } 53 catch(Throwable thr) { 54 System.err.println(thr.getMessage()); 55 System.exit(-2); 56 } 57 } 58 catch(Exception exc) { 59 System.err.println(exc.getMessage()); 60 System.exit(-2); 61 } 62 } 63 }