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 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.junit.jupiter.api.Assumptions.assumeFalse;
25
26 import java.io.IOException;
27 import java.nio.file.Files;
28 import java.nio.file.LinkOption;
29 import java.nio.file.NoSuchFileException;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32
33 import org.apache.commons.io.file.Counters.PathCounters;
34 import org.apache.commons.lang3.SystemUtils;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.Test;
37
38
39
40
41 public class PathUtilsDeleteFileTest extends AbstractTempDirTest {
42
43 @Test
44 public void testDeleteBrokenLink() throws IOException {
45 assumeFalse(SystemUtils.IS_OS_WINDOWS);
46 final Path missingFile = tempDirPath.resolve("missing.txt");
47 final Path brokenLink = tempDirPath.resolve("broken.txt");
48 Files.createSymbolicLink(brokenLink, missingFile);
49 assertTrue(Files.exists(brokenLink, LinkOption.NOFOLLOW_LINKS));
50 assertFalse(Files.exists(missingFile, LinkOption.NOFOLLOW_LINKS));
51 PathUtils.deleteFile(brokenLink);
52 assertFalse(Files.exists(brokenLink, LinkOption.NOFOLLOW_LINKS), "Symbolic link not removed");
53 }
54
55
56
57
58 @Test
59 public void testDeleteFileDirectory1FileSize0() throws IOException {
60 final String fileName = "file-size-0.bin";
61 PathUtils.copyFileToDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName), tempDirPath);
62 assertCounts(0, 1, 0, PathUtils.deleteFile(tempDirPath.resolve(fileName)));
63
64 Files.deleteIfExists(tempDirPath);
65 }
66
67
68
69
70 @Test
71 public void testDeleteFileDirectory1FileSize1() throws IOException {
72 final String fileName = "file-size-1.bin";
73 PathUtils.copyFileToDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName), tempDirPath);
74 assertCounts(0, 1, 1, PathUtils.deleteFile(tempDirPath.resolve(fileName)));
75
76 Files.deleteIfExists(tempDirPath);
77 }
78
79
80
81
82 @Test
83 public void testDeleteFileDoesNotExist() throws IOException {
84 testDeleteFileEmpty(PathUtils.deleteFile(tempDirPath.resolve("file-does-not-exist.bin")));
85
86 Files.deleteIfExists(tempDirPath);
87 }
88
89 private void testDeleteFileEmpty(final PathCounters pathCounts) {
90 assertCounts(0, 0, 0, pathCounts);
91 }
92
93
94
95
96 @Test
97 public void testDeleteFileEmptyDirectory() throws IOException {
98 Assertions.assertThrows(NoSuchFileException.class, () -> testDeleteFileEmpty(PathUtils.deleteFile(tempDirPath)));
99
100 Files.deleteIfExists(tempDirPath);
101 }
102
103
104
105
106 @Test
107 public void testDeleteReadOnlyFileDirectory1FileSize1() throws IOException {
108 final String fileName = "file-size-1.bin";
109 PathUtils.copyFileToDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName), tempDirPath);
110 final Path resolved = tempDirPath.resolve(fileName);
111 PathUtils.setReadOnly(resolved, true);
112 if (SystemUtils.IS_OS_WINDOWS) {
113
114 assertFalse(Files.isWritable(resolved));
115 assertThrows(IOException.class, () -> PathUtils.deleteFile(resolved));
116 }
117 assertCounts(0, 1, 1, PathUtils.deleteFile(resolved, StandardDeleteOption.OVERRIDE_READ_ONLY));
118
119 Files.deleteIfExists(tempDirPath);
120 }
121
122
123
124
125 @Test
126 public void testSetReadOnlyFileDirectory1FileSize1() throws IOException {
127 final String fileName = "file-size-1.bin";
128 PathUtils.copyFileToDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName), tempDirPath);
129 final Path resolved = tempDirPath.resolve(fileName);
130 PathUtils.setReadOnly(resolved, true);
131 if (SystemUtils.IS_OS_WINDOWS) {
132
133 assertFalse(Files.isWritable(resolved));
134 assertThrows(IOException.class, () -> PathUtils.deleteFile(resolved));
135 }
136 PathUtils.setReadOnly(resolved, false);
137 PathUtils.deleteFile(resolved);
138
139 Files.deleteIfExists(tempDirPath);
140 }
141 }