Use Java Files utilities in FileCopyUtils

Use helpers from java.nio.file.Files for some methods in FileCopyUtils.

Additionally, add unit tests for the various file-related methods.

Signed-off-by: Patrick Strawderman <pstrawderman@netflix.com>
This commit is contained in:
Patrick Strawderman
2026-01-28 17:46:09 +01:00
committed by Juergen Hoeller
parent 5d33de943b
commit bf5386712c
2 changed files with 30 additions and 3 deletions
@@ -21,9 +21,12 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
@@ -87,4 +90,29 @@ class FileCopyUtilsTests {
assertThat(result).isEqualTo(content);
}
@Test
void copyFile(@TempDir Path tempDir) throws IOException {
Path source = tempDir.resolve("src");
Path target = tempDir.resolve("target");
Files.write(source, "content".getBytes());
int bytesWritten = FileCopyUtils.copy(source.toFile(), target.toFile());
assertThat(bytesWritten).isEqualTo(7);
assertThat(target).exists();
assertThat(target).content().isEqualTo("content");
}
@Test
void copyFileToByteArray(@TempDir Path tempDir) throws IOException {
Path source = tempDir.resolve("src");
Files.write(source, "content".getBytes());
assertThat(FileCopyUtils.copyToByteArray(source.toFile())).asString().isEqualTo("content");
}
@Test
void copyByteArrayToFile(@TempDir Path tempDir) throws IOException {
Path target = tempDir.resolve("target");
FileCopyUtils.copy("content".getBytes(), target.toFile());
assertThat(target).exists();
assertThat(target).content().isEqualTo("content");
}
}