Skip to content

Commit

Permalink
Fix InvokeParameterisedMethod in handling varargs
Browse files Browse the repository at this point in the history
  • Loading branch information
justhalf committed Sep 30, 2014
1 parent 23cc896 commit 4874ae9
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/junitparams/internal/InvokeParameterisedMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private Object[] castParamsFromObjects(Object params) throws ConversionFailedExc
} catch (ConversionFailedException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
Class<?>[] typesOfParameters = createArrayOfTypesOf(paramset);
Object resultParam = createObjectOfExpectedTypeBasedOnParams(paramset, typesOfParameters);
return new Object[]{resultParam};
Expand Down Expand Up @@ -95,7 +96,17 @@ private Class<?>[] createArrayOfTypesOf(Object[] paramset) {
private Object[] castParamsUsingConverters(Object[] columns) throws ConversionFailedException {
Class<?>[] expectedParameterTypes = testMethod.getMethod().getParameterTypes();
if (testMethodParamsAreVarargs(columns, expectedParameterTypes)) {
columns = new Object[] {columns};
Class<?> varArgType = expectedParameterTypes[expectedParameterTypes.length-1].getComponentType();
Object[] varArgs = (Object[])Array.newInstance(varArgType, columns.length - expectedParameterTypes.length);
for(int i=0; i<varArgs.length; i++){
varArgs[i] = columns[i+expectedParameterTypes.length];
}
Object[] tmp = new Object[expectedParameterTypes.length];
for(int i=0; i<tmp.length-1; i++){
tmp[i] = columns[i];
}
tmp[tmp.length-1] = varArgs;
columns = tmp;
}

Annotation[][] parameterAnnotations = testMethod.getMethod().getParameterAnnotations();
Expand All @@ -105,7 +116,8 @@ private Object[] castParamsUsingConverters(Object[] columns) throws ConversionFa
}

private boolean testMethodParamsAreVarargs(Object[] columns, Class<?>[] expectedParameterTypes) {
return expectedParameterTypes.length == 1 && columns.length > 1 && expectedParameterTypes[0].isArray();
int paramLen = expectedParameterTypes.length;
return expectedParameterTypes.length <= columns.length && expectedParameterTypes[paramLen-1].isArray();
}

private String[] splitAtCommaOrPipe(String input) {
Expand Down

0 comments on commit 4874ae9

Please sign in to comment.