You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2022/11/18 10:24:18 UTC

[maven-resolver] branch master updated: [MRESOLVER-269] FileProcessor.write( File, InputStream ) is defunct (#222)

This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 35611879 [MRESOLVER-269] FileProcessor.write( File, InputStream ) is defunct (#222)
35611879 is described below

commit 35611879c6b5530b3bfaed6639fdd5cf696d77bd
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Fri Nov 18 11:24:14 2022 +0100

    [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
---
 .../aether/internal/impl/DefaultFileProcessor.java |  3 +-
 .../internal/impl/DefaultFileProcessorTest.java    | 33 ++++++++++++++++++++++
 .../java/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 14177ac5..d9385cbe 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.io.UncheckedIOException;
 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 class DefaultFileProcessor
     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 6793977e..4afa5342 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 @@ package org.eclipse.aether.internal.impl;
 
 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 class DefaultFileProcessorTest
         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 d350baf7..18cb2ab9 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 final class FileUtils
     }
 
     /**
-     * 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