Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDir.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.io;
12
13 import static org.apiguardian.api.API.Status.DEPRECATED;
14 import static org.apiguardian.api.API.Status.EXPERIMENTAL;
15 import static org.apiguardian.api.API.Status.STABLE;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.lang.annotation.Documented;
20 import java.lang.annotation.ElementType;
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 import java.lang.annotation.Target;
24 import java.nio.file.Path;
25
26 import org.apiguardian.api.API;
27 import org.junit.jupiter.api.extension.ExtensionConfigurationException;
28 import org.junit.jupiter.api.extension.ParameterResolutionException;
29
30 /**
31 * {@code @TempDir} can be used to annotate a field in a test class or a
32 * parameter in a lifecycle method or test method of type {@link Path} or
33 * {@link File} that should be resolved into a temporary directory.
34 *
35 * <p>Please note that {@code @TempDir} is not supported on constructor
36 * parameters. Please use field injection instead by annotating an instance
37 * field with {@code @TempDir}.
38 *
39 * <h2>Creation</h2>
40 *
41 * <p>The temporary directory is only created if a field in a test class or a
42 * parameter in a lifecycle method or test method is annotated with
43 * {@code @TempDir}. If the field type or parameter type is neither {@link Path}
44 * nor {@link File}, if a field is declared as {@code final}, or if the temporary
45 * directory cannot be created, an {@link ExtensionConfigurationException} or a
46 * {@link ParameterResolutionException} will be thrown as appropriate. In
47 * addition, a {@code ParameterResolutionException} will be thrown for a
48 * constructor parameter annotated with {@code @TempDir}.
49 *
50 * <h2>Scope</h2>
51 *
52 * <p>By default, a separate temporary directory is created for every
53 * declaration of the {@code @TempDir} annotation. If you want to share a
54 * temporary directory across all tests in a test class, you should declare the
55 * annotation on a {@code static} field or on a parameter of a
56 * {@link org.junit.jupiter.api.BeforeAll @BeforeAll} method.
57 *
58 * <h3>Old behavior</h3>
59 *
60 * <p>You can revert to the old behavior of using a single temporary directory
61 * by setting the {@value #SCOPE_PROPERTY_NAME} configuration parameter to
62 * {@code per_context}. In that case, the scope of the temporary directory
63 * depends on where the first {@code @TempDir} annotation is encountered when
64 * executing a test class. The temporary directory will be shared by all tests
65 * in a class when the annotation is present on a {@code static} field or on a
66 * parameter of a {@link org.junit.jupiter.api.BeforeAll @BeforeAll} method.
67 * Otherwise &mdash; for example, when {@code @TempDir} is only used on instance
68 * fields or on parameters in test,
69 * {@link org.junit.jupiter.api.BeforeEach @BeforeEach}, or
70 * {@link org.junit.jupiter.api.AfterEach @AfterEach} methods &mdash; each test
71 * will use its own temporary directory.
72 *
73 * <h2>Clean Up</h2>
74 *
75 * <p>By default, when the end of the scope of a temporary directory is reached,
76 * &mdash; when the test method or class has finished execution &mdash; JUnit will
77 * attempt to clean up the temporary directory by recursively deleting all files
78 * and directories in the temporary directory and, finally, the temporary directory
79 * itself. In case deletion of a file or directory fails, an {@link IOException}
80 * will be thrown that will cause the test or test class to fail.
81 *
82 * <p>The {@link #cleanup} attribute allows you to configure the {@link CleanupMode}.
83 * If the cleanup mode is set to {@link CleanupMode#NEVER NEVER}, the temporary
84 * directory will not be cleaned up after the test completes. If the cleanup mode is
85 * set to {@link CleanupMode#ON_SUCCESS ON_SUCCESS}, the temporary directory will
86 * only be cleaned up if the test completes successfully. By default, the
87 * {@link CleanupMode#ALWAYS ALWAYS} clean up mode will be used, but this can be
88 * configured globally by setting the {@value #DEFAULT_CLEANUP_MODE_PROPERTY_NAME}
89 * configuration parameter.
90 *
91 * @since 5.4
92 */
93 @Target({ ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.PARAMETER })
94 @Retention(RetentionPolicy.RUNTIME)
95 @Documented
96 @API(status = STABLE, since = "5.10")
97 public @interface TempDir {
98
99 /**
100 * Property name used to set the default temporary directory factory class name:
101 * {@value}
102 *
103 * <h4>Supported Values</h4>
104 *
105 * <p>Supported values include fully qualified class names for types that
106 * implement {@link TempDirFactory}.
107 *
108 * <p>If not specified, the default is {@link TempDirFactory.Standard}.
109 *
110 * @since 5.10
111 */
112 @API(status = EXPERIMENTAL, since = "5.10")
113 String DEFAULT_FACTORY_PROPERTY_NAME = "junit.jupiter.tempdir.factory.default";
114
115 /**
116 * Factory for the temporary directory.
117 *
118 * <p>If the {@value #SCOPE_PROPERTY_NAME} configuration parameter is set to
119 * {@code per_context}, no custom factory is allowed.
120 *
121 * <p>Defaults to {@link TempDirFactory.Standard}.
122 *
123 * <p>As an alternative to setting this attribute, a global
124 * {@link TempDirFactory} can be configured for the entire test suite via
125 * the {@value #DEFAULT_FACTORY_PROPERTY_NAME} configuration parameter.
126 * See the User Guide for details. Note, however, that a {@code @TempDir}
127 * declaration with a custom {@code factory} always overrides a global
128 * {@code TempDirFactory}.
129 *
130 * @return the type of {@code TempDirFactory} to use
131 * @since 5.10
132 * @see TempDirFactory
133 */
134 @API(status = EXPERIMENTAL, since = "5.10")
135 Class<? extends TempDirFactory> factory() default TempDirFactory.class;
136
137 /**
138 * Property name used to set the scope of temporary directories created via
139 * the {@link TempDir @TempDir} annotation: {@value}
140 *
141 * <h4>Supported Values</h4>
142 * <ul>
143 * <li>{@code per_context}: creates a single temporary directory for the
144 * entire test class or method, depending on where it's first declared
145 * <li>{@code per_declaration}: creates separate temporary directories for
146 * each declaration site of the {@code @TempDir} annotation.
147 * </ul>
148 *
149 * <p>If not specified, the default is {@code per_declaration}.
150 *
151 * @since 5.8
152 */
153 @SuppressWarnings("DeprecatedIsStillUsed")
154 @Deprecated
155 @API(status = DEPRECATED, since = "5.9")
156 String SCOPE_PROPERTY_NAME = "junit.jupiter.tempdir.scope";
157
158 /**
159 * The name of the configuration parameter that is used to configure the
160 * default {@link CleanupMode}.
161 *
162 * <p>If this configuration parameter is not set, {@link CleanupMode#ALWAYS}
163 * will be used as the default.
164 *
165 * @since 5.9
166 */
167 @API(status = EXPERIMENTAL, since = "5.9")
168 String DEFAULT_CLEANUP_MODE_PROPERTY_NAME = "junit.jupiter.tempdir.cleanup.mode.default";
169
170 /**
171 * How the temporary directory gets cleaned up after the test completes.
172 *
173 * @since 5.9
174 */
175 @API(status = EXPERIMENTAL, since = "5.9")
176 CleanupMode cleanup() default CleanupMode.DEFAULT;
177
178 }