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  
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.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotEquals;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.IOException;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  
32  import org.apache.commons.io.file.Counters.PathCounters;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.params.ParameterizedTest;
35  import org.junit.jupiter.params.provider.MethodSource;
36  
37  /**
38   * Tests {@link DeletingPathVisitor}.
39   */
40  class DeletingPathVisitorTest extends AbstractTempDirTest {
41  
42      private static final String ARGS = "org.apache.commons.io.file.TestArguments#";
43  
44      private void applyDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
45          Files.walkFileTree(tempDirPath, visitor);
46          assertCounts(1, 0, 0, visitor);
47      }
48  
49      /**
50       * Tests an empty folder.
51       */
52      @ParameterizedTest
53      @MethodSource(ARGS + "deletingPathVisitors")
54      void testDeleteEmptyDirectory(final DeletingPathVisitor visitor) throws IOException {
55          applyDeleteEmptyDirectory(visitor);
56          // This will throw if not empty.
57          Files.deleteIfExists(tempDirPath);
58      }
59  
60      /**
61       * Tests an empty folder.
62       */
63      @ParameterizedTest
64      @MethodSource(ARGS + "pathCounters")
65      void testDeleteEmptyDirectoryNullCtorArg(final PathCounters pathCounters) throws IOException {
66          applyDeleteEmptyDirectory(new DeletingPathVisitor(pathCounters, (String[]) null));
67          // This will throw if not empty.
68          Files.deleteIfExists(tempDirPath);
69      }
70  
71      /**
72       * Tests a directory with one file of size 0.
73       */
74      @ParameterizedTest
75      @MethodSource(ARGS + "deletingPathVisitors")
76      void testDeleteFolders1FileSize0(final DeletingPathVisitor visitor) throws IOException {
77          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDirPath);
78          assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor, tempDirPath));
79          // This will throw if not empty.
80          Files.deleteIfExists(tempDirPath);
81      }
82  
83      /**
84       * Tests a directory with one file of size 1.
85       */
86      @ParameterizedTest
87      @MethodSource(ARGS + "deletingPathVisitors")
88      void testDeleteFolders1FileSize1(final DeletingPathVisitor visitor) throws IOException {
89          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath);
90          assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDirPath));
91          // This will throw if not empty.
92          Files.deleteIfExists(tempDirPath);
93      }
94  
95      /**
96       * Tests a directory with one file of size 1 but skip that file.
97       */
98      @ParameterizedTest
99      @MethodSource(ARGS + "pathCounters")
100     void testDeleteFolders1FileSize1Skip(final PathCounters pathCounters) throws IOException {
101         PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDirPath);
102         final String skipFileName = "file-size-1.bin";
103         final CountingPathVisitor visitor = new DeletingPathVisitor(pathCounters, skipFileName);
104         assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor, tempDirPath));
105         final Path skippedFile = tempDirPath.resolve(skipFileName);
106         assertTrue(Files.exists(skippedFile));
107         Files.delete(skippedFile);
108     }
109 
110     /**
111      * Tests a directory with two subdirectories, each containing one file of size 1.
112      */
113     @ParameterizedTest
114     @MethodSource(ARGS + "deletingPathVisitors")
115     void testDeleteFolders2FileSize2(final DeletingPathVisitor visitor) throws IOException {
116         PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDirPath);
117         assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor, tempDirPath));
118         // This will throw if not empty.
119         Files.deleteIfExists(tempDirPath);
120     }
121 
122     @Test
123     void testEqualsHashCode() {
124         final DeletingPathVisitor visitor0 = DeletingPathVisitor.withLongCounters();
125         final DeletingPathVisitor visitor1 = DeletingPathVisitor.withLongCounters();
126         assertEquals(visitor0, visitor0);
127         assertEquals(visitor0, visitor1);
128         assertEquals(visitor1, visitor0);
129         assertEquals(visitor0.hashCode(), visitor0.hashCode());
130         assertEquals(visitor0.hashCode(), visitor1.hashCode());
131         assertEquals(visitor1.hashCode(), visitor0.hashCode());
132         visitor0.getPathCounters().getByteCounter().increment();
133         assertEquals(visitor0, visitor0);
134         assertNotEquals(visitor0, visitor1);
135         assertNotEquals(visitor1, visitor0);
136         assertEquals(visitor0.hashCode(), visitor0.hashCode());
137         assertNotEquals(visitor0.hashCode(), visitor1.hashCode());
138         assertNotEquals(visitor1.hashCode(), visitor0.hashCode());
139     }
140 
141     /**
142      * Tests https://issues.apache.org/jira/browse/IO-850
143      */
144     @Test
145     void testIO850DirectoriesAndFiles() throws IOException {
146         final Path rootDir = Files.createDirectory(managedTempDirPath.resolve("IO850"));
147         createTempSymbolicLinkedRelativeDir(rootDir);
148         final Path targetDir = rootDir.resolve(SUB_DIR);
149         final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
150         Files.write(targetDir.resolve("file0.txt"), "Hello".getBytes(StandardCharsets.UTF_8));
151         final Path subDir0 = Files.createDirectory(targetDir.resolve("subDir0"));
152         Files.write(subDir0.resolve("file1.txt"), "Hello".getBytes(StandardCharsets.UTF_8));
153         final DeletingPathVisitor visitor = DeletingPathVisitor.withLongCounters();
154         Files.walkFileTree(rootDir, visitor);
155         assertFalse(Files.exists(targetDir));
156         assertFalse(Files.exists(symlinkDir));
157         assertFalse(Files.exists(rootDir));
158         assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
159         assertTrue(visitor.getPathCounters().getFileCounter().get() > 0);
160     }
161 
162     /**
163      * Tests https://issues.apache.org/jira/browse/IO-850
164      */
165     @Test
166     void testIO850DirectoriesOnly() throws IOException {
167         final Path rootDir = Files.createDirectory(managedTempDirPath.resolve("IO850"));
168         createTempSymbolicLinkedRelativeDir(rootDir);
169         final Path targetDir = rootDir.resolve(SUB_DIR);
170         final Path symlinkDir = rootDir.resolve(SYMLINKED_DIR);
171         final DeletingPathVisitor visitor = DeletingPathVisitor.withLongCounters();
172         Files.walkFileTree(rootDir, visitor);
173         assertFalse(Files.exists(targetDir));
174         assertFalse(Files.exists(symlinkDir));
175         assertFalse(Files.exists(rootDir));
176         assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
177     }
178 }