Search in sources :

Example 51 with Statement

use of org.junit.runners.model.Statement in project pact-jvm by DiUS.

the class InteractionRunner method interactionBlock.

protected Statement interactionBlock(final Interaction interaction) {
    //1. prepare object
    //2. get Target
    //3. run Rule`s
    //4. run Before`s
    //5. run OnStateChange`s
    //6. run test
    //7. run After`s
    final Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }
    final Target target = testClass.getAnnotatedFieldValues(test, TestTarget.class, Target.class).get(0);
    if (target instanceof TestClassAwareTarget) {
        ((TestClassAwareTarget) target).setTestClass(testClass, test);
    }
    Statement statement = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            target.testInteraction(pact.getConsumer().getName(), interaction);
        }
    };
    statement = withStateChanges(interaction, test, statement);
    statement = withBefores(interaction, test, statement);
    statement = withRules(interaction, test, statement);
    statement = withAfters(interaction, test, statement);
    return statement;
}
Also used : TestClassAwareTarget(au.com.dius.pact.provider.junit.target.TestClassAwareTarget) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) Target(au.com.dius.pact.provider.junit.target.Target) ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) TestTarget(au.com.dius.pact.provider.junit.target.TestTarget) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail) TestClassAwareTarget(au.com.dius.pact.provider.junit.target.TestClassAwareTarget)

Example 52 with Statement

use of org.junit.runners.model.Statement in project pact-jvm by DiUS.

the class MessagePactProviderRule method apply.

/* (non-Javadoc)
	 * @see org.junit.rules.ExternalResource#apply(org.junit.runners.model.Statement, org.junit.runner.Description)
	 */
@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            PactVerifications pactVerifications = description.getAnnotation(PactVerifications.class);
            if (pactVerifications != null) {
                evaluatePactVerifications(pactVerifications, base, description);
                return;
            }
            PactVerification pactDef = description.getAnnotation(PactVerification.class);
            // no pactVerification? execute the test normally
            if (pactDef == null) {
                base.evaluate();
                return;
            }
            Message providedMessage = null;
            Map<String, Message> pacts;
            if (StringUtils.isNoneEmpty(pactDef.fragment())) {
                Optional<Method> possiblePactMethod = findPactMethod(pactDef);
                if (!possiblePactMethod.isPresent()) {
                    base.evaluate();
                    return;
                }
                pacts = new HashMap<>();
                Method method = possiblePactMethod.get();
                Pact pact = method.getAnnotation(Pact.class);
                MessagePactBuilder builder = MessagePactBuilder.consumer(pact.consumer()).hasPactWith(provider);
                messagePact = (MessagePact) method.invoke(testClassInstance, builder);
                for (Message message : messagePact.getMessages()) {
                    pacts.put(message.getProviderState(), message);
                }
            } else {
                pacts = parsePacts();
            }
            if (pactDef.value().length == 2 && !pactDef.value()[1].trim().isEmpty()) {
                providedMessage = pacts.get(pactDef.value()[1].trim());
            } else if (!pacts.isEmpty()) {
                providedMessage = pacts.values().iterator().next();
            }
            if (providedMessage == null) {
                base.evaluate();
                return;
            }
            setMessage(providedMessage.contentsAsBytes(), description);
            try {
                base.evaluate();
                messagePact.write(PactConsumerConfig$.MODULE$.pactRootDir());
            } catch (Throwable t) {
                throw t;
            }
        }
    };
}
Also used : Message(au.com.dius.pact.model.v3.messaging.Message) Statement(org.junit.runners.model.Statement) MessagePact(au.com.dius.pact.model.v3.messaging.MessagePact) Method(java.lang.reflect.Method)

Example 53 with Statement

use of org.junit.runners.model.Statement in project pact-jvm by DiUS.

the class PactRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            PactVerification pactDef = description.getAnnotation(PactVerification.class);
            //no pactVerification? execute the test normally
            if (pactDef == null) {
                base.evaluate();
                return;
            }
            PactFragment fragment = getPacts().get(pactDef.value());
            if (fragment == null) {
                throw new UnsupportedOperationException("Fragment not found: " + pactDef.value());
            }
            VerificationResult result = fragment.runConsumer(config, new TestRun() {

                @Override
                public void run(MockProviderConfig config) throws Throwable {
                    base.evaluate();
                }
            });
            if (!result.equals(PACT_VERIFIED)) {
                if (result instanceof PactError) {
                    throw new RuntimeException(((PactError) result).error());
                }
                if (result instanceof UserCodeFailed) {
                    throw new RuntimeException(((UserCodeFailed<RuntimeException>) result).error());
                }
                if (result instanceof PactMismatch) {
                    PactMismatch mismatch = (PactMismatch) result;
                    throw new PactMismatchException(mismatch);
                }
            }
        }
    };
}
Also used : PactFragment(au.com.dius.pact.model.PactFragment) Statement(org.junit.runners.model.Statement) MockProviderConfig(au.com.dius.pact.model.MockProviderConfig)

Example 54 with Statement

use of org.junit.runners.model.Statement in project tomee by apache.

the class MetaRunner method methodBlock.

@Override
protected Statement methodBlock(final FrameworkMethod method) {
    final Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (final Throwable e) {
        return new Fail(e);
    }
    Statement statement = new MetaTest.$(method, test);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    return statement;
}
Also used : ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail)

Example 55 with Statement

use of org.junit.runners.model.Statement in project tomee by apache.

the class SingleApplicationComposerRunner method rules.

@Override
protected List<MethodRule> rules(final Object test) {
    final List<MethodRule> rules = super.rules(test);
    rules.add(new MethodRule() {

        @Override
        public Statement apply(final Statement base, final FrameworkMethod method, final Object target) {
            return new Statement() {

                @Override
                public void evaluate() throws Throwable {
                    start(getTestClass().getJavaClass());
                    composerInject(target);
                    base.evaluate();
                }
            };
        }
    });
    return rules;
}
Also used : MethodRule(org.junit.rules.MethodRule) Statement(org.junit.runners.model.Statement) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Aggregations

Statement (org.junit.runners.model.Statement)129 Test (org.junit.Test)22 FrameworkMethod (org.junit.runners.model.FrameworkMethod)15 Method (java.lang.reflect.Method)10 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)10 Fail (org.junit.internal.runners.statements.Fail)9 TestRule (org.junit.rules.TestRule)7 Description (org.junit.runner.Description)7 MethodRule (org.junit.rules.MethodRule)6 MultipleFailureException (org.junit.runners.model.MultipleFailureException)6 ExecutorService (java.util.concurrent.ExecutorService)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)5 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 EmptyStatement (com.hazelcast.util.EmptyStatement)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TestExecutorService (org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService)3 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)3 LoggerContextRule (org.apache.logging.log4j.junit.LoggerContextRule)3