org.test4j.integration.junit4.helper.DataFromHelper Maven / Gradle / Ivy
package org.test4j.integration.junit4.helper;
import org.junit.runners.model.FrameworkMethod;
import org.test4j.integration.junit4.DataFrom;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@SuppressWarnings({"rawtypes", "unchecked"})
public class DataFromHelper {
/**
* 构造一系列有参的测试方法
*
* @param testClazz 当前测试类class
* @param testMethod 测试方法
* @param dataFrom DataFrom
* @return ignore
*/
public static List computeParameterizedTestMethods(Class testClazz,
Method testMethod,
DataFrom dataFrom) {
String fromMethod = dataFrom.value();
if ("".equals(fromMethod)) {
throw new RuntimeException("You should specify the value property of @DataFrom() item.");
}
Class dataFromClazz = dataFrom.clazz();
if (dataFromClazz == DataFrom.class) {
dataFromClazz = testMethod.getDeclaringClass();
}
return computeParameterizedFromDataProviderMethod(testClazz, testMethod, fromMethod, dataFromClazz);
}
private static List computeParameterizedFromDataProviderMethod(Class testClazz,
Method testMethod,
String dataFromMethod,
Class dataFromClaz) {
Object data = getDataFromMethod(dataFromMethod, testClazz, dataFromClaz);
if (data instanceof Iterator) {
return computeParameterFromIterator(testMethod, (Iterator) data);
} else if (data instanceof Object[][]) {
return computeParameterFromArray(testMethod, (Object[][]) data);
} else {
throw new RuntimeException(
"The @DataFrom method can only return value of type Iterator