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