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  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  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.nio.file.StandardCopyOption;
27  
28  import org.apache.commons.io.file.PathUtils;
29  import org.apache.commons.io.file.TempFile;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.io.TempDir;
32  
33  /**
34   * This class ensure the correctness of {@link FileUtils#copyDirectoryToDirectory(File, File)}. TODO: currently does not
35   * cover happy cases
36   *
37   * @see FileUtils#copyDirectoryToDirectory(File, File)
38   */
39  class FileUtilsCopyDirectoryToDirectoryTest {
40  
41      private static void assertExceptionTypeAndMessage(final File srcDir, final File destDir, final Class<? extends Exception> expectedExceptionType,
42              final String expectedMessage) {
43          final Exception e = assertThrows(Exception.class, () -> FileUtils.copyDirectoryToDirectory(srcDir, destDir));
44          final String msg = e.getMessage();
45          assertEquals(expectedExceptionType, e.getClass());
46          assertEquals(expectedMessage, msg);
47      }
48  
49      /** Temporary folder managed by JUnit. */
50      @TempDir
51      public File temporaryFolder;
52  
53      private void assertAclEntryList(final Path sourcePath, final Path destPath) throws IOException {
54          assertEquals(PathUtils.getAclEntryList(sourcePath), PathUtils.getAclEntryList(destPath));
55      }
56  
57      @Test
58      void testCopyDirectoryToDirectoryThrowsIllegalArgumentExceptionWithCorrectMessageWhenDstDirIsNotDirectory()
59          throws IOException {
60          final File srcDir = new File(temporaryFolder, "sourceDirectory");
61          srcDir.mkdir();
62          final File destDir = new File(temporaryFolder, "notadirectory");
63          destDir.createNewFile();
64          final String expectedMessage = String.format("Parameter 'destinationDir' is not a directory: '%s'",
65              destDir);
66          assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage);
67      }
68  
69      @Test
70      void testCopyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory()
71          throws IOException {
72          try (TempFile srcDir = TempFile.create("notadirectory", null)) {
73              final File destDir = new File(temporaryFolder, "destinationDirectory");
74              destDir.mkdirs();
75              final String expectedMessage = String.format("Parameter 'srcDir' is not a directory: '%s'", srcDir);
76              assertExceptionTypeAndMessage(srcDir.toFile(), destDir, IllegalArgumentException.class, expectedMessage);
77          }
78      }
79  
80      @Test
81      void testCopyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenDstDirIsNull() {
82          final File srcDir = new File(temporaryFolder, "sourceDirectory");
83          srcDir.mkdir();
84          final File destDir = null;
85          assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, "destinationDir");
86      }
87  
88      @Test
89      void testCopyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenSrcDirIsNull() {
90          final File srcDir = null;
91          final File destinationDirectory = new File(temporaryFolder, "destinationDirectory");
92          destinationDirectory.mkdir();
93          assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class, "sourceDir");
94      }
95  
96      @Test
97      void testCopyFileAndCheckAcl() throws IOException {
98          try (TempFile sourcePath = TempFile.create("TempOutput", ".bin")) {
99              final Path destPath = Paths.get(temporaryFolder.getAbsolutePath(), "SomeFile.bin");
100             // Test copy attributes without replace FIRST.
101             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.COPY_ATTRIBUTES);
102             assertAclEntryList(sourcePath.get(), destPath);
103             //
104             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile());
105             assertAclEntryList(sourcePath.get(), destPath);
106             //
107             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.REPLACE_EXISTING);
108             assertAclEntryList(sourcePath.get(), destPath);
109             //
110             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
111             assertAclEntryList(sourcePath.get(), destPath);
112         }
113     }
114 }