View Javadoc
1   package org.apache.maven.surefire.its.fixture;
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 org.apache.maven.it.VerificationException;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.List;
27  
28  import static java.util.Collections.singletonList;
29  
30  /**
31   * Encapsulate all needed features to start a surefire run
32   * <br>
33   * Also includes thread-safe access to the extracted resource
34   * files
35   *
36   * @author Kristian Rosenvold                                 -
37   */
38  public final class SurefireLauncher
39  {
40      private final MavenLauncher mavenLauncher;
41  
42      private final String surefireVersion = System.getProperty( "surefire.version" );
43  
44      public SurefireLauncher( MavenLauncher mavenLauncher )
45      {
46          this.mavenLauncher = mavenLauncher;
47          reset();
48      }
49  
50      public MavenLauncher maven()
51      {
52          return mavenLauncher;
53      }
54  
55      String getTestMethodName()
56      {
57          return mavenLauncher.getTestMethodName();
58      }
59  
60      public void reset()
61      {
62          mavenLauncher.reset();
63          for ( String s : getInitialGoals() )
64          {
65              mavenLauncher.addGoal( s );
66          }
67          setInProcessJavaHome();
68      }
69  
70      public SurefireLauncher setInProcessJavaHome()
71      {
72          String javaHome = System.getenv( "JAVA_HOME" );
73          if ( javaHome != null && !javaHome.isEmpty() )
74          {
75              try
76              {
77                  File javaHomeAsDir = new File( javaHome ).getCanonicalFile();
78                  if ( javaHomeAsDir.isDirectory() )
79                  {
80                      setLauncherJavaHome( javaHomeAsDir.getPath() );
81                  }
82              }
83              catch ( IOException e )
84              {
85                  throw new RuntimeException( e );
86              }
87          }
88          return this;
89      }
90  
91      public SurefireLauncher setLauncherJavaHome( String javaHome )
92      {
93          mavenLauncher.addEnvVar( "JAVA_HOME", javaHome );
94          return this;
95      }
96  
97      public SurefireLauncher getSubProjectLauncher( String subProject )
98          throws VerificationException
99      {
100         return new SurefireLauncher( mavenLauncher.getSubProjectLauncher( subProject ) );
101     }
102 
103     public OutputValidator getSubProjectValidator( String subProject )
104         throws VerificationException
105     {
106         return mavenLauncher.getSubProjectValidator( subProject );
107     }
108 
109     public SurefireLauncher addEnvVar( String key, String value )
110     {
111         mavenLauncher.addEnvVar( key, value );
112         return this;
113     }
114 
115     public SurefireLauncher setMavenOpts(String opts){
116         addEnvVar( "MAVEN_OPTS", opts );
117         return this;
118     }
119 
120     private List<String> getInitialGoals()
121     {
122         return singletonList( "-Dsurefire.version=" + surefireVersion );
123     }
124 
125     public SurefireLauncher showErrorStackTraces()
126     {
127         mavenLauncher.showErrorStackTraces();
128         return this;
129     }
130 
131     public SurefireLauncher debugLogging()
132     {
133         mavenLauncher.debugLogging();
134         return this;
135     }
136 
137     @SuppressWarnings( "UnusedDeclaration" )
138     public SurefireLauncher debugSurefireFork()
139     {
140         mavenLauncher.sysProp( "maven.surefire.debug", "true" );
141         return this;
142     }
143 
144     public SurefireLauncher failNever()
145     {
146         mavenLauncher.failNever();
147         return this;
148     }
149 
150     public SurefireLauncher groups( String groups )
151     {
152         mavenLauncher.sysProp( "groups", groups );
153         return this;
154     }
155 
156     public SurefireLauncher addGoal( String goal )
157     {
158         mavenLauncher.addGoal( goal );
159         return this;
160     }
161 
162     public OutputValidator executeTest()
163     {
164         return mavenLauncher.execute( "test" );
165     }
166 
167     public OutputValidator executeInstall()
168         throws VerificationException
169     {
170         return mavenLauncher.execute( "install" );
171     }
172 
173 
174     public FailsafeOutputValidator executeVerify()
175     {
176         OutputValidator verify = execute( "verify" );
177         return new FailsafeOutputValidator( verify );
178     }
179 
180     public OutputValidator execute( String goal )
181     {
182         return mavenLauncher.execute( goal );
183     }
184 
185     public OutputValidator executeSurefireReport()
186     {
187         return mavenLauncher.execute( "surefire-report:report" );
188     }
189 
190 
191     public OutputValidator executeCurrentGoals()
192     {
193         return mavenLauncher.executeCurrentGoals();
194     }
195 
196 
197     public SurefireLauncher printSummary( boolean printsummary )
198     {
199         mavenLauncher.sysProp( "printSummary", printsummary );
200         return this;
201     }
202 
203     public SurefireLauncher redirectToFile( boolean redirect )
204     {
205         mavenLauncher.sysProp( "maven.test.redirectTestOutputToFile", redirect );
206         return this;
207     }
208 
209     public SurefireLauncher forkOnce()
210     {
211         return forkMode( "once" );
212     }
213 
214     public SurefireLauncher forkNever()
215     {
216         return forkMode( "never" );
217     }
218 
219     public SurefireLauncher forkAlways()
220     {
221         return forkMode( "always" );
222     }
223 
224     public SurefireLauncher forkPerTest()
225     {
226         return forkMode( "pertest" );
227     }
228 
229     public SurefireLauncher forkPerThread()
230     {
231         return forkMode( "perthread" );
232     }
233 
234     public SurefireLauncher threadCount( int threadCount )
235     {
236         mavenLauncher.sysProp( "threadCount", threadCount );
237         return this;
238     }
239 
240     public SurefireLauncher forkCount( int forkCount )
241     {
242         mavenLauncher.sysProp( "forkCount", forkCount );
243         return this;
244     }
245 
246     public SurefireLauncher reuseForks( boolean reuseForks )
247     {
248         mavenLauncher.sysProp( "reuseForks", reuseForks );
249         return this;
250     }
251 
252     public SurefireLauncher forkMode( String forkMode )
253     {
254         mavenLauncher.sysProp( "forkMode", forkMode );
255         return this;
256     }
257 
258     public SurefireLauncher runOrder( String runOrder )
259     {
260         mavenLauncher.sysProp( "surefire.runOrder", runOrder );
261         return this;
262     }
263 
264     public SurefireLauncher failIfNoTests( boolean fail )
265     {
266         mavenLauncher.sysProp( "failIfNoTests", fail );
267         return this;
268     }
269 
270 
271     public SurefireLauncher mavenTestFailureIgnore( boolean fail )
272     {
273         mavenLauncher.sysProp( "maven.test.failure.ignore", fail );
274         return this;
275     }
276 
277     public SurefireLauncher failIfNoSpecifiedTests( boolean fail )
278     {
279         mavenLauncher.sysProp( "surefire.failIfNoSpecifiedTests", fail );
280         return this;
281     }
282 
283     public SurefireLauncher useSystemClassLoader( boolean useSystemClassLoader )
284     {
285         mavenLauncher.sysProp( "useSystemClassLoader", useSystemClassLoader );
286         return this;
287     }
288 
289     public SurefireLauncher activateProfile( String profile )
290     {
291         mavenLauncher.activateProfile( profile );
292         return this;
293     }
294 
295 
296     protected String getSurefireVersion()
297     {
298         return surefireVersion;
299     }
300 
301     public SurefireLauncher disablePerCoreThreadCount()
302     {
303         mavenLauncher.sysProp( "perCoreThreadCount", false );
304         return this;
305     }
306 
307     public SurefireLauncher disableParallelOptimization()
308     {
309         mavenLauncher.sysProp( "parallelOptimized", "false" );
310         return this;
311     }
312 
313     public SurefireLauncher parallel( String parallel )
314     {
315         mavenLauncher.sysProp( "parallel", parallel );
316         return this;
317     }
318 
319     public SurefireLauncher parallelSuites()
320     {
321         return parallel( "suites" );
322     }
323 
324     public SurefireLauncher parallelClasses()
325     {
326         return parallel( "classes" );
327     }
328 
329     public SurefireLauncher parallelMethods()
330     {
331         return parallel( "methods" );
332     }
333 
334     public SurefireLauncher parallelBoth()
335     {
336         return parallel( "both" );
337     }
338 
339     public SurefireLauncher parallelSuitesAndClasses()
340     {
341         return parallel( "suitesAndClasses" );
342     }
343 
344     public SurefireLauncher parallelSuitesAndMethods()
345     {
346         return parallel( "suitesAndMethods" );
347     }
348 
349     public SurefireLauncher parallelClassesAndMethods()
350     {
351         return parallel( "classesAndMethods" );
352     }
353 
354     public SurefireLauncher parallelAll()
355     {
356         return parallel( "all" );
357     }
358 
359     public SurefireLauncher useUnlimitedThreads()
360     {
361         mavenLauncher.sysProp( "useUnlimitedThreads", true );
362         return this;
363     }
364 
365     public SurefireLauncher threadCountSuites( int count )
366     {
367         mavenLauncher.sysProp( "threadCountSuites", count );
368         return this;
369     }
370 
371     public SurefireLauncher threadCountClasses( int count )
372     {
373         mavenLauncher.sysProp( "threadCountClasses", count );
374         return this;
375     }
376 
377     public SurefireLauncher threadCountMethods( int count )
378     {
379         mavenLauncher.sysProp( "threadCountMethods", count );
380         return this;
381     }
382 
383     public SurefireLauncher parallelTestsTimeoutInSeconds( double timeout )
384     {
385         mavenLauncher.sysProp( "surefire.parallel.timeout", timeout );
386         return this;
387     }
388 
389     public SurefireLauncher parallelTestsTimeoutForcedInSeconds( double timeout )
390     {
391         mavenLauncher.sysProp( "surefire.parallel.forcedTimeout", timeout );
392         return this;
393     }
394 
395     public SurefireLauncher argLine( String value )
396     {
397         mavenLauncher.sysProp( "argLine", value );
398         return this;
399     }
400 
401     public SurefireLauncher sysProp( String variable, String value )
402     {
403         mavenLauncher.sysProp( variable, value );
404         return this;
405     }
406 
407     public SurefireLauncher setJUnitVersion( String version )
408     {
409         mavenLauncher.sysProp( "junit.version", version );
410         return this;
411     }
412 
413     public SurefireLauncher setGroups( String groups )
414     {
415         mavenLauncher.sysProp( "groups", groups );
416         return this;
417     }
418 
419     public SurefireLauncher setExcludedGroups( String excludedGroups )
420     {
421         mavenLauncher.sysProp( "excludedGroups", excludedGroups );
422         return this;
423     }
424 
425 
426     public File getUnpackedAt()
427     {
428         return mavenLauncher.getUnpackedAt();
429     }
430 
431     public SurefireLauncher addFailsafeReportOnlyGoal()
432     {
433         mavenLauncher.addGoal( getReportPluginGoal( ":failsafe-report-only" ) );
434         return this;
435     }
436 
437     public SurefireLauncher addSurefireReportGoal()
438     {
439         mavenLauncher.addGoal( getReportPluginGoal( "report" ) );
440         return this;
441     }
442 
443     public SurefireLauncher addSurefireReportOnlyGoal()
444     {
445         mavenLauncher.addGoal( getReportPluginGoal( "report-only" ) );
446         return this;
447     }
448 
449     private String getReportPluginGoal( String goal )
450     {
451         return "org.apache.maven.plugins:maven-surefire-report-plugin:" + getSurefireVersion() + ":" + goal;
452     }
453 
454     public SurefireLauncher setTestToRun( String basicTest )
455     {
456         mavenLauncher.sysProp( "test", basicTest );
457         return this;
458     }
459 
460     public SurefireLauncher setForkJvm()
461     {
462         mavenLauncher.setForkJvm( true );
463         return this;
464     }
465 }