-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathTempFolderTest.java
More file actions
77 lines (62 loc) · 2.31 KB
/
TempFolderTest.java
File metadata and controls
77 lines (62 loc) · 2.31 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
69
70
71
72
73
74
75
76
77
package gov.loc.repository.bagit;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
abstract public class TempFolderTest {
protected Path folder;
@BeforeEach
public void setupTempFolder() throws IOException{
folder = Files.createTempDirectory("junitTempFolder");
}
@AfterEach
public void teardownTempFolder() throws IOException{
delete(folder);
Assertions.assertFalse(Files.exists(folder));
//Assertions.assertEquals(0, Files.list(folder).count());
}
public Path createDirectory(String name) throws IOException {
Path newDirectory = folder.resolve(name);
return Files.createDirectories(newDirectory);
}
public Path createFile(String name) throws IOException {
Path newFile = folder.resolve(name);
return Files.createFile(newFile);
}
public Path copyBagToTempFolder(Path bagFolder) throws IOException{
Path bagCopyDir = createDirectory(bagFolder.getFileName() + "_copy");
Files.walkFileTree(bagFolder, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Path relative = bagFolder.relativize(file);
if(relative.getParent() != null) {
Files.createDirectories(bagCopyDir.resolve(relative.getParent()));
}
Files.copy(file, bagCopyDir.resolve(relative));
return FileVisitResult.CONTINUE;
}
});
return bagCopyDir;
}
protected void delete(Path tempDirectory) throws IOException {
Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return deleteAndContinue(file);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return deleteAndContinue(dir);
}
private FileVisitResult deleteAndContinue(Path path) throws IOException {
Files.delete(path);
return FileVisitResult.CONTINUE;
}
});
}
}