use of au.com.dius.pact.provider.junit.loader.PactSource in project pact-jvm by DiUS.
the class PactRunner method getPactSource.
protected PactLoader getPactSource(final TestClass clazz) throws InitializationError {
final PactSource pactSource = clazz.getAnnotation(PactSource.class);
final List<Annotation> pactLoaders = Arrays.stream(clazz.getAnnotations()).filter(annotation -> annotation.annotationType().getAnnotation(PactSource.class) != null).collect(toList());
if ((pactSource == null ? 0 : 1) + pactLoaders.size() != 1) {
throw new InitializationError("Exactly one pact source should be set");
}
try {
if (pactSource != null) {
final Class<? extends PactLoader> pactLoaderClass = pactSource.value();
try {
// Checks if there is a constructor with one argument of type Class.
final Constructor<? extends PactLoader> contructorWithClass = pactLoaderClass.getDeclaredConstructor(Class.class);
contructorWithClass.setAccessible(true);
return contructorWithClass.newInstance(clazz.getJavaClass());
} catch (NoSuchMethodException e) {
LOGGER.error(e.getMessage(), e);
return pactLoaderClass.newInstance();
}
} else {
final Annotation annotation = pactLoaders.iterator().next();
return annotation.annotationType().getAnnotation(PactSource.class).value().getConstructor(annotation.annotationType()).newInstance(annotation);
}
} catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
LOGGER.error("Error while creating pact source", e);
throw new InitializationError(e);
}
}
Aggregations