Search in sources :

Example 6 with TestRule

use of org.junit.rules.TestRule in project pact-jvm by DiUS.

the class InteractionRunner method withRules.

protected Statement withRules(final Interaction interaction, final Object target, final Statement statement) {
    final List<TestRule> testRules = testClass.getAnnotatedMethodValues(target, Rule.class, TestRule.class);
    testRules.addAll(testClass.getAnnotatedFieldValues(target, Rule.class, TestRule.class));
    return testRules.isEmpty() ? statement : new RunRules(statement, testRules, describeChild(interaction));
}
Also used : TestRule(org.junit.rules.TestRule) RunRules(org.junit.rules.RunRules) TestRule(org.junit.rules.TestRule) Rule(org.junit.Rule)

Example 7 with TestRule

use of org.junit.rules.TestRule in project hazelcast by hazelcast.

the class AbstractHazelcastClassRunner method getTestRules.

@Override
protected List<TestRule> getTestRules(Object target) {
    List<TestRule> testRules = super.getTestRules(target);
    Set<Class<? extends TestRule>> testRuleClasses = new HashSet<Class<? extends TestRule>>();
    TestClass testClass = getTestClass();
    // find the required test rule classes from test class itself
    Annotation[] classAnnotations = testClass.getAnnotations();
    for (Annotation annotation : classAnnotations) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        AutoRegisteredTestRule autoFilterRule = annotationType.getAnnotation(AutoRegisteredTestRule.class);
        if (autoFilterRule != null) {
            Class<? extends TestRule> testRuleClass = autoFilterRule.testRule();
            testRuleClasses.add(testRuleClass);
        }
    }
    // find the required test rule classes from methods
    List<FrameworkMethod> annotatedMethods = testClass.getAnnotatedMethods();
    for (FrameworkMethod annotatedMethod : annotatedMethods) {
        Annotation[] methodAnnotations = annotatedMethod.getAnnotations();
        for (Annotation annotation : methodAnnotations) {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            AutoRegisteredTestRule autoFilterRule = annotationType.getAnnotation(AutoRegisteredTestRule.class);
            if (autoFilterRule != null) {
                Class<? extends TestRule> testRuleClass = autoFilterRule.testRule();
                testRuleClasses.add(testRuleClass);
            }
        }
    }
    // create and register test rules
    for (Class<? extends TestRule> testRuleClass : testRuleClasses) {
        try {
            TestRule testRule = testRuleClass.newInstance();
            testRules.add(testRule);
        } catch (Throwable t) {
            System.err.println("Unable to create test rule instance of test rule class " + testRuleClass.getName() + " because of " + t);
        }
    }
    return testRules;
}
Also used : TestClass(org.junit.runners.model.TestClass) Annotation(java.lang.annotation.Annotation) TestRule(org.junit.rules.TestRule) TestClass(org.junit.runners.model.TestClass) FrameworkMethod(org.junit.runners.model.FrameworkMethod) HashSet(java.util.HashSet)

Example 8 with TestRule

use of org.junit.rules.TestRule 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 9 with TestRule

use of org.junit.rules.TestRule in project hazelcast by hazelcast.

the class AbstractHazelcastClassRunner method withAfters.

@Override
protected Statement withAfters(FrameworkMethod method, Object target, Statement statement) {
    List<FrameworkMethod> afters = getTestClass().getAnnotatedMethods(After.class);
    Statement nextStatement = statement;
    List<TestRule> testRules = getTestRules(target);
    if (!testRules.isEmpty()) {
        for (TestRule rule : testRules) {
            if (rule instanceof BounceMemberRule) {
                nextStatement = ((BounceMemberRule) rule).stopBouncing(statement);
            }
        }
    }
    if (THREAD_DUMP_ON_FAILURE) {
        return new ThreadDumpAwareRunAfters(method, nextStatement, afters, target);
    }
    if (afters.isEmpty()) {
        return nextStatement;
    } else {
        return new RunAfters(nextStatement, afters, target);
    }
}
Also used : TestRule(org.junit.rules.TestRule) RunAfters(org.junit.internal.runners.statements.RunAfters) Statement(org.junit.runners.model.Statement) EmptyStatement(com.hazelcast.util.EmptyStatement) BounceMemberRule(com.hazelcast.test.bounce.BounceMemberRule) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 10 with TestRule

use of org.junit.rules.TestRule in project randomizedtesting by randomizedtesting.

the class RandomizedRunner method wrapMethodRules.

/**
   * Wrap the given statement in any declared MethodRules (old style rules).
   */
@SuppressWarnings("deprecation")
private Statement wrapMethodRules(Statement s, TestCandidate c, Object instance) {
    FrameworkMethod fm = new FrameworkMethod(c.method);
    // Old-style MethodRules first.
    List<org.junit.rules.MethodRule> methodRules = getAnnotatedFieldValues(instance, Rule.class, org.junit.rules.MethodRule.class);
    for (org.junit.rules.MethodRule rule : methodRules) {
        s = rule.apply(s, fm, instance);
    }
    // New-style TestRule next.
    List<TestRule> testRules = getAnnotatedFieldValues(instance, Rule.class, TestRule.class);
    for (TestRule rule : testRules) {
        s = rule.apply(s, c.description);
    }
    return s;
}
Also used : TestRule(org.junit.rules.TestRule) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Aggregations

TestRule (org.junit.rules.TestRule)11 Statement (org.junit.runners.model.Statement)7 FrameworkMethod (org.junit.runners.model.FrameworkMethod)4 BounceMemberRule (com.hazelcast.test.bounce.BounceMemberRule)2 EmptyStatement (com.hazelcast.util.EmptyStatement)2 ManagementFactory (java.lang.management.ManagementFactory)2 MonitorInfo (java.lang.management.MonitorInfo)2 ThreadInfo (java.lang.management.ThreadInfo)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 FutureTask (java.util.concurrent.FutureTask)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Stream (java.util.stream.Stream)2 StringUtils.join (org.apache.commons.lang.StringUtils.join)2 Rule (org.junit.Rule)2 TestTimedOutException (org.junit.runners.model.TestTimedOutException)2 FieldInfo (org.spockframework.runtime.model.FieldInfo)2