Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 225   Methods: 11
NCLOC: 88   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
FormatterElement.java 50% 61.1% 72.7% 61.4%
 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   
 
 55   
 package org.apache.tools.ant.taskdefs.optional.junit;
 56   
 
 57   
 import java.io.File;
 58   
 import java.io.FileOutputStream;
 59   
 import java.io.OutputStream;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.types.EnumeratedAttribute;
 62   
 
 63   
 /**
 64   
  * <p> A wrapper for the implementations of <code>JUnitResultFormatter</code>.
 65   
  * In particular, used as a nested <code>&lt;formatter&gt;</code> element in a <code>&lt;junit&gt;</code> task.
 66   
  * <p> For example, 
 67   
  * <code><pre>
 68   
  *       &lt;junit printsummary="no" haltonfailure="yes" fork="false"&gt;
 69   
  *           &lt;formatter type="plain" usefile="false" /&gt;
 70   
  *           &lt;test name="org.apache.ecs.InternationalCharTest" /&gt;
 71   
  *       &lt;/junit&gt;</pre></code>
 72   
  * adds a <code>plain</code> type implementation (<code>PlainJUnitResultFormatter</code>) to display the results of the test.
 73   
  *
 74   
  * <p> Either the <code>type</code> or the <code>classname</code> attribute
 75   
  * must be set. 
 76   
  *
 77   
  * @author Stefan Bodewig
 78   
  *
 79   
  * @see JUnitTask
 80   
  * @see XMLJUnitResultFormatter
 81   
  * @see BriefJUnitResultFormatter
 82   
  * @see PlainJUnitResultFormatter
 83   
  * @see JUnitResultFormatter
 84   
  */
 85   
 public class FormatterElement {
 86   
 
 87   
     private String classname;
 88   
     private String extension;
 89   
     private OutputStream out = System.out;
 90   
     private File outFile;
 91   
     private boolean useFile = true;
 92   
 
 93   
     /**
 94   
      * <p> Quick way to use a standard formatter.
 95   
      *
 96   
      * <p> At the moment, there are three supported standard formatters.
 97   
      * <ul>
 98   
      * <li> The <code>xml</code> type uses a <code>XMLJUnitResultFormatter</code>.
 99   
      * <li> The <code>brief</code> type uses a <code>BriefJUnitResultFormatter</code>.
 100   
      * <li> The <code>plain</code> type (the default) uses a <code>PlainJUnitResultFormatter</code>.
 101   
      * </ul>
 102   
      *
 103   
      * <p> Sets <code>classname</code> attribute - so you can't use that attribute if you use this one.
 104   
      */
 105  1
     public void setType(TypeAttribute type) {
 106  1
         if ("xml".equals(type.getValue())) {
 107  0
             setClassname("org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter");
 108  0
             setExtension(".xml");
 109   
         } else {
 110  1
             if ("brief".equals(type.getValue())) {
 111  1
                 setClassname("org.apache.tools.ant.taskdefs.optional.junit.BriefJUnitResultFormatter");
 112   
             } else { // must be plain, ensured by TypeAttribute
 113  0
                 setClassname("org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter");
 114   
             }
 115  1
             setExtension(".txt");
 116   
         }
 117   
     }
 118   
 
 119   
     /**
 120   
      * <p> Set name of class to be used as the formatter.
 121   
      *
 122   
      * <p> This class must implement <code>JUnitResultFormatter</code>
 123   
      */
 124  1
     public void setClassname(String classname) {
 125  1
         this.classname = classname;
 126   
     }
 127   
 
 128   
     /**
 129   
      * Get name of class to be used as the formatter.
 130   
      */
 131  0
     public String getClassname() {
 132  0
         return classname;
 133   
     }
 134   
 
 135  1
     public void setExtension(String ext) {
 136  1
         this.extension = ext;
 137   
     }
 138   
 
 139  0
     public String getExtension() {
 140  0
         return extension;
 141   
     }
 142   
 
 143   
     /**
 144   
      * <p> Set the file which the formatte should log to.
 145   
      *
 146   
      * <p> Note that logging to file must be enabled .
 147   
      */
 148  0
     void setOutfile(File out) {
 149  0
         this.outFile = out;
 150   
     }
 151   
 
 152   
     /**
 153   
      * <p> Set output stream for formatter to use.
 154   
      *
 155   
      * <p> Defaults to standard out.
 156   
      */
 157  136
     public void setOutput(OutputStream out) {
 158  136
         this.out = out;
 159   
     }
 160   
 
 161   
     /**
 162   
      * Set whether the formatter should log to file.
 163   
      */
 164  1
     public void setUseFile(boolean useFile) {
 165  1
         this.useFile = useFile;
 166   
     }
 167   
 
 168   
     /**
 169   
      * Get whether the formatter should log to file.
 170   
      */
 171  136
     boolean getUseFile() {
 172  136
         return useFile;
 173   
     }
 174   
 
 175  136
     JUnitResultFormatter createFormatter() throws BuildException {
 176  136
         if (classname == null) {
 177  0
             throw new BuildException("you must specify type or classname");
 178   
         }
 179   
         
 180  136
         Class f = null;
 181  136
         try {
 182  136
             f = Class.forName(classname);
 183   
         } catch (ClassNotFoundException e) {
 184  0
             throw new BuildException(e);
 185   
         }
 186   
 
 187  136
         Object o = null;
 188  136
         try {
 189  136
             o = f.newInstance();
 190   
         } catch (InstantiationException e) {
 191  0
             throw new BuildException(e);
 192   
         } catch (IllegalAccessException e) {
 193  0
             throw new BuildException(e);
 194   
         }
 195   
 
 196  136
         if (!(o instanceof JUnitResultFormatter)) {
 197  0
             throw new BuildException(classname 
 198   
                 + " is not a JUnitResultFormatter");
 199   
         }
 200   
 
 201  136
         JUnitResultFormatter r = (JUnitResultFormatter) o;
 202   
 
 203  136
         if (useFile && outFile != null) {
 204  0
             try {
 205  0
                 out = new FileOutputStream(outFile);
 206   
             } catch (java.io.IOException e) {
 207  0
                 throw new BuildException(e);
 208   
             }
 209   
         }
 210  136
         r.setOutput(out);
 211  136
         return r;
 212   
     }
 213   
 
 214   
     /**
 215   
      * <p> Enumerated attribute with the values "plain", "xml" and "brief".
 216   
      * 
 217   
      * <p> Use to enumerate options for <code>type</code> attribute.
 218   
      */
 219   
     public static class TypeAttribute extends EnumeratedAttribute {
 220  1
         public String[] getValues() {
 221  1
             return new String[] {"plain", "xml", "brief"};
 222   
         }
 223   
     }
 224   
 }
 225