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.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertNotEquals;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.io.IOException;
28  import java.nio.file.CopyOption;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  import java.nio.file.Paths;
32  import java.nio.file.StandardCopyOption;
33  import java.util.function.Supplier;
34  
35  import org.apache.commons.io.file.Counters.PathCounters;
36  import org.apache.commons.io.filefilter.NameFileFilter;
37  import org.apache.commons.io.filefilter.TrueFileFilter;
38  import org.junit.jupiter.api.io.TempDir;
39  import org.junit.jupiter.params.ParameterizedTest;
40  import org.junit.jupiter.params.provider.MethodSource;
41  
42  /**
43   * Tests {@link CopyDirectoryVisitor}.
44   */
45  class CopyDirectoryVisitorTest extends TestArguments {
46  
47      private static final CopyOption[] EXPECTED_COPY_OPTIONS = {StandardCopyOption.REPLACE_EXISTING};
48  
49      @TempDir
50      private Path targetDir;
51  
52      /**
53       * Tests an empty folder.
54       */
55      @ParameterizedTest
56      @MethodSource("pathCounters")
57      void testCopyDirectoryEmptyFolder(final PathCounters pathCounters) throws IOException {
58          try (TempDirectory sourceDir = TempDirectory.create(getClass().getSimpleName())) {
59              final Supplier<CopyDirectoryVisitor> supplier = () -> new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS);
60              final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(supplier.get(), sourceDir.get());
61              assertCounts(1, 0, 0, visitFileTree);
62              assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
63              assertEquals(sourceDir.get(), ((AbstractPathWrapper) visitFileTree.getSourceDirectory()).get());
64              assertEquals(sourceDir, visitFileTree.getSourceDirectory());
65              assertEquals(targetDir, visitFileTree.getTargetDirectory());
66              // Tests equals and hashCode
67              assertEquals(visitFileTree, supplier.get());
68              assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
69              assertEquals(visitFileTree, visitFileTree);
70              assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
71              assertNotEquals(visitFileTree, "not");
72              assertNotEquals(visitFileTree, new DeletingPathVisitor(pathCounters));
73              assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir));
74              assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, sourceDir, sourceDir, EXPECTED_COPY_OPTIONS));
75              assertNotEquals(visitFileTree, new CopyDirectoryVisitor(pathCounters, targetDir, sourceDir, EXPECTED_COPY_OPTIONS));
76              assertNotEquals(visitFileTree, CountingPathVisitor.withLongCounters());
77          }
78      }
79  
80      /**
81       * Tests an empty folder with filters.
82       */
83      @ParameterizedTest
84      @MethodSource("pathCounters")
85      void testCopyDirectoryEmptyFolderFilters(final PathCounters pathCounters) throws IOException {
86          try (TempDirectory sourceDir = TempDirectory.create(getClass().getSimpleName())) {
87              final Supplier<CopyDirectoryVisitor> supplier = () -> new CopyDirectoryVisitor(pathCounters, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE,
88                  sourceDir, targetDir, EXPECTED_COPY_OPTIONS);
89              final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(supplier.get(), sourceDir.get());
90              assertCounts(1, 0, 0, visitFileTree);
91              assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
92              assertEquals(sourceDir, visitFileTree.getSourceDirectory());
93              assertEquals(targetDir, visitFileTree.getTargetDirectory());
94              // Tests equals and hashCode
95              assertEquals(visitFileTree, supplier.get());
96              assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
97              assertEquals(visitFileTree, visitFileTree);
98              assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
99          }
100     }
101 
102     /**
103      * Tests filters.
104      */
105     @ParameterizedTest
106     @MethodSource("pathCounters")
107     void testCopyDirectoryFilters(final PathCounters pathCounters) throws IOException {
108         final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-4");
109         final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, new NameFileFilter("file-size-1.bin"),
110             new NameFileFilter("dirs-2-file-size-4", "dirs-a-file-size-1"), sourceDir, targetDir, null),
111             sourceDir);
112         assertCounts(2, 1, 2, visitFileTree);
113         assertArrayEquals(PathUtils.EMPTY_COPY_OPTIONS, visitFileTree.getCopyOptions());
114         assertEquals(sourceDir, visitFileTree.getSourceDirectory());
115         assertEquals(targetDir, visitFileTree.getTargetDirectory());
116         assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-1.bin")));
117         assertFalse(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-2.bin")));
118         assertFalse(Files.exists(targetDir.resolve("dirs-a-file-size-2")));
119     }
120 
121     /**
122      * Tests a directory with one file of size 0.
123      */
124     @ParameterizedTest
125     @MethodSource("pathCounters")
126     void testCopyDirectoryFolders1FileSize0(final PathCounters pathCounters) throws IOException {
127         final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0");
128         final Supplier<CopyDirectoryVisitor> supplier = () -> new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS);
129         final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(supplier.get(), sourceDir);
130         assertCounts(1, 1, 0, visitFileTree);
131         assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
132         assertEquals(sourceDir, visitFileTree.getSourceDirectory());
133         assertEquals(targetDir, visitFileTree.getTargetDirectory());
134         assertTrue(Files.exists(targetDir.resolve("file-size-0.bin")));
135         // Tests equals and hashCode
136         assertEquals(visitFileTree, supplier.get());
137         assertEquals(visitFileTree.hashCode(), supplier.get().hashCode());
138         assertEquals(visitFileTree.hashCode(), visitFileTree.hashCode());
139     }
140 
141     /**
142      * Tests a directory with one file of size 1.
143      */
144     @ParameterizedTest
145     @MethodSource("pathCounters")
146     void testCopyDirectoryFolders1FileSize1(final PathCounters pathCounters) throws IOException {
147         final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1");
148         final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS),
149             sourceDir);
150         assertCounts(1, 1, 1, visitFileTree);
151         assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
152         assertEquals(sourceDir, visitFileTree.getSourceDirectory());
153         assertEquals(targetDir, visitFileTree.getTargetDirectory());
154         assertTrue(Files.exists(targetDir.resolve("file-size-1.bin")));
155     }
156 
157     /**
158      * Tests a directory with two subdirectories, each containing one file of size 1.
159      */
160     @ParameterizedTest
161     @MethodSource("pathCounters")
162     void testCopyDirectoryFolders2FileSize2(final PathCounters pathCounters) throws IOException {
163         final Path sourceDir = Paths.get("src/test/resources/org/apache/commons/io/dirs-2-file-size-2");
164         final CopyDirectoryVisitor visitFileTree = PathUtils.visitFileTree(new CopyDirectoryVisitor(pathCounters, sourceDir, targetDir, EXPECTED_COPY_OPTIONS),
165             sourceDir);
166         assertCounts(3, 2, 2, visitFileTree);
167         assertArrayEquals(EXPECTED_COPY_OPTIONS, visitFileTree.getCopyOptions());
168         assertEquals(sourceDir, visitFileTree.getSourceDirectory());
169         assertEquals(targetDir, visitFileTree.getTargetDirectory());
170         assertTrue(Files.exists(targetDir.resolve("dirs-a-file-size-1/file-size-1.bin")));
171         assertTrue(Files.exists(targetDir.resolve("dirs-b-file-size-1/file-size-1.bin")));
172     }
173 
174 }