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