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