|
| 1 | +package com.baeldung.copyfiles; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | + |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class FileCopierTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 17 | + File copied = new File("src/test/resources/copiedWithIo.txt"); |
| 18 | + File original = new File("src/test/resources/original.txt"); |
| 19 | + copied = FileCopier.copyWithIO(original, copied); |
| 20 | + assertTrue(copied.exists()); |
| 21 | + assertTrue(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 26 | + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); |
| 27 | + File original = new File("src/test/resources/original.txt"); |
| 28 | + copied = FileCopier.copyWithCommonsIO(original, copied); |
| 29 | + assertTrue(copied.exists()); |
| 30 | + assertTrue(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
| 35 | + Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); |
| 36 | + Path original = Paths.get("src/test/resources/original.txt"); |
| 37 | + copied = FileCopier.copyWithNio(original, copied); |
| 38 | + assertTrue(Files.exists(copied)); |
| 39 | + assertTrue(Files.readAllLines(original).equals(Files.readAllLines(copied))); |
| 40 | + } |
| 41 | +} |
0 commit comments