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

com.blazemeter.taurus.junit4.JUnit4Runner Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package com.blazemeter.taurus.junit4;

import com.blazemeter.taurus.junit.Reporter;
import com.blazemeter.taurus.junit.JUnitRunner;
import com.blazemeter.taurus.junit.ThreadCounter;
import org.junit.experimental.categories.ExcludeCategories;
import org.junit.experimental.categories.IncludeCategories;
import org.junit.runner.JUnitCore;
import org.junit.runner.JUnitRequest;
import org.junit.runner.Request;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import static com.blazemeter.taurus.junit.CustomRunner.EXCLUDE_CATEGORY;
import static com.blazemeter.taurus.junit.CustomRunner.INCLUDE_CATEGORY;
import static com.blazemeter.taurus.junit.CustomRunner.RUN_ITEMS;

public class JUnit4Runner implements JUnitRunner {
    private static final Logger log = Logger.getLogger(JUnit4Runner.class.getName());

    @Override
    public Request createRequest(List classes, Properties props) {
        String runItems = props.getProperty(RUN_ITEMS);
        if (runItems != null) {
            log.info("Create JUnit 4 request with following items: " + runItems);
            return JUnitRequest.createItemsRequest(runItems);
        } else {
            String[] junitArguments = generateArgs(classes, props);
            log.info("Create JUnit 4 request with following arguments: " + Arrays.toString(junitArguments));
            return JUnitRequest.createCategoryRequest(junitArguments);
        }
    }

    @Override
    public void executeRequest(Object requestItem, Reporter reporter, ThreadCounter counter) {
        JUnit4Listener listener = new JUnit4Listener(reporter, counter);
        JUnitCore runner = new JUnitCore();
        runner.addListener(listener);
        runner.run((Request) requestItem);
    }

    private static String[] generateArgs(List classes, Properties props) {
        List args = new ArrayList<>();

        // Category options should be first!!!
        if (null != props.getProperty(INCLUDE_CATEGORY)) {
            addFilter(args, IncludeCategories.class.getName(), props.getProperty(INCLUDE_CATEGORY));
        }

        if (null != props.getProperty(EXCLUDE_CATEGORY)) {
            addFilter(args, ExcludeCategories.class.getName(), props.getProperty(EXCLUDE_CATEGORY));
        }

        for (Class c : classes) {
            args.add(c.getName());
        }

        return args.toArray(new String[0]);
    }

    private static void addFilter(List args, String name, String property) {
        args.add("--filter=" + name + '=' + property);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy