Repository navigation
Expand file tree
/
Copy pathAbstractIOTest.java
More file actions
68 lines (57 loc) · 1.83 KB
/
Copy pathAbstractIOTest.java
File metadata and controls
68 lines (57 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public abstract class AbstractIOTest {
protected static ExecutorService executorService;
private Path testDirectoryPath = null;
@BeforeAll
public static void setupExecutorService() {
executorService = Executors.newFixedThreadPool(1);
}
@AfterAll
public static void shutdownExecutorService() {
executorService.shutdown();
}
@BeforeEach
public void setUpTmpDirectory() throws IOException {
Path currentDirectory = Paths.get("").toAbsolutePath();
testDirectoryPath = Files.createTempDirectory(currentDirectory, "tempTestDir");
}
@AfterEach
public void deleteTmpDirectory() throws IOException {
File dir = new File(testDirectoryPath.toString());
if (dir.exists())
Files.walkFileTree(testDirectoryPath, new TreeDeleter());
}
protected String getPlaygroundPath(String filePath) {
return testDirectoryPath.resolve(filePath).toString();
}
private static class TreeDeleter extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
if (e == null) {
Files.delete(dir);
return FileVisitResult.CONTINUE;
} else {
// directory iteration failed
throw e;
}
}
}
}