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 @Parameter
165 private List<String> includes;
166
167
168
169
170
171
172
173
174 @Parameter( property = "surefire.useSystemClassLoader", defaultValue = "true" )
175 private boolean useSystemClassLoader;
176
177
178
179
180
181
182
183
184
185
186
187 @Parameter( property = "surefire.useManifestOnlyJar", defaultValue = "true" )
188 private boolean useManifestOnlyJar;
189
190 protected void handleSummary( RunResult summary, Exception firstForkException )
191 throws MojoExecutionException, MojoFailureException
192 {
193 assertNoException( firstForkException );
194
195 SurefireHelper.reportExecution( this, summary, getLog() );
196 }
197
198 private void assertNoException( Exception firstForkException )
199 throws MojoFailureException
200 {
201 if ( firstForkException != null )
202 {
203 throw new MojoFailureException( firstForkException.getMessage(), firstForkException );
204 }
205 }
206
207 private void assertNoFailureOrTimeout( Exception summary )
208 throws MojoFailureException
209 {
210 if ( summary != null )
211 {
212 throw new MojoFailureException( "Failure or timeout" );
213 }
214 }
215
216 protected boolean isSkipExecution()
217 {
218 return isSkip() || isSkipTests() || isSkipExec();
219 }
220
221 protected String getPluginName()
222 {
223 return "surefire";
224 }
225
226 protected String[] getDefaultIncludes()
227 {
228 return new String[]{ "**/Test*.java", "**/*Test.java", "**/*TestCase.java" };
229 }
230
231
232
233 public boolean isSkipTests()
234 {
235 return skipTests;
236 }
237
238 public void setSkipTests( boolean skipTests )
239 {
240 this.skipTests = skipTests;
241 }
242
243
244
245
246 public boolean isSkipExec()
247 {
248 return skipExec;
249 }
250
251
252
253
254 public void setSkipExec( boolean skipExec )
255 {
256 this.skipExec = skipExec;
257 }
258
259 public boolean isSkip()
260 {
261 return skip;
262 }
263
264 public void setSkip( boolean skip )
265 {
266 this.skip = skip;
267 }
268
269 public boolean isTestFailureIgnore()
270 {
271 return testFailureIgnore;
272 }
273
274 public void setTestFailureIgnore( boolean testFailureIgnore )
275 {
276 this.testFailureIgnore = testFailureIgnore;
277 }
278
279 public File getBasedir()
280 {
281 return basedir;
282 }
283
284 public void setBasedir( File basedir )
285 {
286 this.basedir = basedir;
287 }
288
289 public File getTestClassesDirectory()
290 {
291 return testClassesDirectory;
292 }
293
294 public void setTestClassesDirectory( File testClassesDirectory )
295 {
296 this.testClassesDirectory = testClassesDirectory;
297 }
298
299 public File getClassesDirectory()
300 {
301 return classesDirectory;
302 }
303
304 public void setClassesDirectory( File classesDirectory )
305 {
306 this.classesDirectory = classesDirectory;
307 }
308
309 public File getReportsDirectory()
310 {
311 return reportsDirectory;
312 }
313
314 public void setReportsDirectory( File reportsDirectory )
315 {
316 this.reportsDirectory = reportsDirectory;
317 }
318
319 public String getTest()
320 {
321 if ( StringUtils.isBlank( test ) )
322 {
323 return null;
324 }
325 String[] testArray = StringUtils.split( test, "," );
326 StringBuilder tests = new StringBuilder();
327 for ( String aTestArray : testArray )
328 {
329 String singleTest = aTestArray;
330 int index = singleTest.indexOf( '#' );
331 if ( index >= 0 )
332 {
333 singleTest = singleTest.substring( 0, index );
334 }
335 tests.append( singleTest );
336 tests.append( "," );
337 }
338 return tests.toString();
339 }
340
341
342
343
344 public String getTestMethod()
345 {
346 if ( StringUtils.isBlank( test ) )
347 {
348 return null;
349 }
350
351 int index = this.test.indexOf( '#' );
352 int index2 = this.test.indexOf( "," );
353 if ( index >= 0 )
354 {
355 if ( index2 < 0 )
356 {
357 String testStrAfterFirstSharp = this.test.substring( index + 1, this.test.length() );
358 if ( !testStrAfterFirstSharp.contains( "+" ) )
359 {
360 return testStrAfterFirstSharp;
361 }
362 else
363 {
364 return this.test;
365 }
366 }
367 else
368 {
369 return this.test;
370 }
371 }
372 return null;
373 }
374
375 public boolean isUseSystemClassLoader()
376 {
377 return useSystemClassLoader;
378 }
379
380 public void setUseSystemClassLoader( boolean useSystemClassLoader )
381 {
382 this.useSystemClassLoader = useSystemClassLoader;
383 }
384
385 public boolean isUseManifestOnlyJar()
386 {
387 return useManifestOnlyJar;
388 }
389
390 public void setUseManifestOnlyJar( boolean useManifestOnlyJar )
391 {
392 this.useManifestOnlyJar = useManifestOnlyJar;
393 }
394
395 public Boolean getFailIfNoSpecifiedTests()
396 {
397 return failIfNoSpecifiedTests;
398 }
399
400 public void setFailIfNoSpecifiedTests( Boolean failIfNoSpecifiedTests )
401 {
402 this.failIfNoSpecifiedTests = failIfNoSpecifiedTests;
403 }
404
405 public boolean isPrintSummary()
406 {
407 return printSummary;
408 }
409
410 public void setPrintSummary( boolean printSummary )
411 {
412 this.printSummary = printSummary;
413 }
414
415 public String getReportFormat()
416 {
417 return reportFormat;
418 }
419
420 public void setReportFormat( String reportFormat )
421 {
422 this.reportFormat = reportFormat;
423 }
424
425 public boolean isUseFile()
426 {
427 return useFile;
428 }
429
430 public void setUseFile( boolean useFile )
431 {
432 this.useFile = useFile;
433 }
434
435 public String getDebugForkedProcess()
436 {
437 return debugForkedProcess;
438 }
439
440 public void setDebugForkedProcess( String debugForkedProcess )
441 {
442 this.debugForkedProcess = debugForkedProcess;
443 }
444
445 public int getForkedProcessTimeoutInSeconds()
446 {
447 return forkedProcessTimeoutInSeconds;
448 }
449
450 public void setForkedProcessTimeoutInSeconds( int forkedProcessTimeoutInSeconds )
451 {
452 this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
453 }
454
455 public double getParallelTestsTimeoutInSeconds() {
456 return parallelTestsTimeoutInSeconds;
457 }
458
459 public void setParallelTestsTimeoutInSeconds( double parallelTestsTimeoutInSeconds ) {
460 this.parallelTestsTimeoutInSeconds = parallelTestsTimeoutInSeconds;
461 }
462
463 public double getParallelTestsTimeoutForcedInSeconds() {
464 return parallelTestsTimeoutForcedInSeconds;
465 }
466
467 public void setParallelTestsTimeoutForcedInSeconds( double parallelTestsTimeoutForcedInSeconds ) {
468 this.parallelTestsTimeoutForcedInSeconds = parallelTestsTimeoutForcedInSeconds;
469 }
470
471 public void setTest( String test )
472 {
473 this.test = test;
474 }
475
476 @Override
477 public List<String> getIncludes()
478 {
479 return includes;
480 }
481
482 @Override
483 public void setIncludes( List<String> includes )
484 {
485 this.includes = includes;
486 }
487 }