-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from theangrydev/master
Allow custom parameter resolution for table tests
- Loading branch information
Showing
4 changed files
with
80 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.googlecode.yatspec.junit; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public interface ParameterResolver { | ||
Object[] resolveParameters(Row row, Class<?> testClass, Method testMethod) throws Exception; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/com/googlecode/yatspec/junit/ParameterResolverFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.googlecode.yatspec.junit; | ||
|
||
import static com.googlecode.yatspec.Creator.create; | ||
import static java.lang.Class.forName; | ||
import static java.lang.System.getProperty; | ||
|
||
public class ParameterResolverFactory { | ||
|
||
public static final String PARAMETER_RESOLVER = "yatspec.parameter.resolver"; | ||
|
||
public static void setParameterResolver(Class<? extends ParameterResolver> aClass) { | ||
System.setProperty(PARAMETER_RESOLVER, aClass.getName()); | ||
} | ||
|
||
public static ParameterResolver parameterResolver() { | ||
try { | ||
return create(forName(getProperty(PARAMETER_RESOLVER, VarargsParameterResolver.class.getName()))); | ||
} catch (Exception e) { | ||
return new VarargsParameterResolver(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/com/googlecode/yatspec/junit/VarargsParameterResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.googlecode.yatspec.junit; | ||
|
||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
|
||
import static com.googlecode.totallylazy.Sequences.sequence; | ||
|
||
public class VarargsParameterResolver implements ParameterResolver { | ||
|
||
@Override | ||
public Object[] resolveParameters(Row row, Class<?> testClass, Method testMethod) throws Exception { | ||
final Object[] suppliedParams = row.value(); | ||
final Class<?>[] requiredParams = testMethod.getParameterTypes(); | ||
|
||
if (requiresNoVarArgs(requiredParams)) { | ||
return suppliedParams; | ||
} | ||
|
||
if (isMissingRequiredArguments(suppliedParams, requiredParams)) { | ||
throw new IllegalArgumentException("Missing parameters."); | ||
} | ||
|
||
return sequence(suppliedParams).take(Math.min(requiredParams.length - 1, suppliedParams.length)).append(getVarargsFrom(suppliedParams, requiredParams)).toArray(); | ||
} | ||
|
||
private boolean requiresNoVarArgs(Class<?>[] methodParams) { | ||
return !methodParams[methodParams.length - 1].isArray(); | ||
} | ||
|
||
private boolean isMissingRequiredArguments(Object[] input, Class<?>[] expected) { | ||
return input.length < expected.length - 1; | ||
} | ||
|
||
private boolean hasVarArgsSupplied(Object[] input, Class<?>[] expected) { | ||
return input.length >= expected.length; | ||
} | ||
|
||
private Object getVarargsFrom(Object[] suppliedParams, Class<?>[] requiredParams) { | ||
if (hasVarArgsSupplied(suppliedParams, requiredParams)) { | ||
return Arrays.copyOfRange(suppliedParams, requiredParams.length - 1, suppliedParams.length); | ||
} | ||
return Array.newInstance(requiredParams[requiredParams.length - 1].getComponentType(), 0); | ||
} | ||
} |