Skip to content

Commit

Permalink
Remove TestExtensionContext and ContainerExtensionContext
Browse files Browse the repository at this point in the history
Issue: #901
  • Loading branch information
marcphilipp committed Jul 1, 2017
1 parent 1b1cffa commit 83b3eb6
Show file tree
Hide file tree
Showing 65 changed files with 308 additions and 390 deletions.
10 changes: 5 additions & 5 deletions documentation/src/docs/asciidoc/extensions.adoc
Expand Up @@ -116,9 +116,9 @@ APIs for programmatic, _conditional test execution_.


A `ContainerExecutionCondition` is _evaluated_ to determine if all tests in a given A `ContainerExecutionCondition` is _evaluated_ to determine if all tests in a given
container (e.g., a test class) should be executed based on the supplied container (e.g., a test class) should be executed based on the supplied
`ContainerExtensionContext`. Similarly, a `TestExecutionCondition` is _evaluated_ to `ExtensionContext`. Similarly, a `TestExecutionCondition` is _evaluated_ to
determine if a given test method should be executed based on the supplied determine if a given test method should be executed based on the supplied
`TestExtensionContext`. `ExtensionContext`.


When multiple `ContainerExecutionCondition` or `TestExecutionCondition` extensions are registered, When multiple `ContainerExecutionCondition` or `TestExecutionCondition` extensions are registered,
a container or test, respectively, is disabled as soon as one of the conditions returns _disabled_. a container or test, respectively, is disabled as soon as one of the conditions returns _disabled_.
Expand Down Expand Up @@ -270,9 +270,9 @@ you keep the state from one invocation of an extension to the next? The
`ExtensionContext` API provides a `Store` exactly for this purpose. Extensions may `ExtensionContext` API provides a `Store` exactly for this purpose. Extensions may
put values into a store for later retrieval. See the put values into a store for later retrieval. See the
`<<extensions-lifecycle-callbacks-timing-extension, TimingExtension>>` for an example of using the `<<extensions-lifecycle-callbacks-timing-extension, TimingExtension>>` for an example of using the
`Store` with a method-level scope. It is important to remember that values stored in a `Store` with a method-level scope. It is important to remember that values stored in an
`TestExtensionContext` during test execution will not be available in the surrounding `ExtensionContext` during test execution will not be available in the surrounding
`ContainerExtensionContext`. Since `ContainerExtensionContexts` may be nested, the scope of inner `ExtensionContext`. Since `ExtensionContexts` may be nested, the scope of inner
contexts may also be limited. Consult the corresponding Javadoc for details on the methods contexts may also be limited. Consult the corresponding Javadoc for details on the methods
available for storing and retrieving values via the `{ExtensionContext_Store}`. available for storing and retrieving values via the `{ExtensionContext_Store}`.


Expand Down
2 changes: 2 additions & 0 deletions documentation/src/docs/asciidoc/release-notes-5.0.0-M5.adoc
Expand Up @@ -123,6 +123,8 @@ is placed on the Java 9 module path.
`Object getTestInstance()` to `Optional<Object> getTestInstance()`. `Object getTestInstance()` to `Optional<Object> getTestInstance()`.
* The `getTestException()` method in the `TestExtensionContext` API has been moved to the * The `getTestException()` method in the `TestExtensionContext` API has been moved to the
`ExtensionContext` API. `ExtensionContext` API.
* The `TestExtensionContext` and `ContainerExtensionContext` interfaces have been removed,
and all `Extension` interfaces have been changed to use `ExtensionContext` instead.


[[release-notes-5.0.0-m5-migration-extension-api]] [[release-notes-5.0.0-m5-migration-extension-api]]
.Extension API Migration .Extension API Migration
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestReporter; import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.api.extension.ContainerExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith; import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.converter.JavaTimeConversionPattern; import org.junit.jupiter.params.converter.JavaTimeConversionPattern;
Expand Down Expand Up @@ -161,7 +161,7 @@ void testWithArgumentsSource(String argument) {
static class MyArgumentsProvider implements ArgumentsProvider { static class MyArgumentsProvider implements ArgumentsProvider {


@Override @Override
public Stream<? extends Arguments> provideArguments(ContainerExtensionContext context) { public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of("foo", "bar").map(Arguments::of); return Stream.of("foo", "bar").map(Arguments::of);
} }
} }
Expand Down
Expand Up @@ -12,15 +12,15 @@


import java.io.IOException; import java.io.IOException;


import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.junit.jupiter.api.extension.TestExtensionContext;


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


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


if (throwable instanceof IOException) { if (throwable instanceof IOException) {
Expand Down
Expand Up @@ -16,9 +16,9 @@


import org.junit.jupiter.api.extension.AfterTestExecutionCallback; import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store; import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.junit.jupiter.api.extension.TestExtensionContext;


// end::user_guide[] // end::user_guide[]
/** /**
Expand All @@ -33,20 +33,20 @@ public class TimingExtension implements BeforeTestExecutionCallback, AfterTestEx
private static final Logger LOG = Logger.getLogger(TimingExtension.class.getName()); private static final Logger LOG = Logger.getLogger(TimingExtension.class.getName());


@Override @Override
public void beforeTestExecution(TestExtensionContext context) throws Exception { public void beforeTestExecution(ExtensionContext context) throws Exception {
getStore(context).put(context.getTestMethod().get(), System.currentTimeMillis()); getStore(context).put(context.getTestMethod().get(), System.currentTimeMillis());
} }


@Override @Override
public void afterTestExecution(TestExtensionContext context) throws Exception { public void afterTestExecution(ExtensionContext context) throws Exception {
Method testMethod = context.getTestMethod().get(); Method testMethod = context.getTestMethod().get();
long start = getStore(context).remove(testMethod, long.class); long start = getStore(context).remove(testMethod, long.class);
long duration = System.currentTimeMillis() - start; long duration = System.currentTimeMillis() - start;


LOG.info(() -> String.format("Method [%s] took %s ms.", testMethod.getName(), duration)); LOG.info(() -> String.format("Method [%s] took %s ms.", testMethod.getName(), duration));
} }


private Store getStore(TestExtensionContext context) { private Store getStore(ExtensionContext context) {
return context.getStore(Namespace.create(getClass(), context)); return context.getStore(Namespace.create(getClass(), context));
} }


Expand Down
8 changes: 4 additions & 4 deletions documentation/src/test/java/extensions/ExpectToFail.java
Expand Up @@ -19,10 +19,10 @@


import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace; import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store; import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
import org.junit.jupiter.api.extension.TestExtensionContext;


@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
Expand All @@ -32,16 +32,16 @@
static class Extension implements TestExecutionExceptionHandler, AfterEachCallback { static class Extension implements TestExecutionExceptionHandler, AfterEachCallback {


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


@Override @Override
public void afterEach(TestExtensionContext context) throws Exception { public void afterEach(ExtensionContext context) throws Exception {
assertNotNull(getExceptionStore(context).get("exception"), "Test should have failed"); assertNotNull(getExceptionStore(context).get("exception"), "Test should have failed");
} }


private Store getExceptionStore(TestExtensionContext context) { private Store getExceptionStore(ExtensionContext context) {
return context.getStore(Namespace.create(context)); return context.getStore(Namespace.create(context));
} }
} }
Expand Down
Expand Up @@ -41,6 +41,6 @@ public interface AfterAllCallback extends Extension {
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
*/ */
void afterAll(ContainerExtensionContext context) throws Exception; void afterAll(ExtensionContext context) throws Exception;


} }
Expand Up @@ -45,6 +45,6 @@ public interface AfterEachCallback extends Extension {
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
*/ */
void afterEach(TestExtensionContext context) throws Exception; void afterEach(ExtensionContext context) throws Exception;


} }
Expand Up @@ -44,6 +44,6 @@ public interface AfterTestExecutionCallback extends Extension {
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
*/ */
void afterTestExecution(TestExtensionContext context) throws Exception; void afterTestExecution(ExtensionContext context) throws Exception;


} }
Expand Up @@ -41,6 +41,6 @@ public interface BeforeAllCallback extends Extension {
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
*/ */
void beforeAll(ContainerExtensionContext context) throws Exception; void beforeAll(ExtensionContext context) throws Exception;


} }
Expand Up @@ -43,6 +43,6 @@ public interface BeforeEachCallback extends Extension {
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
*/ */
void beforeEach(TestExtensionContext context) throws Exception; void beforeEach(ExtensionContext context) throws Exception;


} }
Expand Up @@ -44,6 +44,6 @@ public interface BeforeTestExecutionCallback extends Extension {
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
*/ */
void beforeTestExecution(TestExtensionContext context) throws Exception; void beforeTestExecution(ExtensionContext context) throws Exception;


} }
Expand Up @@ -19,9 +19,9 @@
* programmatic, <em>conditional container execution</em>. * programmatic, <em>conditional container execution</em>.
* *
* <p>A {@code ContainerExecutionCondition} is * <p>A {@code ContainerExecutionCondition} is
* {@linkplain #evaluateContainerExecutionCondition(ContainerExtensionContext) evaluated} * {@linkplain #evaluateContainerExecutionCondition(ExtensionContext) evaluated}
* to determine if all tests in a given container should be executed based * to determine if all tests in a given container should be executed based
* on the supplied {@link ContainerExtensionContext}. * on the supplied {@link ExtensionContext}.
* *
* <p>Implementations must provide a no-args constructor. * <p>Implementations must provide a no-args constructor.
* *
Expand All @@ -34,7 +34,7 @@
public interface ContainerExecutionCondition extends Extension { public interface ContainerExecutionCondition extends Extension {


/** /**
* Evaluate this condition for the supplied {@link ContainerExtensionContext}. * Evaluate this condition for the supplied {@link ExtensionContext}.
* *
* <p>An {@linkplain ConditionEvaluationResult#enabled enabled} result * <p>An {@linkplain ConditionEvaluationResult#enabled enabled} result
* indicates that the container should be executed; whereas, a * indicates that the container should be executed; whereas, a
Expand All @@ -44,6 +44,6 @@ public interface ContainerExecutionCondition extends Extension {
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
* @return the result of evaluating this condition; never {@code null} * @return the result of evaluating this condition; never {@code null}
*/ */
ConditionEvaluationResult evaluateContainerExecutionCondition(ContainerExtensionContext context); ConditionEvaluationResult evaluateContainerExecutionCondition(ExtensionContext context);


} }

This file was deleted.

Expand Up @@ -130,7 +130,7 @@ public interface ExtensionContext {


/** /**
* Get the exception that was thrown during execution of the test associated * Get the exception that was thrown during execution of the test associated
* with this {@code TestExtensionContext}, if available. * with this {@code ExtensionContext}, if available.
* *
* <p>This method is typically used for logging and tracing purposes. If you * <p>This method is typically used for logging and tracing purposes. If you
* wish to actually <em>handle</em> an exception thrown during test execution, * wish to actually <em>handle</em> an exception thrown during test execution,
Expand Down
Expand Up @@ -19,9 +19,9 @@
* programmatic, <em>conditional test execution</em>. * programmatic, <em>conditional test execution</em>.
* *
* <p>A {@code TestExecutionCondition} is * <p>A {@code TestExecutionCondition} is
* {@linkplain #evaluateTestExecutionCondition(TestExtensionContext) evaluated} * {@linkplain #evaluateTestExecutionCondition(ExtensionContext) evaluated}
* to determine if a given test should be executed based on the supplied * to determine if a given test should be executed based on the supplied
* {@link TestExtensionContext}. * {@link ExtensionContext}.
* *
* <p>Implementations must provide a no-args constructor. * <p>Implementations must provide a no-args constructor.
* *
Expand All @@ -34,7 +34,7 @@
public interface TestExecutionCondition extends Extension { public interface TestExecutionCondition extends Extension {


/** /**
* Evaluate this condition for the supplied {@link TestExtensionContext}. * Evaluate this condition for the supplied {@link ExtensionContext}.
* *
* <p>An {@linkplain ConditionEvaluationResult#enabled enabled} result * <p>An {@linkplain ConditionEvaluationResult#enabled enabled} result
* indicates that the test should be executed; whereas, a * indicates that the test should be executed; whereas, a
Expand All @@ -44,6 +44,6 @@ public interface TestExecutionCondition extends Extension {
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
* @return the result of evaluating this condition; never {@code null} * @return the result of evaluating this condition; never {@code null}
*/ */
ConditionEvaluationResult evaluateTestExecutionCondition(TestExtensionContext context); ConditionEvaluationResult evaluateTestExecutionCondition(ExtensionContext context);


} }
Expand Up @@ -48,14 +48,14 @@ public interface TestExecutionExceptionHandler extends Extension {
* the next registered {@code TestExecutionExceptionHandler} (if there is * the next registered {@code TestExecutionExceptionHandler} (if there is
* one) will be invoked with any {@link Throwable} thrown by this handler. * one) will be invoked with any {@link Throwable} thrown by this handler.
* *
* <p>Note that the {@link TestExtensionContext#getTestException() test * <p>Note that the {@link ExtensionContext#getTestException() test
* exception} in the supplied {@code TestExtensionContext} will <em>not</em> * exception} in the supplied {@code ExtensionContext} will <em>not</em>
* contain the {@code Throwable} thrown during invocation of the corresponding * contain the {@code Throwable} thrown during invocation of the corresponding
* {@code @Test} method. * {@code @Test} method.
* *
* @param context the current extension context; never {@code null} * @param context the current extension context; never {@code null}
* @param throwable the {@code Throwable} to handle; never {@code null} * @param throwable the {@code Throwable} to handle; never {@code null}
*/ */
void handleTestExecutionException(TestExtensionContext context, Throwable throwable) throws Throwable; void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable;


} }

This file was deleted.

Expand Up @@ -54,34 +54,34 @@ public interface TestTemplateInvocationContextProvider extends Extension {
* Determine if this provider supports providing invocation contexts for the * Determine if this provider supports providing invocation contexts for the
* test template method represented by the supplied {@code context}. * test template method represented by the supplied {@code context}.
* *
* @param context the container extension context for the test template * @param context the extension context for the test template method about
* method about to be invoked; never {@code null} * to be invoked; never {@code null}
* @return {@code true} if this provider can provide invocation contexts * @return {@code true} if this provider can provide invocation contexts
* @see #provideTestTemplateInvocationContexts * @see #provideTestTemplateInvocationContexts
* @see ContainerExtensionContext * @see ExtensionContext
*/ */
boolean supportsTestTemplate(ContainerExtensionContext context); boolean supportsTestTemplate(ExtensionContext context);


/** /**
* Provide {@linkplain TestTemplateInvocationContext invocation contexts} * Provide {@linkplain TestTemplateInvocationContext invocation contexts}
* for the test template method represented by the supplied {@code context}. * for the test template method represented by the supplied {@code context}.
* *
* <p>This method is only called by the framework if {@link #supportsTestTemplate} * <p>This method is only called by the framework if {@link #supportsTestTemplate}
* previously returned {@code true} for the same {@link ContainerExtensionContext}. * previously returned {@code true} for the same {@link ExtensionContext}.
* Thus, this method must not return an empty {@code Stream}. * Thus, this method must not return an empty {@code Stream}.
* *
* <p>The returned {@code Stream} will be properly closed by calling * <p>The returned {@code Stream} will be properly closed by calling
* {@link Stream#close()}, making it safe to use a resource such as * {@link Stream#close()}, making it safe to use a resource such as
* {@link java.nio.file.Files#lines(java.nio.file.Path) Files.lines()}. * {@link java.nio.file.Files#lines(java.nio.file.Path) Files.lines()}.
* *
* @param context the container extension context for the test template * @param context the extension context for the test template method about
* method about to be invoked; never {@code null} * to be invoked; never {@code null}
* @return a {@code Stream} of {@code TestTemplateInvocationContext} * @return a {@code Stream} of {@code TestTemplateInvocationContext}
* instances for the invocation of the test template method; never {@code null} * instances for the invocation of the test template method; never {@code null}
* or empty * or empty
* @see #supportsTestTemplate * @see #supportsTestTemplate
* @see ContainerExtensionContext * @see ExtensionContext
*/ */
Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ContainerExtensionContext context); Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context);


} }
Expand Up @@ -87,8 +87,4 @@ public Set<String> getTags() {
return testDescriptor.getTags().stream().map(TestTag::getName).collect(toCollection(LinkedHashSet::new)); return testDescriptor.getTags().stream().map(TestTag::getName).collect(toCollection(LinkedHashSet::new));
} }


@Override
public Optional<Throwable> getTestException() {
return Optional.empty();
}
} }

0 comments on commit 83b3eb6

Please sign in to comment.