From 35611879c6b5530b3bfaed6639fdd5cf696d77bd Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 18 Nov 2022 11:24:14 +0100 Subject: [PATCH] [MRESOLVER-269] FileProcessor.write( File, InputStream ) is defunct (#222) The `FileProcessor.write( File, InputStream )` method is completely broken, it always fails. This method is used ONLY when FileTransformer is present. Result of plain oversight and not being covered by tests. --- https://issues.apache.org/jira/browse/MRESOLVER-296 --- .../internal/impl/DefaultFileProcessor.java | 3 +- .../impl/DefaultFileProcessorTest.java | 33 +++++++++++++++++++ .../org/eclipse/aether/util/FileUtils.java | 4 ++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java index 14177ac51..d9385cbe9 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultFileProcessor.java @@ -32,6 +32,7 @@ import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import org.eclipse.aether.spi.io.FileProcessor; import org.eclipse.aether.util.ChecksumUtils; @@ -94,7 +95,7 @@ public void write( File target, String data ) public void write( File target, InputStream source ) throws IOException { - FileUtils.writeFile( target.toPath(), p -> Files.copy( source, p ) ); + FileUtils.writeFile( target.toPath(), p -> Files.copy( source, p, StandardCopyOption.REPLACE_EXISTING ) ); } public void copy( File source, File target ) diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultFileProcessorTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultFileProcessorTest.java index 6793977e3..4afa53420 100644 --- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultFileProcessorTest.java +++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/DefaultFileProcessorTest.java @@ -21,9 +21,11 @@ import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.aether.internal.impl.DefaultFileProcessor; @@ -124,4 +126,35 @@ public void progressed( ByteBuffer buffer ) target.delete(); } + @Test + public void testWrite() + throws IOException + { + String data = "testCopy\nasdf"; + File target = new File( targetDir, "testWrite.txt" ); + + fileProcessor.write( target, data ); + + assertEquals( data, TestFileUtils.readString( target ) ); + + target.delete(); + } + + /** + * Used ONLY when FileProcessor present, never otherwise. + */ + @Test + public void testWriteStream() + throws IOException + { + String data = "testCopy\nasdf"; + File target = new File( targetDir, "testWriteStream.txt" ); + + fileProcessor.write( target, new ByteArrayInputStream( data.getBytes( StandardCharsets.UTF_8 ) ) ); + + assertEquals( data, TestFileUtils.readString( target ) ); + + target.delete(); + } + } diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java index d350baf7a..18cb2ab94 100644 --- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java +++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java @@ -119,7 +119,9 @@ public void close() throws IOException } /** - * A file writer, that accepts a {@link Path} to write some content to. + * A file writer, that accepts a {@link Path} to write some content to. Note: the file denoted by path may exist, + * hence implementation have to ensure it is able to achieve its goal ("replace existing" option or equivalent + * should be used). */ @FunctionalInterface public interface FileWriter