From f077bb9b465622062c01f2b37c2caa7bb423c8b8 Mon Sep 17 00:00:00 2001 From: Chad Wilson Date: Tue, 25 Jan 2022 21:48:24 +0800 Subject: [PATCH] Fix regression on copyURLToFile per its contract; reverting to earlier impl - when parent dirs need creation - when target file already exists - reverts part of f22b42618d704a09aac21db143bd7ee8d957e13a 43bd7ee8d957e13a --- .../java/org/apache/commons/io/FileUtils.java | 2 +- .../org/apache/commons/io/FileUtilsTest.java | 32 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index f042cc60f8c..1452282abcb 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -1060,7 +1060,7 @@ public static void copyToFile(final InputStream inputStream, final File file) th */ public static void copyURLToFile(final URL source, final File destination) throws IOException { try (final InputStream stream = source.openStream()) { - Files.copy(stream, destination.toPath()); + copyInputStreamToFile(stream, destination); } } diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java b/src/test/java/org/apache/commons/io/FileUtilsTest.java index 1cf902641c2..e7e6ed8b0a8 100644 --- a/src/test/java/org/apache/commons/io/FileUtilsTest.java +++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java @@ -1244,16 +1244,36 @@ public void testCopyURLToFile() throws Exception { final File file = new File(tempDirFile, getName()); file.deleteOnExit(); - // Loads resource - final String resourceName = "/java/lang/Object.class"; - FileUtils.copyURLToFile(getClass().getResource(resourceName), file); + assertContentMatchesAfterCopyURLToFileFor("/java/lang/Object.class", file); + //TODO Maybe test copy to itself like for copyFile() + } - // Tests that resuorce was copied correctly - try (InputStream fis = Files.newInputStream(file.toPath())) { + private void assertContentMatchesAfterCopyURLToFileFor(String resourceName, File destination) throws IOException { + FileUtils.copyURLToFile(getClass().getResource(resourceName), destination); + + try (InputStream fis = Files.newInputStream(destination.toPath())) { assertTrue(IOUtils.contentEquals(getClass().getResourceAsStream(resourceName), fis), "Content is not equal."); } - //TODO Maybe test copy to itself like for copyFile() + } + + @Test + public void testCopyURLToFileCreatesParentDirs() throws Exception { + + final File file = managedTempDirPath.resolve("subdir").resolve(getName()).toFile(); + file.deleteOnExit(); + + assertContentMatchesAfterCopyURLToFileFor("/java/lang/Object.class", file); + } + + @Test + public void testCopyURLToFileReplacesExisting() throws Exception { + + final File file = new File(tempDirFile, getName()); + file.deleteOnExit(); + + assertContentMatchesAfterCopyURLToFileFor("/java/lang/Object.class", file); + assertContentMatchesAfterCopyURLToFileFor("/java/lang/String.class", file); } @Test