Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 259   Methods: 13
NCLOC: 112   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BriefJUnitResultFormatter.java 66.7% 97.9% 100% 93.1%
 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.IOException;
 58   
 import java.io.OutputStream;
 59   
 import java.io.PrintWriter;
 60   
 import java.io.StringWriter;
 61   
 import java.text.NumberFormat;
 62   
 import junit.framework.AssertionFailedError;
 63   
 import junit.framework.Test;
 64   
 import org.apache.tools.ant.BuildException;
 65   
 
 66   
 /**
 67   
  * Prints plain text output of the test to a specified Writer.
 68   
  * Inspired by the PlainJUnitResultFormatter.
 69   
  *
 70   
  * @author <a href="mailto:robertdw@bigpond.net.au">Robert Watkins</a>
 71   
  *
 72   
  * @see FormatterElement
 73   
  * @see PlainJUnitResultFormatter
 74   
  */
 75   
 public class BriefJUnitResultFormatter implements JUnitResultFormatter {
 76   
 
 77   
     /**
 78   
      * Where to write the log to.
 79   
      */
 80   
     private OutputStream out;
 81   
 
 82   
     /**
 83   
      * Used for writing the results.
 84   
      */
 85   
     private PrintWriter output;
 86   
 
 87   
     /**
 88   
      * Used as part of formatting the results.
 89   
      */
 90   
     private StringWriter results;
 91   
 
 92   
     /**
 93   
      * Used for writing formatted results to.
 94   
      */
 95   
     private PrintWriter resultWriter;
 96   
 
 97   
     /**
 98   
      * Formatter for timings.
 99   
      */
 100   
     private NumberFormat numberFormat = NumberFormat.getInstance();
 101   
 
 102   
     /**
 103   
      * Output suite has written to System.out
 104   
      */
 105   
     private String systemOutput = null;
 106   
 
 107   
     /**
 108   
      * Output suite has written to System.err
 109   
      */
 110   
     private String systemError = null;
 111   
 
 112  136
     public BriefJUnitResultFormatter() {
 113  136
         results = new StringWriter();
 114  136
         resultWriter = new PrintWriter(results);
 115   
     }
 116   
 
 117   
     /**
 118   
      * Sets the stream the formatter is supposed to write its results to.
 119   
      */
 120  136
     public void setOutput(OutputStream out) {
 121  136
         this.out = out;
 122  136
         output = new PrintWriter(out);
 123   
     }
 124   
 
 125  136
     public void setSystemOutput(String out) {
 126  136
         systemOutput = out;
 127   
     }
 128   
 
 129  136
     public void setSystemError(String err) {
 130  136
         systemError = err;
 131   
     }
 132   
 
 133   
 
 134   
     /**
 135   
      * The whole testsuite started.
 136   
      */
 137  136
     public void startTestSuite(JUnitTest suite) throws BuildException {
 138   
     }
 139   
 
 140   
     /**
 141   
      * The whole testsuite ended.
 142   
      */
 143  136
     public void endTestSuite(JUnitTest suite) throws BuildException {
 144  136
         String newLine = System.getProperty("line.separator");
 145  136
         StringBuffer sb = new StringBuffer("Testsuite: ");
 146  136
         sb.append(suite.getName());
 147  136
         sb.append(newLine);
 148  136
         sb.append("Tests run: ");
 149  136
         sb.append(suite.runCount());
 150  136
         sb.append(", Failures: ");
 151  136
         sb.append(suite.failureCount());
 152  136
         sb.append(", Errors: ");
 153  136
         sb.append(suite.errorCount());
 154  136
         sb.append(", Time elapsed: ");
 155  136
         sb.append(numberFormat.format(suite.getRunTime() / 1000.0));
 156  136
         sb.append(" sec");
 157  136
         sb.append(newLine);
 158  136
         sb.append(newLine);
 159   
 
 160   
         // append the err and output streams to the log
 161  136
         if (systemOutput != null && systemOutput.length() > 0) {
 162  3
             sb.append("------------- Standard Output ---------------")
 163   
                     .append(newLine)
 164   
                     .append(systemOutput)
 165   
                     .append("------------- ---------------- ---------------")
 166   
                     .append(newLine);
 167   
         }
 168   
 
 169  136
         if (systemError != null && systemError.length() > 0) {
 170  3
             sb.append("------------- Standard Error -----------------")
 171   
                     .append(newLine)
 172   
                     .append(systemError)
 173   
                     .append("------------- ---------------- ---------------")
 174   
                     .append(newLine);
 175   
         }
 176   
 
 177  136
         if (output != null) {
 178  136
             try {
 179  136
                 output.write(sb.toString());
 180  136
                 resultWriter.close();
 181  136
                 output.write(results.toString());
 182  136
                 output.flush();
 183   
             } finally {
 184  136
                 if (out != System.out &&
 185   
                         out != System.err) {
 186  136
                     try {
 187  136
                         out.close();
 188   
                     } catch (IOException e) {
 189   
                     }
 190   
                 }
 191   
             }
 192   
         }
 193   
     }
 194   
 
 195   
     /**
 196   
      * A test started.
 197   
      */
 198  756
     public void startTest(Test test) {
 199   
     }
 200   
 
 201   
     /**
 202   
      * A test ended.
 203   
      */
 204  764
     public void endTest(Test test) {
 205   
     }
 206   
 
 207   
     /**
 208   
      * Interface TestListener for JUnit &lt;= 3.4.
 209   
      *
 210   
      * <p>A Test failed.
 211   
      */
 212  1
     public void addFailure(Test test, Throwable t) {
 213  1
         formatError("\tFAILED", test, t);
 214   
     }
 215   
 
 216   
     /**
 217   
      * Interface TestListener for JUnit &gt; 3.4.
 218   
      *
 219   
      * <p>A Test failed.
 220   
      */
 221  1
     public void addFailure(Test test, AssertionFailedError t) {
 222  1
         addFailure(test, (Throwable) t);
 223   
     }
 224   
 
 225   
     /**
 226   
      * A test caused an error.
 227   
      */
 228  7
     public void addError(Test test, Throwable error) {
 229  7
         formatError("\tCaused an ERROR", test, error);
 230   
     }
 231   
 
 232   
     /**
 233   
      * Format the test for printing..
 234   
      */
 235  8
     protected String formatTest(Test test) {
 236  8
         if (test == null) {
 237  0
             return "Null Test: ";
 238   
         } else {
 239  8
             return "Testcase: " + test.toString() + ":";
 240   
         }
 241   
     }
 242   
 
 243   
     /**
 244   
      * Format an error and print it.
 245   
      */
 246  8
     protected synchronized void formatError(String type, Test test,
 247   
                                             Throwable error) {
 248  8
         if (test != null) {
 249  8
             endTest(test);
 250   
         }
 251   
 
 252  8
         resultWriter.println(formatTest(test) + type);
 253  8
         resultWriter.println(error.getMessage());
 254  8
         String strace = JUnitTestRunner.getFilteredTrace(error);
 255  8
         resultWriter.println(strace);
 256  8
         resultWriter.println();
 257   
     }
 258   
 }
 259