Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 286   Methods: 14
NCLOC: 143   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLJUnitResultFormatter.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000-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.junit;
 56   
 
 57   
 import java.io.BufferedWriter;
 58   
 import java.io.IOException;
 59   
 import java.io.OutputStream;
 60   
 import java.io.OutputStreamWriter;
 61   
 import java.io.Writer;
 62   
 import java.util.Enumeration;
 63   
 import java.util.Hashtable;
 64   
 import java.util.Properties;
 65   
 import javax.xml.parsers.DocumentBuilder;
 66   
 import javax.xml.parsers.DocumentBuilderFactory;
 67   
 import junit.framework.AssertionFailedError;
 68   
 import junit.framework.Test;
 69   
 import org.apache.tools.ant.BuildException;
 70   
 import org.apache.tools.ant.util.DOMElementWriter;
 71   
 import org.w3c.dom.Document;
 72   
 import org.w3c.dom.Element;
 73   
 import org.w3c.dom.Text;
 74   
 
 75   
 
 76   
 /**
 77   
  * Prints XML output of the test to a specified Writer.
 78   
  *
 79   
  * @author Stefan Bodewig
 80   
  * @author <a href="mailto:ehatcher@apache.org">Erik Hatcher</a>
 81   
  *
 82   
  * @see FormatterElement 
 83   
  */
 84   
 
 85   
 public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants {
 86   
 
 87  0
     private static DocumentBuilder getDocumentBuilder() {
 88  0
         try {
 89  0
             return DocumentBuilderFactory.newInstance().newDocumentBuilder();
 90   
         } catch (Exception exc) {
 91  0
             throw new ExceptionInInitializerError(exc);
 92   
         }
 93   
     }
 94   
 
 95   
     /**
 96   
      * The XML document.
 97   
      */
 98   
     private Document doc;
 99   
     /**
 100   
      * The wrapper for the whole testsuite.
 101   
      */
 102   
     private Element rootElement;
 103   
     /**
 104   
      * Element for the current test.
 105   
      */
 106   
     private Hashtable testElements = new Hashtable();
 107   
     /**
 108   
      * tests that failed.
 109   
      */
 110   
     private Hashtable failedTests = new Hashtable();
 111   
     /**
 112   
      * Timing helper.
 113   
      */
 114   
     private Hashtable testStarts = new Hashtable();
 115   
     /**
 116   
      * Where to write the log to.
 117   
      */
 118   
     private OutputStream out;
 119   
 
 120  0
     public XMLJUnitResultFormatter() {}
 121   
 
 122  0
     public void setOutput(OutputStream out) {
 123  0
         this.out = out;
 124   
     }
 125   
 
 126  0
     public void setSystemOutput(String out) {
 127  0
         formatOutput(SYSTEM_OUT, out);
 128   
     }
 129   
 
 130  0
     public void setSystemError(String out) {
 131  0
         formatOutput(SYSTEM_ERR, out);
 132   
     }
 133   
 
 134   
     /**
 135   
      * The whole testsuite started.
 136   
      */
 137  0
     public void startTestSuite(JUnitTest suite) {
 138  0
         doc = getDocumentBuilder().newDocument();
 139  0
         rootElement = doc.createElement(TESTSUITE);
 140  0
         rootElement.setAttribute(ATTR_NAME, suite.getName());
 141   
 
 142   
         // Output properties
 143  0
         Element propsElement = doc.createElement(PROPERTIES);
 144  0
         rootElement.appendChild(propsElement);
 145  0
         Properties props = suite.getProperties();
 146  0
         if (props != null) {
 147  0
             Enumeration e = props.propertyNames();
 148  0
             while (e.hasMoreElements()) {
 149  0
                 String name = (String) e.nextElement();
 150  0
                 Element propElement = doc.createElement(PROPERTY);
 151  0
                 propElement.setAttribute(ATTR_NAME, name);
 152  0
                 propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
 153  0
                 propsElement.appendChild(propElement);
 154   
             }
 155   
         }
 156   
     }
 157   
 
 158   
     /**
 159   
      * The whole testsuite ended.
 160   
      */
 161  0
     public void endTestSuite(JUnitTest suite) throws BuildException {
 162  0
         rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount());
 163  0
         rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount());
 164  0
         rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount());
 165  0
         rootElement.setAttribute(ATTR_TIME, "" + (suite.getRunTime() / 1000.0));
 166  0
         if (out != null) {
 167  0
             Writer wri = null;
 168  0
             try {
 169  0
                 wri = new BufferedWriter(new OutputStreamWriter(out, "UTF8"));
 170  0
                 wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
 171  0
                 (new DOMElementWriter()).write(rootElement, wri, 0, "  ");
 172  0
                 wri.flush();
 173   
             } catch (IOException exc) {
 174  0
                 throw new BuildException("Unable to write log file", exc);
 175   
             } finally {
 176  0
                 if (out != System.out && out != System.err) {
 177  0
                     if (wri != null) {
 178  0
                         try {
 179  0
                             wri.close();
 180   
                         } catch (IOException e) {}
 181   
                     }
 182   
                 }
 183   
             }
 184   
         }
 185   
     }
 186   
 
 187   
     /**
 188   
      * Interface TestListener.
 189   
      *
 190   
      * <p>A new Test is started.
 191   
      */
 192  0
     public void startTest(Test t) {
 193  0
         testStarts.put(t, new Long(System.currentTimeMillis()));
 194   
     }
 195   
 
 196   
     /**
 197   
      * Interface TestListener.
 198   
      *
 199   
      * <p>A Test is finished.
 200   
      */
 201  0
     public void endTest(Test test) {
 202   
         // Fix for bug #5637 - if a junit.extensions.TestSetup is
 203   
         // used and throws an exception during setUp then startTest
 204   
         // would never have been called
 205  0
         if (!testStarts.containsKey(test)) {
 206  0
             startTest(test);
 207   
         }
 208   
 
 209  0
         Element currentTest = null;
 210  0
         if (!failedTests.containsKey(test)) {
 211  0
             currentTest = doc.createElement(TESTCASE);
 212  0
             currentTest.setAttribute(ATTR_NAME, 
 213   
                                      JUnitVersionHelper.getTestCaseName(test));
 214  0
             rootElement.appendChild(currentTest);
 215  0
             testElements.put(test, currentTest);
 216   
         } else {
 217  0
             currentTest = (Element) testElements.get(test);
 218   
         }
 219   
 
 220  0
         Long l = (Long) testStarts.get(test);
 221  0
         currentTest.setAttribute(ATTR_TIME,
 222   
             "" + ((System.currentTimeMillis() - l.longValue()) / 1000.0));
 223   
     }
 224   
 
 225   
     /**
 226   
      * Interface TestListener for JUnit &lt;= 3.4.
 227   
      *
 228   
      * <p>A Test failed.
 229   
      */
 230  0
     public void addFailure(Test test, Throwable t) {
 231  0
         formatError(FAILURE, test, t);
 232   
     }
 233   
 
 234   
     /**
 235   
      * Interface TestListener for JUnit &gt; 3.4.
 236   
      *
 237   
      * <p>A Test failed.
 238   
      */
 239  0
     public void addFailure(Test test, AssertionFailedError t) {
 240  0
         addFailure(test, (Throwable) t);
 241   
     }
 242   
 
 243   
     /**
 244   
      * Interface TestListener.
 245   
      *
 246   
      * <p>An error occured while running the test.
 247   
      */
 248  0
     public void addError(Test test, Throwable t) {
 249  0
         formatError(ERROR, test, t);
 250   
     }
 251   
 
 252  0
     private void formatError(String type, Test test, Throwable t) {
 253  0
         if (test != null) {
 254  0
             endTest(test);
 255  0
             failedTests.put(test, test);
 256   
         }
 257   
 
 258  0
         Element nested = doc.createElement(type);
 259  0
         Element currentTest = null;
 260  0
         if (test != null) {
 261  0
             currentTest = (Element) testElements.get(test);
 262   
         } else {
 263  0
             currentTest = rootElement;
 264   
         }
 265   
 
 266  0
         currentTest.appendChild(nested);
 267   
 
 268  0
         String message = t.getMessage();
 269  0
         if (message != null && message.length() > 0) {
 270  0
             nested.setAttribute(ATTR_MESSAGE, t.getMessage());
 271   
         }
 272  0
         nested.setAttribute(ATTR_TYPE, t.getClass().getName());
 273   
 
 274  0
         String strace = JUnitTestRunner.getFilteredTrace(t);
 275  0
         Text trace = doc.createTextNode(strace);
 276  0
         nested.appendChild(trace);
 277   
     }
 278   
 
 279  0
     private void formatOutput(String type, String output) {
 280  0
         Element nested = doc.createElement(type);
 281  0
         rootElement.appendChild(nested);
 282  0
         nested.appendChild(doc.createCDATASection(output));
 283   
     }
 284   
 
 285   
 } // XMLJUnitResultFormatter
 286