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  
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   * Tests {@link DeletingPathVisitor}.
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          // This will throw if not empty.
46          Files.deleteIfExists(tempDirPath);
47      }
48  
49      /**
50       * Tests a directory with one file of size 0.
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          // This will throw if not empty.
57          Files.deleteIfExists(tempDirPath);
58      }
59  
60      /**
61       * Tests a directory with one file of size 0.
62       */
63      private void testDeleteDirectory1FileSize0(final DeleteOption... options) throws IOException {
64          // TODO Setup the test to use FileVisitOption.
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          // This will throw if not empty.
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       * Tests a directory with one file of size 1.
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          // This will throw if not empty.
89          Files.deleteIfExists(tempDirPath);
90      }
91  
92      /**
93       * Tests a directory with two subdirectories, each containing one file of size 1.
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          // This will throw if not empty.
100         Files.deleteIfExists(tempDirPath);
101     }
102 
103     /**
104      * Tests an empty folder.
105      */
106     @Test
107     public void testDeleteEmptyDirectory() throws IOException {
108         assertCounts(1, 0, 0, PathUtils.deleteDirectory(tempDirPath));
109         // This will throw if not empty.
110         Files.deleteIfExists(tempDirPath);
111     }
112 }