Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 134   Methods: 8
NCLOC: 112   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TestHelper.java 0% 0% 0% 0%
coverage
 1   
 package junit.extensions.abbot;
 2   
 
 3   
 import java.util.ArrayList;
 4   
 import java.util.Locale;
 5   
 
 6   
 import junit.framework.*;
 7   
 import junit.textui.*;
 8   
 
 9   
 import abbot.Log;
 10   
 
 11   
 /** Provides automatic test suite generation given command-line arguments.
 12   
  * Also allows for a single test to be run if it is specified.  Supports "-v"
 13   
  * (verbose) to display each test's name prior to running the test (aids in
 14   
  * identifying which test failed when the VM crashes).
 15   
  * The locale may be set explicitly by the following system properties:
 16   
  * <ul>
 17   
  * <li>abbot.locale.language
 18   
  * <li>abbot.locale.country
 19   
  * <li>abbot.locale.variant
 20   
  * </ul>
 21   
  */
 22   
 
 23   
 public class TestHelper {
 24  0
     protected TestHelper() { }
 25   
 
 26  0
     public static TestSuite generateSuite(Class[] classes) {
 27  0
         TestSuite suite = new TestSuite();
 28  0
         for (int i=0;i < classes.length;i++) {
 29  0
             try {
 30  0
                 java.lang.reflect.Method suiteMethod =
 31   
                     classes[i].getMethod("suite", null);
 32  0
                 suite.addTest((Test)suiteMethod.invoke(null, null));
 33   
             }
 34   
             catch(Exception exc) {
 35  0
                 suite.addTest(new TestSuite(classes[i]));
 36   
             }
 37   
         }
 38  0
         return suite;
 39   
     }
 40   
 
 41   
     private static boolean printTestNames = false;
 42  0
     protected static String[] parseArgs(String[] args) {
 43  0
         String language = System.getProperty("abbot.locale.language");
 44  0
         if (language != null) {
 45  0
             String country = System.getProperty("abbot.locale.country", "");
 46  0
             String variant = System.getProperty("abbot.locale.language", "");
 47  0
             Locale locale = new Locale(language, country, variant);
 48  0
             Locale.setDefault(locale);
 49  0
             System.out.println("Using locale " + locale.getDisplayName());
 50   
         }
 51  0
         ArrayList newArgs = new ArrayList();
 52  0
         for (int i=0;i < args.length;i++) {
 53  0
             if (args[i].equals("-v")) 
 54  0
                 printTestNames = true;
 55   
             else {
 56  0
                 newArgs.add(args[i]);
 57   
             }
 58   
         }
 59  0
         return (String[])newArgs.toArray(new String[newArgs.size()]);
 60   
     }
 61   
 
 62  0
     protected static Test collectTests(String[] args, Class testClass)
 63   
         throws NoSuchMethodException,
 64   
                InstantiationException,
 65   
                IllegalAccessException,
 66   
                java.lang.reflect.InvocationTargetException {
 67  0
         Test test;
 68  0
         if (args.length == 1 && args[0].startsWith("test")) {
 69  0
             try {
 70  0
                 test = (Test)testClass.newInstance();
 71  0
                 ((TestCase)test).setName(args[0]);
 72   
             }
 73   
             catch(InstantiationException e) {
 74  0
                 test = (Test)testClass.getConstructor(new Class[]{
 75   
                     String.class
 76   
                 }).newInstance(new Object[] { args[0] });
 77   
             }
 78   
         }
 79   
         else {
 80  0
             try {
 81  0
                 test = (Test)testClass.getMethod("suite", null).
 82   
                     invoke(null, null);
 83   
             }
 84   
             catch(Exception exc) {
 85  0
                 test = new TestSuite(testClass);
 86   
             }
 87   
         }
 88  0
         return test;
 89   
     }
 90   
 
 91  0
     protected static void runTest(Test test) {
 92  0
         try {
 93  0
             TestRunner runner = new TestRunner(new ResultPrinter(System.out) {
 94  0
                 public void startTest(Test test) {
 95  0
                     if (printTestNames)
 96  0
                         getWriter().print(test.toString());
 97  0
                     super.startTest(test);
 98   
                 }
 99  0
                 public void endTest(Test test) {
 100  0
                     super.endTest(test);
 101  0
                     if (printTestNames)
 102  0
                         getWriter().println();
 103   
                 }
 104   
             });
 105  0
             try {
 106  0
                 TestResult r = runner.doRun(test, false);
 107  0
                 if (!r.wasSuccessful())
 108  0
                     System.exit(-1);
 109  0
                 System.exit(0);
 110   
             }
 111   
             catch(Throwable thr) {
 112  0
                 System.err.println(thr.getMessage());
 113  0
                 System.exit(-2);
 114   
             }
 115   
         }
 116   
         catch(Exception exc) {
 117  0
             System.err.println(exc.getMessage());
 118  0
             System.exit(-2);
 119   
         }
 120   
     }
 121   
 
 122  0
     public static void runTests(String[] args, Class testClass) {
 123  0
         args = Log.init(args);
 124  0
         args = parseArgs(args);
 125  0
         try {
 126  0
             runTest(collectTests(args, testClass));
 127   
         }
 128   
         catch(Exception e) {
 129  0
             System.err.println(e.getMessage());
 130  0
             System.exit(-2);
 131   
         }
 132   
     }
 133   
 }
 134