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