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

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

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

import java.util.HashMap;
import java.util.Map;

/**
 * story测试上下文期望
 *
 * @author darui.wu 2019-10-15
 */
@SuppressWarnings("unused")
public class SpecContext {
    public final static String VALUE_DEFAULT_KEY = "_default_";
    /**
     * 测试各步骤中的共享数据
     */
    private static final ThreadLocal> sharedData = new ThreadLocal<>();
    /**
     * when步骤执行返回值
     */
    private static final ThreadLocal whenResult = new ThreadLocal<>();
    /**
     * when步骤中的异常
     */
    private static final ThreadLocal expectedException = new ThreadLocal<>();

    /**
     * 设置story测试步骤共享数据
     *
     * @param data data value
     */
    public static void setSharedData(String key, Object data) {
        if (sharedData.get() == null) {
            sharedData.set(new HashMap<>(8));
        }
        sharedData.get().put(key == null ? VALUE_DEFAULT_KEY : key, data);
    }

    /**
     * 返回共享数据
     *
     * @return ignore
     */
    public static Object getSharedData(String key) {
        return sharedData.get() == null ? null : sharedData.get().get(key == null ? VALUE_DEFAULT_KEY : key);
    }

    /**
     * 设置期望异常
     *
     * @param e Exception
     */
    public static void setExpectedException(Throwable e) {
        expectedException.set(e);
    }

    /**
     * 返回期望异常
     *
     * @return ignore
     */
    public static Throwable getExpectedException() {
        return expectedException.get();
    }

    public static void setWhenResult(Object result) {
        whenResult.set(result);
    }

    public static Object getWhenResult() {
        return whenResult.get();
    }

    public static void clean() {
        expectedException.remove();
        sharedData.remove();
        whenResult.remove();
    }
}