Search in sources :

Example 71 with Statement

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

the class JUnit4Runner method createUnsecuredStatement.

/**
 * Builds a method statement that executes in an unauthenticated context
 */
protected Statement createUnsecuredStatement(final FrameworkMethod method) {
    final Object test = newTestInstance();
    Statement statement = methodInvoker(method, test);
    statement = possiblyExpectingExceptions(method, test, statement);
    statement = withPotentialTimeout(method, test, statement);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    final TestContext context = runner.newTestContext(method.getMethod());
    return new ContextWrapperStatement(context, statement, test);
}
Also used : Statement(org.junit.runners.model.Statement) ContextWrapperStatement(org.apache.openejb.junit.context.ContextWrapperStatement) ContextWrapperStatement(org.apache.openejb.junit.context.ContextWrapperStatement) TestContext(org.apache.openejb.junit.context.TestContext)

Example 72 with Statement

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

the class JUnit4Runner method createSecuredStatementExecutor.

/**
 * Create a method statement that executes the test for each of the specified
 * roles, both doing authorized and unauthorized tests.
 * <p/>
 * Unauthorized roles are configured to fail with EJBAccessExceptions.
 *
 * @param method
 * @param testSecurity
 * @return created statement
 */
private Statement createSecuredStatementExecutor(final FrameworkMethod method, final TestSecurity testSecurity) {
    final MultiStatementExecutor statementExecutor = new MultiStatementExecutor();
    for (final String role : testSecurity.authorized()) {
        final Statement statement = createSecuredStatement(method, role, false);
        statementExecutor.addStatement(statement);
    }
    for (final String role : testSecurity.unauthorized()) {
        final Statement statement = createSecuredStatement(method, role, true);
        statementExecutor.addStatement(statement);
    }
    return statementExecutor;
}
Also used : Statement(org.junit.runners.model.Statement) ContextWrapperStatement(org.apache.openejb.junit.context.ContextWrapperStatement)

Example 73 with Statement

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

the class JUnit4Runner method createSecuredStatement.

/**
 * Create a new statement to run with a given role and whether it should fail or not.
 *
 * @param method
 * @param role
 * @param failWithAccessException
 * @return statement
 */
private Statement createSecuredStatement(final FrameworkMethod method, final String role, final boolean failWithAccessException) {
    final Object test = newTestInstance();
    Statement statement = methodInvoker(method, test);
    statement = possiblyExpectingAccessException(statement, failWithAccessException);
    // specified in @Test
    statement = possiblyExpectingExceptions(method, test, statement);
    statement = withPotentialTimeout(method, test, statement);
    statement = withBefores(method, test, statement);
    statement = withAfters(method, test, statement);
    final TestContext context = runner.newTestContext(method.getMethod(), role);
    return new ContextWrapperStatement(context, statement, test);
}
Also used : Statement(org.junit.runners.model.Statement) ContextWrapperStatement(org.apache.openejb.junit.context.ContextWrapperStatement) ContextWrapperStatement(org.apache.openejb.junit.context.ContextWrapperStatement) TestContext(org.apache.openejb.junit.context.TestContext)

Example 74 with Statement

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

the class ContainerRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            final ApplicationComposers composers = new ContainerApplicationComposers(instance);
            composers.startContainer(instance);
            try {
                statement.evaluate();
            } finally {
                composers.after();
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers)

Example 75 with Statement

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

the class RunAsRule method apply.

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

        @Override
        public void evaluate() throws Throwable {
            final RunAs annotation = description.getAnnotation(RunAs.class);
            final As as = description.getAnnotation(As.class);
            String currentRole = role.get();
            // no more needed
            role.remove();
            if (annotation == null && as == null && currentRole == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            if (currentRole == null) {
                if (annotation == null) {
                    currentRole = as.value();
                } else {
                    currentRole = annotation.value();
                }
            }
            final String runAs = beanContext.getRunAs();
            final String runAsUser = beanContext.getRunAsUser();
            beanContext.setRunAs(currentRole);
            final ThreadContext old = ThreadContext.enter(new ThreadContext(beanContext, null));
            try {
                base.evaluate();
            } finally {
                // reset for next test
                ThreadContext.exit(old);
                beanContext.setRunAs(runAs);
                beanContext.setRunAsUser(runAsUser);
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) RunAs(javax.annotation.security.RunAs) Statement(org.junit.runners.model.Statement) RunAs(javax.annotation.security.RunAs) ThreadContext(org.apache.openejb.core.ThreadContext)

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