|
| 1 | +package com.baeldung.download; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertThat; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URISyntaxException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.security.MessageDigest; |
| 11 | +import java.security.NoSuchAlgorithmException; |
| 12 | +import java.util.concurrent.ExecutionException; |
| 13 | + |
| 14 | +import javax.xml.bind.DatatypeConverter; |
| 15 | + |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.BeforeClass; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +public class FileDownloadIntegrationTest { |
| 21 | + |
| 22 | + static String FILE_URL = "http://ovh.net/files/1Mio.dat"; |
| 23 | + static String FILE_NAME = "file.dat"; |
| 24 | + static String FILE_MD5_HASH = "6cb91af4ed4c60c11613b75cd1fc6116"; |
| 25 | + |
| 26 | + @Test |
| 27 | + public void givenJavaIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException { |
| 28 | + |
| 29 | + FileDownload.downloadWithJavaIO(FILE_URL, FILE_NAME); |
| 30 | + assertTrue(checkMd5Hash(FILE_NAME)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenJavaNIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException { |
| 35 | + |
| 36 | + FileDownload.downloadWithJavaNIO(FILE_URL, FILE_NAME); |
| 37 | + assertTrue(checkMd5Hash(FILE_NAME)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenJava7IO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException { |
| 42 | + |
| 43 | + FileDownload.downloadWithJava7IO(FILE_URL, FILE_NAME); |
| 44 | + assertTrue(checkMd5Hash(FILE_NAME)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenAHCLibrary_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException, ExecutionException, InterruptedException { |
| 49 | + |
| 50 | + FileDownload.downloadWithAHC(FILE_URL, FILE_NAME); |
| 51 | + assertTrue(checkMd5Hash(FILE_NAME)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenApacheCommonsIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException { |
| 56 | + |
| 57 | + FileDownload.downloadWithApacheCommons(FILE_URL, FILE_NAME); |
| 58 | + assertTrue(checkMd5Hash(FILE_NAME)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void givenJavaIO_whenDownloadingFileStops_thenDownloadShouldBeResumedCorrectly() throws NoSuchAlgorithmException, IOException, URISyntaxException { |
| 63 | + |
| 64 | + ResumableDownload.downloadFileWithResume(FILE_URL, FILE_NAME); |
| 65 | + assertTrue(checkMd5Hash(FILE_NAME)); |
| 66 | + } |
| 67 | + |
| 68 | + private boolean checkMd5Hash(String filename) throws IOException, NoSuchAlgorithmException { |
| 69 | + |
| 70 | + MessageDigest md = MessageDigest.getInstance("MD5"); |
| 71 | + md.update(Files.readAllBytes(Paths.get(filename))); |
| 72 | + byte[] digest = md.digest(); |
| 73 | + String myChecksum = DatatypeConverter.printHexBinary(digest); |
| 74 | + |
| 75 | + return myChecksum.equalsIgnoreCase(FILE_MD5_HASH); |
| 76 | + } |
| 77 | + |
| 78 | + @BeforeClass |
| 79 | + public static void setup() throws IOException { |
| 80 | + if (Files.exists(Paths.get(FILE_NAME))) { |
| 81 | + Files.delete(Paths.get(FILE_NAME)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @After |
| 86 | + public void cleanup() throws IOException { |
| 87 | + if (Files.exists(Paths.get(FILE_NAME))) { |
| 88 | + Files.delete(Paths.get(FILE_NAME)); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments