use of org.junit.rules.TestRule in project junit4 by junit-team.
the class BlockJUnit4ClassRunner method withRules.
private Statement withRules(FrameworkMethod method, Object target, Statement statement) {
List<TestRule> testRules = getTestRules(target);
Statement result = statement;
result = withMethodRules(method, testRules, target, result);
result = withTestRules(method, testRules, result);
return result;
}
use of org.junit.rules.TestRule in project spock by spockframework.
the class ClassRuleInterceptor method intercept.
public void intercept(final IMethodInvocation invocation) throws Throwable {
Statement stat = createBaseStatement(invocation);
for (FieldInfo field : ruleFields) {
TestRule rule = (TestRule) getRuleInstance(field, field.isShared() ? invocation.getSharedInstance() : invocation.getInstance());
stat = rule.apply(stat, invocation.getSpec().getDescription());
}
stat.evaluate();
}
use of org.junit.rules.TestRule in project spock by spockframework.
the class TestRuleInterceptor method intercept.
public void intercept(final IMethodInvocation invocation) throws Throwable {
Statement stat = createBaseStatement(invocation);
for (FieldInfo field : ruleFields) {
TestRule rule = (TestRule) getRuleInstance(field, invocation.getInstance());
stat = rule.apply(stat, invocation.getIteration().getDescription());
}
stat.evaluate();
}
use of org.junit.rules.TestRule in project indy by Commonjava.
the class ThreadDumper method timeoutRule.
public static TestRule timeoutRule(int timeout, TimeUnit units) {
return (base, description) -> new Statement() {
public void evaluate() throws Throwable {
System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
FutureTask<Void> task = new FutureTask<>(() -> {
try {
latch.countDown();
base.evaluate();
} catch (Throwable t) {
error.set(t);
}
return null;
});
ThreadGroup tg = new ThreadGroup("Test Timeout Group");
Thread t = new Thread(tg, task, "Test Timeout Thread");
t.setDaemon(true);
t.start();
try {
System.out.println("Waiting for test to start.");
latch.await();
} catch (InterruptedException e) {
error.set(e);
}
if (error.get() == null) {
try {
System.out.println("Waiting for test to complete (or timeout)");
task.get(timeout, units);
} catch (InterruptedException e) {
error.set(e);
} catch (ExecutionException e) {
error.set(e.getCause());
} catch (TimeoutException e) {
System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
dumpThreads();
StackTraceElement[] stackTrace = t.getStackTrace();
Exception currThreadException = new TestTimedOutException(timeout, units);
if (stackTrace != null) {
currThreadException.setStackTrace(stackTrace);
t.interrupt();
}
throw currThreadException;
}
}
Throwable throwable = error.get();
if (throwable != null) {
throw throwable;
}
}
};
}
use of org.junit.rules.TestRule in project indy by Commonjava.
the class ThreadDumper method timeoutRule.
public static TestRule timeoutRule(int timeout, TimeUnit units) {
return (base, description) -> new Statement() {
public void evaluate() throws Throwable {
System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
FutureTask<Void> task = new FutureTask<>(() -> {
try {
latch.countDown();
base.evaluate();
} catch (Throwable t) {
error.set(t);
}
return null;
});
ThreadGroup tg = new ThreadGroup("Test Timeout Group");
Thread t = new Thread(tg, task, "Test Timeout Thread");
t.setDaemon(true);
t.start();
try {
System.out.println("Waiting for test to start.");
latch.await();
} catch (InterruptedException e) {
error.set(e);
}
if (error.get() == null) {
try {
System.out.println("Waiting for test to complete (or timeout)");
task.get(timeout, units);
} catch (InterruptedException e) {
error.set(e);
} catch (ExecutionException e) {
error.set(e.getCause());
} catch (TimeoutException e) {
System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
dumpThreads();
StackTraceElement[] stackTrace = t.getStackTrace();
Exception currThreadException = new TestTimedOutException(timeout, units);
if (stackTrace != null) {
currThreadException.setStackTrace(stackTrace);
t.interrupt();
}
throw currThreadException;
}
}
Throwable throwable = error.get();
if (throwable != null) {
throw throwable;
}
}
};
}
Aggregations