This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new b06408a [IO-632] Add PathUtils for operations on NIO Path. b06408a is described below commit b06408a0467eac51e9e4bc13ea2d6d4cc3906ebb Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sun Oct 13 18:33:42 2019 -0400 [IO-632] Add PathUtils for operations on NIO Path. Refactor to add PathUtils.copyFileToDirectory(Path, Path, CopyOption...) --- .../java/org/apache/commons/io/file/PathUtils.java | 22 +++++++++-- .../commons/io/file/CleaningPathVisitorTest.java | 1 - .../commons/io/file/CopyDirectoryVisitorTest.java | 2 - .../commons/io/file/DeletingPathVisitorTest.java | 1 - .../io/file/PathUtilsCleanDirectoryTest.java | 1 - .../io/file/PathUtilsDeleteDirectoryTest.java | 1 - .../commons/io/file/PathUtilsDeleteFileTest.java | 11 ++---- .../org/apache/commons/io/file/PathUtilsTest.java | 46 ++++++++++++++++++++++ 8 files changed, 69 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index 7f83101..ef58503 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -48,10 +48,10 @@ public final class PathUtils { } /** - * Copies a source directory to a target directory. + * Copies a directory to another directory. * - * @param sourceDirectory The source directory - * @param targetDirectory The target directory + * @param sourceDirectory The source directory. + * @param targetDirectory The target directory. * @param copyOptions Specifies how the copying should be done. * @return The visitation path counters. * @throws IOException if an I/O error is thrown by a visitor method. @@ -64,6 +64,22 @@ public final class PathUtils { } /** + * Copies a file to a directory. + * + * @param sourceFile The source file + * @param targetDirectory The target directory. + * @param copyOptions Specifies how the copying should be done. + * @return The target file + * @throws IOException if an I/O error occurs + * @see Files#copy(Path, Path, CopyOption...) + */ + public static Path copyFileToDirectory(final Path sourceFile, final Path targetDirectory, + final CopyOption... copyOptions) throws IOException { + return Files.copy(sourceFile, targetDirectory.resolve(sourceFile.getFileName()), copyOptions); + + } + + /** * Counts aspects of a directory including sub-directories. * * @param directory directory to delete. diff --git a/src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java b/src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java index 81b7f55..1bedc41 100644 --- a/src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/CleaningPathVisitorTest.java @@ -25,7 +25,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.Counters.PathCounters; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; diff --git a/src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java b/src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java index b48cfd0..16fdb29 100644 --- a/src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/CopyDirectoryVisitorTest.java @@ -19,14 +19,12 @@ package org.apache.commons.io.file; import static org.apache.commons.io.file.CounterAssertions.assertCounts; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.Counters.PathCounters; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java b/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java index d74ae3a..4db3881 100644 --- a/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java +++ b/src/test/java/org/apache/commons/io/file/DeletingPathVisitorTest.java @@ -24,7 +24,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.Counters.PathCounters; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java index 7a77f02..41f86d4 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsCleanDirectoryTest.java @@ -25,7 +25,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java index 19f2f6c..76b0268 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteDirectoryTest.java @@ -24,7 +24,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java index 8bc15de..32710f9 100644 --- a/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java +++ b/src/test/java/org/apache/commons/io/file/PathUtilsDeleteFileTest.java @@ -25,7 +25,6 @@ import java.nio.file.NotDirectoryException; import java.nio.file.Path; import java.nio.file.Paths; -import org.apache.commons.io.FileUtils; import org.apache.commons.io.file.Counters.PathCounters; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -58,9 +57,8 @@ public class PathUtilsDeleteFileTest { @Test public void testDeleteFileDirectory1FileSize0() throws IOException { final String fileName = "file-size-0.bin"; - FileUtils.copyFileToDirectory( - Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName).toFile(), - tempDir.toFile()); + PathUtils.copyFileToDirectory( + Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-0/" + fileName), tempDir); assertCounts(0, 1, 0, PathUtils.deleteFile(tempDir.resolve(fileName))); // This will throw if not empty. Files.deleteIfExists(tempDir); @@ -72,9 +70,8 @@ public class PathUtilsDeleteFileTest { @Test public void testDeleteFileDirectory1FileSize1() throws IOException { final String fileName = "file-size-1.bin"; - FileUtils.copyFileToDirectory( - Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName).toFile(), - tempDir.toFile()); + PathUtils.copyFileToDirectory( + Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/" + fileName), tempDir); assertCounts(0, 1, 1, PathUtils.deleteFile(tempDir.resolve(fileName))); // This will throw if not empty. Files.deleteIfExists(tempDir); diff --git a/src/test/java/org/apache/commons/io/file/PathUtilsTest.java b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java new file mode 100644 index 0000000..d1e3823 --- /dev/null +++ b/src/test/java/org/apache/commons/io/file/PathUtilsTest.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.io.file; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.junit.jupiter.api.Test; + +public class PathUtilsTest extends TestArguments { + + @Test + public void testCopyFile() throws IOException { + final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName()); + try { + final Path sourceFile = Paths.get("src/test/resources/org/apache/commons/io/dirs-1-file-size-1/file-size-1.bin"); + final Path targetFile = PathUtils.copyFileToDirectory( + sourceFile, tempDir); + assertTrue(Files.exists(targetFile)); + assertEquals(Files.size(sourceFile), Files.size(targetFile)); + } finally { + PathUtils.deleteDirectory(tempDir); + } + } + +}