Search in sources :

Example 76 with Statement

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

the class ScopesRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            final CdiScopes annotation = description.getAnnotation(CdiScopes.class);
            if (annotation == null) {
                base.evaluate();
                return;
            }
            final WebBeansContext wbc = WebBeansContext.currentInstance();
            final Map<Class<?>, Object> init = new HashMap<>();
            Class<? extends Annotation>[] scopes = annotation.value();
            for (final Class<? extends Annotation> c : scopes) {
                final Object o = param(c);
                if (o != null) {
                    init.put(c, o);
                }
                wbc.getContextsService().startContext(c, o);
            }
            try {
                base.evaluate();
            } finally {
                for (final Class<? extends Annotation> c : scopes) {
                    wbc.getContextsService().endContext(c, init.get(c));
                }
            }
        }
    };
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) HashMap(java.util.HashMap) Statement(org.junit.runners.model.Statement) Annotation(java.lang.annotation.Annotation)

Example 77 with Statement

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

the class TransactionRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
            final Transactional annotation2 = description.getAnnotation(Transactional.class);
            if (annotation == null && annotation2 == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
            final TransactionType transactionType = TransactionType.get(annotation == null ? TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
            beanContext.getMethodContext(method).setTransactionType(transactionType);
            ThreadContext tc = ThreadContext.getThreadContext();
            final boolean tcCreated;
            if (tc == null) {
                tcCreated = true;
                tc = ThreadContext.enter(new ThreadContext(beanContext, null));
            } else {
                tcCreated = false;
            }
            final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType, tc);
            try {
                base.evaluate();
            } finally {
                if (rollback) {
                    policy.setRollbackOnly();
                }
                EjbTransactionUtil.afterInvoke(policy, tc);
                if (tcCreated) {
                    ThreadContext.exit(tc);
                }
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionType(org.apache.openejb.core.transaction.TransactionType) TransactionAttribute(javax.ejb.TransactionAttribute) Statement(org.junit.runners.model.Statement) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) Transactional(javax.transaction.Transactional)

Example 78 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class ParentRunner method run.

@Override
public void run(final RunNotifier notifier) {
    EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription());
    testNotifier.fireTestSuiteStarted();
    try {
        Statement statement = classBlock(notifier);
        statement.evaluate();
    } catch (AssumptionViolatedException e) {
        testNotifier.addFailedAssumption(e);
    } catch (StoppedByUserException e) {
        throw e;
    } catch (Throwable e) {
        testNotifier.addFailure(e);
    } finally {
        testNotifier.fireTestSuiteFinished();
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) StoppedByUserException(org.junit.runner.notification.StoppedByUserException)

Example 79 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class BlockJUnit4ClassRunner method runChild.

//
// Implementation of ParentRunner
//
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
    Description description = describeChild(method);
    if (isIgnored(method)) {
        notifier.fireTestIgnored(description);
    } else {
        Statement statement;
        try {
            statement = methodBlock(method);
        } catch (Throwable ex) {
            statement = new Fail(ex);
        }
        runLeaf(statement, description, notifier);
    }
}
Also used : Description(org.junit.runner.Description) Statement(org.junit.runners.model.Statement) Fail(org.junit.internal.runners.statements.Fail)

Example 80 with Statement

use of org.junit.runners.model.Statement in project junit4 by junit-team.

the class ExpectExceptionTest method whenExpectingAssumptionViolatedExceptionStatementsThrowingDifferentExceptionShouldFail.

@Test
public void whenExpectingAssumptionViolatedExceptionStatementsThrowingDifferentExceptionShouldFail() {
    Statement delegate = new Fail(new SomeException("not expected"));
    ExpectException expectException = new ExpectException(delegate, AssumptionViolatedException.class);
    try {
        expectException.evaluate();
        fail("should throw 'Unexpected exception' when statement throws an exception which is not the one expected");
    } catch (Exception e) {
        assertThat(e.getMessage(), equalTo("Unexpected exception, expected<org.junit.internal.AssumptionViolatedException> " + "but was<org.junit.internal.runners.statements.ExpectExceptionTest$SomeException>"));
    }
}
Also used : Statement(org.junit.runners.model.Statement) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Test(org.junit.Test)

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