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