forked from apache/maven-surefire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SurefirePlugin.java
526 lines (452 loc) · 16.8 KB
/
SurefirePlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package org.apache.maven.plugin.surefire;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.surefire.suite.RunResult;
/**
* Run tests using Surefire.
*
* @author Jason van Zyl
* @noinspection JavaDoc
*/
@Mojo( name = "test", defaultPhase = LifecyclePhase.TEST, threadSafe = true,
requiresDependencyResolution = ResolutionScope.TEST )
public class SurefirePlugin
extends AbstractSurefireMojo
implements SurefireReportParameters
{
/**
* Set this to "true" to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on
* occasion.
*/
@Parameter( property = "maven.test.failure.ignore", defaultValue = "false" )
private boolean testFailureIgnore;
/**
* Base directory where all reports are written to.
*/
@Parameter( defaultValue = "${project.build.directory}/surefire-reports" )
private File reportsDirectory;
/**
* Specify this parameter to run individual tests by file name, overriding the <code>includes/excludes</code>
* parameters. Each pattern you specify here will be used to create an include pattern formatted like
* <code>**/${test}.java</code>, so you can just type "-Dtest=MyTest" to run a single test called
* "foo/MyTest.java". The test patterns prefixed with a <code>!</code> will be excluded.<br/>
* This parameter overrides the <code>includes/excludes</code> parameters, and the TestNG <code>suiteXmlFiles</code>
* parameter.
* <p/>
* Since 2.7.3, you can execute a limited number of methods in the test by adding #myMethod or #my*ethod. For
* example, "-Dtest=MyTest#myMethod". This is supported for junit 4.x and testNg.<br/>
* <br/>
* Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit 4.7+, TestNG):<br/>
* "-Dtest=???Test, !Unstable*, pkg/**/Ci*leTest.java, *Test#test*One+testTwo?????, #fast*+slowTest"<br/>
* "-Dtest=Basic*, !%regex[.*.Unstable.*], !%regex[.*.MyTest.class#one.*|two.*], %regex[#fast.*|slow.*]"<br/>
* <br/>
* The Parameterized JUnit runner <em>describes</em> test methods using an index in brackets, so the non-regex
* method pattern would become: <em>#testMethod[*]</em>.
* <br/>
*/
@Parameter( property = "test" )
private String test;
/**
* Option to print summary of test suites or just print the test cases that have errors.
*/
@Parameter( property = "surefire.printSummary", defaultValue = "true" )
private boolean printSummary;
/**
* Selects the formatting for the test report to be generated. Can be set as "brief" or "plain".
* Only applies to the output format of the output files (target/surefire-reports/testName.txt)
*/
@Parameter( property = "surefire.reportFormat", defaultValue = "brief" )
private String reportFormat;
/**
* Option to generate a file test report or just output the test report to the console.
*/
@Parameter( property = "surefire.useFile", defaultValue = "true" )
private boolean useFile;
/**
* Set this to "true" to cause a failure if the none of the tests specified in -Dtest=... are run. Defaults to
* "true".
*
* @since 2.12
*/
@Parameter( property = "surefire.failIfNoSpecifiedTests" )
private Boolean failIfNoSpecifiedTests;
/**
* Attach a debugger to the forked JVM. If set to "true", the process will suspend and wait for a debugger to attach
* on port 5005. If set to some other string, that string will be appended to the argLine, allowing you to configure
* arbitrary debuggability options (without overwriting the other options specified through the <code>argLine</code>
* parameter).
*
* @since 2.4
*/
@Parameter( property = "maven.surefire.debug" )
private String debugForkedProcess;
/**
* Kill the forked test process after a certain number of seconds. If set to 0, wait forever for the process, never
* timing out.
*
* @since 2.4
*/
@Parameter( property = "surefire.timeout" )
private int forkedProcessTimeoutInSeconds;
/**
* Stop executing queued parallel JUnit tests after a certain number of seconds.
* <br/>
* Example values: "3.5", "4"<br/>
* <br/>
* If set to 0, wait forever, never timing out.
* Makes sense with specified <code>parallel</code> different from "none".
*
* @since 2.16
*/
@Parameter( property = "surefire.parallel.timeout" )
private double parallelTestsTimeoutInSeconds;
/**
* Stop executing queued parallel JUnit tests
* and <em>interrupt</em> currently running tests after a certain number of seconds.
* <br/>
* Example values: "3.5", "4"<br/>
* <br/>
* If set to 0, wait forever, never timing out.
* Makes sense with specified <code>parallel</code> different from "none".
*
* @since 2.16
*/
@Parameter( property = "surefire.parallel.forcedTimeout" )
private double parallelTestsTimeoutForcedInSeconds;
/**
* A list of <include> elements specifying the tests (by pattern) that should be included in testing. When not
* specified and when the <code>test</code> parameter is not specified, the default includes will be <code><br/>
* <includes><br/>
* <include>**/Test*.java</include><br/>
* <include>**/*Test.java</include><br/>
* <include>**/*TestCase.java</include><br/>
* </includes><br/>
* </code>
* <p/>
* Each include item may also contain a comma-separated sublist of items, which will be treated as multiple
* <include> entries.<br/>
* Since 2.19 a complex syntax is supported in one parameter (JUnit 4, JUnit 4.7+, TestNG):<br/>
* <include>%regex[.*[Cat|Dog].*], Basic????, !Unstable*</include><br/>
* <include>%regex[.*[Cat|Dog].*], !%regex[pkg.*Slow.*.class], pkg/**/*Fast*.java</include><br/>
* <p/>
* This parameter is ignored if the TestNG <code>suiteXmlFiles</code> parameter is specified.<br/>
* <br/>
* <em>Notice that</em> these values are relative to the directory containing generated test classes of the project
* being tested. This directory is declared by the parameter <code>testClassesDirectory</code> which defaults
* to the POM property <code>${project.build.testOutputDirectory}</code>, typically <em>src/test/java</em>
* unless overridden.
*/
@Parameter
private List<String> includes;
/**
* Option to pass dependencies to the system's classloader instead of using an isolated class loader when forking.
* Prevents problems with JDKs which implement the service provider lookup mechanism by using the system's
* classloader.
*
* @since 2.3
*/
@Parameter( property = "surefire.useSystemClassLoader", defaultValue = "true" )
private boolean useSystemClassLoader;
/**
* By default, Surefire forks your tests using a manifest-only JAR; set this parameter to "false" to force it to
* launch your tests with a plain old Java classpath. (See
* http://maven.apache.org/plugins/maven-surefire-plugin/examples/class-loading.html for a more detailed explanation
* of manifest-only JARs and their benefits.)
* <p/>
* Beware, setting this to "false" may cause your tests to fail on Windows if your classpath is too long.
*
* @since 2.4.3
*/
@Parameter( property = "surefire.useManifestOnlyJar", defaultValue = "true" )
private boolean useManifestOnlyJar;
/**
* (JUnit 4+ providers)
* The number of times each failing test will be rerun. If set larger than 0, rerun failing tests immediately after
* they fail. If a failing test passes in any of those reruns, it will be marked as pass and reported as a "flake".
* However, all the failing attempts will be recorded.
*/
@Parameter( property = "surefire.rerunFailingTestsCount", defaultValue = "0" )
protected int rerunFailingTestsCount;
/**
* (TestNG) List of <suiteXmlFile> elements specifying TestNG suite xml file locations. Note that
* <code>suiteXmlFiles</code> is incompatible with several other parameters of this plugin, like
* <code>includes/excludes</code>.<br/>
* This parameter is ignored if the <code>test</code> parameter is specified (allowing you to run a single test
* instead of an entire suite).
*
* @since 2.2
*/
@Parameter( property = "surefire.suiteXmlFiles" )
private File[] suiteXmlFiles;
/**
* Defines the order the tests will be run in. Supported values are "alphabetical", "reversealphabetical", "random",
* "hourly" (alphabetical on even hours, reverse alphabetical on odd hours), "failedfirst", "balanced" and
* "filesystem".<p/>
* <p/>
* Odd/Even for hourly is determined at the time the of scanning the classpath, meaning it could change during a
* multi-module build.<p/>
* <p/>
* Failed first will run tests that failed on previous run first, as well as new tests for this run.<p/>
* <p/>
* Balanced is only relevant with parallel=classes, and will try to optimize the run-order of the tests reducing the
* overall execution time. Initially a statistics file is created and every next test run will reorder classes.<p/>
* <p/>
* Note that the statistics are stored in a file named .surefire-XXXXXXXXX beside pom.xml, and should not be checked
* into version control. The "XXXXX" is the SHA1 checksum of the entire surefire configuration, so different
* configurations will have different statistics files, meaning if you change any config settings you will re-run
* once before new statistics data can be established.
*
* @since 2.7
*/
@Parameter( property = "surefire.runOrder", defaultValue = "filesystem" )
protected String runOrder;
protected int getRerunFailingTestsCount()
{
return rerunFailingTestsCount;
}
protected void handleSummary( RunResult summary, Exception firstForkException )
throws MojoExecutionException, MojoFailureException
{
assertNoException( firstForkException );
SurefireHelper.reportExecution( this, summary, getLog() );
}
private void assertNoException( Exception firstForkException )
throws MojoFailureException
{
if ( firstForkException != null )
{
throw new MojoFailureException( firstForkException.getMessage(), firstForkException );
}
}
private void assertNoFailureOrTimeout( Exception summary )
throws MojoFailureException
{
if ( summary != null )
{
throw new MojoFailureException( "Failure or timeout" );
}
}
protected boolean isSkipExecution()
{
return isSkip() || isSkipTests() || isSkipExec();
}
protected String getPluginName()
{
return "surefire";
}
protected String[] getDefaultIncludes()
{
return new String[]{ "**/Test*.java", "**/*Test.java", "**/*TestCase.java" };
}
// now for the implementation of the field accessors
public boolean isSkipTests()
{
return skipTests;
}
public void setSkipTests( boolean skipTests )
{
this.skipTests = skipTests;
}
/**
* @noinspection deprecation
*/
public boolean isSkipExec()
{
return skipExec;
}
/**
* @noinspection deprecation
*/
public void setSkipExec( boolean skipExec )
{
this.skipExec = skipExec;
}
public boolean isSkip()
{
return skip;
}
public void setSkip( boolean skip )
{
this.skip = skip;
}
public boolean isTestFailureIgnore()
{
return testFailureIgnore;
}
public void setTestFailureIgnore( boolean testFailureIgnore )
{
this.testFailureIgnore = testFailureIgnore;
}
public File getBasedir()
{
return basedir;
}
public void setBasedir( File basedir )
{
this.basedir = basedir;
}
public File getTestClassesDirectory()
{
return testClassesDirectory;
}
public void setTestClassesDirectory( File testClassesDirectory )
{
this.testClassesDirectory = testClassesDirectory;
}
public File getClassesDirectory()
{
return classesDirectory;
}
public void setClassesDirectory( File classesDirectory )
{
this.classesDirectory = classesDirectory;
}
public File getReportsDirectory()
{
return reportsDirectory;
}
public void setReportsDirectory( File reportsDirectory )
{
this.reportsDirectory = reportsDirectory;
}
public String getTest()
{
return test;
}
public boolean isUseSystemClassLoader()
{
return useSystemClassLoader;
}
public void setUseSystemClassLoader( boolean useSystemClassLoader )
{
this.useSystemClassLoader = useSystemClassLoader;
}
public boolean isUseManifestOnlyJar()
{
return useManifestOnlyJar;
}
public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
{
this.useManifestOnlyJar = useManifestOnlyJar;
}
public Boolean getFailIfNoSpecifiedTests()
{
return failIfNoSpecifiedTests;
}
public void setFailIfNoSpecifiedTests( Boolean failIfNoSpecifiedTests )
{
this.failIfNoSpecifiedTests = failIfNoSpecifiedTests;
}
public boolean isPrintSummary()
{
return printSummary;
}
public void setPrintSummary( boolean printSummary )
{
this.printSummary = printSummary;
}
public String getReportFormat()
{
return reportFormat;
}
public void setReportFormat( String reportFormat )
{
this.reportFormat = reportFormat;
}
public boolean isUseFile()
{
return useFile;
}
public void setUseFile( boolean useFile )
{
this.useFile = useFile;
}
public String getDebugForkedProcess()
{
return debugForkedProcess;
}
public void setDebugForkedProcess( String debugForkedProcess )
{
this.debugForkedProcess = debugForkedProcess;
}
public int getForkedProcessTimeoutInSeconds()
{
return forkedProcessTimeoutInSeconds;
}
public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
{
this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
}
public double getParallelTestsTimeoutInSeconds()
{
return parallelTestsTimeoutInSeconds;
}
public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds )
{
this.parallelTestsTimeoutInSeconds = parallelTestsTimeoutInSeconds;
}
public double getParallelTestsTimeoutForcedInSeconds()
{
return parallelTestsTimeoutForcedInSeconds;
}
public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds )
{
this.parallelTestsTimeoutForcedInSeconds = parallelTestsTimeoutForcedInSeconds;
}
public void setTest( String test )
{
this.test = test;
}
@Override
public List<String> getIncludes()
{
return includes;
}
@Override
public void setIncludes( List<String> includes )
{
this.includes = includes;
}
public File[] getSuiteXmlFiles()
{
return suiteXmlFiles;
}
@SuppressWarnings( "UnusedDeclaration" )
public void setSuiteXmlFiles( File[] suiteXmlFiles )
{
this.suiteXmlFiles = suiteXmlFiles;
}
public String getRunOrder()
{
return runOrder;
}
@SuppressWarnings( "UnusedDeclaration" )
public void setRunOrder( String runOrder )
{
this.runOrder = runOrder;
}
}