View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link DeletingPathVisitor}.
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       * Tests a directory with one file of size 0.
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          // This will throw if not empty.
64          Files.deleteIfExists(tempDirPath);
65      }
66  
67      /**
68       * Tests a directory with one file of size 1.
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          // This will throw if not empty.
76          Files.deleteIfExists(tempDirPath);
77      }
78  
79      /**
80       * Tests a file that does not exist.
81       */
82      @Test
83      public void testDeleteFileDoesNotExist() throws IOException {
84          testDeleteFileEmpty(PathUtils.deleteFile(tempDirPath.resolve("file-does-not-exist.bin")));
85          // This will throw if not empty.
86          Files.deleteIfExists(tempDirPath);
87      }
88  
89      private void testDeleteFileEmpty(final PathCounters pathCounts) {
90          assertCounts(0, 0, 0, pathCounts);
91      }
92  
93      /**
94       * Tests an empty folder.
95       */
96      @Test
97      public void testDeleteFileEmptyDirectory() throws IOException {
98          Assertions.assertThrows(NoSuchFileException.class, () -> testDeleteFileEmpty(PathUtils.deleteFile(tempDirPath)));
99          // This will throw if not empty.
100         Files.deleteIfExists(tempDirPath);
101     }
102 
103     /**
104      * Tests a directory with one file of size 1.
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             // Fails on Windows's Ubuntu subsystem.
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         // This will throw if not empty.
119         Files.deleteIfExists(tempDirPath);
120     }
121 
122     /**
123      * Tests a directory with one file of size 1.
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             // Fails on Windows's Ubuntu subsystem.
133             assertFalse(Files.isWritable(resolved));
134             assertThrows(IOException.class, () -> PathUtils.deleteFile(resolved));
135         }
136         PathUtils.setReadOnly(resolved, false);
137         PathUtils.deleteFile(resolved);
138         // This will throw if not empty.
139         Files.deleteIfExists(tempDirPath);
140     }
141 }