View Javadoc

1   package org.apache.maven.surefire.its.misc;
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.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.Locale;
28  import org.codehaus.plexus.util.DirectoryScanner;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import javax.xml.parsers.ParserConfigurationException;
32  
33  import org.xml.sax.SAXException;
34  
35  /**
36   * @version $Id$
37   */
38  public class SurefireReportParser
39  {
40  
41      private final List reportsDirectories;
42  
43      private final List testSuites = new ArrayList();
44  
45      public SurefireReportParser( List reportsDirectoriesFiles, Locale locale )
46      {
47          this.reportsDirectories = reportsDirectoriesFiles;
48  
49      }
50  
51      public List parseXMLReportFiles()
52      {
53          List xmlReportFileList = new ArrayList();
54          for ( int i = 0; i < reportsDirectories.size(); i++ )
55          {
56              File reportsDirectory = (File) reportsDirectories.get( i );
57              if ( !reportsDirectory.exists() )
58              {
59                  continue;
60              }
61              String[] xmlReportFiles =
62                  getIncludedFiles( reportsDirectory, "*.xml",
63                                    "*.txt, testng-failed.xml, testng-failures.xml, testng-results.xml" );
64              for ( int j = 0; j < xmlReportFiles.length; j++ )
65              {
66                  File xmlReport = new File( reportsDirectory, xmlReportFiles[j] );
67                  xmlReportFileList.add( xmlReport );
68              }
69          }
70          TestSuiteXmlParser parser = new TestSuiteXmlParser();
71          for ( int index = 0; index < xmlReportFileList.size(); index++ )
72          {
73              Collection suites;
74  
75              File currentReport = (File) xmlReportFileList.get( index );
76  
77              try
78              {
79                  suites = parser.parse( currentReport.getAbsolutePath() );
80              }
81              catch ( ParserConfigurationException e )
82              {
83                  throw new RuntimeException( "Error setting up parser for JUnit XML report", e );
84              }
85              catch ( SAXException e )
86              {
87                  throw new RuntimeException( "Error parsing JUnit XML report " + currentReport, e );
88              }
89              catch ( IOException e )
90              {
91                  throw new RuntimeException( "Error reading JUnit XML report " + currentReport, e );
92              }
93  
94              testSuites.addAll( suites );
95          }
96  
97          return testSuites;
98      }
99  
100     private String[] getIncludedFiles( File directory, String includes, String excludes )
101     {
102         DirectoryScanner scanner = new DirectoryScanner();
103 
104         scanner.setBasedir( directory );
105 
106         scanner.setIncludes( StringUtils.split( includes, "," ) );
107 
108         scanner.setExcludes( StringUtils.split( excludes, "," ) );
109 
110         scanner.scan();
111 
112         return scanner.getIncludedFiles();
113     }
114 }