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
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.io.file.Counters.PathCounters;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 public class PathUtilsDeleteTest extends AbstractTempDirTest {
34
35 @Test
36 public void testDeleteDirectory1FileSize0() throws IOException {
37 final String fileName = "file-size-0.bin";
38 FileUtils.copyFileToDirectory(
39 Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(),
40 tempDirPath.toFile());
41 assertCounts(0, 1, 0, PathUtils.delete(tempDirPath.resolve(fileName)));
42
43 Files.deleteIfExists(tempDirPath);
44 }
45
46 private void testDeleteDirectory1FileSize0(final DeleteOption... options) throws IOException {
47 final String fileName = "file-size-0.bin";
48 FileUtils.copyFileToDirectory(
49 Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(),
50 tempDirPath.toFile());
51 assertCounts(0, 1, 0, PathUtils.delete(tempDirPath.resolve(fileName), options));
52
53 Files.deleteIfExists(tempDirPath);
54 }
55
56
57
58
59 @Test
60 public void testDeleteDirectory1FileSize0ForceOff() throws IOException {
61 testDeleteDirectory1FileSize0();
62 }
63
64
65
66
67 @Test
68 public void testDeleteDirectory1FileSize0ForceOn() throws IOException {
69 testDeleteDirectory1FileSize0();
70 }
71
72 @Test
73 public void testDeleteDirectory1FileSize0NoOption() throws IOException {
74 testDeleteDirectory1FileSize0(PathUtils.EMPTY_DELETE_OPTION_ARRAY);
75 }
76
77 @Test
78 public void testDeleteDirectory1FileSize0OverrideReadonly() throws IOException {
79 testDeleteDirectory1FileSize0(StandardDeleteOption.OVERRIDE_READ_ONLY);
80 }
81
82 @Test
83 public void testDeleteDirectory1FileSize1() throws IOException {
84 final String fileName = "file-size-1.bin";
85 FileUtils.copyFileToDirectory(
86 Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(),
87 tempDirPath.toFile());
88 assertCounts(0, 1, 1, PathUtils.delete(tempDirPath.resolve(fileName)));
89
90 Files.deleteIfExists(tempDirPath);
91 }
92
93 private void testDeleteDirectory1FileSize1(final DeleteOption... options) throws IOException {
94
95 final String fileName = "file-size-1.bin";
96 FileUtils.copyFileToDirectory(
97 Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(),
98 tempDirPath.toFile());
99 assertCounts(0, 1, 1, PathUtils.delete(tempDirPath.resolve(fileName), options));
100
101 Files.deleteIfExists(tempDirPath);
102 }
103
104
105
106
107 @Test
108 public void testDeleteDirectory1FileSize1ForceOff() throws IOException {
109 testDeleteDirectory1FileSize1();
110 }
111
112
113
114
115 @Test
116 public void testDeleteDirectory1FileSize1ForceOn() throws IOException {
117 testDeleteDirectory1FileSize1();
118 }
119
120 @Test
121 public void testDeleteDirectory1FileSize1NoOption() throws IOException {
122 testDeleteDirectory1FileSize1(PathUtils.EMPTY_DELETE_OPTION_ARRAY);
123 }
124
125 @Test
126 public void testDeleteDirectory1FileSize1OverrideReadOnly() throws IOException {
127 testDeleteDirectory1FileSize1(StandardDeleteOption.OVERRIDE_READ_ONLY);
128 }
129
130
131
132
133 @Test
134 public void testDeleteEmptyDirectory() throws IOException {
135 testDeleteEmptyDirectory(PathUtils.delete(tempDirPath));
136
137 Files.deleteIfExists(tempDirPath);
138 }
139
140
141
142
143 private void testDeleteEmptyDirectory(final DeleteOption... options) throws IOException {
144 testDeleteEmptyDirectory(PathUtils.delete(tempDirPath, options));
145
146 Files.deleteIfExists(tempDirPath);
147 }
148
149 private void testDeleteEmptyDirectory(final PathCounters pathCounts) {
150 assertCounts(1, 0, 0, pathCounts);
151 }
152
153
154
155
156 @Test
157 public void testDeleteEmptyDirectoryForceOff() throws IOException {
158 testDeleteEmptyDirectory();
159 }
160
161
162
163
164 @Test
165 public void testDeleteEmptyDirectoryForceOn() throws IOException {
166 testDeleteEmptyDirectory();
167 }
168
169 @Test
170 public void testDeleteEmptyDirectoryNoOption() throws IOException {
171 testDeleteEmptyDirectory(PathUtils.EMPTY_DELETE_OPTION_ARRAY);
172 }
173
174 @Test
175 public void testDeleteEmptyDirectoryOverrideReadOnly() throws IOException {
176 testDeleteEmptyDirectory(StandardDeleteOption.OVERRIDE_READ_ONLY);
177 }
178
179
180
181
182 @Test
183 public void testDeleteFileDoesNotExist() throws IOException {
184 assertCounts(0, 0, 0, PathUtils.deleteFile(tempDirPath.resolve("file-does-not-exist.bin")));
185
186 Files.deleteIfExists(tempDirPath);
187 }
188 }