Coverage Report - org.apache.maven.plugin.surefire.report.StatelessXmlReporter
 
Classes in this File Line Coverage Branch Coverage Complexity
StatelessXmlReporter
82%
99/120
47%
35/74
3,882
StatelessXmlReporter$EncodingOutputStream
87%
21/24
87%
7/8
3,882
 
 1  
 package org.apache.maven.plugin.surefire.report;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *     http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.io.output.DeferredFileOutputStream;
 23  
 import org.apache.maven.shared.utils.io.IOUtil;
 24  
 import org.apache.maven.shared.utils.xml.XMLWriter;
 25  
 import org.apache.maven.surefire.report.ReportEntry;
 26  
 import org.apache.maven.surefire.report.ReporterException;
 27  
 import org.apache.maven.surefire.report.SafeThrowable;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.FileOutputStream;
 31  
 import java.io.FilterOutputStream;
 32  
 import java.io.IOException;
 33  
 import java.io.OutputStream;
 34  
 import java.io.OutputStreamWriter;
 35  
 import java.util.Enumeration;
 36  
 import java.util.Properties;
 37  
 import java.util.StringTokenizer;
 38  
 
 39  
 import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars;
 40  
 
 41  
 /**
 42  
  * XML format reporter writing to <code>TEST-<i>reportName</i>[-<i>suffix</i>].xml</code> file like written and read
 43  
  * by Ant's <a href="http://ant.apache.org/manual/Tasks/junit.html"><code>&lt;junit&gt;</code></a> and
 44  
  * <a href="http://ant.apache.org/manual/Tasks/junitreport.html"><code>&lt;junitreport&gt;</code></a> tasks,
 45  
  * then supported by many tools like CI servers.
 46  
  * <p/>
 47  
  * <pre>&lt;?xml version="1.0" encoding="UTF-8"?>
 48  
  * &lt;testsuite name="<i>suite name</i>" [group="<i>group</i>"] tests="<i>0</i>" failures="<i>0</i>" errors="<i>0</i>" skipped="<i>0</i>" time="<i>0,###.###</i>">
 49  
  *  &lt;properties>
 50  
  *    &lt;property name="<i>name</i>" value="<i>value</i>"/>
 51  
  *    [...]
 52  
  *  &lt;/properties>
 53  
  *  &lt;testcase time="<i>0,###.###</i>" name="<i>test name</i> [classname="<i>class name</i>"] [group="<i>group</i>"]"/>
 54  
  *  &lt;testcase time="<i>0,###.###</i>" name="<i>test name</i> [classname="<i>class name</i>"] [group="<i>group</i>"]">
 55  
  *    &lt;<b>error</b> message="<i>message</i>" type="<i>exception class name</i>"><i>stacktrace</i>&lt;/error>
 56  
  *    &lt;system-out><i>system out content (present only if not empty)</i>&lt;/system-out>
 57  
  *    &lt;system-err><i>system err content (present only if not empty)</i>&lt;/system-err>
 58  
  *  &lt;/testcase>
 59  
  *  &lt;testcase time="<i>0,###.###</i>" name="<i>test name</i> [classname="<i>class name</i>"] [group="<i>group</i>"]">
 60  
  *    &lt;<b>failure</b> message="<i>message</i>" type="<i>exception class name</i>"><i>stacktrace</i>&lt;/failure>
 61  
  *    &lt;system-out><i>system out content (present only if not empty)</i>&lt;/system-out>
 62  
  *    &lt;system-err><i>system err content (present only if not empty)</i>&lt;/system-err>
 63  
  *  &lt;/testcase>
 64  
  *  &lt;testcase time="<i>0,###.###</i>" name="<i>test name</i> [classname="<i>class name</i>"] [group="<i>group</i>"]">
 65  
  *    &lt;<b>skipped</b>/>
 66  
  *  &lt;/testcase>
 67  
  *  [...]</pre>
 68  
  *
 69  
  * @author Kristian Rosenvold
 70  
  * @see <a href="http://wiki.apache.org/ant/Proposals/EnhancedTestReports">Ant's format enhancement proposal</a>
 71  
  *      (not yet implemented by Ant 1.8.2)
 72  
  */
 73  37
 public class StatelessXmlReporter
 74  
 {
 75  
 
 76  1
     private static final byte[] ampBytes = "&amp#".getBytes();
 77  
 
 78  
     private final File reportsDirectory;
 79  
 
 80  
     private final String reportNameSuffix;
 81  
 
 82  
     private final boolean trimStackTrace;
 83  
 
 84  3
     private final String encoding = "UTF-8";
 85  
 
 86  
     public StatelessXmlReporter( File reportsDirectory, String reportNameSuffix, boolean trimStackTrace )
 87  3
     {
 88  3
         this.reportsDirectory = reportsDirectory;
 89  3
         this.reportNameSuffix = reportNameSuffix;
 90  3
         this.trimStackTrace = trimStackTrace;
 91  3
     }
 92  
 
 93  
     public void testSetCompleted( WrappedReportEntry testSetReportEntry, TestSetStats testSetStats )
 94  
         throws ReporterException
 95  
     {
 96  
 
 97  2
         FileOutputStream outputStream = getOutputStream( testSetReportEntry );
 98  2
         OutputStreamWriter fw = getWriter( outputStream );
 99  
         try
 100  
         {
 101  
 
 102  2
             org.apache.maven.shared.utils.xml.XMLWriter ppw =
 103  
                 new org.apache.maven.shared.utils.xml.PrettyPrintXMLWriter( fw );
 104  2
             ppw.setEncoding( encoding );
 105  
 
 106  2
             createTestSuiteElement( ppw, testSetReportEntry, testSetStats, reportNameSuffix );
 107  
 
 108  2
             showProperties( ppw );
 109  
 
 110  2
             for ( WrappedReportEntry entry : testSetStats.getReportEntries() )
 111  
             {
 112  3
                 if ( ReportEntryType.success.equals( entry.getReportEntryType() ) )
 113  
                 {
 114  2
                     startTestElement( ppw, entry, reportNameSuffix );
 115  2
                     ppw.endElement();
 116  
                 }
 117  
                 else
 118  
                 {
 119  1
                     getTestProblems( fw, ppw, entry, trimStackTrace, reportNameSuffix, outputStream );
 120  
                 }
 121  
 
 122  
             }
 123  2
             ppw.endElement(); // TestSuite
 124  
 
 125  
         }
 126  
         finally
 127  
         {
 128  2
             IOUtil.close( fw );
 129  2
         }
 130  2
     }
 131  
 
 132  
     private OutputStreamWriter getWriter( FileOutputStream fos )
 133  
     {
 134  
         try
 135  
         {
 136  
 
 137  2
             return new OutputStreamWriter( fos, encoding );
 138  
         }
 139  0
         catch ( IOException e )
 140  
         {
 141  0
             throw new ReporterException( "When writing report", e );
 142  
         }
 143  
     }
 144  
 
 145  
     private FileOutputStream getOutputStream( WrappedReportEntry testSetReportEntry )
 146  
     {
 147  2
         File reportFile = getReportFile( testSetReportEntry, reportsDirectory, reportNameSuffix );
 148  
 
 149  2
         File reportDir = reportFile.getParentFile();
 150  
 
 151  
         //noinspection ResultOfMethodCallIgnored
 152  2
         reportDir.mkdirs();
 153  
 
 154  
         try
 155  
         {
 156  
 
 157  2
             return new FileOutputStream( reportFile );
 158  
         }
 159  0
         catch ( Exception e )
 160  
         {
 161  0
             throw new ReporterException( "When writing report", e );
 162  
         }
 163  
     }
 164  
 
 165  
     private File getReportFile( ReportEntry report, File reportsDirectory, String reportNameSuffix )
 166  
     {
 167  
         File reportFile;
 168  
 
 169  2
         if ( reportNameSuffix != null && reportNameSuffix.length() > 0 )
 170  
         {
 171  0
             reportFile = new File( reportsDirectory, stripIllegalFilenameChars(
 172  
                 "TEST-" + report.getName() + "-" + reportNameSuffix + ".xml" ) );
 173  
         }
 174  
         else
 175  
         {
 176  2
             reportFile = new File( reportsDirectory, stripIllegalFilenameChars( "TEST-" + report.getName() + ".xml" ) );
 177  
         }
 178  
 
 179  2
         return reportFile;
 180  
     }
 181  
 
 182  
     private static void startTestElement( XMLWriter ppw, WrappedReportEntry report, String reportNameSuffix )
 183  
     {
 184  3
         ppw.startElement( "testcase" );
 185  3
         ppw.addAttribute( "name", report.getReportName() );
 186  3
         if ( report.getGroup() != null )
 187  
         {
 188  0
             ppw.addAttribute( "group", report.getGroup() );
 189  
         }
 190  3
         if ( report.getSourceName() != null )
 191  
         {
 192  3
             if ( reportNameSuffix != null && reportNameSuffix.length() > 0 )
 193  
             {
 194  0
                 ppw.addAttribute( "classname", report.getSourceName() + "(" + reportNameSuffix + ")" );
 195  
             }
 196  
             else
 197  
             {
 198  3
                 ppw.addAttribute( "classname", report.getSourceName() );
 199  
             }
 200  
         }
 201  3
         ppw.addAttribute( "time", report.elapsedTimeAsString() );
 202  3
     }
 203  
 
 204  
     private static void createTestSuiteElement( XMLWriter ppw, WrappedReportEntry report, TestSetStats testSetStats,
 205  
                                                 String reportNameSuffix1 )
 206  
     {
 207  2
         ppw.startElement( "testsuite" );
 208  
 
 209  2
         ppw.addAttribute( "name", report.getReportName( reportNameSuffix1 ) );
 210  
 
 211  2
         if ( report.getGroup() != null )
 212  
         {
 213  0
             ppw.addAttribute( "group", report.getGroup() );
 214  
         }
 215  
 
 216  2
         ppw.addAttribute( "time", testSetStats.getElapsedForTestSet() );
 217  
 
 218  2
         ppw.addAttribute( "tests", String.valueOf( testSetStats.getCompletedCount() ) );
 219  
 
 220  2
         ppw.addAttribute( "errors", String.valueOf( testSetStats.getErrors() ) );
 221  
 
 222  2
         ppw.addAttribute( "skipped", String.valueOf( testSetStats.getSkipped() ) );
 223  
 
 224  2
         ppw.addAttribute( "failures", String.valueOf( testSetStats.getFailures() ) );
 225  
 
 226  2
     }
 227  
 
 228  
 
 229  
     private void getTestProblems( OutputStreamWriter outputStreamWriter, XMLWriter ppw, WrappedReportEntry report,
 230  
                                   boolean trimStackTrace, String reportNameSuffix, FileOutputStream fw )
 231  
     {
 232  
 
 233  1
         startTestElement( ppw, report, reportNameSuffix );
 234  
 
 235  1
         ppw.startElement( report.getReportEntryType().name() );
 236  
 
 237  1
         String stackTrace = report.getStackTrace( trimStackTrace );
 238  
 
 239  1
         if ( report.getMessage() != null && report.getMessage().length() > 0 )
 240  
         {
 241  1
             ppw.addAttribute( "message", extraEscape( report.getMessage(), true ) );
 242  
         }
 243  
 
 244  1
         if ( report.getStackTraceWriter() != null )
 245  
         {
 246  
             //noinspection ThrowableResultOfMethodCallIgnored
 247  1
             SafeThrowable t = report.getStackTraceWriter().getThrowable();
 248  1
             if ( t != null )
 249  
             {
 250  1
                 if ( t.getMessage() != null )
 251  
                 {
 252  1
                     ppw.addAttribute( "type", ( stackTrace.contains( ":" )
 253  
                         ? stackTrace.substring( 0, stackTrace.indexOf( ":" ) )
 254  
                         : stackTrace ) );
 255  
                 }
 256  
                 else
 257  
                 {
 258  0
                     ppw.addAttribute( "type", new StringTokenizer( stackTrace ).nextToken() );
 259  
                 }
 260  
             }
 261  
         }
 262  
 
 263  1
         if ( stackTrace != null )
 264  
         {
 265  1
             ppw.writeText( extraEscape( stackTrace, false ) );
 266  
         }
 267  
 
 268  1
         ppw.endElement(); // entry type
 269  
 
 270  1
         EncodingOutputStream eos = new EncodingOutputStream( fw );
 271  
 
 272  1
         addOutputStreamElement( outputStreamWriter, fw, eos, ppw, report.getStdout(), "system-out" );
 273  
 
 274  1
         addOutputStreamElement( outputStreamWriter, fw, eos, ppw, report.getStdErr(), "system-err" );
 275  
 
 276  1
         ppw.endElement(); // test element
 277  1
     }
 278  
 
 279  
     private void addOutputStreamElement( OutputStreamWriter outputStreamWriter, OutputStream fw,
 280  
                                          EncodingOutputStream eos, XMLWriter xmlWriter, DeferredFileOutputStream stdOut,
 281  
                                          String name )
 282  
     {
 283  2
         if ( stdOut != null && stdOut.getByteCount() > 0 )
 284  
         {
 285  
 
 286  2
             xmlWriter.startElement( name );
 287  
 
 288  
             try
 289  
             {
 290  2
                 xmlWriter.writeText( "" ); // Cheat sax to emit element
 291  2
                 outputStreamWriter.flush();
 292  2
                 stdOut.close();
 293  2
                 stdOut.writeTo( eos );
 294  2
                 eos.flush();
 295  
             }
 296  0
             catch ( IOException e )
 297  
             {
 298  0
                 throw new ReporterException( "When writing xml report stdout/stderr", e );
 299  2
             }
 300  2
             xmlWriter.endElement();
 301  
         }
 302  2
     }
 303  
 
 304  
     /**
 305  
      * Adds system properties to the XML report.
 306  
      * <p/>
 307  
      *
 308  
      * @param xmlWriter The test suite to report to
 309  
      */
 310  
     private void showProperties( XMLWriter xmlWriter )
 311  
     {
 312  2
         xmlWriter.startElement( "properties" );
 313  
 
 314  2
         Properties systemProperties = System.getProperties();
 315  
 
 316  2
         if ( systemProperties != null )
 317  
         {
 318  2
             Enumeration<?> propertyKeys = systemProperties.propertyNames();
 319  
 
 320  114
             while ( propertyKeys.hasMoreElements() )
 321  
             {
 322  112
                 String key = (String) propertyKeys.nextElement();
 323  
 
 324  112
                 String value = systemProperties.getProperty( key );
 325  
 
 326  112
                 if ( value == null )
 327  
                 {
 328  0
                     value = "null";
 329  
                 }
 330  
 
 331  112
                 xmlWriter.startElement( "property" );
 332  
 
 333  112
                 xmlWriter.addAttribute( "name", key );
 334  
 
 335  112
                 xmlWriter.addAttribute( "value", extraEscape( value, true ) );
 336  
 
 337  112
                 xmlWriter.endElement();
 338  
 
 339  112
             }
 340  
         }
 341  2
         xmlWriter.endElement();
 342  2
     }
 343  
 
 344  
     /**
 345  
      * Handle stuff that may pop up in java that is not legal in xml
 346  
      *
 347  
      * @param message   The string
 348  
      * @param attribute true if the escaped value is inside an attribute
 349  
      * @return The escaped string
 350  
      */
 351  
     private static String extraEscape( String message, boolean attribute )
 352  
     {
 353  
         // Someday convert to xml 1.1 which handles everything but 0 inside string
 354  114
         if ( !containsEscapesIllegalnXml10( message ) )
 355  
         {
 356  114
             return message;
 357  
         }
 358  0
         return escapeXml( message, attribute );
 359  
     }
 360  
 
 361  
     private static class EncodingOutputStream
 362  
         extends FilterOutputStream
 363  
     {
 364  
 
 365  
         public EncodingOutputStream( OutputStream out )
 366  
         {
 367  1
             super( out );
 368  1
         }
 369  
 
 370  
         @Override
 371  
         public void write( int b )
 372  
             throws IOException
 373  
         {
 374  37
             if ( isIllegalEscape( b ) )
 375  
             {
 376  
                 // uh-oh!  This character is illegal in XML 1.0!
 377  
                 // http://www.w3.org/TR/1998/REC-xml-19980210#charsets
 378  
                 // we're going to deliberately doubly-XML escape it...
 379  
                 // there's nothing better we can do! :-(
 380  
                 // SUREFIRE-456
 381  0
                 out.write( ampBytes );
 382  0
                 out.write( b );
 383  0
                 out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
 384  
             }
 385  37
             else if ( '<' == b )
 386  
             {
 387  1
                 out.write( '&' );
 388  1
                 out.write( 'l' );
 389  1
                 out.write( 't' );
 390  1
                 out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
 391  
             }
 392  36
             else if ( '>' == b )
 393  
             {
 394  1
                 out.write( '&' );
 395  1
                 out.write( 'g' );
 396  1
                 out.write( 't' );
 397  1
                 out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
 398  
             }
 399  35
             else if ( '&' == b )
 400  
             {
 401  3
                 out.write( '&' );
 402  3
                 out.write( 'a' );
 403  3
                 out.write( 'm' );
 404  3
                 out.write( 'p' );
 405  3
                 out.write( ';' ); // & Will be encoded to amp inside xml encodingSHO
 406  
             }
 407  
             else
 408  
             {
 409  32
                 out.write( b );    //To change body of overridden methods use File | Settings | File Templates.
 410  
             }
 411  37
         }
 412  
     }
 413  
 
 414  
     private static boolean containsEscapesIllegalnXml10( String message )
 415  
     {
 416  114
         int size = message.length();
 417  9444
         for ( int i = 0; i < size; i++ )
 418  
         {
 419  9330
             if ( isIllegalEscape( message.charAt( i ) ) )
 420  
             {
 421  0
                 return true;
 422  
             }
 423  
 
 424  
         }
 425  114
         return false;
 426  
     }
 427  
 
 428  
     private static boolean isIllegalEscape( char c )
 429  
     {
 430  9330
         return c >= 0 && c < 32 && c != '\n' && c != '\r' && c != '\t';
 431  
     }
 432  
 
 433  
     private static boolean isIllegalEscape( int c )
 434  
     {
 435  37
         return c >= 0 && c < 32 && c != '\n' && c != '\r' && c != '\t';
 436  
     }
 437  
 
 438  
     private static String escapeXml( String text, boolean attribute )
 439  
     {
 440  0
         StringBuilder sb = new StringBuilder( text.length() * 2 );
 441  0
         for ( int i = 0; i < text.length(); i++ )
 442  
         {
 443  0
             char c = text.charAt( i );
 444  0
             if ( isIllegalEscape( c ) )
 445  
             {
 446  
                 // uh-oh!  This character is illegal in XML 1.0!
 447  
                 // http://www.w3.org/TR/1998/REC-xml-19980210#charsets
 448  
                 // we're going to deliberately doubly-XML escape it...
 449  
                 // there's nothing better we can do! :-(
 450  
                 // SUREFIRE-456
 451  0
                 sb.append( attribute ? "&#" : "&amp#" ).append( (int) c ).append(
 452  
                     ';' ); // & Will be encoded to amp inside xml encodingSHO
 453  
             }
 454  
             else
 455  
             {
 456  0
                 sb.append( c );
 457  
             }
 458  
         }
 459  0
         return sb.toString();
 460  
     }
 461  
 
 462  
 }