Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 217   Methods: 8
NCLOC: 110   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CovMerge.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2003 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.sitraka;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.FileWriter;
 59   
 import java.io.IOException;
 60   
 import java.io.PrintWriter;
 61   
 import java.util.Random;
 62   
 import java.util.Vector;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.DirectoryScanner;
 65   
 import org.apache.tools.ant.Project;
 66   
 import org.apache.tools.ant.taskdefs.Execute;
 67   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 68   
 import org.apache.tools.ant.types.Commandline;
 69   
 import org.apache.tools.ant.types.FileSet;
 70   
 
 71   
 /**
 72   
  * Runs the snapshot merge utility for JProbe Coverage.
 73   
  *
 74   
  * @author <a href="sbailliez@imediation.com">Stephane Bailliez</a>
 75   
  * @ant.task name="jpcovmerge" category="metrics"
 76   
  */
 77   
 public class CovMerge extends CovBase {
 78   
 
 79   
     /** the name of the output snapshot */
 80   
     private File tofile = null;
 81   
 
 82   
     /** the filesets that will get all snapshots to merge */
 83   
     private Vector filesets = new Vector();
 84   
 
 85   
     private boolean verbose;
 86   
 
 87   
     /**
 88   
      * Set the output snapshot file.
 89   
      */
 90  0
     public void setTofile(File value) {
 91  0
         this.tofile = value;
 92   
     }
 93   
 
 94   
     /**
 95   
      * If true, perform the merge in verbose mode giving details
 96   
      * about the snapshot processing.
 97   
      */
 98  0
     public void setVerbose(boolean flag) {
 99  0
         this.verbose = flag;
 100   
     }
 101   
 
 102   
     /**
 103   
      * add a fileset containing the snapshots to include.
 104   
      */
 105  0
     public void addFileset(FileSet fs) {
 106  0
         filesets.addElement(fs);
 107   
     }
 108   
 
 109   
     //---------------- the tedious job begins here
 110   
 
 111  0
     public CovMerge() {
 112   
     }
 113   
 
 114   
     /** execute the jpcovmerge by providing a parameter file */
 115  0
     public void execute() throws BuildException {
 116  0
         checkOptions();
 117   
 
 118  0
         File paramfile = createParamFile();
 119  0
         try {
 120  0
             Commandline cmdl = new Commandline();
 121  0
             cmdl.setExecutable(findExecutable("jpcovmerge"));
 122  0
             if (verbose) {
 123  0
                 cmdl.createArgument().setValue("-v");
 124   
             }
 125  0
             cmdl.createArgument().setValue(getParamFileArgument() 
 126   
                                            + paramfile.getAbsolutePath());
 127   
 
 128  0
             LogStreamHandler handler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
 129  0
             Execute exec = new Execute(handler);
 130  0
             log(cmdl.describeCommand(), Project.MSG_VERBOSE);
 131  0
             exec.setCommandline(cmdl.getCommandline());
 132   
 
 133   
             // JProbe process always return 0 so  we will not be
 134   
             // able to check for failure ! :-(
 135  0
             int exitValue = exec.execute();
 136  0
             if (exitValue != 0) {
 137  0
                 throw new BuildException("JProbe Coverage Merging failed (" + exitValue + ")");
 138   
             }
 139   
         } catch (IOException e) {
 140  0
             throw new BuildException("Failed to run JProbe Coverage Merge: " + e);
 141   
         } finally {
 142   
             //@todo should be removed once switched to JDK1.2
 143  0
             paramfile.delete();
 144   
         }
 145   
     }
 146   
 
 147   
     /** check for mandatory options */
 148  0
     protected void checkOptions() throws BuildException {
 149  0
         if (tofile == null) {
 150  0
             throw new BuildException("'tofile' attribute must be set.");
 151   
         }
 152   
 
 153   
         // check coverage home
 154  0
         if (getHome() == null || !getHome().isDirectory()) {
 155  0
             throw new BuildException("Invalid home directory. Must point to JProbe home directory");
 156   
         }
 157  0
         File jar = findCoverageJar();
 158  0
         if (!jar.exists()) {
 159  0
             throw new BuildException("Cannot find Coverage directory: " + getHome());
 160   
         }
 161   
     }
 162   
 
 163   
     /** get the snapshots from the filesets */
 164  0
     protected File[] getSnapshots() {
 165  0
         Vector v = new Vector();
 166  0
         final int size = filesets.size();
 167  0
         for (int i = 0; i < size; i++) {
 168  0
             FileSet fs = (FileSet) filesets.elementAt(i);
 169  0
             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
 170  0
             ds.scan();
 171  0
             String[] f = ds.getIncludedFiles();
 172  0
             for (int j = 0; j < f.length; j++) {
 173  0
                 String pathname = f[j];
 174  0
                 File file = new File(ds.getBasedir(), pathname);
 175  0
                 file = getProject().resolveFile(file.getPath());
 176  0
                 v.addElement(file);
 177   
             }
 178   
         }
 179   
 
 180  0
         File[] files = new File[v.size()];
 181  0
         v.copyInto(files);
 182  0
         return files;
 183   
     }
 184   
 
 185   
 
 186   
     /**
 187   
      * create the parameters file that contains all file to merge
 188   
      * and the output filename.
 189   
      */
 190  0
     protected File createParamFile() throws BuildException {
 191  0
         File[] snapshots = getSnapshots();
 192  0
         File file = createTempFile("jpcovm");
 193  0
         FileWriter fw = null;
 194  0
         try {
 195  0
             fw = new FileWriter(file);
 196  0
             PrintWriter pw = new PrintWriter(fw);
 197  0
             for (int i = 0; i < snapshots.length; i++) {
 198  0
                 pw.println(snapshots[i].getAbsolutePath());
 199   
             }
 200   
             // last file is the output snapshot
 201  0
             pw.println(getProject().resolveFile(tofile.getPath()));
 202  0
             pw.flush();
 203   
         } catch (IOException e) {
 204  0
             throw new BuildException("I/O error while writing to " + file, e);
 205   
         } finally {
 206  0
             if (fw != null) {
 207  0
                 try {
 208  0
                     fw.close();
 209   
                 } catch (IOException ignored) {
 210   
                 }
 211   
             }
 212   
         }
 213  0
         return file;
 214   
     }
 215   
 
 216   
 }
 217