Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 404   Methods: 19
NCLOC: 202   Classes: 4
 
 Source file Conditionals Statements Methods TOTAL
CovReport.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.IOException;
 59   
 import java.util.Vector;
 60   
 import javax.xml.transform.OutputKeys;
 61   
 import javax.xml.transform.Result;
 62   
 import javax.xml.transform.Source;
 63   
 import javax.xml.transform.Transformer;
 64   
 import javax.xml.transform.TransformerFactory;
 65   
 import javax.xml.transform.dom.DOMSource;
 66   
 import javax.xml.transform.stream.StreamResult;
 67   
 import org.apache.tools.ant.BuildException;
 68   
 import org.apache.tools.ant.Project;
 69   
 import org.apache.tools.ant.taskdefs.Execute;
 70   
 import org.apache.tools.ant.taskdefs.LogStreamHandler;
 71   
 import org.apache.tools.ant.types.Commandline;
 72   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 73   
 import org.apache.tools.ant.types.Path;
 74   
 import org.w3c.dom.Document;
 75   
 
 76   
 
 77   
 /**
 78   
  * Runs the JProbe Coverage 3.0 snapshot merge utility.
 79   
  *
 80   
  * @author <a href="sbailliez@imediation.com">Stephane Bailliez</a>
 81   
  * @ant.task name="jpcovreport" category="metrics"
 82   
  */
 83   
 public class CovReport extends CovBase {
 84   
     /*
 85   
       jpcoverport [options] -output=file -snapshot=snapshot.jpc
 86   
       jpcovreport [options] [-paramfile=file] -output=<fileName> -snapshot=<fileName>
 87   
 
 88   
       Generate a report based on the indicated snapshot
 89   
 
 90   
       -paramfile=file
 91   
       A text file containing the report generation options.
 92   
 
 93   
       -format=(html|text|xml) defaults to html
 94   
       The format of the generated report.
 95   
 
 96   
       -type=(executive|summary|detailed|verydetailed) defaults to detailed
 97   
       The type of report to be generated. For -format=xml,
 98   
       use -type=verydetailed to include source code lines.
 99   
 
 100   
       Note: A very detailed report can be VERY large.
 101   
 
 102   
       -percent=num            Min 1 Max 101 Default 101
 103   
       An integer representing a percentage of coverage.
 104   
       Only methods with test case coverage less than the
 105   
       percentage are included in reports.
 106   
 
 107   
       -filters=string
 108   
       A comma-separated list of filters in the form
 109   
       <package>.<class>:V, where V can be I for Include or
 110   
       E for Exclude. For the default package, omit <package>.
 111   
 
 112   
       -filters_method=string
 113   
       Optional. A comma-separated list of methods that
 114   
       correspond one-to-one with the entries in -filters.
 115   
 
 116   
       -output=string  Must be specified
 117   
       The absolute path and file name for the generated
 118   
       report file.
 119   
 
 120   
       -snapshot=string        Must be specified
 121   
       The absolute path and file name of the snapshot file.
 122   
 
 123   
       -inc_src_text=(on|off)  defaults to on
 124   
       Include text of the source code lines.
 125   
       Only applies for -format=xml and -type=verydetailed.
 126   
 
 127   
       -sourcepath=string      defaults to .
 128   
       A semicolon-separated list of source paths.
 129   
 
 130   
       /*
 131   
 
 132   
     /** format of generated report, optional */
 133   
     private String format = null;
 134   
 
 135   
     /** the name of the output snapshot, mandatory */
 136   
     private File tofile = null;
 137   
 
 138   
     /** type of report, optional */
 139   
     private String type = null;
 140   
 
 141   
     /** threshold value for printing methods, optional */
 142   
     private Integer percent = null;
 143   
 
 144   
     /** comma separated list of filters (???)*/
 145   
     private String filters = null;
 146   
 
 147   
     /** name of the snapshot file to create report from */
 148   
     private File snapshot = null;
 149   
 
 150   
     /** sourcepath to use */
 151   
     private Path sourcePath = null;
 152   
 
 153   
     /** include the text for each line of code (xml report verydetailed)*/
 154   
     private boolean includeSource = true;
 155   
 
 156   
     private Path coveragePath = null;
 157   
 
 158   
     /** */
 159   
     private Reference reference = null;
 160   
 
 161   
 
 162   
     public static class ReportFormat extends EnumeratedAttribute {
 163  0
         public String[] getValues() {
 164  0
             return new String[]{"html", "text", "xml"};
 165   
         }
 166   
     }
 167   
 
 168   
     /**
 169   
      * set the format of the report: "html", "text", or "xml"
 170   
      */
 171  0
     public void setFormat(ReportFormat value) {
 172  0
         this.format = value.getValue();
 173   
     }
 174   
 
 175   
     public static class ReportType extends EnumeratedAttribute {
 176  0
         public String[] getValues() {
 177  0
             return new String[]{"executive", "summary", "detailed", "verydetailed"};
 178   
         }
 179   
     }
 180   
 
 181   
     /**
 182   
      * The type of report to be generated: "executive", "summary",
 183   
      * "detailed" or "verydetailed".
 184   
      */
 185  0
     public void setType(ReportType value) {
 186  0
         this.type = value.getValue();
 187   
     }
 188   
 
 189   
     /**
 190   
      * If true, include text of the source code lines.
 191   
      * Only applies to format="xml" and type="verydetailed"
 192   
      */
 193  0
     public void setIncludesource(boolean value) {
 194  0
         this.includeSource = value;
 195   
     }
 196   
 
 197   
     /**
 198   
      * A numeric value for the threshold for printing methods.
 199   
      * Must be between 0 and 100.
 200   
      */
 201  0
     public void setPercent(Integer value) {
 202  0
         this.percent = value;
 203   
     }
 204   
 
 205   
     /**
 206   
      * set the filters
 207   
      * @ant.attribute ignore="true"
 208   
      */
 209  0
     public void setFilters(String values) {
 210  0
         this.filters = values;
 211   
     }
 212   
 
 213   
     /**
 214   
      * Adds a path to source files.
 215   
      */
 216  0
     public Path createSourcepath() {
 217  0
         if (sourcePath == null) {
 218  0
             sourcePath = new Path(getProject());
 219   
         }
 220  0
         return sourcePath.createPath();
 221   
     }
 222   
 
 223   
     /**
 224   
      * The name of the snapshot file that is the source to the report.
 225   
      */
 226  0
     public void setSnapshot(File value) {
 227  0
         this.snapshot = value;
 228   
     }
 229   
 
 230   
     /**
 231   
      * The name of the generated output file.
 232   
      */
 233  0
     public void setTofile(File value) {
 234  0
         this.tofile = value;
 235   
     }
 236   
 
 237   
     /**
 238   
      * @todo needs to be removed
 239   
      * @ant.element ignore="true"
 240   
      */
 241  0
     public Path createCoveragepath() {
 242  0
         if (coveragePath == null) {
 243  0
             coveragePath = new Path(getProject());
 244   
         }
 245  0
         return coveragePath.createPath();
 246   
     }
 247   
 
 248   
     /**
 249   
      * Adds a set of classes whose coverage information will be
 250   
      * checked against.
 251   
      */
 252  0
     public Reference createReference() {
 253  0
         if (reference == null) {
 254  0
             reference = new Reference();
 255   
         }
 256  0
         return reference;
 257   
     }
 258   
 
 259   
 
 260  0
     public CovReport() {
 261   
     }
 262   
 
 263   
     /** check for mandatory options */
 264  0
     protected void checkOptions() throws BuildException {
 265  0
         if (tofile == null) {
 266  0
             throw new BuildException("'tofile' attribute must be set.");
 267   
         }
 268  0
         if (snapshot == null) {
 269  0
             throw new BuildException("'snapshot' attribute must be set.");
 270   
         }
 271  0
         if (getHome() == null) {
 272  0
             throw new BuildException("'home' attribute must be set to JProbe home directory");
 273   
         }
 274  0
         File jar = findCoverageJar();
 275  0
         if (!jar.exists()) {
 276  0
             throw new BuildException("Cannot find Coverage directory: " + getHome());
 277   
         }
 278  0
         if (reference != null && !"xml".equals(format)) {
 279  0
             log("Ignored reference. It cannot be used in non XML report.");
 280  0
             reference = null; // nullify it so that there is no ambiguity
 281   
         }
 282   
 
 283   
     }
 284   
 
 285  0
     public void execute() throws BuildException {
 286  0
         checkOptions();
 287  0
         try {
 288  0
             Commandline cmdl = new Commandline();
 289   
             // we need to run Coverage from his directory due to dll/jar issues
 290  0
             cmdl.setExecutable(findExecutable("jpcovreport"));
 291  0
             String[] params = getParameters();
 292  0
             for (int i = 0; i < params.length; i++) {
 293  0
                 cmdl.createArgument().setValue(params[i]);
 294   
             }
 295   
 
 296   
             // use the custom handler for stdin issues
 297  0
             LogStreamHandler handler = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN);
 298  0
             Execute exec = new Execute(handler);
 299  0
             log(cmdl.describeCommand(), Project.MSG_VERBOSE);
 300  0
             exec.setCommandline(cmdl.getCommandline());
 301  0
             int exitValue = exec.execute();
 302  0
             if (exitValue != 0) {
 303  0
                 throw new BuildException("JProbe Coverage Report failed (" + exitValue + ")");
 304   
             }
 305  0
             log("coveragePath: " + coveragePath, Project.MSG_VERBOSE);
 306  0
             log("format: " + format, Project.MSG_VERBOSE);
 307  0
             if (reference != null && "xml".equals(format)) {
 308  0
                 reference.createEnhancedXMLReport();
 309   
             }
 310   
 
 311   
         } catch (IOException e) {
 312  0
             throw new BuildException("Failed to execute JProbe Coverage Report.", e);
 313   
         }
 314   
     }
 315   
 
 316   
 
 317  0
     protected String[] getParameters() {
 318  0
         Vector v = new Vector();
 319  0
         if (format != null) {
 320  0
             v.addElement("-format=" + format);
 321   
         }
 322  0
         if (type != null) {
 323  0
             v.addElement("-type=" + type);
 324   
         }
 325  0
         if (percent != null) {
 326  0
             v.addElement("-percent=" + percent);
 327   
         }
 328  0
         if (filters != null) {
 329  0
             v.addElement("-filters=" + filters);
 330   
         }
 331  0
         v.addElement("-output=" + getProject().resolveFile(tofile.getPath()));
 332  0
         v.addElement("-snapshot=" + getProject().resolveFile(snapshot.getPath()));
 333   
         // as a default -sourcepath use . in JProbe, so use project .
 334  0
         if (sourcePath == null) {
 335  0
             sourcePath = new Path(getProject());
 336  0
             sourcePath.createPath().setLocation(getProject().resolveFile("."));
 337   
         }
 338  0
         v.addElement("-sourcepath=" + sourcePath);
 339   
 
 340  0
         if ("verydetailed".equalsIgnoreCase(format) && "xml".equalsIgnoreCase(type)) {
 341  0
             v.addElement("-inc_src_text=" + (includeSource ? "on" : "off"));
 342   
         }
 343   
 
 344  0
         String[] params = new String[v.size()];
 345  0
         v.copyInto(params);
 346  0
         return params;
 347   
     }
 348   
 
 349   
 
 350   
     public class Reference {
 351   
         protected Path classPath;
 352   
         protected ReportFilters filters;
 353   
 
 354  0
         public Path createClasspath() {
 355  0
             if (classPath == null) {
 356  0
                 classPath = new Path(CovReport.this.getProject());
 357   
             }
 358  0
             return classPath.createPath();
 359   
         }
 360   
 
 361  0
         public ReportFilters createFilters() {
 362  0
             if (filters == null) {
 363  0
                 filters = new ReportFilters();
 364   
             }
 365  0
             return filters;
 366   
         }
 367   
 
 368  0
         protected void createEnhancedXMLReport() throws BuildException {
 369   
             // we need a classpath element
 370  0
             if (classPath == null) {
 371  0
                 throw new BuildException("Need a 'classpath' element.");
 372   
             }
 373   
             // and a valid one...
 374  0
             String[] paths = classPath.list();
 375  0
             if (paths.length == 0) {
 376  0
                 throw new BuildException("Coverage path is invalid. It does not contain any existing path.");
 377   
             }
 378   
             // and we need at least one filter include/exclude.
 379  0
             if (filters == null || filters.size() == 0) {
 380  0
                 createFilters();
 381  0
                 log("Adding default include filter to *.*()", Project.MSG_VERBOSE);
 382  0
                 ReportFilters.Include include = new ReportFilters.Include();
 383  0
                 filters.addInclude(include);
 384   
             }
 385  0
             try {
 386  0
                 log("Creating enhanced XML report", Project.MSG_VERBOSE);
 387  0
                 XMLReport report = new XMLReport(CovReport.this, tofile);
 388  0
                 report.setReportFilters(filters);
 389  0
                 report.setJProbehome(new File(getHome().getParent()));
 390  0
                 Document doc = report.createDocument(paths);
 391  0
                 TransformerFactory tfactory = TransformerFactory.newInstance();
 392  0
                 Transformer transformer = tfactory.newTransformer();
 393  0
                 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 394  0
                 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
 395  0
                 Source src = new DOMSource(doc);
 396  0
                 Result res = new StreamResult("file:///" + tofile.toString());
 397  0
                 transformer.transform(src, res);
 398   
             } catch (Exception e) {
 399  0
                 throw new BuildException("Error while performing enhanced XML report from file " + tofile, e);
 400   
             }
 401   
         }
 402   
     }
 403   
 }
 404