Skip to content

Commit c82c098

Browse files
committed
Apply comment
Issue #4434
1 parent c555910 commit c82c098

File tree

9 files changed

+54
-43
lines changed

9 files changed

+54
-43
lines changed

Diff for: documentation/src/docs/asciidoc/user-guide/extensions.adoc

-9
Original file line numberDiff line numberDiff line change
@@ -918,15 +918,6 @@ public class MyResource implements Store.CloseableResource, AutoCloseable {
918918

919919
This ensures that your resource will be properly closed regardless of which JUnit Jupiter version is being used.
920920

921-
To determine if your extension is running under JUnit Jupiter 5.13+, you can check if auto-close is enabled via the configuration:
922-
923-
[source,java,indent=0]
924-
----
925-
boolean isJupiter513OrNewer = context.getConfigurationParameter("junit.jupiter.extensions.autoclose.enabled")
926-
.map(Boolean::parseBoolean)
927-
.orElse(true); // Default is true in 5.13+
928-
----
929-
930921
[[extensions-supported-utilities]]
931922
=== Supported Utilities in Extensions
932923

Diff for: junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstantiationAwareExtension.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public interface TestInstantiationAwareExtension extends Extension {
6464
* <li>{@link ExtensionContext#getTestMethod() getTestMethod()} is no longer
6565
* empty, unless the {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS}
6666
* lifecycle is used.</li>
67-
* <li>If the callback adds a new {@link Store.CloseableResource} to the
68-
* {@link Store Store}, the resource is closed just after the instance is
69-
* destroyed.</li>
67+
* <li>If the callback adds a new {@link Store.CloseableResource} or
68+
* {@link AutoCloseable} to the {@link Store Store}, the resource is closed
69+
* just after the instance is destroyed.</li>
7070
* <li>The callbacks can now access data previously stored by
7171
* {@link TestTemplateInvocationContext}, unless the
7272
* {@link TestInstance.Lifecycle#PER_CLASS PER_CLASS} lifecycle is used.</li>
@@ -87,7 +87,6 @@ public interface TestInstantiationAwareExtension extends Extension {
8787
* configuration parameters; never {@code null}
8888
* @since 5.12
8989
*/
90-
@SuppressWarnings("deprecation")
9190
@API(status = EXPERIMENTAL, since = "5.12")
9291
default ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) {
9392
return ExtensionContextScope.DEFAULT;

Diff for: junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/AbstractExtensionContext.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,19 @@ abstract class AbstractExtensionContext<T extends TestDescriptor> implements Ext
8989
@SuppressWarnings("deprecation")
9090
private NamespacedHierarchicalStore.CloseAction<org.junit.platform.engine.support.store.Namespace> createCloseResources() {
9191
return (__, ___, value) -> {
92-
if (value instanceof Store.CloseableResource) {
93-
((Store.CloseableResource) value).close();
94-
return;
95-
}
96-
9792
boolean isAutoCloseEnabled = this.configuration.isAutoCloseEnabled();
98-
if (!isAutoCloseEnabled) {
93+
94+
if (value instanceof AutoCloseable && isAutoCloseEnabled) {
95+
((AutoCloseable) value).close();
9996
return;
10097
}
10198

102-
if (value instanceof AutoCloseable) {
103-
((AutoCloseable) value).close();
99+
if (value instanceof Store.CloseableResource) {
100+
if (isAutoCloseEnabled) {
101+
engineExecutionListener.reportingEntryPublished(testDescriptor, ReportEntry.from("WARNING",
102+
"Type implements CloseableResource but not AutoCloseable: " + value.getClass().getName()));
103+
}
104+
((Store.CloseableResource) value).close();
104105
}
105106
};
106107
}

Diff for: junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/execution/NamespaceAwareStore.java

-5
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,9 @@ public <K, V> V getOrComputeIfAbsent(K key, Function<K, V> defaultCreator, Class
7070
() -> this.valuesStore.getOrComputeIfAbsent(this.namespace, key, defaultCreator, requiredType));
7171
}
7272

73-
@SuppressWarnings("deprecation")
7473
@Override
7574
public void put(Object key, Object value) {
7675
Preconditions.notNull(key, "key must not be null");
77-
78-
if (value instanceof CloseableResource && !(value instanceof AutoCloseable)) {
79-
LOGGER.warn(() -> "The object implements CloseableResource but not AutoCloseable: " + value.getClass());
80-
}
8176
accessStore(() -> this.valuesStore.put(this.namespace, key, value));
8277
}
8378

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

+23-7
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
5454
import org.junit.jupiter.api.extension.ExtensionContext;
5555
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
56+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
5657
import org.junit.jupiter.api.extension.ParameterContext;
5758
import org.junit.jupiter.api.extension.ParameterResolver;
5859
import org.junit.jupiter.api.io.CleanupMode;
@@ -134,12 +135,7 @@ private static void installFailureTracker(ExtensionContext context) {
134135
}
135136

136137
private static void installFailureTracker(ExtensionContext context, ExtensionContext parentContext) {
137-
context.getStore(NAMESPACE).put(FAILURE_TRACKER, (AutoCloseable) () -> context.getParent() //
138-
.ifPresent(parentContext -> {
139-
if (selfOrChildFailed(context)) {
140-
parentContext.getStore(NAMESPACE).put(CHILD_FAILED, true);
141-
}
142-
}));
138+
context.getStore(NAMESPACE).put(FAILURE_TRACKER, new FailureTracker(context, parentContext));
143139
}
144140

145141
private void injectStaticFields(ExtensionContext context, Class<?> testClass) {
@@ -298,7 +294,8 @@ private static ExtensionContext.Store getContextSpecificStore(ExtensionContext c
298294
return context.getStore(NAMESPACE.append(context));
299295
}
300296

301-
static class CloseablePath implements AutoCloseable {
297+
@SuppressWarnings("deprecation")
298+
static class CloseablePath implements Store.CloseableResource, AutoCloseable {
302299

303300
private static final Logger LOGGER = LoggerFactory.getLogger(CloseablePath.class);
304301

@@ -613,4 +610,23 @@ public String toString() {
613610

614611
}
615612

613+
@SuppressWarnings("deprecation")
614+
private static class FailureTracker implements Store.CloseableResource, AutoCloseable {
615+
616+
private final ExtensionContext context;
617+
private final ExtensionContext parentContext;
618+
619+
private FailureTracker(ExtensionContext context, ExtensionContext parentContext) {
620+
this.context = context;
621+
this.parentContext = parentContext;
622+
}
623+
624+
@Override
625+
public void close() {
626+
if (selfOrChildFailed(context)) {
627+
getContextSpecificStore(parentContext).put(CHILD_FAILED, true);
628+
}
629+
}
630+
}
631+
616632
}

Diff for: junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TimeoutInvocationFactory.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ private ScheduledExecutorService getThreadExecutorForSameThreadInvocation() {
5151
return store.getOrComputeIfAbsent(SingleThreadExecutorResource.class).get();
5252
}
5353

54-
@SuppressWarnings("deprecation")
55-
private static abstract class ExecutorResource implements Store.CloseableResource {
54+
@SuppressWarnings({ "deprecation", "try" })
55+
private static abstract class ExecutorResource implements Store.CloseableResource, AutoCloseable {
5656

5757
protected final ScheduledExecutorService executor;
5858

@@ -65,7 +65,7 @@ ScheduledExecutorService get() {
6565
}
6666

6767
@Override
68-
public void close() throws Throwable {
68+
public void close() throws Exception {
6969
executor.shutdown();
7070
boolean terminated = executor.awaitTermination(5, TimeUnit.SECONDS);
7171
if (!terminated) {
@@ -75,6 +75,7 @@ public void close() throws Throwable {
7575
}
7676
}
7777

78+
@SuppressWarnings("try")
7879
static class SingleThreadExecutorResource extends ExecutorResource {
7980

8081
@SuppressWarnings("unused")

Diff for: junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedInvocationContext.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ private void storeParameterInfo(ExtensionContext context) {
7676
new DefaultParameterInfo(declarations, accessor).store(context);
7777
}
7878

79-
@SuppressWarnings("deprecation")
80-
private static class CloseableArgument implements ExtensionContext.Store.CloseableResource {
79+
@SuppressWarnings({ "deprecation", "try" })
80+
private static class CloseableArgument implements ExtensionContext.Store.CloseableResource, AutoCloseable {
8181

8282
private final AutoCloseable autoCloseable;
8383

@@ -86,7 +86,7 @@ private static class CloseableArgument implements ExtensionContext.Store.Closeab
8686
}
8787

8888
@Override
89-
public void close() throws Throwable {
89+
public void close() throws Exception {
9090
this.autoCloseable.close();
9191
}
9292

Diff for: jupiter-tests/src/test/java/org/junit/jupiter/api/extension/CloseableResourceIntegrationTests.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,16 @@ static class ThrowingOnCloseExtension implements BeforeEachCallback {
7979

8080
@Override
8181
public void beforeEach(ExtensionContext context) {
82-
context.getStore(GLOBAL).put("throwingResource", (AutoCloseable) () -> {
83-
throw new RuntimeException("Exception in onClose");
84-
});
82+
context.getStore(GLOBAL).put("throwingResource", new ThrowingResource());
83+
}
84+
}
85+
86+
@SuppressWarnings({ "deprecation", "try" })
87+
static class ThrowingResource implements ExtensionContext.Store.CloseableResource, AutoCloseable {
88+
89+
@Override
90+
public void close() throws Exception {
91+
throw new RuntimeException("Exception in onClose");
8592
}
8693
}
8794

Diff for: jupiter-tests/src/test/java/org/junit/jupiter/engine/ClassTemplateInvocationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
14381438

14391439
}
14401440

1441-
private static class CustomCloseableResource implements AutoCloseable {
1441+
@SuppressWarnings("deprecation")
1442+
private static class CustomCloseableResource implements ExtensionContext.Store.CloseableResource {
14421443

14431444
static boolean closed;
14441445

0 commit comments

Comments
 (0)