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