Search in sources :

Example 46 with Statement

use of org.junit.runners.model.Statement in project java-faker by DiUS.

the class RepeatRule method apply.

@Override
public Statement apply(Statement statement, Description description) {
    Statement result = statement;
    Repeat repeat = description.getAnnotation(Repeat.class);
    if (repeat != null) {
        int times = repeat.times();
        result = new RepeatStatement(times, statement);
    }
    return result;
}
Also used : Statement(org.junit.runners.model.Statement)

Example 47 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 48 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 49 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 50 with Statement

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

the class TomEEEmbeddedSingleRunner 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(test);
                    getRunner().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)138 Test (org.junit.Test)22 FrameworkMethod (org.junit.runners.model.FrameworkMethod)15 Method (java.lang.reflect.Method)11 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)11 Fail (org.junit.internal.runners.statements.Fail)9 Description (org.junit.runner.Description)8 TestRule (org.junit.rules.TestRule)7 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 MethodRule (org.junit.rules.MethodRule)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