1 package org.apache.maven.surefire.its.misc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }