View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.its.fixture;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.shared.verifier.VerificationException;
26  
27  /**
28   * Encapsulate all needed features to start a surefire run
29   * <br>
30   * Also includes thread-safe access to the extracted resource
31   * files
32   *
33   * @author Kristian Rosenvold                                 -
34   */
35  public final class SurefireLauncher {
36      private final MavenLauncher mavenLauncher;
37  
38      private final String surefireVersion = System.getProperty("surefire.version");
39  
40      public SurefireLauncher(MavenLauncher mavenLauncher) {
41          this.mavenLauncher = mavenLauncher;
42          reset();
43      }
44  
45      public MavenLauncher maven() {
46          return mavenLauncher;
47      }
48  
49      String getTestMethodName() {
50          return mavenLauncher.getTestMethodName();
51      }
52  
53      public void reset() {
54          mavenLauncher.reset();
55          for (String s : getInitialGoals()) {
56              mavenLauncher.addGoal(s);
57          }
58      }
59  
60      public SurefireLauncher getSubProjectLauncher(String subProject) {
61          return new SurefireLauncher(mavenLauncher.getSubProjectLauncher(subProject));
62      }
63  
64      public OutputValidator getSubProjectValidator(String subProject) throws VerificationException {
65          return mavenLauncher.getSubProjectValidator(subProject);
66      }
67  
68      private SurefireLauncher addEnvVar(String key, String value) {
69          mavenLauncher.addEnvVar(key, value);
70          return this;
71      }
72  
73      public SurefireLauncher setMavenOpts(String opts) {
74          return addEnvVar("MAVEN_OPTS", opts);
75      }
76  
77      private List<String> getInitialGoals() {
78          List<String> goals = new ArrayList<>();
79  
80          goals.add("-Dsurefire.version=" + surefireVersion);
81  
82          String jacocoAgent = System.getProperty("jacoco.agent", "");
83          // Remove unnecessary backslash escaping for Windows, see https://github.com/jacoco/jacoco/issues/1559
84          jacocoAgent = jacocoAgent.replace("\\\\", "\\");
85          // Remove quotes which will cause a syntax error raised by cmd.exe because of quote escaping
86          jacocoAgent = jacocoAgent.replace("\"", "");
87          goals.add("-Djacoco.agent=" + jacocoAgent);
88          goals.add("-nsu");
89  
90          return goals;
91      }
92  
93      public SurefireLauncher showErrorStackTraces() {
94          mavenLauncher.showErrorStackTraces();
95          return this;
96      }
97  
98      public SurefireLauncher debugLogging() {
99          mavenLauncher.debugLogging();
100         return this;
101     }
102 
103     @SuppressWarnings("UnusedDeclaration")
104     public SurefireLauncher debugSurefireFork() {
105         mavenLauncher.sysProp("maven.surefire.debug", "true");
106         return this;
107     }
108 
109     public SurefireLauncher failNever() {
110         mavenLauncher.failNever();
111         return this;
112     }
113 
114     public SurefireLauncher groups(String groups) {
115         mavenLauncher.sysProp("groups", groups);
116         return this;
117     }
118 
119     public SurefireLauncher addGoal(String goal) {
120         mavenLauncher.addGoal(goal);
121         return this;
122     }
123 
124     public OutputValidator executeTest() {
125         return mavenLauncher.execute("test");
126     }
127 
128     public OutputValidator executeInstall() {
129         return mavenLauncher.execute("install");
130     }
131 
132     public FailsafeOutputValidator executeVerify() {
133         OutputValidator verify = execute("verify");
134         return new FailsafeOutputValidator(verify);
135     }
136 
137     public OutputValidator execute(String goal) {
138         return mavenLauncher.execute(goal);
139     }
140 
141     public OutputValidator executeSurefireReport() {
142         return mavenLauncher.execute("surefire-report:report");
143     }
144 
145     public OutputValidator executeCurrentGoals() {
146         return mavenLauncher.executeCurrentGoals();
147     }
148 
149     public SurefireLauncher printSummary(boolean printsummary) {
150         mavenLauncher.sysProp("printSummary", printsummary);
151         return this;
152     }
153 
154     public SurefireLauncher redirectToFile(boolean redirect) {
155         mavenLauncher.sysProp("maven.test.redirectTestOutputToFile", redirect);
156         return this;
157     }
158 
159     public SurefireLauncher forkOnce() {
160         return forkCount(1).reuseForks(true);
161     }
162 
163     public SurefireLauncher forkNever() {
164         return forkCount(0);
165     }
166 
167     public SurefireLauncher forkAlways() {
168         return forkCount(1).reuseForks(false);
169     }
170 
171     public SurefireLauncher forkPerThread(int threadCount) {
172         return forkCount(threadCount).reuseForks(true);
173     }
174 
175     public SurefireLauncher threadCount(int threadCount) {
176         mavenLauncher.sysProp("threadCount", threadCount);
177         return this;
178     }
179 
180     public SurefireLauncher forkCount(int forkCount) {
181         mavenLauncher.sysProp("forkCount", forkCount);
182         return this;
183     }
184 
185     public SurefireLauncher reuseForks(boolean reuseForks) {
186         mavenLauncher.sysProp("reuseForks", reuseForks);
187         return this;
188     }
189 
190     public SurefireLauncher runOrder(String runOrder) {
191         mavenLauncher.sysProp("surefire.runOrder", runOrder);
192         return this;
193     }
194 
195     public SurefireLauncher runOrderRandomSeed(String runOrderRandomSeed) {
196         mavenLauncher.sysProp("surefire.runOrder.random.seed", runOrderRandomSeed);
197         return this;
198     }
199 
200     public SurefireLauncher failIfNoTests(boolean fail) {
201         mavenLauncher.sysProp("failIfNoTests", fail);
202         return this;
203     }
204 
205     public SurefireLauncher mavenTestFailureIgnore(boolean fail) {
206         mavenLauncher.sysProp("maven.test.failure.ignore", fail);
207         return this;
208     }
209 
210     public SurefireLauncher failIfNoSpecifiedTests(boolean fail) {
211         mavenLauncher.sysProp("surefire.failIfNoSpecifiedTests", fail);
212         return this;
213     }
214 
215     public SurefireLauncher useSystemClassLoader(boolean useSystemClassLoader) {
216         mavenLauncher.sysProp("useSystemClassLoader", useSystemClassLoader);
217         return this;
218     }
219 
220     public SurefireLauncher activateProfile(String profile) {
221         mavenLauncher.activateProfile(profile);
222         return this;
223     }
224 
225     public SurefireLauncher disablePerCoreThreadCount() {
226         mavenLauncher.sysProp("perCoreThreadCount", false);
227         return this;
228     }
229 
230     public SurefireLauncher disableParallelOptimization() {
231         mavenLauncher.sysProp("parallelOptimized", "false");
232         return this;
233     }
234 
235     public SurefireLauncher parallel(String parallel) {
236         mavenLauncher.sysProp("parallel", parallel);
237         return this;
238     }
239 
240     public SurefireLauncher parallelSuites() {
241         return parallel("suites");
242     }
243 
244     public SurefireLauncher parallelClasses() {
245         return parallel("classes");
246     }
247 
248     public SurefireLauncher parallelMethods() {
249         return parallel("methods");
250     }
251 
252     public SurefireLauncher parallelBoth() {
253         return parallel("both");
254     }
255 
256     public SurefireLauncher parallelSuitesAndClasses() {
257         return parallel("suitesAndClasses");
258     }
259 
260     public SurefireLauncher parallelSuitesAndMethods() {
261         return parallel("suitesAndMethods");
262     }
263 
264     public SurefireLauncher parallelClassesAndMethods() {
265         return parallel("classesAndMethods");
266     }
267 
268     public SurefireLauncher parallelAll() {
269         return parallel("all");
270     }
271 
272     public SurefireLauncher useUnlimitedThreads() {
273         mavenLauncher.sysProp("useUnlimitedThreads", true);
274         return this;
275     }
276 
277     public SurefireLauncher threadCountSuites(int count) {
278         mavenLauncher.sysProp("threadCountSuites", count);
279         return this;
280     }
281 
282     public SurefireLauncher threadCountClasses(int count) {
283         mavenLauncher.sysProp("threadCountClasses", count);
284         return this;
285     }
286 
287     public SurefireLauncher threadCountMethods(int count) {
288         mavenLauncher.sysProp("threadCountMethods", count);
289         return this;
290     }
291 
292     public SurefireLauncher parallelTestsTimeoutInSeconds(double timeout) {
293         mavenLauncher.sysProp("surefire.parallel.timeout", timeout);
294         return this;
295     }
296 
297     public SurefireLauncher parallelTestsTimeoutForcedInSeconds(double timeout) {
298         mavenLauncher.sysProp("surefire.parallel.forcedTimeout", timeout);
299         return this;
300     }
301 
302     public SurefireLauncher argLine(String value) {
303         mavenLauncher.sysProp("argLine", value);
304         return this;
305     }
306 
307     public SurefireLauncher sysProp(String variable, String value) {
308         mavenLauncher.sysProp(variable, value);
309         return this;
310     }
311 
312     public SurefireLauncher setJUnitVersion(String version) {
313         mavenLauncher.sysProp("junit.version", version);
314         return this;
315     }
316 
317     public SurefireLauncher setGroups(String groups) {
318         mavenLauncher.sysProp("groups", groups);
319         return this;
320     }
321 
322     public SurefireLauncher setExcludedGroups(String excludedGroups) {
323         mavenLauncher.sysProp("excludedGroups", excludedGroups);
324         return this;
325     }
326 
327     public File getUnpackedAt() {
328         return mavenLauncher.getUnpackedAt();
329     }
330 
331     public SurefireLauncher addFailsafeReportOnlyGoal() {
332         mavenLauncher.addGoal(getReportPluginGoal("failsafe-report-only"));
333         return this;
334     }
335 
336     public SurefireLauncher addSurefireReportGoal() {
337         mavenLauncher.addGoal(getReportPluginGoal("report"));
338         return this;
339     }
340 
341     public SurefireLauncher addSurefireReportOnlyGoal() {
342         mavenLauncher.addGoal(getReportPluginGoal("report-only"));
343         return this;
344     }
345 
346     private String getReportPluginGoal(String goal) {
347         return "org.apache.maven.plugins:maven-surefire-report-plugin:" + surefireVersion + ":" + goal;
348     }
349 
350     public SurefireLauncher setTestToRun(String basicTest) {
351         mavenLauncher.sysProp("test", basicTest);
352         return this;
353     }
354 
355     public SurefireLauncher setForkJvm() {
356         setForkJvm(true);
357         return this;
358     }
359 
360     public SurefireLauncher setForkJvm(boolean forkJvm) {
361         mavenLauncher.setForkJvm(forkJvm);
362         return this;
363     }
364 
365     public SurefireLauncher setLogFileName(String logFileName) {
366         mavenLauncher.setLogFileName(logFileName);
367         return this;
368     }
369 }