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

com.tascape.qa.th.suite.AbstractSuite Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
/*
 * Copyright 2015.
 *
 * 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.tascape.qa.th.suite;

import com.tascape.qa.th.SystemConfiguration;
import com.tascape.qa.th.TestHarness;
import com.tascape.qa.th.driver.EntityDriver;
import com.tascape.qa.th.driver.TestDriver;
import com.tascape.qa.th.driver.PoolableEntityDriver;
import com.tascape.qa.th.test.AbstractTest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author linsong wang
 */
public abstract class AbstractSuite {
    private static final Logger LOG = LoggerFactory.getLogger(AbstractSuite.class);

    private static final ThreadLocal>> ENVIRONMENTS
        = new ThreadLocal>>() {
            @Override
            protected Map> initialValue() {
                return new HashMap<>();
            }
        };

    public static void putEnvionment(String suiteClass, Map drivers) {
        ENVIRONMENTS.get().put(suiteClass, drivers);
    }

    public static Map getEnvionment(String suiteClass) {
        Map drivers = ENVIRONMENTS.get().get(suiteClass);
        return drivers;
    }

    private static final Set SUITES = new HashSet<>();

    private final List> testClasses = new ArrayList<>();

    protected Map suiteEnvironment = new HashMap<>();

    protected final SystemConfiguration SYSCONFIG = SystemConfiguration.getInstance();

    public static void addSuite(AbstractSuite suite) {
        SUITES.add(suite);
    }

    public static Set getSuites() {
        return SUITES;
    }

    public void setUp() throws Exception {
        Map env = AbstractSuite.getEnvionment(this.getClass().getName());
        if (env == null || env.isEmpty()) {
            this.setUpEnvironment();
            AbstractSuite.putEnvionment(this.getClass().getName(), this.suiteEnvironment);
        }
    }

    public void runByClass() throws Exception {
        for (Class clazz : this.testClasses) {
            JUnitCore core = new JUnitCore();
            core.run(Request.classWithoutSuiteMethod(clazz));
        }
    }

    public void tearDown() throws Exception {
        this.tearDownEnvironment();
    }

    public List> getTestClasses() {
        return testClasses;
    }

    protected void putTestDirver(TestDriver testDriver, EntityDriver driver) {
        String key = testDriver.toString();
        LOG.debug("Putting runtime driver {}={} into suite test environment", key, driver);
        Class clazz = testDriver.getDriverClass();
        if (clazz != null && !clazz.isInstance(driver)) {
            throw new RuntimeException("wrong driver type, " + key + " vs " + driver);
        }

        EntityDriver d = this.suiteEnvironment.get(key);
        if (d == null) {
            this.suiteEnvironment.put(key, driver);
            return;
        }
        if (driver.equals(d)) {
            LOG.warn("Tried to add the same driver again: {}={}", key, driver);
            return;
        }
        if (d instanceof PoolableEntityDriver && driver instanceof PoolableEntityDriver) {
            PoolableEntityDriver.class.cast(d).next(PoolableEntityDriver.class.cast(driver));
            return;
        }
        throw new UnsupportedOperationException("Cannot add non-poolable driver with the same key " + key);
    }

    protected  void addTestClass(Class clazz) {
        if (testClasses.contains(clazz)) {
            throw new UnsupportedOperationException("Adding same test class multiple times is not supported yet.");
        }
        this.testClasses.add(clazz);
    }

    protected String getSuiteProperty(String name, String defValue) {
        String value = this.SYSCONFIG.getProperty(name);
        if (value == null) {
            value = defValue;
        }
        return value;
    }

    public String getName() {
        return this.getClass().getName();
    }

    public abstract void setUpTestClasses();

    protected abstract void setUpEnvironment() throws Exception;

    protected abstract void tearDownEnvironment();

    /**
     * This is used to launch TestHarness from within individual test suite classes.
     *
     * @param args command arguments
     *
     * @throws Exception any issue
     */
    public static void main(String[] args) throws Exception {
        SystemConfiguration sysConfig = SystemConfiguration.getInstance();
        Field fClasses = ClassLoader.class.getDeclaredField("classes");
        ClassLoader cl = AbstractSuite.class.getClassLoader();
        fClasses.setAccessible(true);

        @SuppressWarnings("unchecked")
        List> classes = (List>) fClasses.get(cl);
        String suiteClassName = "";
        String stop = AbstractSuite.class.getName() + "$1";
        for (Class c : classes) {
            String className = c.getName();
            if (className.equals(stop)) {
                break;
            }
            suiteClassName = className;
        }
        sysConfig.setTestSuite(suiteClassName);
        TestHarness.main(args);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy