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

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

The newest version!
package org.apache.maven.surefire.testng;

/*
 * 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.
 */

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

import org.apache.maven.surefire.report.RunListener;
import org.apache.maven.surefire.testset.TestSetFailedException;

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

/**
 * Handles suite xml file definitions for TestNG.
 *
 * @author jkuhnert
 * @author Alex Popescu
 */
public 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.
     */
    public 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;
    }

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

    public void 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();

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy