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

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

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

import holmos.webtest.basetools.file.MyString;
import holmos.webtest.exceptions.HolmosFailedError;
import holmos.webtest.junitextentions.threadpool.HolmosThreadPoolExecutor;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Test.None;
import org.junit.internal.AssumptionViolatedException;
import org.junit.internal.runners.model.EachTestNotifier;
import org.junit.internal.runners.statements.ExpectException;
import org.junit.internal.runners.statements.FailOnTimeout;
import org.junit.internal.runners.statements.InvokeMethod;
import org.junit.internal.runners.statements.RunAfters;
import org.junit.internal.runners.statements.RunBefores;
import org.junit.rules.RunRules;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
/**
 * 针对含有@RunCount注解的测试方法,生成此对象
 * 
 * @author 吴银龙([email protected])
 * */
public class RunCountFrameWorkMethod{
	/**种子方法*/
	private FrameworkMethod method;
	/**用来表示这是该测试方法的第?次运行*/
	private int index;
	/**执行这么多组方法的线程池大小*/
	private int parameterThreadpoolSize;
	/**执行方法*/
	private ArrayListparameterFrameWorkMethods;
	/**参数列表*/
	private ArrayListparameters;
	
	public RunCountFrameWorkMethod(FrameworkMethod method,int index,ArrayListparameters,int parameterThreadpoolSize){
		this.method=method;this.index=index;this.parameters=parameters;
		this.parameterThreadpoolSize=parameterThreadpoolSize;
	}
	/**获取单次运行中所要运行的带参数方法列表*/
	public ArrayListgetParameterFrameWorkMethods(){
		if(null==parameterFrameWorkMethods){
			this.parameters=convertParametersType();
			parameterFrameWorkMethods=new ArrayList();
			if(parameters==null||parameters.size()==0)
				parameterFrameWorkMethods.add(new ParameterFrameWorkMethod(method.getMethod(), null, index));
			else{
				for(Object[]parameter:parameters)
					parameterFrameWorkMethods.add(new ParameterFrameWorkMethod(method.getMethod(), parameter, index));
			}
		}return parameterFrameWorkMethods;
	}
	/**按照参数类型进行参数类型转换*/
	private ArrayListconvertParametersType(){
		if(null==parameters)return null;
		ArrayListmyParameters=new ArrayList();
		Class[] types=method.getMethod().getParameterTypes();
		for(Object[] parameter:parameters){
			myParameters.add(getActualParameter(types, parameter));
		}
		return myParameters;
	}
	private Object[] getActualParameter(Class[]types,Object[]parameter){
		if(types.length!=parameter.length)
			throw new HolmosFailedError("测试方法   "+method.getName()+"()  中的参数个数和参数源文件中的参数的个数不一致!");
		Object[]myParameter=new Object[types.length];
		for(int i=0;itype,Object obj){
		MyString typeStr=new MyString(type.getName());
		if(null==obj){
			throw new HolmosFailedError("参数值为null!无法完成转换!");
		}
		if(typeStr.includeOr("java.lang.String")){
			return obj;
		}
		if(typeStr.includeOr("java.lang.Byte","byte")){
			return Byte.parseByte((String)obj);
		}else if(typeStr.includeOr("java.lang.Integer","int")){
			return Integer.parseInt((String)obj);
		}else if(typeStr.includeOr("java.lang.Character","char")){
			if(((String)obj).length()>1)
				throw new HolmosFailedError("字符窜长度大于1,无法转换成为字符!");
			return ((String)obj).charAt(0);
		}else if(typeStr.includeOr("java.lang.Double","double")){
			return Double.parseDouble((String)obj);
		}else if(typeStr.includeOr("java.lang.Float","float")){
			return Float.parseFloat((String)obj);
		}else if(typeStr.includeOr("java.lang.Long","long")){
			return Long.parseLong((String)obj);
		}else{
			return type.cast(obj);
		}
	}
	public int getParameterThreadpoolSize() {
		return parameterThreadpoolSize;
	}
	/**执行一次运行下的所有参数方法*/
	public void runParameterMethod(Object target,RunNotifier notifier){
		try {
			if(parameters==null||parameters.size()==0){//无参方法,无需多线程处理多组参数
				new ParameterMethodRunner(getParameterFrameWorkMethods().get(0), target, notifier).run();
			}else{//需要线程池
				HolmosThreadPoolExecutor executor=(HolmosThreadPoolExecutor)new HolmosThreadPoolExecutor(parameterThreadpoolSize,parameters.size());
				for(int i=0;i 0 ? new FailOnTimeout(next, timeout) : next;
		}

		/**
		 * Returns a {@link Statement}: run all non-overridden {@code @Before}
		 * methods on this class and superclasses before running {@code next}; if
		 * any throws an Exception, stop execution and pass the exception on.
		 * 
		 * @deprecated Will be private soon: use Rules instead
		 */
		@Deprecated
		protected Statement withBefores(FrameworkMethod method, Object target,
				Statement statement) {
			List befores= getTestClass().getAnnotatedMethods(
					Before.class);
			return befores.isEmpty() ? statement : new RunBefores(statement,
					befores, target);
		}

		private TestClass getTestClass() {
			return new TestClass(target.getClass());
		}
		/**
		 * Returns a {@link Statement}: run all non-overridden {@code @After}
		 * methods on this class and superclasses before running {@code next}; all
		 * After methods are always executed: exceptions thrown by previous steps
		 * are combined, if necessary, with exceptions from After methods into a
		 * {@link MultipleFailureException}.
		 * 
		 * @deprecated Will be private soon: use Rules instead
		 */
		@Deprecated
		protected Statement withAfters(FrameworkMethod method, Object target,
				Statement statement) {
			List afters= getTestClass().getAnnotatedMethods(
					After.class);
			return afters.isEmpty() ? statement : new RunAfters(statement, afters,
					target);
		}

		private Statement withRules(FrameworkMethod method, Object target,
				Statement statement) {
			Statement result= statement;
			result= withMethodRules(method, target, result);
			result= withTestRules(method, target, result);
			return result;
		}

		@SuppressWarnings("deprecation")
		private Statement withMethodRules(FrameworkMethod method, Object target,
				Statement result) {
			List testRules= getTestRules(target);
			for (org.junit.rules.MethodRule each : getMethodRules(target))
				if (! testRules.contains(each))
					result= each.apply(result, method, target);
			return result;
		}

		@SuppressWarnings("deprecation")
		private List getMethodRules(Object target) {
			return rules(target);
		}

		/**
		 * @param target
		 *            the test case instance
		 * @return a list of MethodRules that should be applied when executing this
		 *         test
		 * @deprecated {@link org.junit.rules.MethodRule} is a deprecated interface. Port to
		 *             {@link TestRule} and
		 *             {@link BlockJUnit4ClassRunner#getTestRules(Object)}
		 */
		@Deprecated
		protected List rules(Object target) {
			return getTestClass().getAnnotatedFieldValues(target, Rule.class,
					org.junit.rules.MethodRule.class);
		}

		/**
		 * Returns a {@link Statement}: apply all non-static {link Value} fields
		 * annotated with {@link Rule}.
		 *
		 * @param statement The base statement
		 * @return a RunRules statement if any class-level {@link Rule}s are
		 *         found, or the base statement
		 */
		private Statement withTestRules(FrameworkMethod method, Object target,
				Statement statement) {
			List testRules= getTestRules(target);
			return testRules.isEmpty() ? statement :
				new RunRules(statement, testRules, describeChild(method));
		}

		private Description describeChild(FrameworkMethod method) {
			return Description.createTestDescription(getTestClass().getJavaClass(),
					testName(method), method.getAnnotations());
		}
		
		protected String testName(FrameworkMethod method) {
			if(method instanceof ParameterFrameWorkMethod)
				return method.toString();
			return method.getName();
		}
		
		/**
		 * @param target
		 *            the test case instance
		 * @return a list of TestRules that should be applied when executing this
		 *         test
		 */
		protected List getTestRules(Object target) {
			return getTestClass().getAnnotatedFieldValues(target,
					Rule.class, TestRule.class);
		}

		private Class getExpectedException(Test annotation) {
			if (annotation == null || annotation.expected() == None.class)
				return null;
			else
				return annotation.expected();
		}

		private boolean expectsException(Test annotation) {
			return getExpectedException(annotation) != null;
		}

		private long getTimeout(Test annotation) {
			if (annotation == null)
				return 0;
			return annotation.timeout();
		}
		private final void runLeaf(Statement statement, Description description,
				RunNotifier notifier) {
			EachTestNotifier eachNotifier= new EachTestNotifier(notifier, description);
			eachNotifier.fireTestStarted();
			try {
			    statement.evaluate();
			} catch (AssumptionViolatedException e) {
				eachNotifier.addFailedAssumption(e);
			} catch (Throwable e) {
				eachNotifier.addFailure(e);
			} finally {
				eachNotifier.fireTestFinished();
			}
		}
		@Override
		public void run() {
			try {
				if(parameterFrameWorkMethod.getAnnotation(Ignore.class) != null){
					notifier.fireTestIgnored(description);
				}else
					runLeaf(methodBlock(), description, notifier);
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy