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.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.List;
27
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugin.surefire.AbstractSurefireMojo;
31 import org.apache.maven.plugin.surefire.booterclient.ChecksumCalculator;
32 import org.apache.maven.plugins.annotations.LifecyclePhase;
33 import org.apache.maven.plugins.annotations.Mojo;
34 import org.apache.maven.plugins.annotations.Parameter;
35 import org.apache.maven.plugins.annotations.ResolutionScope;
36 import org.apache.maven.shared.utils.ReaderFactory;
37 import org.apache.maven.shared.utils.StringUtils;
38 import org.apache.maven.surefire.suite.RunResult;
39
40 import static org.apache.maven.shared.utils.io.IOUtil.close;
41
42
43
44
45
46
47
48
49 @Mojo( name = "integration-test", requiresProject = true, requiresDependencyResolution = ResolutionScope.TEST,
50 defaultPhase = LifecyclePhase.INTEGRATION_TEST, threadSafe = true )
51 public class IntegrationTestMojo
52 extends AbstractSurefireMojo
53 {
54
55 private static final String FAILSAFE_IN_PROGRESS_CONTEXT_KEY = "failsafe-in-progress";
56
57
58
59
60
61
62
63 @Parameter( property = "skipITs" )
64 private boolean skipITs;
65
66
67
68
69 @Parameter( defaultValue = "${project.build.directory}/failsafe-reports" )
70 private File reportsDirectory;
71
72
73
74
75
76
77
78
79
80
81
82
83 @Parameter( property = "it.test" )
84 private String test;
85
86
87
88
89 @Parameter( defaultValue = "${project.build.directory}/failsafe-reports/failsafe-summary.xml", required = true )
90 private File summaryFile;
91
92
93
94
95 @Parameter( property = "failsafe.printSummary", defaultValue = "true" )
96 private boolean printSummary;
97
98
99
100
101
102 @Parameter( property = "failsafe.reportFormat", defaultValue = "brief" )
103 private String reportFormat;
104
105
106
107
108 @Parameter( property = "failsafe.useFile", defaultValue = "true" )
109 private boolean useFile;
110
111
112
113
114
115
116
117 @Parameter( property = "it.failIfNoSpecifiedTests" )
118 private Boolean failIfNoSpecifiedTests;
119
120
121
122
123
124
125
126
127
128 @Parameter( property = "maven.failsafe.debug" )
129 private String debugForkedProcess;
130
131
132
133
134
135
136
137 @Parameter( property = "failsafe.timeout" )
138 private int forkedProcessTimeoutInSeconds;
139
140
141
142
143
144
145
146
147
148
149
150 @Parameter( property = "failsafe.parallel.timeout" )
151 private double parallelTestsTimeoutInSeconds;
152
153
154
155
156
157
158
159
160
161
162
163
164 @Parameter( property = "failsafe.parallel.forcedTimeout" )
165 private double parallelTestsTimeoutForcedInSeconds;
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 @Parameter
188 private List<String> includes;
189
190
191
192
193
194
195
196
197 @Parameter( property = "failsafe.useSystemClassLoader", defaultValue = "true" )
198 private boolean useSystemClassLoader;
199
200
201
202
203
204
205
206
207
208
209
210
211 @Parameter( property = "failsafe.useManifestOnlyJar", defaultValue = "true" )
212 private boolean useManifestOnlyJar;
213
214
215
216
217 @Parameter( property = "encoding", defaultValue = "${project.reporting.outputEncoding}" )
218 private String encoding;
219
220
221
222
223
224
225
226 @Parameter( property = "failsafe.rerunFailingTestsCount", defaultValue = "0" )
227 protected int rerunFailingTestsCount;
228
229
230
231
232
233
234
235
236
237
238 @Parameter( property = "failsafe.suiteXmlFiles" )
239 private File[] suiteXmlFiles;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 @Parameter( property = "failsafe.runOrder", defaultValue = "filesystem" )
266 protected String runOrder;
267
268 protected int getRerunFailingTestsCount()
269 {
270 return rerunFailingTestsCount;
271 }
272
273 protected void handleSummary( RunResult summary, Exception firstForkException )
274 throws MojoExecutionException, MojoFailureException
275 {
276 writeSummary( summary, firstForkException );
277 }
278
279 @SuppressWarnings( "unchecked" )
280 private void writeSummary( RunResult summary, Exception firstForkException )
281 throws MojoExecutionException
282 {
283 File summaryFile = getSummaryFile();
284 if ( !summaryFile.getParentFile().isDirectory() )
285 {
286
287 summaryFile.getParentFile().mkdirs();
288 }
289
290 FileOutputStream fout = null;
291 FileInputStream fin = null;
292 try
293 {
294 Object token = getPluginContext().get( FAILSAFE_IN_PROGRESS_CONTEXT_KEY );
295 summary.writeSummary( summaryFile, token != null, getEncodingOrDefault() );
296 }
297 catch ( IOException e )
298 {
299 throw new MojoExecutionException( e.getMessage(), e );
300 }
301 finally
302 {
303 close( fin );
304 close( fout );
305 }
306
307 getPluginContext().put( FAILSAFE_IN_PROGRESS_CONTEXT_KEY, FAILSAFE_IN_PROGRESS_CONTEXT_KEY );
308 }
309
310 private String getEncodingOrDefault()
311 {
312 if ( StringUtils.isEmpty( encoding ) )
313 {
314 getLog().warn( "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
315 + ", i.e. build is platform dependent! The file encoding for reports output files "
316 + "should be provided by the POM property ${project.reporting.outputEncoding}." );
317 return ReaderFactory.FILE_ENCODING;
318 }
319 else
320 {
321 return encoding;
322 }
323 }
324
325 @SuppressWarnings( "deprecation" )
326 protected boolean isSkipExecution()
327 {
328 return isSkip() || isSkipTests() || isSkipITs() || isSkipExec();
329 }
330
331 protected String getPluginName()
332 {
333 return "failsafe";
334 }
335
336 protected String[] getDefaultIncludes()
337 {
338 return new String[]{ "**/IT*.java", "**/*IT.java", "**/*ITCase.java" };
339 }
340
341 public boolean isSkipTests()
342 {
343 return skipTests;
344 }
345
346 public void setSkipTests( boolean skipTests )
347 {
348 this.skipTests = skipTests;
349 }
350
351 public boolean isSkipITs()
352 {
353 return skipITs;
354 }
355
356 public void setSkipITs( boolean skipITs )
357 {
358 this.skipITs = skipITs;
359 }
360
361 @SuppressWarnings( "deprecation" )
362 @Deprecated
363 public boolean isSkipExec()
364 {
365 return skipExec;
366 }
367
368 @SuppressWarnings( "deprecation" )
369 @Deprecated
370 public void setSkipExec( boolean skipExec )
371 {
372 this.skipExec = skipExec;
373 }
374
375 public boolean isSkip()
376 {
377 return skip;
378 }
379
380 public void setSkip( boolean skip )
381 {
382 this.skip = skip;
383 }
384
385 public File getBasedir()
386 {
387 return basedir;
388 }
389
390 public void setBasedir( File basedir )
391 {
392 this.basedir = basedir;
393 }
394
395 public File getTestClassesDirectory()
396 {
397 return testClassesDirectory;
398 }
399
400 public void setTestClassesDirectory( File testClassesDirectory )
401 {
402 this.testClassesDirectory = testClassesDirectory;
403 }
404
405 public File getClassesDirectory()
406 {
407 return classesDirectory;
408 }
409
410 public void setClassesDirectory( File classesDirectory )
411 {
412 this.classesDirectory = classesDirectory;
413 }
414
415 public File getReportsDirectory()
416 {
417 return reportsDirectory;
418 }
419
420 public void setReportsDirectory( File reportsDirectory )
421 {
422 this.reportsDirectory = reportsDirectory;
423 }
424
425 public String getTest()
426 {
427 if ( StringUtils.isBlank( test ) )
428 {
429 return null;
430 }
431 int index = test.indexOf( '#' );
432 if ( index >= 0 )
433 {
434 return test.substring( 0, index );
435 }
436 return test;
437 }
438
439 public void setTest( String test )
440 {
441 this.test = test;
442 }
443
444
445
446
447 public String getTestMethod()
448 {
449 if ( StringUtils.isBlank( test ) )
450 {
451 return null;
452 }
453 int index = this.test.indexOf( '#' );
454 if ( index >= 0 )
455 {
456 return this.test.substring( index + 1, this.test.length() );
457 }
458 return null;
459 }
460
461
462 public File getSummaryFile()
463 {
464 return summaryFile;
465 }
466
467 public void setSummaryFile( File summaryFile )
468 {
469 this.summaryFile = summaryFile;
470 }
471
472 public boolean isPrintSummary()
473 {
474 return printSummary;
475 }
476
477 public void setPrintSummary( boolean printSummary )
478 {
479 this.printSummary = printSummary;
480 }
481
482 public String getReportFormat()
483 {
484 return reportFormat;
485 }
486
487 public void setReportFormat( String reportFormat )
488 {
489 this.reportFormat = reportFormat;
490 }
491
492 public boolean isUseFile()
493 {
494 return useFile;
495 }
496
497 public void setUseFile( boolean useFile )
498 {
499 this.useFile = useFile;
500 }
501
502 public String getDebugForkedProcess()
503 {
504 return debugForkedProcess;
505 }
506
507 public void setDebugForkedProcess( String debugForkedProcess )
508 {
509 this.debugForkedProcess = debugForkedProcess;
510 }
511
512 public int getForkedProcessTimeoutInSeconds()
513 {
514 return forkedProcessTimeoutInSeconds;
515 }
516
517 public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
518 {
519 this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
520 }
521
522 public double getParallelTestsTimeoutInSeconds()
523 {
524 return parallelTestsTimeoutInSeconds;
525 }
526
527 public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
528 {
529 this.parallelTestsTimeoutInSeconds = parallelTestsTimeoutInSeconds;
530 }
531
532 public double getParallelTestsTimeoutForcedInSeconds()
533 {
534 return parallelTestsTimeoutForcedInSeconds;
535 }
536
537 public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
538 {
539 this.parallelTestsTimeoutForcedInSeconds = parallelTestsTimeoutForcedInSeconds;
540 }
541
542 public boolean isUseSystemClassLoader()
543 {
544 return useSystemClassLoader;
545 }
546
547 public void setUseSystemClassLoader( boolean useSystemClassLoader )
548 {
549 this.useSystemClassLoader = useSystemClassLoader;
550 }
551
552 public boolean isUseManifestOnlyJar()
553 {
554 return useManifestOnlyJar;
555 }
556
557 public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
558 {
559 this.useManifestOnlyJar = useManifestOnlyJar;
560 }
561
562
563
564 public boolean isTestFailureIgnore()
565 {
566 return true;
567 }
568
569 public void setTestFailureIgnore( boolean testFailureIgnore )
570 {
571
572 }
573
574 protected void addPluginSpecificChecksumItems( ChecksumCalculator checksum )
575 {
576 checksum.add( skipITs );
577 checksum.add( summaryFile );
578 }
579
580 public Boolean getFailIfNoSpecifiedTests()
581 {
582 return failIfNoSpecifiedTests;
583 }
584
585 public void setFailIfNoSpecifiedTests( Boolean failIfNoSpecifiedTests )
586 {
587 this.failIfNoSpecifiedTests = failIfNoSpecifiedTests;
588 }
589
590 @Override
591 public List<String> getIncludes()
592 {
593 return includes;
594 }
595
596 @Override
597 public void setIncludes( List<String> includes )
598 {
599 this.includes = includes;
600 }
601
602 public File[] getSuiteXmlFiles()
603 {
604 return suiteXmlFiles;
605 }
606
607 @SuppressWarnings( "UnusedDeclaration" )
608 public void setSuiteXmlFiles( File[] suiteXmlFiles )
609 {
610 this.suiteXmlFiles = suiteXmlFiles;
611 }
612
613 public String getRunOrder()
614 {
615 return runOrder;
616 }
617
618 @SuppressWarnings( "UnusedDeclaration" )
619 public void setRunOrder( String runOrder )
620 {
621 this.runOrder = runOrder;
622 }
623 }