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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.nio.charset.Charset;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  import java.nio.file.attribute.PosixFilePermission;
30  import java.util.Set;
31  
32  import org.apache.commons.io.file.AbstractTempDirTest;
33  import org.apache.commons.io.file.PathUtils;
34  import org.apache.commons.io.file.StandardDeleteOption;
35  import org.apache.commons.io.function.IOConsumer;
36  import org.junit.jupiter.api.Test;
37  import org.junit.jupiter.api.condition.DisabledOnOs;
38  import org.junit.jupiter.api.condition.OS;
39  
40  /**
41   * Tests <a href="https://issues.apache.org/jira/browse/IO-751">IO-751</a>.
42   * <p>
43   * Must be run on a POSIX file system, macOS or Linux, disabled on Windows.
44   * </p>
45   */
46  @DisabledOnOs(OS.WINDOWS)
47  public class DeleteDirectoryTest extends AbstractTempDirTest {
48  
49      private void testDeleteDirectory(final IOConsumer<Path> deleter) throws IOException {
50          // Create a test file
51          final String contents = "Hello!";
52          final Path file = tempDirPath.resolve("file.txt");
53          final Charset charset = StandardCharsets.UTF_8;
54          PathUtils.writeString(file, contents, charset);
55          final Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(file);
56          // Sanity check: Owner has write permission on the new file
57          assertTrue(permissions.contains(PosixFilePermission.OWNER_WRITE), permissions::toString);
58  
59          // Create a test directory
60          final Path testDir = tempDirPath.resolve("dir");
61          Files.createDirectory(testDir);
62  
63          // Inside the test directory, create a symlink to the test file
64          final Path symLink = testDir.resolve("symlink.txt");
65          Files.createSymbolicLink(symLink, file);
66          // Sanity check: The symlink really points to the test file
67          assertEquals(contents, PathUtils.readString(symLink, charset));
68  
69          // Delete the test directory using the given implementation
70          deleter.accept(testDir);
71          // Symlink is gone -- passes
72          assertFalse(Files.exists(symLink), symLink::toString);
73          // The test file still exists -- passes
74          assertTrue(Files.exists(file), file::toString);
75          // The permissions of the test file should still be the same
76          assertEquals(permissions, Files.getPosixFilePermissions(file), file::toString);
77      }
78  
79      @Test
80      public void testDeleteDirectoryWithFileUtils() throws IOException {
81          testDeleteDirectory(dir -> FileUtils.deleteDirectory(dir.toFile()));
82      }
83  
84      @Test
85      public void testDeleteDirectoryWithPathUtils() throws IOException {
86          testDeleteDirectory(PathUtils::deleteDirectory);
87      }
88  
89      @Test
90      public void testDeleteDirectoryWithPathUtilsOverrideReadOnly() throws IOException {
91          testDeleteDirectory(dir -> PathUtils.deleteDirectory(dir, StandardDeleteOption.OVERRIDE_READ_ONLY));
92      }
93  
94      @Test
95      @DisabledOnOs(OS.LINUX) // TODO
96      public void testDeleteFileCheckParentAccess() throws IOException {
97          // Create a test directory
98          final Path testDir = tempDirPath.resolve("dir");
99          Files.createDirectory(testDir);
100 
101         // Create a test file
102         final Path file = testDir.resolve("file.txt");
103         final Charset charset = StandardCharsets.UTF_8;
104         PathUtils.writeString(file, "Hello!", charset);
105 
106         // A file is RO in POSIX if the parent is not W and not E.
107         PathUtils.setReadOnly(file, true);
108         final Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(testDir);
109         assertFalse(Files.isWritable(testDir),
110                 () -> String.format("Parent directory '%s' of '%s' should NOT be Writable, permissions are %s ", testDir, file, permissions));
111         assertFalse(Files.isExecutable(testDir),
112                 () -> String.format("Parent directory '%s' of '%s' should NOT be Executable, permissions are %s ", testDir, file, permissions));
113 
114         assertThrows(IOException.class, () -> PathUtils.delete(file));
115         // Nothing happened, we're not even allowed to test attributes, so the file seems deleted, but it is not.
116 
117         PathUtils.delete(file, StandardDeleteOption.OVERRIDE_READ_ONLY);
118 
119         assertFalse(Files.exists(file));
120 
121         assertEquals(permissions, Files.getPosixFilePermissions(testDir), testDir::toString);
122         assertFalse(Files.isWritable(testDir));
123         assertFalse(Files.isExecutable(testDir));
124     }
125 }