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