Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 202   Methods: 12
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ScriptTestSuite.java 68.2% 61.4% 50% 61.5%
coverage coverage
 1   
 package junit.extensions.abbot;
 2   
 
 3   
 import java.io.File;
 4   
 import java.lang.reflect.Constructor;
 5   
 import java.util.*;
 6   
 
 7   
 import junit.framework.*;
 8   
 import abbot.Log;
 9   
 import abbot.script.Script;
 10   
 //import junit.ui.TestRunner;
 11   
 //import junit.swingui;
 12   
 
 13   
 /** Similar to TestSuite, except that it auto-generates a suite based on test
 14   
  * scripts matching certain criteria.
 15   
  * <p>
 16   
  *  By default, generate a suite of all scripts found in a given directory for
 17   
  * which the accept method returns true.  Note that there is no guarantee of
 18   
  * the order of the scripts.
 19   
  * <p>
 20   
  *   The ScriptTestSuite constructors which require a class argument provide a
 21   
  * means for using custom fixtures derived from
 22   
  * <a href="ScriptFixture.html">ScriptFixture</a>.  The default fixture
 23   
  * preserves existing environment windows (e.g. the JUnit Swing UI TestRunner)
 24   
  * and disposes of all windows generated by the code under test.  Derived
 25   
  * fixtures may provide arbitrary code in their setUp/tearDown methods (such
 26   
  * as install/uninstall a custom security manager, set system properties,
 27   
  * etc), the same as you would do in  any other derivation of 
 28   
  * junit.framework.TestCase. 
 29   
  * <p>
 30   
  * <h3>Example 1</h3>
 31   
  * Following is a ScriptTestSuite which will aggregate all tests in the
 32   
  * directory "src/example", whose filenames begin with "MyCode-" and end with
 33   
  * ".xml":<br> 
 34   
  * <pre><code>
 35   
  * public class MyCodeTest extends ScriptFixture {
 36   
  *     public MyCodeTest(String name) { super(name); }
 37   
  *     public static Test suite() {
 38   
  *         return new ScriptTestSuite(MyCodeTest.class, "src/example") {
 39   
  *             public boolean accept(File file) {
 40   
  *                 String name = file.getName();
 41   
  *                 return name.startsWith("MyCode-") && name.endsWith(".xml");
 42   
  *             }
 43   
  *         };
 44   
  *     }
 45   
  * }
 46   
  * </code></pre>
 47   
  */
 48   
 public class ScriptTestSuite extends TestSuite {
 49   
 
 50   
     private File primaryDirectory;
 51   
 
 52   
     /** Constructs a suite of tests from all the scripts found in the current
 53   
      * directory.  Does not recurse to subdirectories.  The Class argument
 54   
      * must be a subclass of junit.extensions.abbot.ScriptFixture.
 55   
      */
 56  0
     public ScriptTestSuite(Class fixtureClass) {
 57  0
         this(fixtureClass, System.getProperty("user.dir"), false);
 58   
     }
 59   
 
 60   
     /** Constructs a suite of tests from all the scripts found in the given
 61   
      * directory.  Does not recurse to subdirectories. The Class argument
 62   
      * must be a subclass of junit.extensions.abbot.ScriptFixture.
 63   
      */
 64  1
     public ScriptTestSuite(Class fixtureClass, String dirname) {
 65  1
         this(fixtureClass, dirname, false);
 66   
     }
 67   
 
 68   
     /** Constructs an ScriptTestSuite from all the scripts in the given
 69   
      * directory, recursing if recurse is true. The Class argument
 70   
      * must be a class derived from junit.extensions.abbot.ScriptFixture.
 71   
      */
 72  1
     public ScriptTestSuite(Class fixtureClass, String dirname, boolean recurse) {
 73  1
         this(fixtureClass, findFilenames(dirname, recurse));
 74  1
         primaryDirectory = new File(dirname);
 75  1
         if (!primaryDirectory.exists() || !primaryDirectory.isDirectory()) {
 76  0
             String msg = "Directory '" + dirname + "' did not exist"
 77   
                 + " when scanning for test scripts";
 78  0
             addTest(warning(msg));
 79   
         }
 80   
     }
 81   
 
 82   
     /** Constructs a suite of tests for each script given in the argument
 83   
      * list.
 84   
      */
 85  0
     public ScriptTestSuite(String[] filenames) {
 86  0
         this(ScriptFixture.class, filenames);
 87   
     }
 88   
 
 89   
     /** Constructs a suite of tests for each script given in the argument
 90   
      * list, using the given class derived from ScriptFixture to wrap each
 91   
      * script. 
 92   
      */
 93  1
     public ScriptTestSuite(Class fixtureClass, String[] filenames) {
 94  1
         super(fixtureClass.getName());
 95  1
         primaryDirectory = new File(System.getProperty("user.dir"));
 96  1
         Log.debug("Loading " + fixtureClass + ", with "
 97   
                   + filenames.length + " files");
 98  1
         for (int i=0;i < filenames.length;i++) {
 99  17
             File file = new File(filenames[i]);
 100   
             // Filter for desired files only
 101  17
             if (!accept(file))
 102  5
                 continue;
 103  12
             try {
 104  12
                 Log.debug("Attempting to create " + fixtureClass);
 105  12
                 Constructor ctor =
 106   
                     fixtureClass.getConstructor(new Class[] { String.class });
 107  12
                 Test test = (Test)ctor.newInstance(new Object[] {
 108   
                                                        file.getAbsolutePath()
 109   
                                                    });
 110  12
                 Log.debug("Created an instance of " + fixtureClass);
 111  12
                 addTest(test);
 112   
             }
 113   
             catch(Throwable thr) {
 114  0
                 Log.warn(thr);
 115  0
                 addTest(warning("Could not construct an instance of "
 116   
                                 + fixtureClass));
 117  0
                 break;
 118   
             }
 119   
         }
 120  1
         if (testCount() == 0) {
 121  0
             addTest(warning("No scripts found"));
 122   
         }
 123   
     }
 124   
 
 125  0
     public File getDirectory() {
 126  0
         return primaryDirectory;
 127   
     }
 128   
 
 129   
     /** Return whether to accept the given file.   The default implementation
 130   
      * omits common backup files.
 131   
      */
 132  17
     public boolean accept(File file) {
 133  17
         String name = file.getName();
 134  17
         return !name.startsWith(".#")
 135   
             && !name.endsWith("~")
 136   
             && !name.endsWith(".bak");
 137   
     }
 138   
 
 139   
     /**
 140   
      * Returns a test which will fail and log a warning message.
 141   
      */
 142  0
     private Test warning(final String message) {
 143  0
         return new TestCase("warning") {
 144  0
             protected void runTest() {
 145  0
                 fail(message);
 146   
             }
 147   
         };        
 148   
     }
 149   
 
 150   
     /** Add all test scripts in the given directory, optionally recursing to
 151   
      * subdirectories.  Returns a list of absolute paths.
 152   
      */
 153  2
     protected static List findTestScripts(File dir, List files,
 154   
                                           boolean recurse) {
 155  2
         File[] flist = dir.listFiles();
 156  2
         for (int i=0;flist != null && i < flist.length;i++) {
 157   
             //Log.debug("Examining " + flist[i]);
 158  98
             if (flist[i].isDirectory()) {
 159  10
                 if (recurse)
 160  0
                     findTestScripts(flist[i], files, recurse);
 161   
             }
 162  88
             else if (Script.isScript(flist[i])) {
 163  63
                 String filename = flist[i].getAbsolutePath();
 164  63
                 if (!files.contains(filename)) {
 165  63
                     Log.debug("Adding " + filename);
 166  63
                     files.add(filename);
 167   
                 }
 168   
             }
 169   
         }
 170  2
         return files;
 171   
     }
 172   
 
 173   
     /** Scan for test scripts and return an array of filenames for all scripts
 174   
         found.
 175   
     */
 176  2
     static String[] findFilenames(String dirname, boolean recurse) {
 177  2
         File dir = new File(dirname);
 178  2
         ArrayList list = new ArrayList();
 179  2
         if (dir.exists() && dir.isDirectory()) {
 180  2
             findTestScripts(dir, list, recurse);
 181   
         }
 182  2
         return (String[])list.toArray(new String[list.size()]);
 183   
     }
 184   
 
 185   
     /** Run all scripts on the command line as a single suite. */
 186  0
     public static void main(String[] args) {
 187  0
         args = Log.init(args);
 188  0
         ScriptTestSuite suite = new ScriptTestSuite(args);
 189  0
         junit.textui.TestRunner runner = new junit.textui.TestRunner();
 190  0
         try {
 191  0
             TestResult r = runner.doRun(suite, false);
 192  0
             if (!r.wasSuccessful())
 193  0
                 System.exit(-1);
 194  0
             System.exit(0);
 195   
         }
 196   
         catch(Throwable thr) {
 197  0
             System.err.println(thr.getMessage());
 198  0
             System.exit(-2);
 199   
         }
 200   
     }
 201   
 }
 202