com.tngtech.junit.dataprovider.resolver.DataProviderMethodResolverHelper Maven / Gradle / Ivy
Show all versions of junit-dataprovider-core Show documentation
package com.tngtech.junit.dataprovider.resolver;
import static com.tngtech.junit.dataprovider.Preconditions.checkNotNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class DataProviderMethodResolverHelper {
/**
* Method searches for dataprovider methods for the resolver context.
*
* If {@link ResolveStrategy} is pointing to {@link ResolveStrategy#UNTIL_FIRST_MATCH}, the first found dataprovider
* methods of the first resolver will be returned. {@link ResolveStrategy#AGGREGATE_ALL_MATCHES} will aggregate the
* results of all resolvers such that all found dataprovider methods are returned.
*
* @param context to be used for resolving dataprovider methods
* @return the found dataprovider methods or an empty list
* @throws NullPointerException if and only if given context is {@code null}
*/
public static List findDataProviderMethods(DataProviderResolverContext context) {
checkNotNull(context, "'context' must not be null");
List result = new ArrayList();
for (Class extends DataProviderMethodResolver> resolverClass : context.getResolverClasses()) {
DataProviderMethodResolver resolver = newInstance(resolverClass);
List dataProviderMethods = resolver.resolve(context);
if (context.getResolveStrategy() == ResolveStrategy.UNTIL_FIRST_MATCH && !dataProviderMethods.isEmpty()) {
result.addAll(dataProviderMethods);
break;
} else if (context.getResolveStrategy() == ResolveStrategy.AGGREGATE_ALL_MATCHES) {
result.addAll(dataProviderMethods);
}
}
return result;
}
private static T newInstance(Class clazz) {
Constructor constructor;
try {
constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(
String.format("Could not find default constructor to instantiate '%s'.", clazz), e);
} catch (SecurityException e) {
throw new IllegalArgumentException(String.format(
"Security violation while trying to access default constructor to instantiate '%s'.", clazz), e);
}
try {
return constructor.newInstance();
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(
String.format("Could not access default constructor to instantiate '%s'.", clazz), e);
} catch (InstantiationException e) {
throw new IllegalArgumentException(
String.format("Could not instantiate '%s' using default constructor.", clazz), e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(
String.format("The default constructor of '%s' has thrown an exception: %s", clazz, e.getMessage()),
e);
}
}
}