1 | |
package org.apache.maven.plugin.failsafe; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.BufferedInputStream; |
23 | |
import java.io.File; |
24 | |
import java.io.FileInputStream; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.io.Reader; |
28 | |
import org.apache.maven.plugin.AbstractMojo; |
29 | |
import org.apache.maven.plugin.MojoExecutionException; |
30 | |
import org.apache.maven.plugin.MojoFailureException; |
31 | |
import org.apache.maven.plugin.surefire.SurefireHelper; |
32 | |
import org.apache.maven.plugin.surefire.SurefireReportParameters; |
33 | |
import org.apache.maven.surefire.failsafe.model.FailsafeSummary; |
34 | |
import org.apache.maven.surefire.failsafe.model.io.xpp3.FailsafeSummaryXpp3Reader; |
35 | |
import org.codehaus.plexus.util.IOUtil; |
36 | |
import org.codehaus.plexus.util.ReaderFactory; |
37 | |
import org.codehaus.plexus.util.StringUtils; |
38 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class VerifyMojo |
51 | |
extends AbstractMojo |
52 | |
implements SurefireReportParameters |
53 | |
{ |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private boolean skipTests; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private boolean skipITs; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
private boolean skipExec; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
private boolean skip; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
private boolean testFailureIgnore; |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private File basedir; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
private File testClassesDirectory; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private File reportsDirectory; |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
private File summaryFile; |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
private File[] summaryFiles; |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
private Boolean failIfNoTests; |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
private String encoding; |
152 | |
|
153 | |
|
154 | |
public void execute() |
155 | |
throws MojoExecutionException, MojoFailureException |
156 | |
{ |
157 | 0 | if ( verifyParameters() ) |
158 | |
{ |
159 | 0 | getLog().info( |
160 | |
StringUtils.capitalizeFirstLetter( getPluginName() ) + " report directory: " + getReportsDirectory() ); |
161 | |
|
162 | |
int result; |
163 | |
try |
164 | |
{ |
165 | |
String encoding; |
166 | 0 | if ( StringUtils.isEmpty( this.encoding ) ) |
167 | |
{ |
168 | 0 | getLog().warn( |
169 | |
"File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING |
170 | |
+ ", i.e. build is platform dependent!" ); |
171 | 0 | encoding = ReaderFactory.FILE_ENCODING; |
172 | |
} |
173 | |
else |
174 | |
{ |
175 | 0 | encoding = this.encoding; |
176 | |
} |
177 | |
|
178 | |
final FailsafeSummary summary; |
179 | 0 | if ( !summaryFile.isFile() && summaryFiles != null ) |
180 | |
{ |
181 | 0 | summary = new FailsafeSummary(); |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | summary = readSummary( encoding, summaryFile ); |
186 | |
} |
187 | 0 | if ( summaryFiles != null ) |
188 | |
{ |
189 | 0 | for ( int i = 0; i < summaryFiles.length; i++ ) |
190 | |
{ |
191 | 0 | summary.merge( readSummary( encoding, summaryFiles[i] ) ); |
192 | |
} |
193 | |
} |
194 | 0 | result = summary.getResult(); |
195 | |
} |
196 | 0 | catch ( IOException e ) |
197 | |
{ |
198 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
199 | |
} |
200 | 0 | catch ( XmlPullParserException e ) |
201 | |
{ |
202 | 0 | throw new MojoExecutionException( e.getMessage(), e ); |
203 | 0 | } |
204 | |
|
205 | 0 | SurefireHelper.reportExecution( this, result, getLog() ); |
206 | |
} |
207 | 0 | } |
208 | |
|
209 | |
private FailsafeSummary readSummary( String encoding, File summaryFile ) |
210 | |
throws IOException, XmlPullParserException |
211 | |
{ |
212 | 0 | FileInputStream fileInputStream = null; |
213 | 0 | BufferedInputStream bufferedInputStream = null; |
214 | 0 | Reader reader = null; |
215 | |
try |
216 | |
{ |
217 | 0 | fileInputStream = new FileInputStream( summaryFile ); |
218 | 0 | bufferedInputStream = new BufferedInputStream( fileInputStream ); |
219 | 0 | reader = new InputStreamReader( bufferedInputStream, encoding ); |
220 | 0 | FailsafeSummaryXpp3Reader xpp3Reader = new FailsafeSummaryXpp3Reader(); |
221 | 0 | return xpp3Reader.read( reader ); |
222 | |
} |
223 | |
finally |
224 | |
{ |
225 | 0 | IOUtil.close( reader ); |
226 | 0 | IOUtil.close( bufferedInputStream ); |
227 | 0 | IOUtil.close( fileInputStream ); |
228 | |
} |
229 | |
} |
230 | |
|
231 | |
protected boolean verifyParameters() |
232 | |
throws MojoFailureException |
233 | |
{ |
234 | 0 | if ( isSkip() || isSkipTests() || isSkipITs() || isSkipExec() ) |
235 | |
{ |
236 | 0 | getLog().info( "Tests are skipped." ); |
237 | 0 | return false; |
238 | |
} |
239 | |
|
240 | 0 | if ( !getTestClassesDirectory().exists() ) |
241 | |
{ |
242 | 0 | if ( getFailIfNoTests() != null && getFailIfNoTests().booleanValue() ) |
243 | |
{ |
244 | 0 | throw new MojoFailureException( "No tests to run!" ); |
245 | |
} |
246 | 0 | getLog().info( "No tests to run." ); |
247 | 0 | return false; |
248 | |
} |
249 | |
|
250 | 0 | return true; |
251 | |
} |
252 | |
|
253 | |
protected String getPluginName() |
254 | |
{ |
255 | 0 | return "failsafe"; |
256 | |
} |
257 | |
|
258 | |
protected String[] getDefaultIncludes() |
259 | |
{ |
260 | 0 | return null; |
261 | |
} |
262 | |
|
263 | |
public boolean isSkipTests() |
264 | |
{ |
265 | 0 | return skipTests; |
266 | |
} |
267 | |
|
268 | |
public void setSkipTests( boolean skipTests ) |
269 | |
{ |
270 | 0 | this.skipTests = skipTests; |
271 | 0 | } |
272 | |
|
273 | |
public boolean isSkipITs() |
274 | |
{ |
275 | 0 | return skipITs; |
276 | |
} |
277 | |
|
278 | |
public void setSkipITs( boolean skipITs ) |
279 | |
{ |
280 | 0 | this.skipITs = skipITs; |
281 | 0 | } |
282 | |
|
283 | |
public boolean isSkipExec() |
284 | |
{ |
285 | 0 | return skipExec; |
286 | |
} |
287 | |
|
288 | |
public void setSkipExec( boolean skipExec ) |
289 | |
{ |
290 | 0 | this.skipExec = skipExec; |
291 | 0 | } |
292 | |
|
293 | |
public boolean isSkip() |
294 | |
{ |
295 | 0 | return skip; |
296 | |
} |
297 | |
|
298 | |
public void setSkip( boolean skip ) |
299 | |
{ |
300 | 0 | this.skip = skip; |
301 | 0 | } |
302 | |
|
303 | |
public boolean isTestFailureIgnore() |
304 | |
{ |
305 | 0 | return testFailureIgnore; |
306 | |
} |
307 | |
|
308 | |
public void setTestFailureIgnore( boolean testFailureIgnore ) |
309 | |
{ |
310 | 0 | this.testFailureIgnore = testFailureIgnore; |
311 | 0 | } |
312 | |
|
313 | |
public File getBasedir() |
314 | |
{ |
315 | 0 | return basedir; |
316 | |
} |
317 | |
|
318 | |
public void setBasedir( File basedir ) |
319 | |
{ |
320 | 0 | this.basedir = basedir; |
321 | 0 | } |
322 | |
|
323 | |
public File getTestClassesDirectory() |
324 | |
{ |
325 | 0 | return testClassesDirectory; |
326 | |
} |
327 | |
|
328 | |
public void setTestClassesDirectory( File testClassesDirectory ) |
329 | |
{ |
330 | 0 | this.testClassesDirectory = testClassesDirectory; |
331 | 0 | } |
332 | |
|
333 | |
public File getReportsDirectory() |
334 | |
{ |
335 | 0 | return reportsDirectory; |
336 | |
} |
337 | |
|
338 | |
public void setReportsDirectory( File reportsDirectory ) |
339 | |
{ |
340 | 0 | this.reportsDirectory = reportsDirectory; |
341 | 0 | } |
342 | |
|
343 | |
public Boolean getFailIfNoTests() |
344 | |
{ |
345 | 0 | return failIfNoTests; |
346 | |
} |
347 | |
|
348 | |
public void setFailIfNoTests( Boolean failIfNoTests ) |
349 | |
{ |
350 | 0 | this.failIfNoTests = failIfNoTests; |
351 | 0 | } |
352 | |
|
353 | |
} |