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    *      https://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  package org.apache.commons.io;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.BufferedOutputStream;
25  import java.io.File;
26  import java.io.IOException;
27  import java.nio.file.Files;
28  
29  import org.apache.commons.io.test.TestUtils;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.io.TempDir;
32  
33  /**
34   * Tests {@link FileDeleteStrategy}.
35   */
36  class FileDeleteStrategyTest {
37  
38      @TempDir
39      public File temporaryFolder;
40  
41      @Test
42      void testDeleteForce() throws Exception {
43          final File baseDir = temporaryFolder;
44          final File subDir = new File(baseDir, "test");
45          assertTrue(subDir.mkdir());
46          final File subFile = new File(subDir, "a.txt");
47          if (!subFile.getParentFile().exists()) {
48              throw new IOException("Cannot create file " + subFile
49                      + " as the parent directory does not exist");
50          }
51          try (BufferedOutputStream output =
52                  new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) {
53              TestUtils.generateTestData(output, 16);
54          }
55  
56          assertTrue(subDir.exists());
57          assertTrue(subFile.exists());
58          // delete dir
59          FileDeleteStrategy.FORCE.delete(subDir);
60          assertFalse(subDir.exists());
61          assertFalse(subFile.exists());
62          // delete dir
63          FileDeleteStrategy.FORCE.delete(subDir);  // no error
64          assertFalse(subDir.exists());
65      }
66  
67      @Test
68      void testDeleteNormal() throws Exception {
69          final File baseDir = temporaryFolder;
70          final File subDir = new File(baseDir, "test");
71          assertTrue(subDir.mkdir());
72          final File subFile = new File(subDir, "a.txt");
73          if (!subFile.getParentFile().exists()) {
74              throw new IOException("Cannot create file " + subFile
75                      + " as the parent directory does not exist");
76          }
77          try (BufferedOutputStream output =
78                  new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) {
79              TestUtils.generateTestData(output, 16);
80          }
81  
82          assertTrue(subDir.exists());
83          assertTrue(subFile.exists());
84          // delete dir
85          assertThrows(IOException.class, () -> FileDeleteStrategy.NORMAL.delete(subDir));
86          assertTrue(subDir.exists());
87          assertTrue(subFile.exists());
88          // delete file
89          FileDeleteStrategy.NORMAL.delete(subFile);
90          assertTrue(subDir.exists());
91          assertFalse(subFile.exists());
92          // delete dir
93          FileDeleteStrategy.NORMAL.delete(subDir);
94          assertFalse(subDir.exists());
95          // delete dir
96          FileDeleteStrategy.NORMAL.delete(subDir);  // no error
97          assertFalse(subDir.exists());
98      }
99  
100     @Test
101     void testDeleteNull() throws Exception {
102         assertThrows(NullPointerException.class, () -> FileDeleteStrategy.NORMAL.delete(null));
103         assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(null));
104     }
105 
106     @Test
107     void testDeleteQuietlyNormal() throws Exception {
108         final File baseDir = temporaryFolder;
109         final File subDir = new File(baseDir, "test");
110         assertTrue(subDir.mkdir());
111         final File subFile = new File(subDir, "a.txt");
112         if (!subFile.getParentFile().exists()) {
113             throw new IOException("Cannot create file " + subFile
114                     + " as the parent directory does not exist");
115         }
116         try (BufferedOutputStream output =
117                 new BufferedOutputStream(Files.newOutputStream(subFile.toPath()))) {
118             TestUtils.generateTestData(output, 16);
119         }
120 
121         assertTrue(subDir.exists());
122         assertTrue(subFile.exists());
123         // delete dir
124         assertFalse(FileDeleteStrategy.NORMAL.deleteQuietly(subDir));
125         assertTrue(subDir.exists());
126         assertTrue(subFile.exists());
127         // delete file
128         assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(subFile));
129         assertTrue(subDir.exists());
130         assertFalse(subFile.exists());
131         // delete dir
132         assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(subDir));
133         assertFalse(subDir.exists());
134         // delete dir
135         assertTrue(FileDeleteStrategy.NORMAL.deleteQuietly(subDir));  // no error
136         assertFalse(subDir.exists());
137     }
138 
139     @Test
140     void testToString() {
141         assertEquals("FileDeleteStrategy[Normal]", FileDeleteStrategy.NORMAL.toString());
142         assertEquals("FileDeleteStrategy[Force]", FileDeleteStrategy.FORCE.toString());
143     }
144 }