Skip to content

Commit

Permalink
Rename ExceptionHandler to TestExecutionExceptionHandler
Browse files Browse the repository at this point in the history
Issue: #249
  • Loading branch information
sbrannen committed May 24, 2016
1 parent ebfdc4d commit c7c0d24
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 39 deletions.
4 changes: 2 additions & 2 deletions documentation/src/docs/asciidoc/extensions.adoc
Expand Up @@ -156,8 +156,8 @@ concrete example.

=== Exception Handling

`{ExceptionHandler}` defines the API for `Extensions` that wish to handle exceptions
thrown from tests.
`{TestExecutionExceptionHandler}` defines the API for `Extensions` that wish to handle
exceptions thrown during test execution.

The following example shows an extension which will swallow all instances of `IOException`
but rethrow any other type of exception:
Expand Down
2 changes: 1 addition & 1 deletion documentation/src/docs/asciidoc/index.adoc
Expand Up @@ -28,7 +28,7 @@ Stefan Bechtold; Sam Brannen; Johannes Link; Matthias Merdes; Marc Philipp
:CustomTypeParameterResolver: {master-branch}/junit-tests/src/test/java/org/junit/gen5/engine/junit5/execution/injection/sample/CustomTypeParameterResolver.java[CustomTypeParameterResolver]
:Disabled: {master-branch}/junit5-api/src/main/java/org/junit/gen5/api/Disabled.java[@Disabled]
:DisabledCondition: {master-branch}/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/extension/DisabledCondition.java[DisabledCondition]
:ExceptionHandler: {master-branch}/junit5-api/src/main/java/org/junit/gen5/api/extension/ExceptionHandler.java[ExceptionHandler]
:TestExecutionExceptionHandler: {master-branch}/junit5-api/src/main/java/org/junit/gen5/api/extension/TestExecutionExceptionHandler.java[TestExecutionExceptionHandler]
:ExtendWith: {master-branch}/junit5-api/src/main/java/org/junit/gen5/api/extension/ExtendWith.java[@ExtendWith]
:ExtensionRegistrar: {master-branch}/junit5-api/src/main/java/org/junit/gen5/api/extension/ExtensionRegistrar.java[ExtensionRegistrar]
:TestInstancePostProcessor: {master-branch}/junit5-api/src/main/java/org/junit/gen5/api/extension/TestInstancePostProcessor.java[TestInstancePostProcessor]
Expand Down
Expand Up @@ -12,15 +12,15 @@

import java.io.IOException;

import org.junit.gen5.api.extension.ExceptionHandler;
import org.junit.gen5.api.extension.TestExecutionExceptionHandler;
import org.junit.gen5.api.extension.TestExtensionContext;

// @formatter:off
// tag::user_guide[]
public class IgnoreIOExceptionExtension implements ExceptionHandler {
public class IgnoreIOExceptionExtension implements TestExecutionExceptionHandler {

@Override
public void handleException(TestExtensionContext context, Throwable throwable)
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable)
throws Throwable {

if (throwable instanceof IOException) {
Expand Down
Expand Up @@ -13,15 +13,15 @@
import static org.junit.gen5.api.Assertions.assertNotNull;

import org.junit.gen5.api.extension.AfterEachCallback;
import org.junit.gen5.api.extension.ExceptionHandler;
import org.junit.gen5.api.extension.ExtensionContext.Namespace;
import org.junit.gen5.api.extension.ExtensionContext.Store;
import org.junit.gen5.api.extension.TestExecutionExceptionHandler;
import org.junit.gen5.api.extension.TestExtensionContext;

public class ExpectToFailExtension implements ExceptionHandler, AfterEachCallback {
public class ExpectToFailExtension implements TestExecutionExceptionHandler, AfterEachCallback {

@Override
public void handleException(TestExtensionContext context, Throwable throwable) throws Throwable {
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {
getExceptionStore(context).put("exception", throwable);
}

Expand Down
Expand Up @@ -34,17 +34,17 @@

import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.api.Test;
import org.junit.gen5.api.extension.ExceptionHandler;
import org.junit.gen5.api.extension.ExtendWith;
import org.junit.gen5.api.extension.TestExecutionExceptionHandler;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.engine.ExecutionEventRecorder;
import org.junit.gen5.engine.junit5.AbstractJUnit5TestEngineTests;
import org.junit.gen5.launcher.TestDiscoveryRequest;

/**
* Integration tests that verify support for {@link ExceptionHandler}.
* Integration tests that verify support for {@link TestExecutionExceptionHandler}.
*/
class ExceptionHandlerTests extends AbstractJUnit5TestEngineTests {
class TestExecutionExceptionHandlerTests extends AbstractJUnit5TestEngineTests {

static List<String> handlerCalls = new ArrayList<>();

Expand All @@ -63,7 +63,7 @@ void exceptionHandlerRethrowsException() {

ExecutionEventRecorder eventRecorder = executeTests(request);

assertTrue(RethrowException.handleExceptionCalled, "ExceptionHandler should have been called");
assertTrue(RethrowException.handleExceptionCalled, "TestExecutionExceptionHandler should have been called");

assertRecordedExecutionEventsContainsExactly(eventRecorder.getExecutionEvents(), //
event(engine(), started()), //
Expand All @@ -80,7 +80,7 @@ void exceptionHandlerSwallowsException() {

ExecutionEventRecorder eventRecorder = executeTests(request);

assertTrue(SwallowException.handleExceptionCalled, "ExceptionHandler should have been called");
assertTrue(SwallowException.handleExceptionCalled, "TestExecutionExceptionHandler should have been called");

assertRecordedExecutionEventsContainsExactly(eventRecorder.getExecutionEvents(), //
event(engine(), started()), //
Expand All @@ -97,7 +97,7 @@ void exceptionHandlerConvertsException() {

ExecutionEventRecorder eventRecorder = executeTests(request);

assertTrue(ConvertException.handleExceptionCalled, "ExceptionHandler should have been called");
assertTrue(ConvertException.handleExceptionCalled, "TestExecutionExceptionHandler should have been called");

assertRecordedExecutionEventsContainsExactly(eventRecorder.getExecutionEvents(), //
event(engine(), started()), //
Expand Down Expand Up @@ -162,12 +162,12 @@ void testSeveral() {
}
}

private static class RethrowException implements ExceptionHandler {
private static class RethrowException implements TestExecutionExceptionHandler {

static boolean handleExceptionCalled = false;

@Override
public void handleException(TestExtensionContext context, Throwable throwable) throws Throwable {
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {
assertTrue(throwable instanceof IOException);
handleExceptionCalled = true;
handlerCalls.add("rethrow");
Expand All @@ -176,38 +176,38 @@ public void handleException(TestExtensionContext context, Throwable throwable) t
}
}

private static class SwallowException implements ExceptionHandler {
private static class SwallowException implements TestExecutionExceptionHandler {

static boolean handleExceptionCalled = false;

@Override
public void handleException(TestExtensionContext context, Throwable throwable) throws Throwable {
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {
assertTrue(throwable instanceof IOException);
handleExceptionCalled = true;
handlerCalls.add("swallow");
//swallow exception by not rethrowing it
}
}

private static class ConvertException implements ExceptionHandler {
private static class ConvertException implements TestExecutionExceptionHandler {

static boolean handleExceptionCalled = false;

@Override
public void handleException(TestExtensionContext context, Throwable throwable) throws Throwable {
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {
assertTrue(throwable instanceof RuntimeException);
handleExceptionCalled = true;
handlerCalls.add("convert");
throw new IOException("checked");
}

}
private static class ShouldNotBeCalled implements ExceptionHandler {
private static class ShouldNotBeCalled implements TestExecutionExceptionHandler {

static boolean handleExceptionCalled = false;

@Override
public void handleException(TestExtensionContext context, Throwable throwable) throws Throwable {
public void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable {
handleExceptionCalled = true;
}
}
Expand Down
Expand Up @@ -39,7 +39,7 @@
* <li>{@link AfterTestMethodCallback}</li>
* <li>{@link TestInstancePostProcessor}</li>
* <li>{@link MethodParameterResolver}</li>
* <li>{@link ExceptionHandler}</li>
* <li>{@link TestExecutionExceptionHandler}</li>
* </ul>
*
* @since 5.0
Expand Down
Expand Up @@ -15,8 +15,8 @@
import org.junit.gen5.commons.meta.API;

/**
* {@code ExceptionHandler} defines the API for {@link Extension Extensions}
* that wish to handle exceptions thrown from tests.
* {@code TestExecutionExceptionHandler} defines the API for {@link Extension
* Extensions} that wish to handle exceptions thrown during test execution.
*
* <p>Common use cases include swallowing an exception if it's anticipated
* or rolling back a transaction in certain error scenarios.
Expand All @@ -27,7 +27,7 @@
*/
@FunctionalInterface
@API(Experimental)
public interface ExceptionHandler extends Extension {
public interface TestExecutionExceptionHandler extends Extension {

/**
* Handle the supplied {@link Throwable throwable}.
Expand All @@ -40,10 +40,10 @@ public interface ExceptionHandler extends Extension {
* </ol>
*
* <p>If the supplied {@code throwable} is swallowed, subsequent
* {@link ExceptionHandler ExceptionHandlers} will not be invoked; otherwise,
* the next registered {@code ExceptionHandler} (if there is one) will be invoked
* with any {@link Throwable} thrown by this handler.
* {@code TestExecutionExceptionHandlers} will not be invoked; otherwise,
* the next registered {@code TestExecutionExceptionHandler} (if there is
* one) will be invoked with any {@link Throwable} thrown by this handler.
*/
void handleException(TestExtensionContext context, Throwable throwable) throws Throwable;
void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable;

}
Expand Up @@ -24,8 +24,8 @@
import org.junit.gen5.api.extension.BeforeEachCallback;
import org.junit.gen5.api.extension.BeforeTestMethodCallback;
import org.junit.gen5.api.extension.ConditionEvaluationResult;
import org.junit.gen5.api.extension.ExceptionHandler;
import org.junit.gen5.api.extension.MethodInvocationContext;
import org.junit.gen5.api.extension.TestExecutionExceptionHandler;
import org.junit.gen5.api.extension.TestExtensionContext;
import org.junit.gen5.commons.meta.API;
import org.junit.gen5.commons.util.ExceptionUtils;
Expand Down Expand Up @@ -185,28 +185,33 @@ protected void invokeTestMethod(JUnit5EngineExecutionContext context, TestExtens
new MethodInvoker(testExtensionContext, context.getExtensionRegistry()).invoke(methodInvocationContext);
}
catch (Throwable throwable) {
invokeExceptionHandlers(context.getExtensionRegistry(), testExtensionContext, throwable);
invokeTestExecutionExceptionHandlers(context.getExtensionRegistry(), testExtensionContext, throwable);
}
});
}

private void invokeExceptionHandlers(ExtensionRegistry registry, TestExtensionContext context, Throwable ex) {
List<ExceptionHandler> exceptionHandlers = registry.stream(ExceptionHandler.class).collect(toList());
invokeExceptionHandlers(ex, exceptionHandlers, context);
private void invokeTestExecutionExceptionHandlers(ExtensionRegistry registry, TestExtensionContext context,
Throwable ex) {

List<TestExecutionExceptionHandler> exceptionHandlers = registry.stream(
TestExecutionExceptionHandler.class).collect(toList());

invokeTestExecutionExceptionHandlers(ex, exceptionHandlers, context);
}

private void invokeExceptionHandlers(Throwable ex, List<ExceptionHandler> handlers, TestExtensionContext context) {
private void invokeTestExecutionExceptionHandlers(Throwable ex, List<TestExecutionExceptionHandler> handlers,
TestExtensionContext context) {
// No handlers left?
if (handlers.isEmpty()) {
ExceptionUtils.throwAsUncheckedException(ex);
}

try {
// Invoke next available handler
handlers.remove(0).handleException(context, ex);
handlers.remove(0).handleTestExecutionException(context, ex);
}
catch (Throwable t) {
invokeExceptionHandlers(t, handlers, context);
invokeTestExecutionExceptionHandlers(t, handlers, context);
}
}

Expand Down

0 comments on commit c7c0d24

Please sign in to comment.