|
| 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 org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.rules.TemporaryFolder; |
| 22 | + |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | +import static org.junit.Assert.fail; |
| 28 | + |
| 29 | +/** |
| 30 | + * This class ensure the correctness of {@link FileUtils#copyDirectoryToDirectory(File, File)}. |
| 31 | + * TODO: currently does not cover happy cases |
| 32 | + * |
| 33 | + * @see FileUtils#copyDirectoryToDirectory(File, File) |
| 34 | + */ |
| 35 | +public class FileUtilsCopyDirectoryToDirectoryTestCase { |
| 36 | + |
| 37 | + @Rule |
| 38 | + public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 39 | + |
| 40 | + @Test |
| 41 | + public void copyDirectoryToDirectoryThrowsIllegalExceptionWithCorrectMessageWhenSrcDirIsNotDirectory() throws IOException { |
| 42 | + final File srcDir = temporaryFolder.newFile("notadirectory"); |
| 43 | + final File destDir = temporaryFolder.newFolder("destinationDirectory"); |
| 44 | + final String expectedMessage = String.format("Source '%s' is not a directory", srcDir); |
| 45 | + assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void copyDirectoryToDirectoryThrowsIllegalArgumentExceptionWithCorrectMessageWhenDstDirIsNotDirectory() throws IOException { |
| 50 | + final File srcDir = temporaryFolder.newFolder("sourceDirectory"); |
| 51 | + final File destDir = temporaryFolder.newFile("notadirectory"); |
| 52 | + String expectedMessage = String.format("Destination '%s' is not a directory", destDir); |
| 53 | + assertExceptionTypeAndMessage(srcDir, destDir, IllegalArgumentException.class, expectedMessage); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenSrcDirIsNull() throws IOException { |
| 58 | + final File srcDir = null; |
| 59 | + final File destinationDirectory = temporaryFolder.newFolder("destinationDirectory"); |
| 60 | + final String expectedMessage = "Source must not be null"; |
| 61 | + assertExceptionTypeAndMessage(srcDir, destinationDirectory, NullPointerException.class, expectedMessage); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void copyDirectoryToDirectoryThrowsNullPointerExceptionWithCorrectMessageWhenDstDirIsNull() throws IOException { |
| 66 | + final File srcDir = temporaryFolder.newFolder("sourceDirectory"); |
| 67 | + final File destDir = null; |
| 68 | + final String expectedMessage = "Destination must not be null"; |
| 69 | + assertExceptionTypeAndMessage(srcDir, destDir, NullPointerException.class, expectedMessage); |
| 70 | + } |
| 71 | + |
| 72 | + private static void assertExceptionTypeAndMessage(final File srcDir, final File destDir, final Class expectedExceptionType, final String expectedMessage) { |
| 73 | + try { |
| 74 | + FileUtils.copyDirectoryToDirectory(srcDir, destDir); |
| 75 | + } catch (final Exception e) { |
| 76 | + final String msg = e.getMessage(); |
| 77 | + assertEquals(expectedExceptionType, e.getClass()); |
| 78 | + assertEquals(expectedMessage, msg); |
| 79 | + return; |
| 80 | + } |
| 81 | + fail(); |
| 82 | + } |
| 83 | +} |
0 commit comments