Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
32 changes: 26 additions & 6 deletions src/test/java/org/apache/commons/io/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down