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.it.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          goals.add("-Djacoco.agent=" + jacocoAgent);
84          goals.add("-nsu");
85  
86          return goals;
87      }
88  
89      public SurefireLauncher showErrorStackTraces() {
90          mavenLauncher.showErrorStackTraces();
91          return this;
92      }
93  
94      public SurefireLauncher debugLogging() {
95          mavenLauncher.debugLogging();
96          return this;
97      }
98  
99      @SuppressWarnings("UnusedDeclaration")
100     public SurefireLauncher debugSurefireFork() {
101         mavenLauncher.sysProp("maven.surefire.debug", "true");
102         return this;
103     }
104 
105     public SurefireLauncher failNever() {
106         mavenLauncher.failNever();
107         return this;
108     }
109 
110     public SurefireLauncher groups(String groups) {
111         mavenLauncher.sysProp("groups", groups);
112         return this;
113     }
114 
115     public SurefireLauncher addGoal(String goal) {
116         mavenLauncher.addGoal(goal);
117         return this;
118     }
119 
120     public OutputValidator executeTest() {
121         return mavenLauncher.execute("test");
122     }
123 
124     public OutputValidator executeInstall() {
125         return mavenLauncher.execute("install");
126     }
127 
128     public FailsafeOutputValidator executeVerify() {
129         OutputValidator verify = execute("verify");
130         return new FailsafeOutputValidator(verify);
131     }
132 
133     public OutputValidator execute(String goal) {
134         return mavenLauncher.execute(goal);
135     }
136 
137     public OutputValidator executeSurefireReport() {
138         return mavenLauncher.execute("surefire-report:report");
139     }
140 
141     public OutputValidator executeCurrentGoals() {
142         return mavenLauncher.executeCurrentGoals();
143     }
144 
145     public SurefireLauncher printSummary(boolean printsummary) {
146         mavenLauncher.sysProp("printSummary", printsummary);
147         return this;
148     }
149 
150     public SurefireLauncher redirectToFile(boolean redirect) {
151         mavenLauncher.sysProp("maven.test.redirectTestOutputToFile", redirect);
152         return this;
153     }
154 
155     public SurefireLauncher forkOnce() {
156         return forkCount(1).reuseForks(true);
157     }
158 
159     public SurefireLauncher forkNever() {
160         return forkCount(0);
161     }
162 
163     public SurefireLauncher forkAlways() {
164         return forkCount(1).reuseForks(false);
165     }
166 
167     public SurefireLauncher forkPerThread(int threadCount) {
168         return forkCount(threadCount).reuseForks(true);
169     }
170 
171     public SurefireLauncher threadCount(int threadCount) {
172         mavenLauncher.sysProp("threadCount", threadCount);
173         return this;
174     }
175 
176     public SurefireLauncher forkCount(int forkCount) {
177         mavenLauncher.sysProp("forkCount", forkCount);
178         return this;
179     }
180 
181     public SurefireLauncher reuseForks(boolean reuseForks) {
182         mavenLauncher.sysProp("reuseForks", reuseForks);
183         return this;
184     }
185 
186     public SurefireLauncher runOrder(String runOrder) {
187         mavenLauncher.sysProp("surefire.runOrder", runOrder);
188         return this;
189     }
190 
191     public SurefireLauncher runOrderRandomSeed(String runOrderRandomSeed) {
192         mavenLauncher.sysProp("surefire.runOrder.random.seed", runOrderRandomSeed);
193         return this;
194     }
195 
196     public SurefireLauncher failIfNoTests(boolean fail) {
197         mavenLauncher.sysProp("failIfNoTests", fail);
198         return this;
199     }
200 
201     public SurefireLauncher mavenTestFailureIgnore(boolean fail) {
202         mavenLauncher.sysProp("maven.test.failure.ignore", fail);
203         return this;
204     }
205 
206     public SurefireLauncher failIfNoSpecifiedTests(boolean fail) {
207         mavenLauncher.sysProp("surefire.failIfNoSpecifiedTests", fail);
208         return this;
209     }
210 
211     public SurefireLauncher useSystemClassLoader(boolean useSystemClassLoader) {
212         mavenLauncher.sysProp("useSystemClassLoader", useSystemClassLoader);
213         return this;
214     }
215 
216     public SurefireLauncher activateProfile(String profile) {
217         mavenLauncher.activateProfile(profile);
218         return this;
219     }
220 
221     public SurefireLauncher disablePerCoreThreadCount() {
222         mavenLauncher.sysProp("perCoreThreadCount", false);
223         return this;
224     }
225 
226     public SurefireLauncher disableParallelOptimization() {
227         mavenLauncher.sysProp("parallelOptimized", "false");
228         return this;
229     }
230 
231     public SurefireLauncher parallel(String parallel) {
232         mavenLauncher.sysProp("parallel", parallel);
233         return this;
234     }
235 
236     public SurefireLauncher parallelSuites() {
237         return parallel("suites");
238     }
239 
240     public SurefireLauncher parallelClasses() {
241         return parallel("classes");
242     }
243 
244     public SurefireLauncher parallelMethods() {
245         return parallel("methods");
246     }
247 
248     public SurefireLauncher parallelBoth() {
249         return parallel("both");
250     }
251 
252     public SurefireLauncher parallelSuitesAndClasses() {
253         return parallel("suitesAndClasses");
254     }
255 
256     public SurefireLauncher parallelSuitesAndMethods() {
257         return parallel("suitesAndMethods");
258     }
259 
260     public SurefireLauncher parallelClassesAndMethods() {
261         return parallel("classesAndMethods");
262     }
263 
264     public SurefireLauncher parallelAll() {
265         return parallel("all");
266     }
267 
268     public SurefireLauncher useUnlimitedThreads() {
269         mavenLauncher.sysProp("useUnlimitedThreads", true);
270         return this;
271     }
272 
273     public SurefireLauncher threadCountSuites(int count) {
274         mavenLauncher.sysProp("threadCountSuites", count);
275         return this;
276     }
277 
278     public SurefireLauncher threadCountClasses(int count) {
279         mavenLauncher.sysProp("threadCountClasses", count);
280         return this;
281     }
282 
283     public SurefireLauncher threadCountMethods(int count) {
284         mavenLauncher.sysProp("threadCountMethods", count);
285         return this;
286     }
287 
288     public SurefireLauncher parallelTestsTimeoutInSeconds(double timeout) {
289         mavenLauncher.sysProp("surefire.parallel.timeout", timeout);
290         return this;
291     }
292 
293     public SurefireLauncher parallelTestsTimeoutForcedInSeconds(double timeout) {
294         mavenLauncher.sysProp("surefire.parallel.forcedTimeout", timeout);
295         return this;
296     }
297 
298     public SurefireLauncher argLine(String value) {
299         mavenLauncher.sysProp("argLine", value);
300         return this;
301     }
302 
303     public SurefireLauncher sysProp(String variable, String value) {
304         mavenLauncher.sysProp(variable, value);
305         return this;
306     }
307 
308     public SurefireLauncher setJUnitVersion(String version) {
309         mavenLauncher.sysProp("junit.version", version);
310         return this;
311     }
312 
313     public SurefireLauncher setGroups(String groups) {
314         mavenLauncher.sysProp("groups", groups);
315         return this;
316     }
317 
318     public SurefireLauncher setExcludedGroups(String excludedGroups) {
319         mavenLauncher.sysProp("excludedGroups", excludedGroups);
320         return this;
321     }
322 
323     public File getUnpackedAt() {
324         return mavenLauncher.getUnpackedAt();
325     }
326 
327     public SurefireLauncher addFailsafeReportOnlyGoal() {
328         mavenLauncher.addGoal(getReportPluginGoal(":failsafe-report-only"));
329         return this;
330     }
331 
332     public SurefireLauncher addSurefireReportGoal() {
333         mavenLauncher.addGoal(getReportPluginGoal("report"));
334         return this;
335     }
336 
337     public SurefireLauncher addSurefireReportOnlyGoal() {
338         mavenLauncher.addGoal(getReportPluginGoal("report-only"));
339         return this;
340     }
341 
342     private String getReportPluginGoal(String goal) {
343         return "org.apache.maven.plugins:maven-surefire-report-plugin:" + surefireVersion + ":" + goal;
344     }
345 
346     public SurefireLauncher setTestToRun(String basicTest) {
347         mavenLauncher.sysProp("test", basicTest);
348         return this;
349     }
350 
351     public SurefireLauncher setForkJvm() {
352         mavenLauncher.setForkJvm(true);
353         return this;
354     }
355 
356     public SurefireLauncher setLogFileName(String logFileName) {
357         mavenLauncher.setLogFileName(logFileName);
358         return this;
359     }
360 }