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  
18  package org.apache.commons.io.file;
19  
20  import static org.apache.commons.io.file.CounterAssertions.assertCounts;
21  
22  import java.io.IOException;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.io.TempDir;
28  
29  /**
30   * Tests {@link DeletingPathVisitor}.
31   */
32  public class PathUtilsCleanDirectoryTest {
33  
34      @TempDir
35      private Path tempDir;
36  
37      /**
38       * Tests a directory with one file of size 0.
39       */
40      @Test
41      public void testCleanDirectory1FileSize0() throws IOException {
42          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0"), tempDir);
43          assertCounts(1, 1, 0, PathUtils.cleanDirectory(tempDir));
44      }
45  
46      /**
47       * Tests a directory with one file of size 1.
48       */
49      @Test
50      public void testCleanDirectory1FileSize1() throws IOException {
51          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1"), tempDir);
52          assertCounts(1, 1, 1, PathUtils.cleanDirectory(tempDir));
53      }
54  
55      /**
56       * Tests a directory with two subdirectories, each containing one file of size 1.
57       */
58      @Test
59      public void testCleanDirectory2FileSize2() throws IOException {
60          PathUtils.copyDirectory(Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2"), tempDir);
61          assertCounts(3, 2, 2, PathUtils.cleanDirectory(tempDir));
62      }
63  
64      /**
65       * Tests an empty folder.
66       */
67      @Test
68      public void testCleanEmptyDirectory() throws IOException {
69          assertCounts(1, 0, 0, PathUtils.cleanDirectory(tempDir));
70      }
71  }