Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 231   Methods: 12
NCLOC: 108   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
AggregateTransformer.java 41.7% 47.6% 50% 47%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-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   
 package org.apache.tools.ant.taskdefs.optional.junit;
 55   
 
 56   
 import java.io.File;
 57   
 import java.io.FileInputStream;
 58   
 import java.io.FileNotFoundException;
 59   
 import java.io.IOException;
 60   
 import java.io.InputStream;
 61   
 import java.net.URL;
 62   
 import javax.xml.parsers.DocumentBuilder;
 63   
 import javax.xml.parsers.DocumentBuilderFactory;
 64   
 import org.apache.tools.ant.BuildException;
 65   
 import org.apache.tools.ant.Project;
 66   
 import org.apache.tools.ant.Task;
 67   
 import org.apache.tools.ant.util.JAXPUtils;
 68   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 69   
 import org.w3c.dom.Document;
 70   
 
 71   
 /**
 72   
  * Transform a JUnit xml report.
 73   
  * The default transformation generates an html report in either framed or non-framed
 74   
  * style. The non-framed style is convenient to have a concise report via mail, the
 75   
  * framed report is much more convenient if you want to browse into different
 76   
  * packages or testcases since it is a Javadoc like report.
 77   
  *
 78   
  * @author <a href="mailto:sbailliez@apache.org">Stephane Bailliez</a>
 79   
  */
 80   
 public class AggregateTransformer {
 81   
 
 82   
     public static final String FRAMES = "frames";
 83   
 
 84   
     public static final String NOFRAMES = "noframes";
 85   
 
 86   
     public static class Format extends EnumeratedAttribute {
 87  0
         public String[] getValues(){
 88  0
             return new String[]{FRAMES, NOFRAMES};
 89   
         }
 90   
     }
 91   
 
 92   
     /** Task */
 93   
     protected Task task;
 94   
 
 95   
     /** the xml document to process */
 96   
     protected Document document;
 97   
 
 98   
     /** the style directory. XSLs should be read from here if necessary */
 99   
     protected File styleDir;
 100   
 
 101   
     /** the destination directory, this is the root from where html should be generated */
 102   
     protected File toDir;
 103   
 
 104   
     /** the format to use for the report. Must be <tt>FRAMES</tt> or <tt>NOFRAMES</tt> */
 105   
     protected String format = FRAMES;
 106   
 
 107   
     /** XML Parser factory */
 108   
     private static DocumentBuilderFactory privateDBFactory;
 109   
     
 110   
     /** XML Parser factory accessible to subclasses */
 111   
     protected static DocumentBuilderFactory dbfactory;
 112   
     
 113   
     static {
 114  1
        privateDBFactory = DocumentBuilderFactory.newInstance();
 115  1
        dbfactory = privateDBFactory;
 116   
     }
 117   
 
 118  1
     public AggregateTransformer(Task task){
 119  1
         this.task = task;
 120   
     }
 121   
 
 122   
     /**
 123   
      * Get the Document Builder Factory
 124   
      *
 125   
      * @return the DocumentBuilderFactory instance in use
 126   
      */
 127  0
     protected static DocumentBuilderFactory getDocumentBuilderFactory() {
 128  0
         return privateDBFactory;
 129   
     }
 130   
     
 131  0
     public void setFormat(Format format){
 132  0
         this.format = format.getValue();
 133   
     }
 134   
 
 135  1
     public void setXmlDocument(Document doc){
 136  1
         this.document = doc;
 137   
     }
 138   
 
 139   
     /**
 140   
      * Set the xml file to be processed. This is a helper if you want
 141   
      * to set the file directly. Much more for testing purposes.
 142   
      * @param xmlfile xml file to be processed
 143   
      */
 144  0
     protected void setXmlfile(File xmlfile) throws BuildException {
 145  0
         try {
 146  0
             DocumentBuilder builder = privateDBFactory.newDocumentBuilder();
 147  0
             InputStream in = new FileInputStream(xmlfile);
 148  0
             try {
 149  0
                 Document doc = builder.parse(in);
 150  0
                 setXmlDocument(doc);
 151   
             } finally {
 152  0
                 in.close();
 153   
             }
 154   
         } catch (Exception e){
 155  0
             throw new BuildException("Error while parsing document: " + xmlfile, e);
 156   
         }
 157   
     }
 158   
 
 159   
     /**
 160   
      * set the style directory. It is optional and will override the
 161   
      * default xsl used.
 162   
      * @param styledir  the directory containing the xsl files if the user
 163   
      * would like to override with its own style.
 164   
      */
 165  0
     public void setStyledir(File styledir){
 166  0
         this.styleDir = styledir;
 167   
     }
 168   
 
 169   
     /** set the destination directory */
 170  1
     public void setTodir(File todir){
 171  1
         this.toDir = todir;
 172   
     }
 173   
 
 174   
     /** set the extension of the output files */
 175  0
     public void setExtension(String ext){
 176  0
         task.log("extension is not used anymore", Project.MSG_WARN);
 177   
     }
 178   
 
 179  1
     public void transform() throws BuildException {
 180  1
         checkOptions();
 181  1
         final long t0 = System.currentTimeMillis();
 182  1
         XalanExecutor executor = XalanExecutor.newInstance(this);
 183  1
         try {
 184  1
             executor.execute();
 185   
         } catch (Exception e){
 186  0
             throw new BuildException("Errors while applying transformations: "
 187   
                 + e.getMessage(), e);
 188   
         }
 189  1
         final long dt = System.currentTimeMillis() - t0;
 190  1
         task.log("Transform time: " + dt + "ms");
 191   
     }
 192   
 
 193   
     /** check for invalid options */
 194  1
     protected void checkOptions() throws BuildException {
 195   
         // set the destination directory relative from the project if needed.
 196  1
         if (toDir == null) {
 197  0
             toDir = task.getProject().resolveFile(".");
 198  1
         } else if (!toDir.isAbsolute()) {
 199  0
             toDir = task.getProject().resolveFile(toDir.getPath());
 200   
         }
 201   
     }
 202   
 
 203   
     /**
 204   
      * Get the systemid of the appropriate stylesheet based on its
 205   
      * name and styledir. If no styledir is defined it will load
 206   
      * it as a java resource in the xsl child package, otherwise it
 207   
      * will get it from the given directory.
 208   
      * @throws IOException thrown if the requested stylesheet does
 209   
      * not exist.
 210   
      */
 211  1
     protected String getStylesheetSystemId() throws IOException {
 212  1
         String xslname = "junit-frames.xsl";
 213  1
         if (NOFRAMES.equals(format)){
 214  0
             xslname = "junit-noframes.xsl";
 215   
         }
 216  1
         if (styleDir == null){
 217  1
             URL url = getClass().getResource("xsl/" + xslname);
 218  1
             if (url == null){
 219  0
                 throw new FileNotFoundException("Could not find jar resource " + xslname);
 220   
             }
 221  1
             return url.toExternalForm();
 222   
         }
 223  0
         File file = new File(styleDir, xslname);
 224  0
         if (!file.exists()){
 225  0
             throw new FileNotFoundException("Could not find file '" + file + "'");
 226   
         }
 227  0
         return JAXPUtils.getSystemId(file);
 228   
     }
 229   
 
 230   
 }
 231