Coverage Report - org.apache.maven.plugins.surefire.report.SurefireReportParser
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireReportParser
92%
88/96
88%
21/24
2.286
 
 1  
 package org.apache.maven.plugins.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 java.io.File;
 23  
 import java.io.IOException;
 24  
 import java.text.NumberFormat;
 25  
 import java.util.ArrayList;
 26  
 import java.util.Collection;
 27  
 import java.util.Collections;
 28  
 import java.util.HashMap;
 29  
 import java.util.List;
 30  
 import java.util.ListIterator;
 31  
 import java.util.Locale;
 32  
 import java.util.Map;
 33  
 
 34  
 import javax.xml.parsers.ParserConfigurationException;
 35  
 
 36  
 import org.apache.maven.reporting.MavenReportException;
 37  
 import org.codehaus.plexus.util.DirectoryScanner;
 38  
 import org.codehaus.plexus.util.StringUtils;
 39  
 import org.xml.sax.SAXException;
 40  
 
 41  
 /**
 42  
  * @version $Id: SurefireReportParser.java 1074755 2011-02-26 00:12:21Z hboutemy $
 43  
  */
 44  
 public class SurefireReportParser
 45  
 {
 46  12
     private NumberFormat numberFormat = NumberFormat.getInstance();
 47  
 
 48  
     private List reportsDirectories;
 49  
 
 50  12
     private final List testSuites = new ArrayList();
 51  
 
 52  
     private static final int PCENT = 100;
 53  
 
 54  
     public SurefireReportParser()
 55  8
     {
 56  8
     }
 57  
 
 58  
     public SurefireReportParser( List reportsDirectoriesFiles , Locale locale )
 59  4
     {
 60  4
         this.reportsDirectories = reportsDirectoriesFiles;
 61  
 
 62  4
         setLocale( locale );
 63  4
     }
 64  
 
 65  
     public List parseXMLReportFiles()
 66  
         throws MavenReportException
 67  
     {
 68  5
         List xmlReportFileList = new ArrayList();
 69  10
         for ( int i = 0; i < reportsDirectories.size(); i++ )
 70  
         {
 71  5
             File reportsDirectory = (File) reportsDirectories.get( i );
 72  5
             if ( !reportsDirectory.exists() )
 73  
             {
 74  0
                 continue;
 75  
             }
 76  5
             String[] xmlReportFiles =
 77  
                 getIncludedFiles( reportsDirectory, "*.xml",
 78  
                                   "*.txt, testng-failed.xml, testng-failures.xml, testng-results.xml" );
 79  16
             for ( int j = 0; j < xmlReportFiles.length; j++ )
 80  
             {
 81  11
                 File xmlReport = new File( reportsDirectory, xmlReportFiles[j] );
 82  11
                 xmlReportFileList.add( xmlReport );
 83  
             }
 84  
         }
 85  5
         TestSuiteXmlParser parser = new TestSuiteXmlParser();
 86  16
         for ( int index = 0; index < xmlReportFileList.size(); index++ )
 87  
         {
 88  
             Collection suites;
 89  
 
 90  11
             File currentReport = (File) xmlReportFileList.get( index );
 91  
 
 92  
             try
 93  
             {
 94  11
                 suites = parser.parse( currentReport.getAbsolutePath() );
 95  
             }
 96  0
             catch ( ParserConfigurationException e )
 97  
             {
 98  0
                 throw new MavenReportException( "Error setting up parser for JUnit XML report", e );
 99  
             }
 100  0
             catch ( SAXException e )
 101  
             {
 102  0
                 throw new MavenReportException( "Error parsing JUnit XML report " + currentReport, e );
 103  
             }
 104  0
             catch ( IOException e )
 105  
             {
 106  0
                 throw new MavenReportException( "Error reading JUnit XML report " + currentReport, e );
 107  11
             }
 108  
 
 109  11
             testSuites.addAll( suites );
 110  
         }
 111  
 
 112  5
         return testSuites;
 113  
     }
 114  
 
 115  
     protected String parseTestSuiteName( String lineString )
 116  
     {
 117  1
         return lineString.substring( lineString.lastIndexOf( "." ) + 1, lineString.length() );
 118  
     }
 119  
 
 120  
     protected String parseTestSuitePackageName( String lineString )
 121  
     {
 122  1
         return lineString.substring( lineString.indexOf( ":" ) + 2, lineString.lastIndexOf( "." ) );
 123  
     }
 124  
 
 125  
     protected String parseTestCaseName( String lineString )
 126  
     {
 127  1
         return lineString.substring( 0, lineString.indexOf( "(" ) );
 128  
     }
 129  
 
 130  
     public Map getSummary( List suites )
 131  
     {
 132  9
         Map totalSummary = new HashMap();
 133  
 
 134  9
         ListIterator iter = suites.listIterator();
 135  
 
 136  9
         int totalNumberOfTests = 0;
 137  
 
 138  9
         int totalNumberOfErrors = 0;
 139  
 
 140  9
         int totalNumberOfFailures = 0;
 141  
 
 142  9
         int totalNumberOfSkipped = 0;
 143  
 
 144  9
         float totalElapsedTime = 0.0f;
 145  
 
 146  19
         while ( iter.hasNext() )
 147  
         {
 148  10
             ReportTestSuite suite = (ReportTestSuite) iter.next();
 149  
 
 150  10
             totalNumberOfTests += suite.getNumberOfTests();
 151  
 
 152  10
             totalNumberOfErrors += suite.getNumberOfErrors();
 153  
 
 154  10
             totalNumberOfFailures += suite.getNumberOfFailures();
 155  
 
 156  10
             totalNumberOfSkipped += suite.getNumberOfSkipped();
 157  
 
 158  10
             totalElapsedTime += suite.getTimeElapsed();
 159  10
         }
 160  
 
 161  9
         String totalPercentage =
 162  
             computePercentage( totalNumberOfTests, totalNumberOfErrors, totalNumberOfFailures, totalNumberOfSkipped );
 163  
 
 164  9
         totalSummary.put( "totalTests", Integer.toString( totalNumberOfTests ) );
 165  
 
 166  9
         totalSummary.put( "totalErrors", Integer.toString( totalNumberOfErrors ) );
 167  
 
 168  9
         totalSummary.put( "totalFailures", Integer.toString( totalNumberOfFailures ) );
 169  
 
 170  9
         totalSummary.put( "totalSkipped", Integer.toString( totalNumberOfSkipped ) );
 171  
 
 172  9
         totalSummary.put( "totalElapsedTime", numberFormat.format( totalElapsedTime ) );
 173  
 
 174  9
         totalSummary.put( "totalPercentage", totalPercentage );
 175  
 
 176  9
         return totalSummary;
 177  
     }
 178  
 
 179  
     public void setReportsDirectory( File reportsDirectory )
 180  
     {
 181  1
         this.reportsDirectories = Collections.singletonList( reportsDirectory );
 182  1
     }
 183  
 
 184  
     public final void setLocale( Locale locale )
 185  
     {
 186  12
         numberFormat = NumberFormat.getInstance( locale );
 187  12
     }
 188  
 
 189  
     public NumberFormat getNumberFormat()
 190  
     {
 191  10
         return this.numberFormat;
 192  
     }
 193  
 
 194  
     public Map getSuitesGroupByPackage( List testSuitesList )
 195  
     {
 196  5
         ListIterator iter = testSuitesList.listIterator();
 197  
 
 198  5
         Map suitePackage = new HashMap();
 199  
 
 200  12
         while ( iter.hasNext() )
 201  
         {
 202  7
             ReportTestSuite suite = (ReportTestSuite) iter.next();
 203  
 
 204  7
             List suiteList = new ArrayList();
 205  
 
 206  7
             if ( suitePackage.get( suite.getPackageName() ) != null )
 207  
             {
 208  1
                 suiteList = (List) suitePackage.get( suite.getPackageName() );
 209  
             }
 210  
 
 211  7
             suiteList.add( suite );
 212  
 
 213  7
             suitePackage.put( suite.getPackageName(), suiteList );
 214  7
         }
 215  
 
 216  5
         return suitePackage;
 217  
     }
 218  
 
 219  
     public String computePercentage( int tests, int errors, int failures, int skipped )
 220  
     {
 221  
         float percentage;
 222  14
         if ( tests == 0 )
 223  
         {
 224  0
             percentage = 0;
 225  
         }
 226  
         else
 227  
         {
 228  14
             percentage = ( (float) ( tests - errors - failures - skipped ) / (float) tests ) * PCENT;
 229  
         }
 230  
 
 231  14
         return numberFormat.format( percentage );
 232  
     }
 233  
 
 234  
     public List getFailureDetails( List testSuitesList )
 235  
     {
 236  5
         ListIterator iter = testSuitesList.listIterator();
 237  
 
 238  5
         List failureDetailList = new ArrayList();
 239  
 
 240  11
         while ( iter.hasNext() )
 241  
         {
 242  6
             ReportTestSuite suite = (ReportTestSuite) iter.next();
 243  
 
 244  6
             List testCaseList = suite.getTestCases();
 245  
 
 246  6
             if ( testCaseList != null )
 247  
             {
 248  6
                 ListIterator caseIter = testCaseList.listIterator();
 249  
 
 250  41
                 while ( caseIter.hasNext() )
 251  
                 {
 252  35
                     ReportTestCase tCase = (ReportTestCase) caseIter.next();
 253  
 
 254  35
                     if ( tCase.getFailure() != null )
 255  
                     {
 256  10
                         failureDetailList.add( tCase );
 257  
                     }
 258  35
                 }
 259  
             }
 260  6
         }
 261  
 
 262  5
         return failureDetailList;
 263  
     }
 264  
 
 265  
     private String[] getIncludedFiles( File directory, String includes, String excludes )
 266  
     {
 267  5
         DirectoryScanner scanner = new DirectoryScanner();
 268  
 
 269  5
         scanner.setBasedir( directory );
 270  
 
 271  5
         scanner.setIncludes( StringUtils.split( includes, "," ) );
 272  
 
 273  5
         scanner.setExcludes( StringUtils.split( excludes, "," ) );
 274  
 
 275  5
         scanner.scan();
 276  
 
 277  5
         return scanner.getIncludedFiles();
 278  
     }
 279  
 }