// Go through all the parameters declared on this Data Provider and
// make sure we have at most one Method and one ITestContext.
// Anything else is an error
Class[] parameterTypes = dataProvider.getParameterTypes();
if (parameterTypes.length > 2) {
throw new TestNGException("DataProvider " + dataProvider + " cannot have more than two parameters");
}
for (Class cls : parameterTypes) {
if (cls.equals(Method.class)) {
lParameters.add(testMethod);
}
else if (cls.equals(ITestContext.class)) {
lParameters.add(testContext);
}
}
Object[] parameters = lParameters.toArray(new Object[lParameters.size()]);
// if (parameterTypes.length > 0 && parameterTypes[0].equals(Method.class)) {
// parameters = new Object[] {
// testMethod
// };
// }
// else if (parameterTypes.length > 0) {
// throw new TestNGException("DataProvider " + dataProvider + " needs to have "
// + " either zero parameters or one parameter of type java.lang.reflect.Method");
// }
Class< ? > returnType = dataProvider.getReturnType();
if (Object[][].class.isAssignableFrom(returnType)) {
Object[][] oResult = (Object[][]) MethodHelper.invokeMethod(
dataProvider, instance, parameters);
method.setParameterInvocationCount(oResult.length);
result = MethodHelper.createArrayIterator(oResult);
}
else if (Iterator.class.isAssignableFrom(returnType)) {
// Already an Iterable<Object[]>, assign it directly
result = (Iterator<Object[]>) MethodHelper.invokeMethod(dataProvider,
instance, parameters);
}
else {
throw new TestNGException("Data Provider " + dataProvider
+ " must return" + " either Object[][] or Iterator<Object>[], not "
+ returnType);
}
}
catch (InvocationTargetException e) {
throw new TestNGException(e);
}
catch (IllegalAccessException e) {
throw new TestNGException(e);
}
return result;
}