/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.maven.surefire.its;

import java.util.HashMap;
import java.util.Map;

import org.apache.maven.surefire.its.fixture.MavenLauncher;
import org.apache.maven.surefire.its.fixture.OutputValidator;
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import static org.apache.maven.surefire.its.fixture.HelperAssertions.assumeJavaVersion;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

/**
 * Base test class for SUREFIRE-580, configuration parameter {@code skipAfterFailureCount}.
 *
 * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
 * @since 2.19
 */
public abstract class AbstractFailFastIT extends SurefireJUnit4IntegrationTestCase {
    private static final String LEGACY_FORK_NODE = "org.apache.maven.plugin.surefire.extensions.LegacyForkNodeFactory";

    private static final String SUREFIRE_FORK_NODE =
            "org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory";

    protected abstract String withProvider();

    private OutputValidator prepare(
            String description,
            String profile,
            Map<String, String> properties,
            boolean useProcessPipes,
            int failures,
            int errors) {
        MavenLauncher launcher = unpack("/fail-fast-" + withProvider(), "_" + description)
                .maven()
                .debugLogging();

        if (profile != null) {
            launcher.activateProfile(profile);
        }

        if (!useProcessPipes) {
            launcher.activateProfile("tcp");
        }

        if (failures != 0 || errors != 0) {
            launcher.withFailure();
        }

        return launcher.sysProp(properties).executeTest();
    }

    static Map<String, String> props(int forkCount, int skipAfterFailureCount, boolean reuseForks) {
        Map<String, String> props = new HashMap<>(3);
        props.put("surefire.skipAfterFailureCount", "" + skipAfterFailureCount);
        props.put("forkCount", "" + forkCount);
        props.put("reuseForks", "" + reuseForks);
        return props;
    }

    @ParameterizedTest(name = "{0}")
    @MethodSource("data")
    void test(
            String description,
            String profile,
            Map<String, String> properties,
            int total,
            int failures,
            int errors,
            int skipped,
            boolean useProcessPipes)
            throws Exception {
        // JUnit 6.0.0 requires Java 17+
        assumeJavaVersion(17);
        String cls = useProcessPipes ? LEGACY_FORK_NODE : SUREFIRE_FORK_NODE;
        OutputValidator validator = prepare(description, profile, properties, useProcessPipes, failures, errors);
        validator
                .assertTestSuiteResults(total, errors, failures, skipped)
                .assertThatLogLine(containsString("Found implementation of fork node factory: " + cls), equalTo(1));
        performExtraChecks(validator);
    }

    protected void performExtraChecks(OutputValidator validator) throws Exception {
        // may be overriden in subclasses
    }
}
