Coverage Report - org.apache.maven.plugins.surefire.report.SurefireReportParser
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireReportParser
88% 
100% 
1.941
 
 1  
 package org.apache.maven.plugins.surefire.report;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.apache.maven.reporting.MavenReportException;
 20  
 import org.codehaus.plexus.util.DirectoryScanner;
 21  
 import org.codehaus.plexus.util.StringUtils;
 22  
 import org.xml.sax.SAXException;
 23  
 
 24  
 import javax.xml.parsers.ParserConfigurationException;
 25  
 import java.io.File;
 26  
 import java.io.IOException;
 27  
 import java.text.NumberFormat;
 28  
 import java.util.ArrayList;
 29  
 import java.util.HashMap;
 30  
 import java.util.List;
 31  
 import java.util.ListIterator;
 32  
 import java.util.Locale;
 33  
 import java.util.Map;
 34  
 
 35  
 public class SurefireReportParser
 36  
 {
 37  26
     private NumberFormat numberFormat = NumberFormat.getInstance();
 38  
 
 39  
     private File reportsDirectory;
 40  
 
 41  26
     private List testSuites = new ArrayList();
 42  
 
 43  
     private Locale locale;
 44  
 
 45  
     private static final int PCENT = 100;
 46  
 
 47  
     public SurefireReportParser()
 48  18
     {
 49  18
     }
 50  
 
 51  
     public SurefireReportParser( File reportsDirectory, Locale locale )
 52  8
     {
 53  8
         this.reportsDirectory = reportsDirectory;
 54  
 
 55  8
         setLocale( locale );
 56  8
     }
 57  
 
 58  
     public List parseXMLReportFiles()
 59  
         throws MavenReportException
 60  
     {
 61  10
         if ( reportsDirectory.exists() )
 62  
         {
 63  10
             String[] xmlReportFiles = getIncludedFiles( reportsDirectory, "*.xml", "*.txt" );
 64  
 
 65  26
             for ( int index = 0; index < xmlReportFiles.length; index++ )
 66  
             {
 67  16
                 ReportTestSuite testSuite = new ReportTestSuite();
 68  
                 
 69  16
                 String currentReport = xmlReportFiles[index];
 70  
 
 71  
                 try
 72  
                 {
 73  16
                     testSuite.parse( reportsDirectory + "/" + currentReport );
 74  
                 }
 75  0
                 catch ( ParserConfigurationException e )
 76  
                 {
 77  0
                     throw new MavenReportException( "Error setting up parser for JUnit XML report", e );
 78  
                 }
 79  0
                 catch ( SAXException e )
 80  
                 {
 81  0
                     throw new MavenReportException( "Error parsing JUnit XML report " + currentReport, e );
 82  
                 }
 83  0
                 catch ( IOException e )
 84  
                 {
 85  0
                     throw new MavenReportException( "Error reading JUnit XML report " + currentReport, e );
 86  16
                 }
 87  
 
 88  16
                 testSuites.add( testSuite );
 89  
             }
 90  
         }
 91  
 
 92  10
         return testSuites;
 93  
     }
 94  
 
 95  
     protected String parseTestSuiteName( String lineString )
 96  
     {
 97  2
         return lineString.substring( lineString.lastIndexOf( "." ) + 1, lineString.length() );
 98  
     }
 99  
 
 100  
     protected String parseTestSuitePackageName( String lineString )
 101  
     {
 102  2
         return lineString.substring( lineString.indexOf( ":" ) + 2, lineString.lastIndexOf( "." ) );
 103  
     }
 104  
 
 105  
     protected String parseTestCaseName( String lineString )
 106  
     {
 107  2
         return lineString.substring( 0, lineString.indexOf( "(" ) );
 108  
     }
 109  
 
 110  
     public Map getSummary( List suites )
 111  
     {
 112  18
         Map totalSummary = new HashMap();
 113  
 
 114  18
         ListIterator iter = suites.listIterator();
 115  
 
 116  18
         int totalNumberOfTests = 0;
 117  
 
 118  18
         int totalNumberOfErrors = 0;
 119  
 
 120  18
         int totalNumberOfFailures = 0;
 121  
 
 122  18
         int totalNumberOfSkipped = 0;
 123  
 
 124  18
         float totalElapsedTime = 0.0f;
 125  
 
 126  38
         while ( iter.hasNext() )
 127  
         {
 128  20
             ReportTestSuite suite = (ReportTestSuite) iter.next();
 129  
 
 130  20
             totalNumberOfTests += suite.getNumberOfTests();
 131  
 
 132  20
             totalNumberOfErrors += suite.getNumberOfErrors();
 133  
 
 134  20
             totalNumberOfFailures += suite.getNumberOfFailures();
 135  
 
 136  20
             totalNumberOfSkipped += suite.getNumberOfSkipped();
 137  
 
 138  20
             totalElapsedTime += suite.getTimeElapsed();
 139  20
         }
 140  
 
 141  18
         String totalPercentage = computePercentage( totalNumberOfTests, totalNumberOfErrors, totalNumberOfFailures,
 142  
                                                     totalNumberOfSkipped );
 143  
 
 144  18
         totalSummary.put( "totalTests", Integer.toString( totalNumberOfTests ) );
 145  
 
 146  18
         totalSummary.put( "totalErrors", Integer.toString( totalNumberOfErrors ) );
 147  
 
 148  18
         totalSummary.put( "totalFailures", Integer.toString( totalNumberOfFailures ) );
 149  
 
 150  18
         totalSummary.put( "totalSkipped", Integer.toString( totalNumberOfSkipped ) );
 151  
 
 152  18
         totalSummary.put( "totalElapsedTime", numberFormat.format( totalElapsedTime ) );
 153  
 
 154  18
         totalSummary.put( "totalPercentage", totalPercentage );
 155  
 
 156  18
         return totalSummary;
 157  
     }
 158  
 
 159  
     public void setReportsDirectory( File reportsDirectory )
 160  
     {
 161  4
         this.reportsDirectory = reportsDirectory;
 162  4
     }
 163  
 
 164  
     public File getReportsDirectory()
 165  
     {
 166  2
         return this.reportsDirectory;
 167  
     }
 168  
 
 169  
     public final void setLocale( Locale locale )
 170  
     {
 171  26
         this.locale = locale;
 172  26
         numberFormat = NumberFormat.getInstance( locale );
 173  26
     }
 174  
 
 175  
     public Locale getLocale()
 176  
     {
 177  0
         return this.locale;
 178  
     }
 179  
 
 180  
     public void setNumberFormat( NumberFormat numberFormat )
 181  
     {
 182  0
         this.numberFormat = numberFormat;
 183  0
     }
 184  
 
 185  
     public NumberFormat getNumberFormat()
 186  
     {
 187  20
         return this.numberFormat;
 188  
     }
 189  
 
 190  
     public Map getSuitesGroupByPackage( List testSuitesList )
 191  
     {
 192  10
         ListIterator iter = testSuitesList.listIterator();
 193  
 
 194  10
         Map suitePackage = new HashMap();
 195  
 
 196  24
         while ( iter.hasNext() )
 197  
         {
 198  14
             ReportTestSuite suite = (ReportTestSuite) iter.next();
 199  
 
 200  14
             List suiteList = new ArrayList();
 201  
 
 202  14
             if ( suitePackage.get( suite.getPackageName() ) != null )
 203  
             {
 204  2
                 suiteList = (List) suitePackage.get( suite.getPackageName() );
 205  
             }
 206  
 
 207  14
             suiteList.add( suite );
 208  
 
 209  14
             suitePackage.put( suite.getPackageName(), suiteList );
 210  14
         }
 211  
 
 212  10
         return suitePackage;
 213  
     }
 214  
 
 215  
     public String computePercentage( int tests, int errors, int failures, int skipped )
 216  
     {
 217  
         float percentage;
 218  28
         if ( tests == 0 )
 219  
         {
 220  0
             percentage = 0;
 221  0
         }
 222  
         else
 223  
         {
 224  28
             percentage = ( (float) ( tests - errors - failures - skipped ) / (float) tests ) * PCENT;
 225  
         }
 226  
 
 227  28
         return numberFormat.format( percentage );
 228  
     }
 229  
 
 230  
     public List getFailureDetails( List testSuitesList )
 231  
     {
 232  10
         ListIterator iter = testSuitesList.listIterator();
 233  
 
 234  10
         List failureDetailList = new ArrayList();
 235  
 
 236  22
         while ( iter.hasNext() )
 237  
         {
 238  12
             ReportTestSuite suite = (ReportTestSuite) iter.next();
 239  
 
 240  12
             List testCaseList = suite.getTestCases();
 241  
 
 242  12
             if ( testCaseList != null )
 243  
             {
 244  12
                 ListIterator caseIter = testCaseList.listIterator();
 245  
 
 246  82
                 while ( caseIter.hasNext() )
 247  
                 {
 248  70
                     ReportTestCase tCase = (ReportTestCase) caseIter.next();
 249  
 
 250  70
                     if ( tCase.getFailure() != null )
 251  
                     {
 252  20
                         failureDetailList.add( tCase );
 253  
                     }
 254  70
                 }
 255  
             }
 256  12
         }
 257  
 
 258  10
         return failureDetailList;
 259  
     }
 260  
 
 261  
     private String[] getIncludedFiles( File directory, String includes, String excludes )
 262  
     {
 263  10
         DirectoryScanner scanner = new DirectoryScanner();
 264  
 
 265  10
         scanner.setBasedir( directory );
 266  
 
 267  10
         scanner.setIncludes( StringUtils.split( includes, "," ) );
 268  
 
 269  10
         scanner.setExcludes( StringUtils.split( excludes, "," ) );
 270  
 
 271  10
         scanner.scan();
 272  
 
 273  10
         return scanner.getIncludedFiles();
 274  
     }
 275  
 }