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.fail;
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  public class FileUtilsCopyDirectoryToDirectoryTest {
40  
41      private static void assertExceptionTypeAndMessage(final File srcDir, final File destDir,
42          final Class<? extends Exception> expectedExceptionType, final String expectedMessage) {
43          try {
44              FileUtils.copyDirectoryToDirectory(srcDir, destDir);
45          } catch (final Exception e) {
46              final String msg = e.getMessage();
47              assertEquals(expectedExceptionType, e.getClass());
48              assertEquals(expectedMessage, msg);
49              return;
50          }
51          fail();
52      }
53  
54      /** Temporary folder managed by JUnit. */
55      @TempDir
56      public File temporaryFolder;
57  
58      private void assertAclEntryList(final Path sourcePath, final Path destPath) throws IOException {
59          assertEquals(PathUtils.getAclEntryList(sourcePath), PathUtils.getAclEntryList(destPath));
60      }
61  
62      @Test
63      public void testCopyDirectoryToDirectoryThrowsIllegalArgumentExceptionWithCorrectMessageWhenDstDirIsNotDirectory()
64          throws IOException {
65          final File srcDir = new File(temporaryFolder, "sourceDirectory");
66          srcDir.mkdir();
67          final File destDir = new File(temporaryFolder, "notadirectory");
68          destDir.createNewFile();
69          final String expectedMessage = String.format("Parameter 'destinationDir' is not a directory: '%s'",
70              destDir);
71          assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage);
72      }
73  
74      @Test
75      public void testCopyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory()
76          throws IOException {
77          try (TempFile srcDir = TempFile.create("notadirectory", null)) {
78              final File destDir = new File(temporaryFolder, "destinationDirectory");
79              destDir.mkdirs();
80              final String expectedMessage = String.format("Parameter 'srcDir' is not a directory: '%s'", srcDir);
81              assertExceptionTypeAndMessage(srcDir.toFile(), destDir, IllegalArgumentException.class, expectedMessage);
82          }
83      }
84  
85      @Test
86      public void testCopyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenDstDirIsNull() {
87          final File srcDir = new File(temporaryFolder, "sourceDirectory");
88          srcDir.mkdir();
89          final File destDir = null;
90          assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, "destinationDir");
91      }
92  
93      @Test
94      public void testCopyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenSrcDirIsNull() {
95          final File srcDir = null;
96          final File destinationDirectory = new File(temporaryFolder, "destinationDirectory");
97          destinationDirectory.mkdir();
98          assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class, "sourceDir");
99      }
100 
101     @Test
102     public void testCopyFileAndCheckAcl() throws IOException {
103         try (TempFile sourcePath = TempFile.create("TempOutput", ".bin")) {
104             final Path destPath = Paths.get(temporaryFolder.getAbsolutePath(), "SomeFile.bin");
105             // Test copy attributes without replace FIRST.
106             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.COPY_ATTRIBUTES);
107             assertAclEntryList(sourcePath.get(), destPath);
108             //
109             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile());
110             assertAclEntryList(sourcePath.get(), destPath);
111             //
112             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.REPLACE_EXISTING);
113             assertAclEntryList(sourcePath.get(), destPath);
114             //
115             FileUtils.copyFile(sourcePath.toFile(), destPath.toFile(), true, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
116             assertAclEntryList(sourcePath.get(), destPath);
117         }
118     }
119 }