Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api.extension;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.function.Consumer;
20
21 import org.apiguardian.api.API;
22 import org.junit.jupiter.api.Nested;
23 import org.junit.jupiter.api.TestInstance.Lifecycle;
24
25 /**
26 * {@code TestInstancePreDestroyCallback} defines the API for {@link Extension
27 * Extensions} that wish to process test instances <em>after</em> they have been
28 * used in tests but <em>before</em> they are destroyed.
29 *
30 * <p>Common use cases include releasing resources that have been created for
31 * the test instance, invoking custom clean-up methods on the test instance, etc.
32 *
33 * <p>Extensions that implement {@code TestInstancePreDestroyCallback} must be
34 * registered at the class level if the test class is configured with
35 * {@link Lifecycle @TestInstance(Lifecycle.PER_CLASS)}
36 * semantics. If the test class is configured with
37 * {@link Lifecycle @TestInstance(Lifecycle.PER_METHOD)}
38 * semantics, {@code TestInstancePreDestroyCallback} extensions may be registered
39 * at the class level or at the method level. In the latter case, the
40 * {@code TestInstancePreDestroyCallback} extension will only be applied to the
41 * test method for which it is registered.
42 *
43 * <p>A symmetric {@link TestInstancePreConstructCallback} extension defines a callback
44 * hook that is invoked prior to any test class instances being constructed.
45 *
46 * <h2>Constructor Requirements</h2>
47 *
48 * <p>Consult the documentation in {@link Extension} for details on constructor
49 * requirements.
50 *
51 * @since 5.6
52 * @see #preDestroyTestInstance(ExtensionContext)
53 * @see TestInstancePostProcessor
54 * @see TestInstancePreConstructCallback
55 * @see TestInstanceFactory
56 * @see ParameterResolver
57 */
58 @FunctionalInterface
59 @API(status = STABLE, since = "5.7")
60 public interface TestInstancePreDestroyCallback extends Extension {
61
62 /**
63 * Callback for processing test instances before they are destroyed.
64 *
65 * <p>Contrary to {@link TestInstancePostProcessor#postProcessTestInstance}
66 * this method is only called once for each {@link ExtensionContext} even if
67 * there are multiple test instances about to be destroyed in case of
68 * {@link Nested @Nested} tests. Please use the provided
69 * {@link #preDestroyTestInstances(ExtensionContext, Consumer)} utility
70 * method to ensure that all test instances are handled.
71 *
72 * @param context the current extension context; never {@code null}
73 * @see ExtensionContext#getTestInstance()
74 * @see ExtensionContext#getRequiredTestInstance()
75 * @see ExtensionContext#getTestInstances()
76 * @see ExtensionContext#getRequiredTestInstances()
77 * @see #preDestroyTestInstances(ExtensionContext, Consumer)
78 */
79 void preDestroyTestInstance(ExtensionContext context) throws Exception;
80
81 /**
82 * Utility method for processing <em>all</em> test instances of an
83 * {@link ExtensionContext} that are not present in any of its parent
84 * contexts.
85 *
86 * <p>This method should be called in order to implement this interface
87 * correctly since it ensures that the right test instances are processed
88 * regardless of the used {@linkplain Lifecycle lifecycle}. The supplied
89 * callback is called once per test instance that is about to be destroyed
90 * starting with the innermost one.
91 *
92 * <p>This method is intended to be called from an implementation of
93 * {@link #preDestroyTestInstance(ExtensionContext)} like this:
94 *
95 * <pre>{@code class MyExtension implements TestInstancePreDestroyCallback {
96 * @Override
97 * public void preDestroyTestInstance(ExtensionContext context) {
98 * TestInstancePreDestroyCallback.preDestroyTestInstances(context, testInstance -> {
99 * // custom logic that processes testInstance
100 * });
101 * }
102 *}}</pre>
103 *
104 * @param context the current extension context; never {@code null}
105 * @param callback the callback to be invoked for every test instance of the
106 * current extension context that is about to be destroyed; never
107 * {@code null}
108 * @since 5.7.1
109 */
110 @API(status = STABLE, since = "5.10")
111 static void preDestroyTestInstances(ExtensionContext context, Consumer<Object> callback) {
112 List<Object> destroyedInstances = new ArrayList<>(context.getRequiredTestInstances().getAllInstances());
113 for (Optional<ExtensionContext> current = context.getParent(); current.isPresent(); current = current.get().getParent()) {
114 current.get().getTestInstances().map(TestInstances::getAllInstances).ifPresent(
115 destroyedInstances::removeAll);
116 }
117 Collections.reverse(destroyedInstances);
118 destroyedInstances.forEach(callback);
119 }
120
121 }