Search in sources :

Example 1 with RunBefores

use of org.junit.internal.runners.statements.RunBefores in project spring-security-oauth by spring-projects.

the class OAuth2ContextSetup method initializeIfNecessary.

private void initializeIfNecessary(FrameworkMethod method, final Object target) {
    final TestClass testClass = new TestClass(target.getClass());
    OAuth2ContextConfiguration contextConfiguration = findOAuthContextConfiguration(method, testClass);
    if (contextConfiguration == null) {
        // Nothing to do
        return;
    }
    this.initializeAccessToken = contextConfiguration.initialize();
    this.resource = creatResource(target, contextConfiguration);
    final List<FrameworkMethod> befores = testClass.getAnnotatedMethods(BeforeOAuth2Context.class);
    if (!befores.isEmpty()) {
        logger.debug("Running @BeforeOAuth2Context methods");
        for (FrameworkMethod before : befores) {
            RestOperations savedServerClient = clientHolder.getRestTemplate();
            OAuth2ContextConfiguration beforeConfiguration = findOAuthContextConfiguration(before, testClass);
            if (beforeConfiguration != null) {
                OAuth2ProtectedResourceDetails resource = creatResource(target, beforeConfiguration);
                AccessTokenRequest beforeRequest = new DefaultAccessTokenRequest();
                beforeRequest.setAll(parameters);
                OAuth2RestTemplate client = createRestTemplate(resource, beforeRequest);
                clientHolder.setRestTemplate(client);
            }
            AccessTokenRequest request = new DefaultAccessTokenRequest();
            request.setAll(parameters);
            this.client = createRestTemplate(this.resource, request);
            List<FrameworkMethod> list = Arrays.asList(before);
            try {
                new RunBefores(new Statement() {

                    public void evaluate() {
                    }
                }, list, target).evaluate();
            } catch (AssumptionViolatedException e) {
                throw e;
            } catch (RuntimeException e) {
                throw e;
            } catch (AssertionError e) {
                throw e;
            } catch (Throwable e) {
                logger.debug("Exception in befores", e);
                Assert.assertThat(e, CoreMatchers.not(CoreMatchers.anything()));
            } finally {
                clientHolder.setRestTemplate(savedServerClient);
            }
        }
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) TestClass(org.junit.runners.model.TestClass) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) RestOperations(org.springframework.web.client.RestOperations) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)

Example 2 with RunBefores

use of org.junit.internal.runners.statements.RunBefores in project intellij-community by JetBrains.

the class GuiTestRunner method methodBlock.

@Override
protected Statement methodBlock(FrameworkMethod method) {
    FrameworkMethod newMethod;
    try {
        if (Arrays.stream(getTestClass().getAnnotations()).anyMatch(annotation -> annotation instanceof ParentPlugin)) {
            loadClassesWithNewPluginClassLoader();
        } else {
            loadClassesWithIdeClassLoader();
        }
        Method methodFromClassLoader = myTestClass.getJavaClass().getMethod(method.getName());
        newMethod = new FrameworkMethod(methodFromClassLoader);
    } catch (Exception e) {
        return new Fail(e);
    }
    Object test;
    try {
        test = new ReflectiveCallable() {

            @Override
            protected Object runReflectiveCall() throws Throwable {
                return createTest();
            }
        }.run();
    } catch (Throwable e) {
        return new Fail(e);
    }
    Statement statement = methodInvoker(newMethod, test);
    List<FrameworkMethod> beforeMethods = myTestClass.getAnnotatedMethods(Before.class);
    if (!beforeMethods.isEmpty()) {
        statement = new RunBefores(statement, beforeMethods, test);
    }
    List<FrameworkMethod> afterMethods = myTestClass.getAnnotatedMethods(After.class);
    if (!afterMethods.isEmpty()) {
        statement = new RunAfters(statement, afterMethods, test);
    }
    return statement;
}
Also used : RunAfters(org.junit.internal.runners.statements.RunAfters) ReflectiveCallable(org.junit.internal.runners.model.ReflectiveCallable) Statement(org.junit.runners.model.Statement) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Fail(org.junit.internal.runners.statements.Fail)

Example 3 with RunBefores

use of org.junit.internal.runners.statements.RunBefores in project hazelcast by hazelcast.

the class AbstractHazelcastClassRunner method withBefores.

// Override withBefores to accommodate spawning the member bouncing thread after @Before's have been executed and before @Test
// when MemberBounceRule is in use
@Override
protected Statement withBefores(FrameworkMethod method, Object target, Statement statement) {
    List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class);
    List<TestRule> testRules = getTestRules(target);
    Statement nextStatement = statement;
    if (!testRules.isEmpty()) {
        for (TestRule rule : testRules) {
            if (rule instanceof BounceMemberRule) {
                nextStatement = ((BounceMemberRule) rule).startBouncing(statement);
            }
        }
    }
    return befores.isEmpty() ? nextStatement : new RunBefores(nextStatement, befores, target);
}
Also used : TestRule(org.junit.rules.TestRule) Statement(org.junit.runners.model.Statement) EmptyStatement(com.hazelcast.util.EmptyStatement) BounceMemberRule(com.hazelcast.test.bounce.BounceMemberRule) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 4 with RunBefores

use of org.junit.internal.runners.statements.RunBefores in project pact-jvm by DiUS.

the class InteractionRunner method withStateChanges.

protected Statement withStateChanges(final Interaction interaction, final Object target, final Statement statement) {
    if (StringUtils.isNotEmpty(interaction.getProviderState())) {
        final String state = interaction.getProviderState();
        final List<FrameworkMethod> onStateChange = new ArrayList<FrameworkMethod>();
        for (FrameworkMethod ann : testClass.getAnnotatedMethods(State.class)) {
            for (String annotationState : ann.getAnnotation(State.class).value()) {
                if (annotationState.equalsIgnoreCase(state)) {
                    onStateChange.add(ann);
                    break;
                }
            }
        }
        if (onStateChange.isEmpty()) {
            return new Fail(new MissingStateChangeMethod("MissingStateChangeMethod: Did not find a test class method annotated with @State(\"" + state + "\")"));
        }
        return new RunBefores(statement, onStateChange, target);
    } else {
        return statement;
    }
}
Also used : ArrayList(java.util.ArrayList) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Fail(org.junit.internal.runners.statements.Fail)

Aggregations

RunBefores (org.junit.internal.runners.statements.RunBefores)4 FrameworkMethod (org.junit.runners.model.FrameworkMethod)4 Statement (org.junit.runners.model.Statement)3 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)2 Fail (org.junit.internal.runners.statements.Fail)2 BounceMemberRule (com.hazelcast.test.bounce.BounceMemberRule)1 EmptyStatement (com.hazelcast.util.EmptyStatement)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ReflectiveCallable (org.junit.internal.runners.model.ReflectiveCallable)1 RunAfters (org.junit.internal.runners.statements.RunAfters)1 TestRule (org.junit.rules.TestRule)1 TestClass (org.junit.runners.model.TestClass)1 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)1 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)1 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)1 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)1 RestOperations (org.springframework.web.client.RestOperations)1