|
| 1 | +package com.baeldung.copyfiles; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileOutputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.OutputStream; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.nio.file.StandardCopyOption; |
| 15 | + |
| 16 | +import org.apache.commons.io.FileUtils; |
| 17 | +import org.junit.Before; |
| 18 | +import org.junit.Test; |
| 19 | +import static org.assertj.core.api.Assertions.*; |
| 20 | + |
| 21 | +public class FileCopierTest { |
| 22 | + File original = new File("src/test/resources/original.txt"); |
| 23 | + |
| 24 | + @Before |
| 25 | + public void init() throws IOException { |
| 26 | + if (!original.exists()) |
| 27 | + Files.createFile(original.toPath()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 32 | + File copied = new File("src/test/resources/copiedWithIo.txt"); |
| 33 | + try (InputStream in = new BufferedInputStream(new FileInputStream(original)); |
| 34 | + OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) { |
| 35 | + byte[] buffer = new byte[1024]; |
| 36 | + int lengthRead; |
| 37 | + while ((lengthRead = in.read(buffer)) > 0) { |
| 38 | + out.write(buffer, 0, lengthRead); |
| 39 | + out.flush(); |
| 40 | + } |
| 41 | + } |
| 42 | + assertThat(copied).exists(); |
| 43 | + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 48 | + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); |
| 49 | + FileUtils.copyFile(original, copied); |
| 50 | + assertThat(copied).exists(); |
| 51 | + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 56 | + Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); |
| 57 | + Path originalPath = original.toPath(); |
| 58 | + Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING); |
| 59 | + assertThat(copied).exists(); |
| 60 | + assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied))); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 65 | + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); |
| 66 | + com.google.common.io.Files.copy(original, copied); |
| 67 | + assertThat(copied).exists(); |
| 68 | + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); |
| 69 | + } |
| 70 | +} |
0 commit comments