Skip to content

Commit 6b80f42

Browse files
committed
Return enum instead of boolean
This avoids being able to opt out should the default be changed to test-scoped.
1 parent 1db2432 commit 6b80f42

File tree

14 files changed

+122
-64
lines changed

14 files changed

+122
-64
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@ JUnit repository on GitHub.
5858
extensions.
5959
* Allow determining "shared resources" at runtime via the new `@ResourceLock#providers`
6060
attribute that accepts implementations of `ResourceLocksProvider`.
61-
* Overriding `Extension.isTestScopedConstructorContextEnabled()` enables using a
62-
test-scoped `ExtensionContext` in `Extension` methods called during test class
63-
instantiation.
64-
This behavior will become the default in future versions of JUnit Jupiter.
61+
* Overriding `Extension.getExtensionContextScopeDuringTestInstanceConstruction()` enables
62+
using a test-scoped `ExtensionContext` in `Extension` methods called during test class
63+
construction. This behavior will become the default in future versions of JUnit.
6564
* `@TempDir` is now supported on test class constructors.
6665

6766

documentation/src/docs/asciidoc/user-guide/extensions.adoc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,9 @@ instances and their lifecycle.
383383

384384
[NOTE]
385385
====
386-
You may override the `{Extension}.isTestScopedConstructorContextEnabled(...)` method to
387-
return `true` for revised handling of `CloseableResource` and to make test-specific data
386+
You may override the
387+
`{Extension}.getExtensionContextScopeDuringTestInstanceConstruction(...)` method to return
388+
`TEST_SCOPED` for revised handling of `CloseableResource` and to make test-specific data
388389
available to your implementation.
389390
====
390391

@@ -416,8 +417,9 @@ registered for any specific test class.
416417

417418
[NOTE]
418419
====
419-
You may override the `{Extension}.isTestScopedConstructorContextEnabled(...)` method to
420-
return `true` for revised handling of `CloseableResource` and to make test-specific data
420+
You may override the
421+
`{Extension}.getExtensionContextScopeDuringTestInstanceConstruction(...)` method to return
422+
`TEST_SCOPED` for revised handling of `CloseableResource` and to make test-specific data
421423
available to your implementation.
422424
====
423425

@@ -435,8 +437,9 @@ For a concrete example, consult the source code for the `{MockitoExtension}` and
435437

436438
[NOTE]
437439
====
438-
You may override the `{Extension}.isTestScopedConstructorContextEnabled(...)` method to
439-
return `true` for revised handling of `CloseableResource` and to make test-specific data
440+
You may override the
441+
`{Extension}.getExtensionContextScopeDuringTestInstanceConstruction(...)` method to return
442+
`TEST_SCOPED` for revised handling of `CloseableResource` and to make test-specific data
440443
available to your implementation.
441444
====
442445

@@ -488,8 +491,9 @@ those provided in `java.lang.reflect.Parameter` in order to avoid this bug in th
488491

489492
[NOTE]
490493
====
491-
You may override the `{Extension}.isTestScopedConstructorContextEnabled(...)` method to
492-
return `true` to support injecting test specific data into constructor parameters of the
494+
You may override the
495+
`{Extension}.getExtensionContextScopeDuringTestInstanceConstruction(...)` method to return
496+
`TEST_SCOPED` to support injecting test specific data into constructor parameters of the
493497
test instance. Doing so causes a test-specific `{ExtensionContext}` to be used while
494498
resolving constructor parameters, unless the
495499
<<writing-tests-test-instance-lifecycle, test instance lifecycle>> is set to `PER_CLASS`.
@@ -727,8 +731,9 @@ include::{testDir}/example/interceptor/SwingEdtInterceptor.java[tags=user_guide]
727731

728732
[NOTE]
729733
====
730-
You may override the `{Extension}.isTestScopedConstructorContextEnabled(...)` method to
731-
return `true` to make test-specific data available to your implementation of
734+
You may override the
735+
`{Extension}.getExtensionContextScopeDuringTestInstanceConstruction(...)` method to return
736+
`TEST_SCOPED` to make test-specific data available to your implementation of
732737
`interceptTestClassConstructor` and for a revised scope of the provided `Store` instance.
733738
====
734739

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/Extension.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.junit.jupiter.api.extension;
1212

13+
import static org.apiguardian.api.API.Status.DEPRECATED;
1314
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
1415
import static org.apiguardian.api.API.Status.STABLE;
1516

@@ -47,10 +48,11 @@ public interface Extension {
4748
* Whether this extension should receive a test-scoped
4849
* {@link ExtensionContext} during the creation of test instances.
4950
*
50-
* <p>If an extension returns {@code true} from this method, the following\
51-
* extension methods will be called with a test-scoped
52-
* {@link ExtensionContext}, unless the test class is annotated with
53-
* {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}:
51+
* <p>If an extension returns
52+
* {@link ExtensionContextScope#TEST_SCOPED TEST_SCOPED} from this method,
53+
* the following extension methods will be called with a test-scoped
54+
* {@link ExtensionContext} instead of a class-scoped one, unless the
55+
* {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS} lifecycle is used:
5456
*
5557
* <ul>
5658
* <li>{@link InvocationInterceptor#interceptTestClassConstructor}</li>
@@ -60,8 +62,8 @@ public interface Extension {
6062
* <li>{@link TestInstanceFactory}</li>
6163
* </ul>
6264
*
63-
* <p>Implementations of these extension callbacks can observe the following
64-
* differences if they are returning {@code true} from this method:
65+
* <p>In such cases, implementations of these extension callbacks can
66+
* observe the following differences:
6567
*
6668
* <ul>
6769
* <li>{@link ExtensionContext#getElement() getElement()} may refer to the
@@ -81,16 +83,16 @@ public interface Extension {
8183
* </ul>
8284
*
8385
* <p><strong>Note</strong>: The behavior which is enabled by returning
84-
* {@code true} from this method will become the default in future versions
85-
* of JUnit Jupiter. To ensure future compatibility, extension implementors
86-
* are therefore advised to opt in, even if they don't require the new
87-
* functionality.
86+
* {@link ExtensionContextScope#TEST_SCOPED TEST_SCOPED} from this method
87+
* will become the default in future versions of JUnit. To ensure future
88+
* compatibility, extension implementors are therefore advised to opt in,
89+
* even if they don't require the new functionality.
8890
*
8991
* @implNote There are no guarantees about how often this method is called.
9092
* Therefore, implementations should be idempotent and avoid side
9193
* effects. They may, however, cache the result for performance in
9294
* the {@link Store Store} of the supplied
93-
* {@link ExtensionContext}.
95+
* {@link ExtensionContext}, if necessary.
9496
* @param rootContext the root extension context to allow inspection of
9597
* configuration parameters; never {@code null}
9698
* @since 5.12
@@ -101,8 +103,37 @@ public interface Extension {
101103
* @see TestInstanceFactory
102104
*/
103105
@API(status = EXPERIMENTAL, since = "5.12")
104-
default boolean isTestScopedConstructorContextEnabled(ExtensionContext rootContext) {
105-
return false;
106+
default ExtensionContextScope getExtensionContextScopeDuringTestInstanceConstruction(ExtensionContext rootContext) {
107+
return ExtensionContextScope.DEFAULT;
108+
}
109+
110+
/**
111+
* {@code ExtensionContextScope} is used to define the scope of the
112+
* {@link ExtensionContext} passed to an extension during the creation of
113+
* test instances.
114+
*
115+
* @since 5.12
116+
* @see org.junit.jupiter.api.extension.Extension#getExtensionContextScopeDuringTestInstanceConstruction
117+
*/
118+
@API(status = EXPERIMENTAL, since = "5.12")
119+
enum ExtensionContextScope {
120+
121+
/**
122+
* The extension should receive an {@link ExtensionContext} scoped to
123+
* the test class.
124+
*
125+
* @deprecated This behavior will be removed from future versions of
126+
* JUnit and {@link #TEST_SCOPED} will become the default.
127+
*/
128+
@API(status = DEPRECATED, since = "5.12") //
129+
@Deprecated
130+
DEFAULT,
131+
132+
/**
133+
* The extension should receive an {@link ExtensionContext} scoped to
134+
* the test instance.
135+
*/
136+
TEST_SCOPED
106137
}
107138

108139
}

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/InvocationInterceptor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ public interface InvocationInterceptor extends Extension {
5959
* <p>Note that the test class may <em>not</em> have been initialized
6060
* (static initialization) when this method is invoked.
6161
*
62-
* <p>Extensions may override {@link #isTestScopedConstructorContextEnabled}
63-
* to make test-specific data available to the implementation of this method
64-
* and for a revised scope of the provided {@link Store} instance.
62+
* <p>Extensions may override
63+
* {@link #getExtensionContextScopeDuringTestInstanceConstruction} to make
64+
* test-specific data available to the implementation of this method and for
65+
* a revised scope of the provided {@link Store} instance.
6566
*
6667
* @param invocation the invocation that is being intercepted; never
6768
* {@code null}

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/ParameterResolver.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
* an argument for the parameter must be resolved at runtime by a
3232
* {@code ParameterResolver}.
3333
*
34-
* <p>Extensions may override {@link #isTestScopedConstructorContextEnabled}
35-
* to support injecting test specific data into constructor parameters of the
36-
* test instance. Returning {@code true} from this method, causes a
37-
* test-specific {@link ExtensionContext} to be used while resolving constructor
38-
* parameters, unless the lifecycle is set to {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS}.
34+
* <p>Extensions may override
35+
* {@link #getExtensionContextScopeDuringTestInstanceConstruction} to support
36+
* injecting test specific data into constructor parameters of the test
37+
* instance. Returning {@link ExtensionContextScope#TEST_SCOPED TEST_SCOPED}
38+
* from this method, causes a test-specific {@link ExtensionContext} to be used
39+
* while resolving constructor parameters, unless the lifecycle is set to
40+
* {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS}.
3941
*
4042
* <h2>Constructor Requirements</h2>
4143
*

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstanceFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public interface TestInstanceFactory extends Extension {
5757
/**
5858
* Callback for creating a test instance for the supplied context.
5959
*
60-
* <p>Extensions may override {@link #isTestScopedConstructorContextEnabled}
61-
* for revised handling of {@link CloseableResource CloseableResource} and
62-
* to make test-specific data available to your implementation.
60+
* <p>Extensions may override
61+
* {@link #getExtensionContextScopeDuringTestInstanceConstruction} for
62+
* revised handling of {@link CloseableResource CloseableResource} and to
63+
* make test-specific data available to your implementation.
6364
*
6465
* <p><strong>Note</strong>: the {@code ExtensionContext} supplied to a
6566
* {@code TestInstanceFactory} will always return an empty

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePostProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ public interface TestInstancePostProcessor extends Extension {
4646
/**
4747
* Callback for post-processing the supplied test instance.
4848
*
49-
* <p>Extensions may override {@link #isTestScopedConstructorContextEnabled}
50-
* for revised handling of {@link CloseableResource CloseableResource} and
51-
* to make test-specific data available to your implementation.
49+
* <p>Extensions may override
50+
* {@link #getExtensionContextScopeDuringTestInstanceConstruction} for
51+
* revised handling of {@link CloseableResource CloseableResource} and to
52+
* make test-specific data available to your implementation.
5253
*
5354
* <p><strong>Note</strong>: the {@code ExtensionContext} supplied to a
5455
* {@code TestInstancePostProcessor} will always return an empty

junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstancePreConstructCallback.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ public interface TestInstancePreConstructCallback extends Extension {
4949
/**
5050
* Callback invoked prior to test instances being constructed.
5151
*
52-
* <p>Extensions may override {@link #isTestScopedConstructorContextEnabled}
53-
* to make test-specific data available to the implementation of this method
54-
* and for a revised scope of the provided {@link ExtensionContext.Store} instance.
52+
* <p>Extensions may override
53+
* {@link #getExtensionContextScopeDuringTestInstanceConstruction} to make
54+
* test-specific data available to the implementation of this method and for
55+
* a revised scope of the provided {@link ExtensionContext.Store Store}
56+
* instance.
5557
*
5658
* @param factoryContext the context for the test instance about to be instantiated;
5759
* never {@code null}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/ExtensionContextSupplier.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package org.junit.jupiter.engine.execution;
1212

1313
import static org.apiguardian.api.API.Status.INTERNAL;
14+
import static org.junit.jupiter.api.extension.Extension.ExtensionContextScope.TEST_SCOPED;
1415

1516
import org.apiguardian.api.API;
1617
import org.junit.jupiter.api.extension.Extension;
@@ -34,12 +35,16 @@ public ExtensionContextSupplier(ExtensionContext currentExtensionContext, Extens
3435
}
3536

3637
public ExtensionContext get(Extension extension) {
37-
if (currentExtensionContext == legacyExtensionContext
38-
|| extension.isTestScopedConstructorContextEnabled(currentExtensionContext.getRoot())) {
38+
if (currentExtensionContext == legacyExtensionContext || isTestScoped(extension)) {
3939
return currentExtensionContext;
4040
}
4141
else {
4242
return legacyExtensionContext;
4343
}
4444
}
45+
46+
private boolean isTestScoped(Extension extension) {
47+
ExtensionContext rootContext = currentExtensionContext.getRoot();
48+
return extension.getExtensionContextScopeDuringTestInstanceConstruction(rootContext) == TEST_SCOPED;
49+
}
4550
}

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import static java.nio.file.FileVisitResult.CONTINUE;
1414
import static java.util.stream.Collectors.joining;
15+
import static org.junit.jupiter.api.extension.Extension.ExtensionContextScope.TEST_SCOPED;
1516
import static org.junit.jupiter.api.io.CleanupMode.DEFAULT;
1617
import static org.junit.jupiter.api.io.CleanupMode.NEVER;
1718
import static org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS;
@@ -93,8 +94,8 @@ public TempDirectory(JupiterConfiguration configuration) {
9394
}
9495

9596
@Override
96-
public boolean isTestScopedConstructorContextEnabled(ExtensionContext rootContext) {
97-
return true;
97+
public ExtensionContextScope getExtensionContextScopeDuringTestInstanceConstruction(ExtensionContext rootContext) {
98+
return TEST_SCOPED;
9899
}
99100

100101
/**

0 commit comments

Comments
 (0)