1 package org.apache.maven.continuum.reports.surefire;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.continuum.configuration.ConfigurationException;
23 import org.apache.maven.continuum.configuration.ConfigurationService;
24 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
25 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
26 import org.codehaus.plexus.util.DirectoryScanner;
27 import org.springframework.stereotype.Service;
28 import org.xml.sax.SAXException;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.LinkedList;
35 import java.util.List;
36 import javax.annotation.Resource;
37 import javax.xml.parsers.ParserConfigurationException;
38
39
40
41
42
43
44 @Service( "reportTestSuiteGenerator" )
45 public class DefaultReportTestSuiteGenerator
46 implements ReportTestSuiteGenerator, Initializable
47 {
48
49 @Resource
50 private ConfigurationService configurationService;
51
52 private List<String> defaultIncludes;
53
54 private List<String> defaultexcludes;
55
56
57
58
59
60 public void initialize()
61 throws InitializationException
62 {
63 defaultIncludes = new ArrayList<String>( 1 );
64 defaultIncludes.add( "*.xml" );
65 defaultexcludes = new ArrayList<String>( 1 );
66 defaultexcludes.add( "*.txt" );
67 }
68
69
70
71
72 public List<ReportTestSuite> generateReports( File directory, List<String> includes, List<String> excludes )
73 throws ReportTestSuiteGeneratorException
74 {
75 if ( directory == null )
76 {
77 return Collections.EMPTY_LIST;
78 }
79 if ( !directory.exists() )
80 {
81 return Collections.EMPTY_LIST;
82 }
83 List<ReportTestSuite> reportTestSuites = new LinkedList<ReportTestSuite>();
84 String[] includesArray;
85 if ( includes == null )
86 {
87 includesArray = new String[0];
88 }
89 else
90 {
91 includesArray = includes.toArray( new String[includes.size()] );
92 }
93 String[] excludesArray;
94 if ( excludes == null )
95 {
96 excludesArray = new String[0];
97 }
98 else
99 {
100 excludesArray = excludes.toArray( new String[excludes.size()] );
101 }
102 String[] xmlReportFiles = getIncludedFiles( directory, includesArray, excludesArray );
103
104 if ( xmlReportFiles == null )
105 {
106 return Collections.EMPTY_LIST;
107 }
108 if ( xmlReportFiles.length == 0 )
109 {
110 return Collections.EMPTY_LIST;
111 }
112 for ( String currentReport : xmlReportFiles )
113 {
114 ReportTestSuite testSuite = new ReportTestSuite();
115
116 try
117 {
118 testSuite.parse( directory + File.separator + currentReport );
119 }
120 catch ( ParserConfigurationException e )
121 {
122 throw new ReportTestSuiteGeneratorException( "Error setting up parser for Surefire XML report", e );
123 }
124 catch ( SAXException e )
125 {
126 throw new ReportTestSuiteGeneratorException( "Error parsing Surefire XML report " + currentReport, e );
127 }
128 catch ( IOException e )
129 {
130 throw new ReportTestSuiteGeneratorException( "Error reading Surefire XML report " + currentReport, e );
131 }
132
133 reportTestSuites.add( testSuite );
134 }
135 return reportTestSuites;
136 }
137
138
139
140
141 public List<ReportTestSuite> generateReports( File directory )
142 throws ReportTestSuiteGeneratorException
143 {
144 return generateReports( directory, defaultIncludes, defaultexcludes );
145 }
146
147
148
149
150 public List<ReportTestSuite> generateReports( int buildId, int projectId )
151 throws ReportTestSuiteGeneratorException
152 {
153 try
154 {
155 File directory = configurationService.getTestReportsDirectory( buildId, projectId );
156 return generateReports( directory );
157 }
158 catch ( ConfigurationException e )
159 {
160 throw new ReportTestSuiteGeneratorException( e.getMessage(), e );
161 }
162 }
163
164
165
166
167 public ReportTestResult generateReportTestResult( int buildId, int projectId )
168 throws ReportTestSuiteGeneratorException
169 {
170 List<ReportTestSuite> reportTestSuites = generateReports( buildId, projectId );
171 ReportTestResult reportTestResult = new ReportTestResult();
172 for ( ReportTestSuite reportTestSuite : reportTestSuites )
173 {
174 reportTestResult.addReportTestSuite( reportTestSuite );
175 }
176 return reportTestResult;
177 }
178
179 private String[] getIncludedFiles( File directory, String[] includes, String[] excludes )
180 {
181 DirectoryScanner scanner = new DirectoryScanner();
182
183 scanner.setBasedir( directory );
184
185 scanner.setIncludes( includes );
186
187 scanner.setExcludes( excludes );
188
189 scanner.scan();
190
191 return scanner.getIncludedFiles();
192 }
193
194 }