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  
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   * Tests {@link DeletingPathVisitor}.
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          // This will throw if not empty.
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          // This will throw if not empty.
53          Files.deleteIfExists(tempDirPath);
54      }
55  
56      /**
57       * Tests a directory with one file of size 0.
58       */
59      @Test
60      public void testDeleteDirectory1FileSize0ForceOff() throws IOException {
61          testDeleteDirectory1FileSize0();
62      }
63  
64      /**
65       * Tests a directory with one file of size 0.
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          // This will throw if not empty.
90          Files.deleteIfExists(tempDirPath);
91      }
92  
93      private void testDeleteDirectory1FileSize1(final DeleteOption... options) throws IOException {
94          // TODO Setup the test to use LinkOption.
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         // This will throw if not empty.
101         Files.deleteIfExists(tempDirPath);
102     }
103 
104     /**
105      * Tests a directory with one file of size 1.
106      */
107     @Test
108     public void testDeleteDirectory1FileSize1ForceOff() throws IOException {
109         testDeleteDirectory1FileSize1();
110     }
111 
112     /**
113      * Tests a directory with one file of size 1.
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      * Tests an empty folder.
132      */
133     @Test
134     public void testDeleteEmptyDirectory() throws IOException {
135         testDeleteEmptyDirectory(PathUtils.delete(tempDirPath));
136         // This will throw if not empty.
137         Files.deleteIfExists(tempDirPath);
138     }
139 
140     /**
141      * Tests an empty folder.
142      */
143     private void testDeleteEmptyDirectory(final DeleteOption... options) throws IOException {
144         testDeleteEmptyDirectory(PathUtils.delete(tempDirPath, options));
145         // This will throw if not empty.
146         Files.deleteIfExists(tempDirPath);
147     }
148 
149     private void testDeleteEmptyDirectory(final PathCounters pathCounts) {
150         assertCounts(1, 0, 0, pathCounts);
151     }
152 
153     /**
154      * Tests an empty folder.
155      */
156     @Test
157     public void testDeleteEmptyDirectoryForceOff() throws IOException {
158         testDeleteEmptyDirectory();
159     }
160 
161     /**
162      * Tests an empty folder.
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      * Tests a file that does not exist.
181      */
182     @Test
183     public void testDeleteFileDoesNotExist() throws IOException {
184         assertCounts(0, 0, 0, PathUtils.deleteFile(tempDirPath.resolve("file-does-not-exist.bin")));
185         // This will throw if not empty.
186         Files.deleteIfExists(tempDirPath);
187     }
188 }