Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/io/TempDirFactory.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.EXPERIMENTAL;
14
15 import java.io.Closeable;
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19
20 import org.apiguardian.api.API;
21 import org.junit.jupiter.api.extension.AnnotatedElementContext;
22 import org.junit.jupiter.api.extension.ExtensionContext;
23
24 /**
25 * {@code TempDirFactory} defines the SPI for creating temporary directories
26 * programmatically.
27 *
28 * <p>A temporary directory factory is typically used to gain control over the
29 * temporary directory creation, like defining the parent directory or the file
30 * system that should be used.
31 *
32 * <p>Implementations must provide a no-args constructor and should not make any
33 * assumptions regarding when and how many times they are instantiated, but they
34 * can assume that {@link #createTempDirectory(AnnotatedElementContext, ExtensionContext)}
35 * and {@link #close()} will both be called once per instance, in this order,
36 * and from the same thread.
37 *
38 * <p>A {@link TempDirFactory} can be configured <em>globally</em> for the
39 * entire test suite via the {@value TempDir#DEFAULT_FACTORY_PROPERTY_NAME}
40 * configuration parameter (see the User Guide for details) or <em>locally</em>
41 * for a test class field or method parameter via the {@link TempDir @TempDir}
42 * annotation.
43 *
44 * @since 5.10
45 * @see TempDir @TempDir
46 */
47 @FunctionalInterface
48 @API(status = EXPERIMENTAL, since = "5.10")
49 public interface TempDirFactory extends Closeable {
50
51 /**
52 * Create a new temporary directory.
53 *
54 * <p>Depending on the implementation, the resulting {@link Path} may or may
55 * not be associated with the {@link java.nio.file.FileSystems#getDefault()
56 * default FileSystem}.
57 *
58 * @param elementContext the context of the field or parameter where
59 * {@code @TempDir} is declared; never {@code null}
60 * @param extensionContext the current extension context; never {@code null}
61 * @return the path to the newly created temporary directory; never {@code null}
62 * @throws Exception in case of failures
63 */
64 Path createTempDirectory(AnnotatedElementContext elementContext, ExtensionContext extensionContext)
65 throws Exception;
66
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 default void close() throws IOException {
72 }
73
74 /**
75 * Standard {@link TempDirFactory} implementation which delegates to
76 * {@link Files#createTempDirectory} using {@code "junit"} as the prefix.
77 *
78 * @see Files#createTempDirectory(java.lang.String, java.nio.file.attribute.FileAttribute[])
79 */
80 class Standard implements TempDirFactory {
81
82 public static final TempDirFactory INSTANCE = new Standard();
83
84 private static final String TEMP_DIR_PREFIX = "junit";
85
86 public Standard() {
87 }
88
89 @Override
90 public Path createTempDirectory(AnnotatedElementContext elementContext, ExtensionContext extensionContext)
91 throws IOException {
92 return Files.createTempDirectory(TEMP_DIR_PREFIX);
93 }
94
95 }
96
97 }