Skip to content

Commit

Permalink
[MRESOLVER-269] FileProcessor.write( File, InputStream ) is defunct (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
cstamas authored Nov 18, 2022
1 parent ed029a7 commit 3561187
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3561187

Please sign in to comment.