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