Added more HttpDownloader tests

This commit is contained in:
daniel oeh 2014-04-27 19:03:52 +02:00
parent 00ef0f6e77
commit 999490310b

View File

@ -1,6 +1,7 @@
package instrumentationTest.de.test.antennapod.service.download;
import java.io.File;
import java.io.IOException;
import android.test.InstrumentationTestCase;
import de.danoeh.antennapod.feed.FeedFile;
@ -8,6 +9,8 @@ import de.danoeh.antennapod.service.download.*;
import android.test.AndroidTestCase;
import android.util.Log;
import de.danoeh.antennapod.util.DownloadError;
import org.apache.commons.io.FileUtils;
public class HttpDownloaderTest extends InstrumentationTestCase {
private static final String TAG = "HttpDownloaderTest";
@ -17,6 +20,7 @@ public class HttpDownloaderTest extends InstrumentationTestCase {
private File destDir;
public HttpDownloaderTest() {
super();
}
@ -38,28 +42,39 @@ public class HttpDownloaderTest extends InstrumentationTestCase {
assertTrue(destDir.exists());
}
private FeedFileImpl setupFeedFile(String downloadUrl, String title) {
private FeedFileImpl setupFeedFile(String downloadUrl, String title, boolean deleteExisting) {
FeedFileImpl feedfile = new FeedFileImpl(downloadUrl);
String fileUrl = new File(destDir, title).getAbsolutePath();
File file = new File(fileUrl);
if (deleteExisting) {
Log.d(TAG, "Deleting file: " + file.delete());
}
feedfile.setFile_url(fileUrl);
return feedfile;
}
private void download(String url, String title, boolean expectedResult) {
FeedFile feedFile = setupFeedFile(url, title);
DownloadRequest request = new DownloadRequest(feedFile.getFile_url(), url, title, 0, feedFile.getTypeAsInt());
private Downloader download(String url, String title, boolean expectedResult) {
return download(url, title, expectedResult, true, null, null, true);
}
private Downloader download(String url, String title, boolean expectedResult, boolean deleteExisting, String username, String password, boolean deleteOnFail) {
FeedFile feedFile = setupFeedFile(url, title, deleteExisting);
DownloadRequest request = new DownloadRequest(feedFile.getFile_url(), url, title, 0, feedFile.getTypeAsInt(), username, password, deleteOnFail);
Downloader downloader = new HttpDownloader(request);
downloader.call();
DownloadStatus status = downloader.getResult();
assertNotNull(status);
assertTrue(status.isSuccessful() == expectedResult);
assertTrue(status.isDone());
// the file should not exist if the download has failed
assertTrue(new File(feedFile.getFile_url()).exists() == expectedResult);
// the file should not exist if the download has failed and deleteExisting was true
assertTrue(!deleteExisting || new File(feedFile.getFile_url()).exists() == expectedResult);
return downloader;
}
private static final String URL_404 = "http://httpbin.org/status/404";
private static final String URL_AUTH = "http://httpbin.org/basic-auth/user/passwd";
public void testPassingHttp() {
download("http://httpbin.org/status/200", "test200", true);
}
@ -81,12 +96,12 @@ public class HttpDownloaderTest extends InstrumentationTestCase {
}
public void test404() {
download("http://httpbin.org/status/404", "test404", false);
download(URL_404, "test404", false);
}
public void testCancel() {
final String url = "http://httpbin.org/delay/3";
FeedFileImpl feedFile = setupFeedFile(url, "delay");
FeedFileImpl feedFile = setupFeedFile(url, "delay", true);
final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt()));
Thread t = new Thread() {
@Override
@ -108,6 +123,29 @@ public class HttpDownloaderTest extends InstrumentationTestCase {
assertFalse(new File(feedFile.getFile_url()).exists());
}
public void testDeleteOnFailShouldDelete() {
Downloader downloader = download(URL_404, "testDeleteOnFailShouldDelete", false, true, null, null, true);
assertFalse(new File(downloader.getDownloadRequest().getDestination()).exists());
}
public void testDeleteOnFailShouldNotDelete() throws IOException {
String filename = "testDeleteOnFailShouldDelete";
File dest = new File(destDir, filename);
dest.delete();
assertTrue(dest.createNewFile());
Downloader downloader = download(URL_404, filename, false, false, null, null, false);
assertTrue(new File(downloader.getDownloadRequest().getDestination()).exists());
}
public void testAuthenticationShouldSucceed() {
download(URL_AUTH, "testAuthSuccess", true, true, "user", "passwd", true);
}
public void testAuthenticationShouldFail() {
Downloader downloader = download(URL_AUTH, "testAuthSuccess", false, true, "user", "Wrong passwd", true);
assertEquals(DownloadError.ERROR_UNAUTHORIZED, downloader.getResult().getReason());
}
/* TODO: replace with smaller test file
public void testUrlWithSpaces() {
download("http://acedl.noxsolutions.com/ace/Don't Call Salman Rushdie Sneezy in Finland.mp3", "testUrlWithSpaces", true);