Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 215   Methods: 8
NCLOC: 79   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BatchTest.java 71.4% 87% 87.5% 83.8%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.junit;
 56   
 
 57   
 
 58   
 import java.io.File;
 59   
 import java.util.Enumeration;
 60   
 import java.util.Vector;
 61   
 import org.apache.tools.ant.DirectoryScanner;
 62   
 import org.apache.tools.ant.Project;
 63   
 import org.apache.tools.ant.types.FileSet;
 64   
 
 65   
 /**
 66   
  * <p> Create then run <code>JUnitTest</code>'s based on the list of files given by the fileset attribute.
 67   
  *
 68   
  * <p> Every <code>.java</code> or <code>.class</code> file in the fileset is 
 69   
  * assumed to be a testcase. 
 70   
  * A <code>JUnitTest</code> is created for each of these named classes with basic setup
 71   
  * inherited from the parent <code>BatchTest</code>.
 72   
  *
 73   
  * @author <a href="mailto:jeff.martin@synamic.co.uk">Jeff Martin</a>
 74   
  * @author Stefan Bodewig
 75   
  * @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a>
 76   
  *
 77   
  * @see JUnitTest
 78   
  */
 79   
 public final class BatchTest extends BaseTest {
 80   
 
 81   
     /** the reference to the project */
 82   
     private Project project;
 83   
 
 84   
     /** the list of filesets containing the testcase filename rules */
 85   
     private Vector filesets = new Vector();
 86   
 
 87   
     /**
 88   
      * create a new batchtest instance
 89   
      * @param project     the project it depends on.
 90   
      */
 91  1
     public BatchTest(Project project){
 92  1
         this.project = project;
 93   
     }
 94   
 
 95   
     /**
 96   
      * Add a new fileset instance to this batchtest. Whatever the fileset is,
 97   
      * only filename that are <tt>.java</tt> or <tt>.class</tt> will be
 98   
      * considered as 'candidates'.
 99   
      * @param     fs the new fileset containing the rules to get the testcases.
 100   
      */
 101  1
     public void addFileSet(FileSet fs) {
 102  1
         filesets.addElement(fs);
 103   
     }
 104   
 
 105   
     /**
 106   
      * Return all <tt>JUnitTest</tt> instances obtain by applying the fileset rules.
 107   
      * @return  an enumeration of all elements of this batchtest that are
 108   
      * a <tt>JUnitTest</tt> instance.
 109   
      */
 110  1
     public final Enumeration elements(){
 111  1
         JUnitTest[] tests = createAllJUnitTest();
 112  1
         return Enumerations.fromArray(tests);
 113   
     }
 114   
 
 115   
     /**
 116   
      * Convenient method to merge the <tt>JUnitTest</tt>s of this batchtest
 117   
      * to a <tt>Vector</tt>.
 118   
      * @param v the vector to which should be added all individual tests of this
 119   
      * batch test.
 120   
      */
 121  0
     final void addTestsTo(Vector v){
 122  0
         JUnitTest[] tests = createAllJUnitTest();
 123  0
         v.ensureCapacity(v.size() + tests.length);
 124  0
         for (int i = 0; i < tests.length; i++) {
 125  0
             v.addElement(tests[i]);
 126   
         }
 127   
     }
 128   
 
 129   
     /**
 130   
      * Create all <tt>JUnitTest</tt>s based on the filesets. Each instance
 131   
      * is configured to match this instance properties.
 132   
      * @return the array of all <tt>JUnitTest</tt>s that belongs to this batch.
 133   
      */
 134  1
     private JUnitTest[] createAllJUnitTest(){
 135  1
         String[] filenames = getFilenames();
 136  1
         JUnitTest[] tests = new JUnitTest[filenames.length];
 137  1
         for (int i = 0; i < tests.length; i++) {
 138  136
             String classname = javaToClass(filenames[i]);
 139  136
             tests[i] = createJUnitTest(classname);
 140   
         }
 141  1
         return tests;
 142   
     }
 143   
 
 144   
     /**
 145   
      * Iterate over all filesets and return the filename of all files
 146   
      * that end with <tt>.java</tt> or <tt>.class</tt>. This is to avoid
 147   
      * wrapping a <tt>JUnitTest</tt> over an xml file for example. A Testcase
 148   
      * is obviously a java file (compiled or not).
 149   
      * @return an array of filenames without their extension. As they should
 150   
      * normally be taken from their root, filenames should match their fully
 151   
      * qualified class name (If it is not the case it will fail when running the test).
 152   
      * For the class <tt>org/apache/Whatever.class</tt> it will return <tt>org/apache/Whatever</tt>.
 153   
      */
 154  1
     private String[] getFilenames(){
 155  1
         Vector v = new Vector();
 156  1
         final int size = this.filesets.size();
 157  1
         for (int j = 0; j < size; j++) {
 158  1
             FileSet fs = (FileSet) filesets.elementAt(j);
 159  1
             DirectoryScanner ds = fs.getDirectoryScanner(project);
 160  1
             ds.scan();
 161  1
             String[] f = ds.getIncludedFiles();
 162  1
             for (int k = 0; k < f.length; k++) {
 163  138
                 String pathname = f[k];
 164  138
                 if (pathname.endsWith(".java")) {
 165  136
                     v.addElement(pathname.substring(0, pathname.length() - ".java".length()));
 166  2
                 } else if (pathname.endsWith(".class")) {
 167  0
                     v.addElement(pathname.substring(0, pathname.length() - ".class".length()));
 168   
                 }
 169   
             }
 170   
         }
 171   
 
 172  1
         String[] files = new String[v.size()];
 173  1
         v.copyInto(files);
 174  1
         return files;
 175   
     }
 176   
 
 177   
     /**
 178   
      * Convenient method to convert a pathname without extension to a
 179   
      * fully qualified classname. For example <tt>org/apache/Whatever</tt> will
 180   
      * be converted to <tt>org.apache.Whatever</tt>
 181   
      * @param filename the filename to "convert" to a classname.
 182   
      * @return the classname matching the filename.
 183   
      */
 184  136
     public static final String javaToClass(String filename){
 185  136
         return filename.replace(File.separatorChar, '.');
 186   
     }
 187   
 
 188   
     /**
 189   
      * Create a <tt>JUnitTest</tt> that has the same property as this
 190   
      * <tt>BatchTest</tt> instance.
 191   
      * @param classname the name of the class that should be run as a
 192   
      * <tt>JUnitTest</tt>. It must be a fully qualified name.
 193   
      * @return the <tt>JUnitTest</tt> over the given classname.
 194   
      */
 195  136
     private JUnitTest createJUnitTest(String classname){
 196  136
         JUnitTest test = new JUnitTest();
 197  136
         test.setName(classname);
 198  136
         test.setHaltonerror(this.haltOnError);
 199  136
         test.setHaltonfailure(this.haltOnFail);
 200  136
         test.setFiltertrace(this.filtertrace);
 201  136
         test.setFork(this.fork);
 202  136
         test.setIf(this.ifProperty);
 203  136
         test.setUnless(this.unlessProperty);
 204  136
         test.setTodir(this.destDir);
 205  136
         test.setFailureProperty(failureProperty);
 206  136
         test.setErrorProperty(errorProperty);
 207  136
         Enumeration list = this.formatters.elements();
 208  136
         while (list.hasMoreElements()) {
 209  0
             test.addFormatter((FormatterElement) list.nextElement());
 210   
         }
 211  136
         return test;
 212   
     }
 213   
 
 214   
 }
 215