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

cucumber.runtime.formatter.RerunFormatter Maven / Gradle / Ivy

There is a newer version: 7.18.1
Show newest version
package cucumber.runtime.formatter;

import cucumber.api.TestCase;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseFinished;
import cucumber.api.event.TestRunFinished;
import cucumber.api.formatter.Formatter;
import cucumber.api.formatter.NiceAppendable;
import cucumber.api.formatter.StrictAware;

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


/**
 * Formatter for reporting all failed test cases and print their locations
 * Failed means: results that make the exit code non-zero.
 */
final class RerunFormatter implements Formatter, StrictAware {
    private final NiceAppendable out;
    private Map> featureAndFailedLinesMapping = new HashMap>();
    private boolean isStrict = false;

    private EventHandler testCaseFinishedHandler = new EventHandler() {
        @Override
        public void receive(TestCaseFinished event) {
            handleTestCaseFinished(event);
        }
    };
    private EventHandler runFinishHandler = new EventHandler() {
        @Override
        public void receive(TestRunFinished event) {
            handleTestRunFinished();
        }
    };

    @SuppressWarnings("WeakerAccess") // Used by PluginFactory
    public RerunFormatter(Appendable out) {
        this.out = new NiceAppendable(out);
    }

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestCaseFinished.class, testCaseFinishedHandler);
        publisher.registerHandlerFor(TestRunFinished.class, runFinishHandler);
    }

    @Override
    public void setStrict(boolean strict) {
        isStrict = strict;
    }

    private void handleTestCaseFinished(TestCaseFinished event) {
        if (!event.result.isOk(isStrict)) {
            recordTestFailed(event.testCase);
        }
    }

    private void handleTestRunFinished() {
        reportFailedTestCases();
        out.close();
    }

    private void recordTestFailed(TestCase testCase) {
        String path = testCase.getUri();
        ArrayList failedTestCases = this.featureAndFailedLinesMapping.get(path);
        if (failedTestCases == null) {
            failedTestCases = new ArrayList();
            this.featureAndFailedLinesMapping.put(path, failedTestCases);
        }

        failedTestCases.add(testCase.getLine());
    }

    private void reportFailedTestCases() {
        Set>> entries = featureAndFailedLinesMapping.entrySet();
        for (Map.Entry> entry : entries) {
            if (!entry.getValue().isEmpty()) {
                out.append(entry.getKey());
                for (Integer line : entry.getValue()) {
                    out.append(":").append(line.toString());
                }
                out.println();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy