Search in sources :

Example 1 with PactLoader

use of au.com.dius.pact.provider.junit.loader.PactLoader 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);
    }
}
Also used : PactSource(au.com.dius.pact.provider.junit.loader.PactSource) Arrays(java.util.Arrays) PactBroker(au.com.dius.pact.provider.junit.loader.PactBroker) Logger(org.slf4j.Logger) Pact(au.com.dius.pact.model.Pact) LoggerFactory(org.slf4j.LoggerFactory) Description(org.junit.runner.Description) IOException(java.io.IOException) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) HttpTarget(au.com.dius.pact.provider.junit.target.HttpTarget) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TestClass(org.junit.runners.model.TestClass) Annotation(java.lang.annotation.Annotation) PactLoader(au.com.dius.pact.provider.junit.loader.PactLoader) ParentRunner(org.junit.runners.ParentRunner) RunNotifier(org.junit.runner.notification.RunNotifier) InitializationError(org.junit.runners.model.InitializationError) PactFolder(au.com.dius.pact.provider.junit.loader.PactFolder) PactSource(au.com.dius.pact.provider.junit.loader.PactSource) Target(au.com.dius.pact.provider.junit.target.Target) InitializationError(org.junit.runners.model.InitializationError) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Pact (au.com.dius.pact.model.Pact)1 PactBroker (au.com.dius.pact.provider.junit.loader.PactBroker)1 PactFolder (au.com.dius.pact.provider.junit.loader.PactFolder)1 PactLoader (au.com.dius.pact.provider.junit.loader.PactLoader)1 PactSource (au.com.dius.pact.provider.junit.loader.PactSource)1 HttpTarget (au.com.dius.pact.provider.junit.target.HttpTarget)1 Target (au.com.dius.pact.provider.junit.target.Target)1 TestTarget (au.com.dius.pact.provider.junit.target.TestTarget)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Description (org.junit.runner.Description)1 RunNotifier (org.junit.runner.notification.RunNotifier)1 ParentRunner (org.junit.runners.ParentRunner)1