Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 260   Methods: 12
NCLOC: 124   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PlainJUnitResultFormatter.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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 java.util.Hashtable;
 63   
 import junit.framework.AssertionFailedError;
 64   
 import junit.framework.Test;
 65   
 import org.apache.tools.ant.BuildException;
 66   
 
 67   
 
 68   
 /**
 69   
  * Prints plain text output of the test to a specified Writer.
 70   
  *
 71   
  * @author Stefan Bodewig
 72   
  */
 73   
 
 74   
 public class PlainJUnitResultFormatter implements JUnitResultFormatter {
 75   
 
 76   
     /**
 77   
      * Formatter for timings.
 78   
      */
 79   
     private NumberFormat nf = NumberFormat.getInstance();
 80   
     /**
 81   
      * Timing helper.
 82   
      */
 83   
     private Hashtable testStarts = new Hashtable();
 84   
     /**
 85   
      * Where to write the log to.
 86   
      */
 87   
     private OutputStream out;
 88   
     /**
 89   
      * Helper to store intermediate output.
 90   
      */
 91   
     private StringWriter inner;
 92   
     /**
 93   
      * Convenience layer on top of {@link #inner inner}.
 94   
      */
 95   
     private PrintWriter wri;
 96   
     /**
 97   
      * Suppress endTest if testcase failed.
 98   
      */
 99   
     private Hashtable failed = new Hashtable();
 100   
 
 101   
     private String systemOutput = null;
 102   
     private String systemError = null;
 103   
 
 104  0
     public PlainJUnitResultFormatter() {
 105  0
         inner = new StringWriter();
 106  0
         wri = new PrintWriter(inner);
 107   
     }
 108   
 
 109  0
     public void setOutput(OutputStream out) {
 110  0
         this.out = out;
 111   
     }
 112   
 
 113  0
     public void setSystemOutput(String out) {
 114  0
         systemOutput = out;
 115   
     }
 116   
 
 117  0
     public void setSystemError(String err) {
 118  0
         systemError = err;
 119   
     }
 120   
 
 121   
     /**
 122   
      * Empty.
 123   
      */
 124  0
     public void startTestSuite(JUnitTest suite) {
 125   
     }
 126   
 
 127   
     /**
 128   
      * The whole testsuite ended.
 129   
      */
 130  0
     public void endTestSuite(JUnitTest suite) throws BuildException {
 131  0
         String newLine = System.getProperty("line.separator");
 132  0
         StringBuffer sb = new StringBuffer("Testsuite: ");
 133  0
         sb.append(suite.getName());
 134  0
         sb.append(newLine);
 135  0
         sb.append("Tests run: ");
 136  0
         sb.append(suite.runCount());
 137  0
         sb.append(", Failures: ");
 138  0
         sb.append(suite.failureCount());
 139  0
         sb.append(", Errors: ");
 140  0
         sb.append(suite.errorCount());
 141  0
         sb.append(", Time elapsed: ");
 142  0
         sb.append(nf.format(suite.getRunTime() / 1000.0));
 143  0
         sb.append(" sec");
 144  0
         sb.append(newLine);
 145   
         
 146   
         // append the err and output streams to the log
 147  0
         if (systemOutput != null && systemOutput.length() > 0) {
 148  0
             sb.append("------------- Standard Output ---------------")
 149   
                 .append(newLine)
 150   
                 .append(systemOutput)
 151   
                 .append("------------- ---------------- ---------------")
 152   
                 .append(newLine);
 153   
         }
 154   
         
 155  0
         if (systemError != null && systemError.length() > 0) {
 156  0
             sb.append("------------- Standard Error -----------------")
 157   
                 .append(newLine)
 158   
                 .append(systemError)
 159   
                 .append("------------- ---------------- ---------------")
 160   
                 .append(newLine);
 161   
         }
 162   
 
 163  0
         sb.append(newLine);
 164   
 
 165  0
         if (out != null) {
 166  0
             try {
 167  0
                 out.write(sb.toString().getBytes());
 168  0
                 wri.close();
 169  0
                 out.write(inner.toString().getBytes());
 170  0
                 out.flush();
 171   
             } catch (IOException ioex) {
 172  0
                 throw new BuildException("Unable to write output", ioex);
 173   
             } finally {
 174  0
                 if (out != System.out && out != System.err) {
 175  0
                     try {
 176  0
                         out.close();
 177   
                     } catch (IOException e) {}
 178   
                 }
 179   
             }
 180   
         }
 181   
     }
 182   
 
 183   
     /**
 184   
      * Interface TestListener.
 185   
      *
 186   
      * <p>A new Test is started.
 187   
      */
 188  0
     public void startTest(Test t) {
 189  0
         testStarts.put(t, new Long(System.currentTimeMillis()));
 190  0
         failed.put(t, Boolean.FALSE);
 191   
     }
 192   
 
 193   
     /**
 194   
      * Interface TestListener.
 195   
      *
 196   
      * <p>A Test is finished.
 197   
      */
 198  0
     public void endTest(Test test) {
 199  0
         synchronized (wri) {
 200  0
             wri.print("Testcase: " 
 201   
                       + JUnitVersionHelper.getTestCaseName(test));
 202  0
             if (Boolean.TRUE.equals(failed.get(test))) {
 203  0
                 return;
 204   
             }
 205  0
             Long l = (Long) testStarts.get(test);
 206  0
             double seconds = 0;
 207   
             // can be null if an error occured in setUp
 208  0
             if (l != null) {
 209  0
                 seconds = 
 210   
                     (System.currentTimeMillis() - l.longValue()) / 1000.0;
 211   
             }
 212   
             
 213  0
             wri.println(" took " + nf.format(seconds) + " sec");
 214   
         }
 215   
     }
 216   
 
 217   
     /**
 218   
      * Interface TestListener for JUnit &lt;= 3.4.
 219   
      *
 220   
      * <p>A Test failed.
 221   
      */
 222  0
     public void addFailure(Test test, Throwable t) {
 223  0
         formatError("\tFAILED", test, t);
 224   
     }
 225   
 
 226   
     /**
 227   
      * Interface TestListener for JUnit &gt; 3.4.
 228   
      *
 229   
      * <p>A Test failed.
 230   
      */
 231  0
     public void addFailure(Test test, AssertionFailedError t) {
 232  0
         addFailure(test, (Throwable) t);
 233   
     }
 234   
 
 235   
     /**
 236   
      * Interface TestListener.
 237   
      *
 238   
      * <p>An error occured while running the test.
 239   
      */
 240  0
     public void addError(Test test, Throwable t) {
 241  0
         formatError("\tCaused an ERROR", test, t);
 242   
     }
 243   
 
 244  0
     private void formatError(String type, Test test, Throwable t) {
 245  0
         synchronized (wri) {
 246  0
             if (test != null) {
 247  0
                 endTest(test);
 248  0
                 failed.put(test, Boolean.TRUE);
 249   
             }
 250   
 
 251  0
             wri.println(type);
 252  0
             wri.println(t.getMessage());
 253  0
             String strace = JUnitTestRunner.getFilteredTrace(t);
 254  0
             wri.print(strace);
 255  0
             wri.println("");
 256   
         }
 257   
     }
 258   
     
 259   
 } // PlainJUnitResultFormatter
 260