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

org.test4j.module.spec.internal.ScenarioResult Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package org.test4j.module.spec.internal;


import lombok.Getter;
import lombok.Setter;
import org.test4j.functions.ReturnExecutor;
import org.test4j.functions.SExecutor;
import org.test4j.tools.commons.StringHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * ScenarioResult
 *
 * @author wudarui
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class ScenarioResult implements Serializable {
    private static final List Filter_List = new ArrayList();

    static {
        addFilter("*.lambda.");
        addFilter("java.util.");
        addFilter("org.test4j.");
        addFilter("mockit.integration.");
        addFilter("org.springframework.");
        addFilter("*\\$\\$EnhancerByCGLIB\\$\\$*");

    }

    @Setter
    @Getter
    private String scenarioName;

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

    @Getter
    private StepResult lastStep = null;

    @Getter
    @Setter
    private Throwable exception;

    public ScenarioResult(String scenarioName) {
        this.scenarioName = scenarioName;
    }

    private static void addFilter(String filter) {
        Filter_List.add("\\s*at\\s+" +
            filter.replaceAll("\\.", "\\\\.").replaceAll("\\*", "\\.\\*") +
            "[^\\)]*\\)"
        );
    }

    /**
     * 执行lambda表达式
     *
     * @param type        type
     * @param description description
     * @param lambda      lambda
     * @param eKlass      期望抛出的异常类型
     */
    public void doStep(StepType type, String description, ReturnExecutor lambda, Class eKlass) throws Throwable {
        StepResult stepResult = this.addResult(type, description);
        try {
            if (type == StepType.When) {
                TableDataAround.ready(stepResult);
            }
            Object whenResult = lambda.doIt();
            SpecContext.setWhenResult(whenResult);
            if (eKlass != null) {
                throw new AssertionError("not found expected exception: " + eKlass.getName());
            }
            if (type == StepType.When) {
                TableDataAround.check(stepResult);
            }
        } catch (Throwable e) {
            if (eKlass != null && !(e instanceof AssertionError)) {
                SpecContext.setExpectedException(e);
            } else {
                stepResult.setError(e);
                throw e;
            }
        } finally {
            TableDataAround.remove();
        }
    }

    /**
     * 执行lambda表达式
     *
     * @param type   type
     * @param lambda lambda
     * @param eKlass 期望抛出的异常类型
     */
    public void doStep(StepType type, ReturnExecutor lambda, Class eKlass) throws Throwable {
        this.doStep(type, null, lambda, eKlass);
    }

    public void doStep(StepType type, String description, SExecutor lambda, Class eKlass) throws Throwable {
        this.doStep(type, description, ReturnExecutor.wrap(lambda), eKlass);
    }

    public void doStep(StepType type, SExecutor lambda, Class eKlass) throws Throwable {
        this.doStep(type, null, ReturnExecutor.wrap(lambda), eKlass);
    }

    private StepResult addResult(StepType type, String description) {
        this.lastStep = new StepResult(this, type, description);
        this.steps.add(this.lastStep);
        return this.lastStep;
    }

    public boolean isFailure() {
        return this.exception != null;
    }

    /**
     * 输出测试结果
     */
    public String scenarioResult() {
        StringBuilder buff = new StringBuilder();
        buff.append("\n\n")
            .append("=========Scenario: ").append(this.scenarioName).append(" =============\n");
        steps.forEach(stepResult -> buff.append(stepResult.toString()).append("\n"));
        if (exception != null) {
            buff.append(StringHelper.toString(this.exception, Filter_List));
        }
        return buff.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy