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

com.consol.citrus.container.AbstractSuiteActionContainer Maven / Gradle / Ivy

/*
 * Copyright 2006-2014 the original author or authors.
 *
 * Licensed 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 com.consol.citrus.container;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * Abstract suit container actions executed before and after test suite run. Container decides
 * weather to execute according to given suite name and included test groups if any.
 *
 * @author Christoph Deppisch
 * @since 2.0
 */
public abstract class AbstractSuiteActionContainer extends AbstractActionContainer {

    /** List of suite names that match for this container */
    private List suiteNames = new ArrayList<>();

    /** List of test group names that match for this container */
    private List testGroups = new ArrayList<>();

    /** Optional env parameters */
    private Map env = new HashMap<>();

    /** Optional system properties */
    private Map systemProperties = new HashMap<>();

    /**
     * Checks if this suite actions should execute according to suite name and included test groups.
     * @param suiteName
     * @param includedGroups
     * @return
     */
    public boolean shouldExecute(String suiteName, String[] includedGroups) {
        String baseErrorMessage = "Skip before/after suite container because of %s restriction - do not execute container '%s'";

        if (StringUtils.hasText(suiteName) &&
                !CollectionUtils.isEmpty(suiteNames) && ! suiteNames.contains(suiteName)) {
            log.warn(String.format(baseErrorMessage, "suite name", getName()));
            return false;
        }

        if (!checkTestGroups(includedGroups)) {
            log.warn(String.format(baseErrorMessage, "test groups", getName()));
            return false;
        }

        for (Map.Entry envEntry : env.entrySet()) {
            if (!System.getenv().containsKey(envEntry.getKey()) ||
                    (StringUtils.hasText(envEntry.getValue()) && !System.getenv().get(envEntry.getKey()).equals(envEntry.getValue()))) {
                log.warn(String.format(baseErrorMessage, "env properties", getName()));
                return false;
            }
        }

        for (Map.Entry systemProperty : systemProperties.entrySet()) {
            if (!System.getProperties().containsKey(systemProperty.getKey()) ||
                    (StringUtils.hasText(systemProperty.getValue()) && !System.getProperties().get(systemProperty.getKey()).equals(systemProperty.getValue()))) {
                log.warn(String.format(baseErrorMessage, "system properties", getName()));
                return false;
            }
        }

        return true;
    }

    /**
     * Checks on included test groups if we should execute sequence. Included group list should have
     * at least one entry matching the sequence test groups restriction.
     *
     * @param includedGroups
     * @return
     */
    private boolean checkTestGroups(String[] includedGroups) {
        if (testGroups.isEmpty()) {
            return true;
        }

        if (includedGroups != null) {
            for (String includedGroup : includedGroups) {
                if (testGroups.contains(includedGroup)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Gets the test groups that restrict the container execution.
     * @return
     */
    public List getTestGroups() {
        return testGroups;
    }

    /**
     * Sets the test groups that restrict the container execution.
     * @param testGroups
     */
    public void setTestGroups(List testGroups) {
        this.testGroups = testGroups;
    }

    /**
     * Gets the suite names that restrict the container execution.
     * @return
     */
    public List getSuiteNames() {
        return suiteNames;
    }

    /**
     * Sets the suite names that restrict the container execution.
     * @param suiteNames
     */
    public void setSuiteNames(List suiteNames) {
        this.suiteNames = suiteNames;
    }

    /**
     * Gets the env.
     *
     * @return
     */
    public Map getEnv() {
        return env;
    }

    /**
     * Sets the env.
     *
     * @param env
     */
    public void setEnv(Map env) {
        this.env = env;
    }

    /**
     * Gets the systemProperties.
     *
     * @return
     */
    public Map getSystemProperties() {
        return systemProperties;
    }

    /**
     * Sets the systemProperties.
     *
     * @param systemProperties
     */
    public void setSystemProperties(Map systemProperties) {
        this.systemProperties = systemProperties;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy