All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.surefire.testng.TestNGXmlTestSuite Maven / Gradle / Ivy

There is a newer version: 3.5.2
Show newest version
/*
 * 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.testng;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.maven.surefire.api.testset.TestSetFailedException;

import static org.apache.maven.surefire.testng.TestNGExecutor.run;

/**
 * Handles suite xml file definitions for TestNG.
 *
 * @author jkuhnert
 * @author Alex Popescu
 */
final class TestNGXmlTestSuite extends TestSuite {
    private final List suiteFiles;

    private List suiteFilePaths;

    private final String testSourceDirectory;

    private final Map options;

    private final File reportsDirectory;

    private final int skipAfterFailureCount;

    /**
     * Creates a testng testset to be configured by the specified
     * xml file(s). The XML files are suite definitions files according to TestNG DTD.
     */
    TestNGXmlTestSuite(
            List suiteFiles,
            String testSourceDirectory,
            Map confOptions,
            File reportsDirectory,
            int skipAfterFailureCount) {
        this.suiteFiles = suiteFiles;
        this.options = confOptions;
        this.testSourceDirectory = testSourceDirectory;
        this.reportsDirectory = reportsDirectory;
        this.skipAfterFailureCount = skipAfterFailureCount;
    }

    void execute(TestNGReporter testNGReporter) throws TestSetFailedException {
        if (suiteFilePaths == null) {
            throw new IllegalStateException("You must call locateTestSets before calling execute");
        }
        startTestSuite(testNGReporter.getRunListener());
        run(suiteFilePaths, testSourceDirectory, options, testNGReporter, reportsDirectory, skipAfterFailureCount);
        finishTestSuite(testNGReporter.getRunListener());
    }

    Iterable locateTestSets() throws TestSetFailedException {
        if (suiteFilePaths != null) {
            throw new IllegalStateException("You can't call locateTestSets twice");
        }

        if (suiteFiles.isEmpty()) {
            throw new IllegalStateException("No suite files were specified");
        }

        suiteFilePaths = new ArrayList<>(suiteFiles.size());
        ArrayList testSets = new ArrayList<>(suiteFiles.size());

        for (File suiteFile : suiteFiles) {
            if (!suiteFile.isFile()) {
                throw new TestSetFailedException("Suite file " + suiteFile + " is not a valid file");
            }
            testSets.add(suiteFile);
            suiteFilePaths.add(suiteFile.getAbsolutePath());
        }
        return testSets;
    }

    @Override
    Map getOptions() {
        return options;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy