diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-04-27 19:03:52 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-04-27 19:03:52 +0200 |
commit | 999490310b9380c14e6e6d919e9a5899cafc0a3b (patch) | |
tree | 5d41e6938f0009371aab3d6715938f067c9ffba0 /src/instrumentationTest/de | |
parent | 00ef0f6e771f4de0230800444f50bb3a00981f0d (diff) | |
download | AntennaPod-999490310b9380c14e6e6d919e9a5899cafc0a3b.zip |
Added more HttpDownloader tests
Diffstat (limited to 'src/instrumentationTest/de')
-rw-r--r-- | src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java b/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java index 5506a3bc9..68d07e7ca 100644 --- a/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java +++ b/src/instrumentationTest/de/test/antennapod/service/download/HttpDownloaderTest.java @@ -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); - Log.d(TAG, "Deleting file: " + file.delete()); + 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); |