|
1 | 1 | package com.baeldung.copyfiles; |
2 | 2 |
|
3 | | -import static org.junit.Assert.*; |
4 | | - |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
5 | 5 | import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.io.FileOutputStream; |
6 | 8 | import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.OutputStream; |
7 | 11 | import java.nio.file.Files; |
8 | 12 | import java.nio.file.Path; |
9 | 13 | import java.nio.file.Paths; |
| 14 | +import java.nio.file.StandardCopyOption; |
10 | 15 |
|
| 16 | +import org.apache.commons.io.FileUtils; |
11 | 17 | import org.junit.Before; |
12 | 18 | import org.junit.Test; |
| 19 | +import static org.assertj.core.api.Assertions.*; |
13 | 20 |
|
14 | 21 | public class FileCopierTest { |
15 | | - File original =new File("src/test/resources/original.txt"); |
16 | | - @Before |
17 | | - public void init() throws IOException{ |
18 | | - if(!original.exists()) |
19 | | - Files.createFile(original.toPath()); |
20 | | - } |
21 | | - @Test |
22 | | - public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
23 | | - File copied = new File("src/test/resources/copiedWithIo.txt"); |
24 | | - copied = FileCopier.copyWithIO(original, copied); |
25 | | - assertTrue(copied.exists()); |
26 | | - assertTrue(Files.readAllLines(original.toPath()) |
27 | | - .equals(Files.readAllLines(copied.toPath()))); |
28 | | - } |
29 | | - |
30 | | - @Test |
31 | | - public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
32 | | - File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); |
33 | | - copied = FileCopier.copyWithCommonsIO(original, copied); |
34 | | - assertTrue(copied.exists()); |
35 | | - assertTrue(Files.readAllLines(original.toPath()) |
36 | | - .equals(Files.readAllLines(copied.toPath()))); |
37 | | - } |
38 | | - |
39 | | - @Test |
40 | | - public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { |
41 | | - Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); |
42 | | - Path originalPath = original.toPath(); |
43 | | - copied = FileCopier.copyWithNio(originalPath, copied); |
44 | | - assertTrue(Files.exists(copied)); |
45 | | - assertTrue(Files.readAllLines(originalPath) |
46 | | - .equals(Files.readAllLines(copied))); |
47 | | - } |
| 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 | + } |
48 | 70 | } |
0 commit comments