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  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  public 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      public void testCleanDirectoryToForceDelete() throws Exception {
69          final File file = new File(tempDirFile, "restricted");
70          FileUtils.touch(file);
71  
72          // 300 = owner: WE.
73          // 500 = owner: RE.
74          // 700 = owner: RWE.
75          assumeTrue(chmod(tempDirFile, 700, false));
76  
77          // cleanDirectory calls forceDelete
78          FileUtils.cleanDirectory(tempDirFile);
79      }
80  
81      @Test
82      public void testCleanEmpty() throws Exception {
83          assertEquals(0, tempDirFile.list().length);
84  
85          FileUtils.cleanDirectory(tempDirFile);
86  
87          assertEquals(0, tempDirFile.list().length);
88      }
89  
90      @Test
91      public void testDeletesNested() throws Exception {
92          final File nested = new File(tempDirFile, "nested");
93  
94          assertTrue(nested.mkdirs());
95  
96          FileUtils.touch(new File(nested, "file"));
97  
98          assertEquals(1, tempDirFile.list().length);
99  
100         FileUtils.cleanDirectory(tempDirFile);
101 
102         assertEquals(0, tempDirFile.list().length);
103     }
104 
105     @Test
106     public void testDeletesRegular() throws Exception {
107         FileUtils.touch(new File(tempDirFile, "regular"));
108         FileUtils.touch(new File(tempDirFile, ".hidden"));
109 
110         assertEquals(2, tempDirFile.list().length);
111 
112         FileUtils.cleanDirectory(tempDirFile);
113 
114         assertEquals(0, tempDirFile.list().length);
115     }
116 
117     @DisabledOnOs(OS.WINDOWS)
118     @Test
119     public void testThrowsOnNullList() throws Exception {
120         // test won't work if we can't restrict permissions on the
121         // directory, so skip it.
122         assumeTrue(chmod(tempDirFile, 0, false));
123 
124         try {
125             // cleanDirectory calls forceDelete
126             final IOException e = assertThrows(IOException.class, () -> FileUtils.cleanDirectory(tempDirFile));
127             assertEquals("Unknown I/O error listing contents of directory: " + tempDirFile.getAbsolutePath(), e.getMessage());
128         } finally {
129             chmod(tempDirFile, 755, false);
130         }
131     }
132 
133 }