Clover coverage report - Cactus 1.4 for J2EE API 13
Coverage timestamp: Sun Aug 25 2002 18:02:10 BST
file stats: LOC: 240   Methods: 14
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XMLFormatter.java 0% 0% 0% 0%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus.server.runner;
 3   
 import java.text.NumberFormat;
 4   
 import junit.framework.Test;
 5   
 import junit.framework.TestResult;
 6   
 import junit.framework.TestFailure;
 7   
 import junit.framework.TestListener;
 8   
 import junit.framework.AssertionFailedError;
 9   
 import org.apache.cactus.util.JUnitVersionHelper;
 10   
 import org.apache.cactus.util.StringUtil;
 11   
 
 12   
 /** 
 13   
  * Format the test results in XML. 
 14   
  * 
 15   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 16   
  * 
 17   
  * @version $Id: XMLFormatter.java,v 1.4.2.1 2002/08/14 07:18:14 vmassol Exp $ 
 18   
  */
 19   
 public class XMLFormatter implements XMLConstants, TestListener {
 20   
   /** 
 21   
        * (optional) Name of the XSL stylesheet to put in the returned XML string 
 22   
        * so that the browser will try to apply it (IE at least, I don't know 
 23   
        * about the others). 
 24   
        */
 25   
   private String xslFileName;
 26   
   /** 
 27   
        * The name of the test suite class. 
 28   
        */
 29   
   private String suiteClassName;
 30   
   /** 
 31   
        * Duration it took to execute all the tests. 
 32   
        */
 33   
   private long totalDuration;
 34   
   /** 
 35   
        * Time current test was started 
 36   
        */
 37   
   private long currentTestStartTime;
 38   
   /** 
 39   
        * XML string containing executed test case results 
 40   
        */
 41   
   private StringBuffer currentTestCaseResults;
 42   
   /** 
 43   
        * Current test failure (XML string) : failure or error. 
 44   
        */
 45   
   private String currentTestFailure;
 46   
   /** 
 47   
        * Sets the XSL stylesheet file name to put in the returned XML string 
 48   
        * so that the browser will try to apply it (IE at least, I don't know 
 49   
        * about the others). 
 50   
        * 
 51   
        * @param theXslFileName the file name (relative to the webapp root) 
 52   
        */
 53  0
   public void setXslFileName(String theXslFileName) {
 54  0
     this.xslFileName = theXslFileName;
 55   
   } 
 56   
 
 57   
   /** 
 58   
        * @return the suite class name 
 59   
        */
 60  0
   public String getSuiteClassName() {
 61  0
     return this.suiteClassName;
 62   
   } 
 63   
 
 64   
   /** 
 65   
        * Sets the suite class name that was executed. 
 66   
        * 
 67   
        * @param theSuiteClassName the suite class name 
 68   
        */
 69  0
   public void setSuiteClassName(String theSuiteClassName) {
 70  0
     this.suiteClassName = theSuiteClassName;
 71   
   } 
 72   
 
 73   
   /** 
 74   
        * @return the total duration as a string 
 75   
        */
 76  0
   public String getTotalDurationAsString() {
 77  0
     return this.getDurationAsString(this.totalDuration);
 78   
   } 
 79   
 
 80   
   /** 
 81   
        * Comvert a duration expressed as a long into a string. 
 82   
        * 
 83   
        * @param theDuration the duration to convert to string 
 84   
        * @return the total duration as a string 
 85   
        */
 86  0
   private String getDurationAsString(long theDuration) {
 87  0
     return NumberFormat.getInstance().format((double)theDuration / 1000);
 88   
   } 
 89   
 
 90   
   /** 
 91   
        * Sets the duration it took to execute all the tests. 
 92   
        * 
 93   
        * @param theDuration the time it took 
 94   
        */
 95  0
   public void setTotalDuration(long theDuration) {
 96  0
     this.totalDuration = theDuration;
 97   
   } 
 98   
 
 99   
   /** 
 100   
        * Formats the test result as an XML string. 
 101   
        * 
 102   
        * @param theResult the test result object 
 103   
        * @return the XML string representation of the test results 
 104   
        */
 105  0
   public String toXML(TestResult theResult) {
 106  0
     StringBuffer xml = new StringBuffer();
 107  0
     xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
 108  0
     if (this.xslFileName != null) {
 109  0
       xml.append("<?xml-stylesheet type=\"text/xsl\" href=\"" + this.xslFileName + "\"?>");
 110   
     } 
 111  0
     xml.append("<testsuites>");
 112  0
     xml.append("<testsuite name=\"" + this.getSuiteClassName() + "\" " + "tests" + "=\"" + 
 113   
         theResult.runCount() + "\" " + "failures" + "=\"" + theResult.failureCount() + "\" " + 
 114   
         "errors" + "=\"" + theResult.errorCount() + "\" " + "time" + "=\"" + 
 115   
         this.getTotalDurationAsString() + "\">");
 116  0
     xml.append(this.currentTestCaseResults.toString());
 117  0
     xml.append("</testsuite>");
 118  0
     xml.append("</testsuites>");
 119  0
     return xml.toString();
 120   
   } 
 121   
 
 122   
   /** 
 123   
        * Event called by the base test runner when the test starts. 
 124   
        * 
 125   
        * @param theTest the test object being executed 
 126   
        */
 127  0
   public void startTest(Test theTest) {
 128  0
     this.currentTestStartTime = System.currentTimeMillis();
 129  0
     this.currentTestFailure = null;
 130   
   } 
 131   
 
 132   
   /** 
 133   
        * Event called by the base test runner when the test fails with an error. 
 134   
        * 
 135   
        * @param theTest the test object that failed 
 136   
        * @param theThrowable the exception that was thrown 
 137   
        */
 138  0
   public void addError(Test theTest, Throwable theThrowable) {
 139  0
     TestFailure failure = new TestFailure(theTest, theThrowable);
 140  0
     StringBuffer xml = new StringBuffer();
 141  0
     xml.append("<error message=\"" + this.xmlEncode(failure.thrownException().getMessage()) + 
 142   
         "\" " + "type" + "=\"" + failure.thrownException().getClass().getName() + "\">");
 143  0
     xml.append(this.xmlEncode(StringUtil.exceptionToString(failure.thrownException())));
 144  0
     xml.append("</error>");
 145  0
     this.currentTestFailure = xml.toString();
 146   
   } 
 147   
 
 148   
   /** 
 149   
        * Event called by the base test runner when the test fails with a failure. 
 150   
        * 
 151   
        * @param theTest the test object that failed 
 152   
        * @param theError the exception that was thrown 
 153   
        */
 154  0
   public void addFailure(Test theTest, AssertionFailedError theError) {
 155  0
     TestFailure failure = new TestFailure(theTest, theError);
 156  0
     StringBuffer xml = new StringBuffer();
 157  0
     xml.append("<failure message=\"" + this.xmlEncode(failure.thrownException().getMessage()) + 
 158   
         "\" " + "type" + "=\"" + failure.thrownException().getClass().getName() + "\">");
 159  0
     xml.append(this.xmlEncode(StringUtil.exceptionToString(failure.thrownException())));
 160  0
     xml.append("</failure>");
 161  0
     this.currentTestFailure = xml.toString();
 162   
   } 
 163   
 
 164   
   /** 
 165   
        * Event called by the base test runner when the test ends. 
 166   
        * 
 167   
        * @param theTest the test object being executed 
 168   
        */
 169  0
   public void endTest(Test theTest) {
 170  0
     StringBuffer xml = new StringBuffer();
 171  0
     String duration = this.getDurationAsString(System.currentTimeMillis() - 
 172   
         this.currentTestStartTime);
 173  0
     xml.append("<testcase name=\"" + JUnitVersionHelper.getTestCaseName(theTest) + "\" " + "time" 
 174   
         + "=\"" + duration + "\">");
 175  0
     if (this.currentTestFailure != null) {
 176  0
       xml.append(this.currentTestFailure);
 177   
     } 
 178  0
     xml.append("</testcase>");
 179  0
     this.currentTestCaseResults.append(xml.toString());
 180   
   } 
 181   
 
 182   
   /** 
 183   
        * Escapes reserved XML characters. 
 184   
        * 
 185   
        * @param theString the string to escape 
 186   
        * @return the escaped string 
 187   
        */
 188  0
   private String xmlEncode(String theString) {
 189  0
     String newString;
 190  0
     newString = XMLFormatter.replace(theString, '&', "&amp;");
 191  0
     newString = XMLFormatter.replace(newString, '<', "&lt;");
 192  0
     newString = XMLFormatter.replace(newString, '>', "&gt;");
 193  0
     newString = XMLFormatter.replace(newString, '\"', "&quot;");
 194  0
     return newString;
 195   
   } 
 196   
 
 197   
   /** 
 198   
        * Replaces a character in a string by a substring. 
 199   
        * 
 200   
        * @param theBaseString the base string in which to perform replacements 
 201   
        * @param theChar the char to look for 
 202   
        * @param theNewString the string with which to replace the char 
 203   
        * @return the string with replacements done 
 204   
        */
 205  0
   public static String replace(String theBaseString, char theChar, String theNewString) {
 206  0
     final int len = theBaseString.length() - 1;
 207  0
     int pos = -1;
 208  0
     while ((pos = theBaseString.indexOf(theChar, pos + 1)) > -1){
 209  0
       if (pos == 0) {
 210  0
         final String after = theBaseString.substring(1);
 211  0
         theBaseString = theNewString + after;
 212  0
       } else if (pos == len) {
 213  0
         final String before = theBaseString.substring(0, pos);
 214  0
         theBaseString = before + theNewString;
 215   
       } else {
 216  0
         final String before = theBaseString.substring(0, pos);
 217  0
         final String after = theBaseString.substring(pos + 1);
 218  0
         theBaseString = before + theNewString + after;
 219   
       } 
 220   
     } 
 221  0
     return theBaseString;
 222   
   } 
 223   
 
 224   
   /** 
 225   
    * Format the test results in XML. 
 226   
    * 
 227   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 228   
    * 
 229   
    * @version $Id: XMLFormatter.java,v 1.4.2.1 2002/08/14 07:18:14 vmassol Exp $ 
 230   
    */
 231  0
   public XMLFormatter() {
 232  0
     super();
 233   
     {
 234   
       /** 
 235   
            * XML string containing executed test case results 
 236   
            */
 237  0
       this.currentTestCaseResults = new StringBuffer();
 238   
     } 
 239   
   } 
 240   
 }