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.apache.commons.io.file.CounterAssertions.assertCounts;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.nio.file.NoSuchFileException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 public class PathUtilsDeleteDirectoryTest extends AbstractTempDirTest {
36
37 @Test
38 public void testDeleteAbsentDirectory() throws IOException {
39 final Path absent = tempDirPath.resolve("ThisDirectoryDoesNotExist");
40 assertFalse(Files.exists(absent));
41 final Class<NoSuchFileException> expectedType = NoSuchFileException.class;
42 assertThrows(expectedType, () -> PathUtils.deleteDirectory(absent));
43 assertThrows(expectedType, () -> PathUtils.deleteDirectory(absent, StandardDeleteOption.OVERRIDE_READ_ONLY));
44 assertThrows(expectedType, () -> PathUtils.deleteDirectory(absent, PathUtils.EMPTY_DELETE_OPTION_ARRAY));
45
46 Files.deleteIfExists(tempDirPath);
47 }
48
49
50
51
52 @Test
53 public void testDeleteDirectory1FileSize0() throws IOException {
54 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDirPath);
55 assertCounts(1, 1, 0, PathUtils.deleteDirectory(tempDirPath));
56
57 Files.deleteIfExists(tempDirPath);
58 }
59
60
61
62
63 private void testDeleteDirectory1FileSize0(final DeleteOption... options) throws IOException {
64
65 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDirPath);
66 assertCounts(1, 1, 0, PathUtils.deleteDirectory(tempDirPath, options));
67
68 Files.deleteIfExists(tempDirPath);
69 }
70
71 @Test
72 public void testDeleteDirectory1FileSize0NoOptions() throws IOException {
73 testDeleteDirectory1FileSize0(PathUtils.EMPTY_DELETE_OPTION_ARRAY);
74 }
75
76 @Test
77 public void testDeleteDirectory1FileSize0OverrideReadOnly() throws IOException {
78 testDeleteDirectory1FileSize0(StandardDeleteOption.OVERRIDE_READ_ONLY);
79 }
80
81
82
83
84 @Test
85 public void testDeleteDirectory1FileSize1() throws IOException {
86 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath);
87 assertCounts(1, 1, 1, PathUtils.deleteDirectory(tempDirPath));
88
89 Files.deleteIfExists(tempDirPath);
90 }
91
92
93
94
95 @Test
96 public void testDeleteDirectory2FileSize2() throws IOException {
97 PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDirPath);
98 assertCounts(3, 2, 2, PathUtils.deleteDirectory(tempDirPath));
99
100 Files.deleteIfExists(tempDirPath);
101 }
102
103
104
105
106 @Test
107 public void testDeleteEmptyDirectory() throws IOException {
108 assertCounts(1, 0, 0, PathUtils.deleteDirectory(tempDirPath));
109
110 Files.deleteIfExists(tempDirPath);
111 }
112 }