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

holmos.webtest.junitextentions.parameters.HolmosFrameWorkMethod Maven / Gradle / Ivy

There is a newer version: 1.0.2u10
Show newest version
package holmos.webtest.junitextentions.parameters;

import holmos.webtest.basetools.HolmosAnnotationTool;
import holmos.webtest.exceptions.HolmosFailedError;
import holmos.webtest.junitextentions.HolmosRunner;
import holmos.webtest.junitextentions.annotations.MultiThread;
import holmos.webtest.junitextentions.annotations.Parameter;
import holmos.webtest.junitextentions.annotations.RunAfter;
import holmos.webtest.junitextentions.annotations.RunCount;
import holmos.webtest.junitextentions.excute.ExcuteRunnable;
import holmos.webtest.junitextentions.excute.ExcuteThread;
import holmos.webtest.log.MyLogger;

import java.util.ArrayList;

import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.FrameworkMethod;

/**
 * 以{@link HolmosFrameWorkMethod}类为单位进行组织测试方法,用来代表是否有多线程,执行顺序,执行次数
 * 
 * @author 吴银龙([email protected])
 * */
public class HolmosFrameWorkMethod{
	private static MyLogger logger=MyLogger.getLogger(HolmosFrameWorkMethod.class);
	/**参数获取器*/
	private ParameterDataInfo dataGetter;
	/**是否需要多线程执行方法*/
	private boolean isMultiThread=false;
	public boolean isMultiThread() {
		return isMultiThread;
	}
	/**表示执行的次数*/
	private int runcount=1;
	/**表示执行这么多次给出的线程池的大小*/
	private int runcountThreadpoolSize=1;
	/**用来指示此方法是否被执行过*/
	private boolean isExecuted;
	public int getRuncountThreadpoolSize() {
		return runcountThreadpoolSize;
	}
	/**所有的参数组列表*/
	private ArrayListparameters;
	/**执行所有参数组所需要的线程池大小*/
	private int parameterThreadpoolSize=1;
	/**在多次运行的时候用此方法列表*/
	private ArrayListrunCountFrameWorkMethods;
	/**可执行方法列表,递归调用RunCountFrameWorkMethod的coumputeMethod方法*/
	private ArrayListexcuteFrameWorkMethods;
	/**种子方法,junit原生方法类型*/
	private FrameworkMethod method;
	/**运行时候的后继函数,即在有{@link RunAfter}标签的时候才会用到这个列表*/
	private ArrayList successorMethods;
	/**前驱函数的个数,只有当此数位0的时候才能开始执行当前方法*/
	private volatile int precursorMethodCount;
	
	private Object target;
	private RunNotifier notifier;
	
	public void setTarget(Object target) {
		this.target = target;
	}
	public void setNotifier(RunNotifier notifier) {
		this.notifier = notifier;
	}
	public FrameworkMethod getMethod() {
		return method;
	}
	public HolmosFrameWorkMethod(FrameworkMethod method){
		this.method=method;
		isMultiThread=HolmosAnnotationTool.hasClassOrMethodLevelAnnotation(HolmosRunner.getTestClassForGetter(), method.getMethod(), MultiThread.class);
		initRunCountInfo();
		initParameterInfo();
		this.successorMethods=new ArrayList();
		this.precursorMethodCount=0;
		this.isExecuted=false;
	}
	public void setGetter(ParameterDataInfo dataGetter){
		this.dataGetter=dataGetter;
	}
	public ArrayListgetRunCountFrameWorkMethods(){
		if(null==runCountFrameWorkMethods){
			runCountFrameWorkMethods=new ArrayList();
			for(int index=0;indexgetExcuteFrameWorkMethods(){
		if(null==excuteFrameWorkMethods){
			excuteFrameWorkMethods=new ArrayList();
			for(RunCountFrameWorkMethod runCountFrameWorkMethod:getRunCountFrameWorkMethods()){
				excuteFrameWorkMethods.addAll(runCountFrameWorkMethod.getParameterFrameWorkMethods());
			}
		}return excuteFrameWorkMethods;
	}
	/**初始化方法的参数信息*/
	private void initParameterInfo() {
		Parameter parameter=method.getAnnotation(Parameter.class);
		if(null==parameter){//对于没有参数的方法而言
			parameters=null;
			return;
		}
		if(parameter.value()==null){//配置了参数,但是没有配置参数的来源,这个框架判定为错误
			throw new HolmosFailedError(method.getName()+"参数没有配置!请检查!");
		}
		dataGetter=HolmosRunner.sources.get(parameter.sourceID());
		if(null==dataGetter)
			throw new HolmosFailedError("没有配置sourceID为"+parameter.sourceID()+"的参数源信息!出错方法为:"+method.getName());
		parameters=dataGetter.getParameters(parameter.value());
		parameterThreadpoolSize=parameter.threadPoolSize();
	}
	/**初始化{@link RunCount}注解的信息*/
	private void initRunCountInfo(){
		RunCount count=method.getAnnotation(RunCount.class);
		if(count!=null){
			this.runcount=count.runCount()<=0?1:count.runCount();
			runcountThreadpoolSize=count.threadPoolSize()<=0?1:count.threadPoolSize()>
				runcount?runcount:count.threadPoolSize();
		}
	}
	/**
	 * 当此方法执行完毕的时候,将会告知所有的后继方法,各个后继方法的前驱个数-1
	 * @throws Throwable 
	 * */
	public void notifySuccessorMethods(){
		for(HolmosFrameWorkMethod method:successorMethods){
			method.precursorMethodCount--;
			if(0==method.precursorMethodCount)
				try {
					method.invokeExplosively(target, notifier);
				} catch (Throwable e) {
					e.printStackTrace();
				}
		}
	}
	public void addSuccessorMethods(HolmosFrameWorkMethod method){
		this.successorMethods.add(method);
		method.precursorMethodCount++;
	}
	/**
	 * 运行此HolmosFrameWorkMethod,这只是一个组织单位,运行命令的发出者
	 * */
	public void invokeExplosively(Object target,RunNotifier notifier) throws Throwable {
		if(!isExecuted){
			if(isMultiThread){
				//如果是多线程运行此方法,那么为该方法的运行单独开一个线程
				new ExcuteThread(this, target,parameters,notifier).start();
			}else{
				//否则这个方法运行就在其所在线程上运行
				new ExcuteRunnable(this, target,parameters,notifier).run();
			}
		}
	}
	public int getRuncount() {
		return runcount;
	}
	public boolean isExecuted() {
		return isExecuted;
	}
	public void setExecuted(){
		this.isExecuted=true;
	}
	public boolean hasPrecursorMethod(){
		return this.precursorMethodCount==0?false:true;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy