1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.file;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.FileSystems;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28
29 import org.junit.jupiter.api.AfterEach;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.io.TempDir;
32
33
34
35
36 public abstract class AbstractTempDirTest {
37
38 protected static final String SUB_DIR = "subdir";
39 protected static final String SYMLINKED_DIR = "symlinked-dir";
40
41 public static void assertCreateNewFile(final File file) throws IOException {
42 assertTrue(file.createNewFile(), file::toString);
43 }
44
45 public static void assertDelete(final boolean expected, final File file) {
46 assertEquals(expected, file.delete(), file::toString);
47 }
48
49 public static void assertMkdir(final boolean expected, final File subDir) {
50 assertEquals(expected, subDir.mkdir(), subDir::toString);
51 }
52
53
54
55
56
57
58
59
60
61
62
63 protected static Path createTempSymbolicLinkedRelativeDir(final Path rootDir) throws IOException {
64 final Path targetDir = rootDir.resolve(SUB_DIR);
65 final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
66 Files.createDirectory(targetDir);
67 return Files.createSymbolicLink(symlinkDir, targetDir);
68 }
69
70
71
72
73 @TempDir
74 public Path managedTempDirPath;
75
76
77
78
79 public File tempDirFile;
80
81
82
83
84 public Path tempDirPath;
85
86 @AfterEach
87 void afterEachDeleteTempDir() throws IOException {
88
89 if (Files.exists(tempDirPath)) {
90 PathUtils.deleteDirectory(tempDirPath);
91 }
92 }
93
94 @BeforeEach
95 public void beforeEachCreateTempDirs() throws IOException {
96 tempDirPath = Files.createDirectory(managedTempDirPath.resolve(getClass().getSimpleName()));
97 tempDirFile = tempDirPath.toFile();
98 }
99
100 @SuppressWarnings("resource")
101 protected final boolean isPosixFilePermissionsSupported() {
102 return FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
103 }
104 }