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.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  import static org.junit.jupiter.api.Assumptions.assumeTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.nio.file.Files;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.io.file.AbstractTempDirTest;
31  import org.apache.commons.lang3.ArrayUtils;
32  import org.junit.jupiter.api.Test;
33  import org.junit.jupiter.api.condition.DisabledOnOs;
34  import org.junit.jupiter.api.condition.OS;
35  
36  /**
37   * Test cases for FileUtils.cleanDirectory() method.
38   *
39   * TODO Redo this test using
40   * {@link Files#createSymbolicLink(java.nio.file.Path, java.nio.file.Path, java.nio.file.attribute.FileAttribute...)}.
41   */
42  class FileUtilsCleanDirectoryTest extends AbstractTempDirTest {
43  
44      /** Only runs on Linux. */
45      private boolean chmod(final File file, final int mode, final boolean recurse) throws InterruptedException {
46          final List<String> args = new ArrayList<>();
47          args.add("chmod");
48  
49          if (recurse) {
50              args.add("-R");
51          }
52  
53          args.add(Integer.toString(mode));
54          args.add(file.getAbsolutePath());
55  
56          final Process proc;
57  
58          try {
59              proc = Runtime.getRuntime().exec(args.toArray(ArrayUtils.EMPTY_STRING_ARRAY));
60          } catch (final IOException e) {
61              return false;
62          }
63          return proc.waitFor() == 0;
64      }
65  
66      @DisabledOnOs(OS.WINDOWS)
67      @Test
68      void testCleanDirectoryToForceDelete() throws Exception {
69          final File file = new File(tempDirFile, "restricted");
70          FileUtils.touch(file);
71          // 300 = owner: WE.
72          // 500 = owner: RE.
73          // 700 = owner: RWE.
74          assumeTrue(chmod(tempDirFile, 700, false));
75          // cleanDirectory calls forceDelete
76          FileUtils.cleanDirectory(tempDirFile);
77          assertTrue(tempDirFile.exists());
78      }
79  
80      @Test
81      void testCleanEmpty() throws Exception {
82          assertEquals(0, tempDirFile.list().length);
83          FileUtils.cleanDirectory(tempDirFile);
84          assertTrue(tempDirFile.exists());
85          assertEquals(0, tempDirFile.list().length);
86      }
87  
88      @Test
89      void testDeletesNested() throws Exception {
90          final File nested = new File(tempDirFile, "nested");
91          assertTrue(nested.mkdirs());
92          FileUtils.touch(new File(nested, "file"));
93          assertEquals(1, tempDirFile.list().length);
94          FileUtils.cleanDirectory(tempDirFile);
95          assertTrue(tempDirFile.exists());
96          assertEquals(0, tempDirFile.list().length);
97      }
98  
99      @Test
100     void testDeletesRegular() throws Exception {
101         FileUtils.touch(new File(tempDirFile, "regular"));
102         FileUtils.touch(new File(tempDirFile, ".hidden"));
103         assertEquals(2, tempDirFile.list().length);
104         FileUtils.cleanDirectory(tempDirFile);
105         assertTrue(tempDirFile.exists());
106         assertEquals(0, tempDirFile.list().length);
107     }
108 
109     @DisabledOnOs(OS.WINDOWS)
110     @Test
111     void testThrowsOnNullList() throws Exception {
112         // test won't work if we can't restrict permissions on the directory, so skip it.
113         assumeTrue(chmod(tempDirFile, 0, false));
114         try {
115             // cleanDirectory calls forceDelete
116             final IOException e = assertThrows(IOException.class, () -> FileUtils.cleanDirectory(tempDirFile));
117             assertEquals("Unknown I/O error listing contents of directory: " + tempDirFile.getAbsolutePath(), e.getMessage());
118         } finally {
119             chmod(tempDirFile, 755, false);
120         }
121         assertTrue(tempDirFile.exists());
122     }
123 
124 }