View Javadoc

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