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

net.serenity.test.utils.rules.TestCase Maven / Gradle / Ivy

package net.serenity.test.utils.rules;

import net.thucydides.core.steps.StepEventBus;
import net.thucydides.core.steps.StepListener;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * User: YamStranger
 * Date: 3/3/16
 * Time: 11:54 AM
 */
public class TestCase implements MethodRule {

    private List listeners;
    private boolean finished;
    private String name;

    public TestCase(T... listener) {
        this(UUID.randomUUID().toString(), listener);
    }

    public TestCase(final String name, final T... listener) {
        this.listeners = new ArrayList<>(listener.length);
        register(listener);
        for (T regiter : listener) {
            this.listeners.add(regiter);
        }
        this.name = name;
    }


    public Collection listeners() {
        return new ArrayList(this.listeners);
    }

    public void register(final T... listener) {
        for (T regiter : listener) {
            this.listeners.add(regiter);
            StepEventBus.getEventBus().registerListener(regiter);
            regiter.testStarted(name);
        }
    }

    public T firstListener() {
        return this.listeners.get(0);
    }

    public TestCase finish() {
        if (!finished) {
            StepEventBus.getEventBus().testFinished();
            this.finished = true;
        }
        return this;
    }

    public String name() {
        return this.name;
    }

    @Override
    public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, Object o) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    for (T regiter : listeners) {
                        StepEventBus.getEventBus().registerListener(regiter);
                    }
                    StepEventBus.getEventBus().testStarted(name);

                    statement.evaluate();
                } finally {
                    finish();
                }
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy