BAEL-1646 (#4255)
* Adding code for BAEL-1646 * Adding integration tests for BAEL-1646 * Using equalsIgnoreCase to match hashes. Adding cleanup methods * Corrected junit names
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.download;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FileDownloadIntegrationTest {
|
||||
|
||||
static String FILE_URL = "http://ovh.net/files/1Mio.dat";
|
||||
static String FILE_NAME = "file.dat";
|
||||
static String FILE_MD5_HASH = "6cb91af4ed4c60c11613b75cd1fc6116";
|
||||
|
||||
@Test
|
||||
public void givenJavaIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException {
|
||||
|
||||
FileDownload.downloadWithJavaIO(FILE_URL, FILE_NAME);
|
||||
assertTrue(checkMd5Hash(FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaNIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException {
|
||||
|
||||
FileDownload.downloadWithJavaNIO(FILE_URL, FILE_NAME);
|
||||
assertTrue(checkMd5Hash(FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJava7IO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException {
|
||||
|
||||
FileDownload.downloadWithJava7IO(FILE_URL, FILE_NAME);
|
||||
assertTrue(checkMd5Hash(FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAHCLibrary_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException, ExecutionException, InterruptedException {
|
||||
|
||||
FileDownload.downloadWithAHC(FILE_URL, FILE_NAME);
|
||||
assertTrue(checkMd5Hash(FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApacheCommonsIO_whenDownloadingFile_thenDownloadShouldBeCorrect() throws NoSuchAlgorithmException, IOException {
|
||||
|
||||
FileDownload.downloadWithApacheCommons(FILE_URL, FILE_NAME);
|
||||
assertTrue(checkMd5Hash(FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaIO_whenDownloadingFileStops_thenDownloadShouldBeResumedCorrectly() throws NoSuchAlgorithmException, IOException, URISyntaxException {
|
||||
|
||||
ResumableDownload.downloadFileWithResume(FILE_URL, FILE_NAME);
|
||||
assertTrue(checkMd5Hash(FILE_NAME));
|
||||
}
|
||||
|
||||
private boolean checkMd5Hash(String filename) throws IOException, NoSuchAlgorithmException {
|
||||
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(Files.readAllBytes(Paths.get(filename)));
|
||||
byte[] digest = md.digest();
|
||||
String myChecksum = DatatypeConverter.printHexBinary(digest);
|
||||
|
||||
return myChecksum.equalsIgnoreCase(FILE_MD5_HASH);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setup() throws IOException {
|
||||
if (Files.exists(Paths.get(FILE_NAME))) {
|
||||
Files.delete(Paths.get(FILE_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() throws IOException {
|
||||
if (Files.exists(Paths.get(FILE_NAME))) {
|
||||
Files.delete(Paths.get(FILE_NAME));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user