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;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.maven.surefire.its.fixture.MavenLauncher;
25  import org.apache.maven.surefire.its.fixture.OutputValidator;
26  import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
27  import org.junit.jupiter.params.ParameterizedTest;
28  import org.junit.jupiter.params.provider.MethodSource;
29  
30  import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaVersion;
31  import static org.hamcrest.Matchers.containsString;
32  import static org.hamcrest.Matchers.equalTo;
33  
34  /**
35   * Base test class for SUREFIRE-580, configuration parameter {@code skipAfterFailureCount}.
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 2.19
39   */
40  public abstract class AbstractFailFastIT extends SurefireJUnit4IntegrationTestCase {
41      private static final String LEGACY_FORK_NODE = "org.apache.maven.plugin.surefire.extensions.LegacyForkNodeFactory";
42  
43      private static final String SUREFIRE_FORK_NODE =
44              "org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory";
45  
46      protected abstract String withProvider();
47  
48      private OutputValidator prepare(
49              String description,
50              String profile,
51              Map<String, String> properties,
52              boolean useProcessPipes,
53              int failures,
54              int errors) {
55          MavenLauncher launcher = unpack("/fail-fast-" + withProvider(), "_" + description)
56                  .maven()
57                  .debugLogging();
58  
59          if (profile != null) {
60              launcher.activateProfile(profile);
61          }
62  
63          if (!useProcessPipes) {
64              launcher.activateProfile("tcp");
65          }
66  
67          if (failures != 0 || errors != 0) {
68              launcher.withFailure();
69          }
70  
71          return launcher.sysProp(properties).executeTest();
72      }
73  
74      static Map<String, String> props(int forkCount, int skipAfterFailureCount, boolean reuseForks) {
75          Map<String, String> props = new HashMap<>(3);
76          props.put("surefire.skipAfterFailureCount", "" + skipAfterFailureCount);
77          props.put("forkCount", "" + forkCount);
78          props.put("reuseForks", "" + reuseForks);
79          return props;
80      }
81  
82      @ParameterizedTest(name = "{0}")
83      @MethodSource("data")
84      void test(
85              String description,
86              String profile,
87              Map<String, String> properties,
88              int total,
89              int failures,
90              int errors,
91              int skipped,
92              boolean useProcessPipes)
93              throws Exception {
94          // JUnit 6.0.0 requires Java 17+
95          assumeJavaVersion(17);
96          String cls = useProcessPipes ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
97          OutputValidator validator = prepare(description, profile, properties, useProcessPipes, failures, errors);
98          validator
99                  .assertTestSuiteResults(total, errors, failures, skipped)
100                 .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
101         performExtraChecks(validator);
102     }
103 
104     protected void performExtraChecks(OutputValidator validator) throws Exception {
105         // may be overriden in subclasses
106     }
107 }