HttpDownloaderTest: delete test files only if test succeeds

This helps with debugging. And since these tests run on the JVM,
deleteOnExit() actually works.
This commit is contained in:
Hans-Christoph Steiner 2016-04-05 23:30:59 +02:00
parent eb93a38cf5
commit 6b3004160f

View File

@ -32,11 +32,11 @@ public class HttpDownloaderTest {
for (String urlString : urls) { for (String urlString : urls) {
URL url = new URL(urlString); URL url = new URL(urlString);
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
destFile.deleteOnExit(); // this probably does nothing, but maybe...
HttpDownloader httpDownloader = new HttpDownloader(url, destFile); HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
httpDownloader.download(); httpDownloader.download();
assertTrue(destFile.exists()); assertTrue(destFile.exists());
assertTrue(destFile.canRead()); assertTrue(destFile.canRead());
destFile.deleteOnExit();
} }
} }
@ -46,7 +46,6 @@ public class HttpDownloaderTest {
receivedProgress = false; receivedProgress = false;
URL url = new URL(urlString); URL url = new URL(urlString);
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
destFile.deleteOnExit(); // this probably does nothing, but maybe...
HttpDownloader httpDownloader = new HttpDownloader(url, destFile); HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
httpDownloader.setListener(new Downloader.DownloaderProgressListener() { httpDownloader.setListener(new Downloader.DownloaderProgressListener() {
@Override @Override
@ -58,6 +57,7 @@ public class HttpDownloaderTest {
assertTrue(destFile.exists()); assertTrue(destFile.exists());
assertTrue(destFile.canRead()); assertTrue(destFile.canRead());
assertTrue(receivedProgress); assertTrue(receivedProgress);
destFile.deleteOnExit();
} }
} }
@ -65,39 +65,38 @@ public class HttpDownloaderTest {
public void downloadHttpBasicAuth() throws IOException, InterruptedException { public void downloadHttpBasicAuth() throws IOException, InterruptedException {
URL url = new URL("https://httpbin.org/basic-auth/myusername/supersecretpassword"); URL url = new URL("https://httpbin.org/basic-auth/myusername/supersecretpassword");
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
destFile.deleteOnExit(); // this probably does nothing, but maybe...
HttpDownloader httpDownloader = new HttpDownloader(url, destFile, "myusername", "supersecretpassword"); HttpDownloader httpDownloader = new HttpDownloader(url, destFile, "myusername", "supersecretpassword");
httpDownloader.download(); httpDownloader.download();
assertTrue(destFile.exists()); assertTrue(destFile.exists());
assertTrue(destFile.canRead()); assertTrue(destFile.canRead());
destFile.deleteOnExit();
} }
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void downloadHttpBasicAuthWrongPassword() throws IOException, InterruptedException { public void downloadHttpBasicAuthWrongPassword() throws IOException, InterruptedException {
URL url = new URL("https://httpbin.org/basic-auth/myusername/supersecretpassword"); URL url = new URL("https://httpbin.org/basic-auth/myusername/supersecretpassword");
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
destFile.deleteOnExit(); // this probably does nothing, but maybe...
HttpDownloader httpDownloader = new HttpDownloader(url, destFile, "myusername", "wrongpassword"); HttpDownloader httpDownloader = new HttpDownloader(url, destFile, "myusername", "wrongpassword");
httpDownloader.download(); httpDownloader.download();
assertFalse(destFile.exists()); assertFalse(destFile.exists());
destFile.deleteOnExit();
} }
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void downloadHttpBasicAuthWrongUsername() throws IOException, InterruptedException { public void downloadHttpBasicAuthWrongUsername() throws IOException, InterruptedException {
URL url = new URL("https://httpbin.org/basic-auth/myusername/supersecretpassword"); URL url = new URL("https://httpbin.org/basic-auth/myusername/supersecretpassword");
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
destFile.deleteOnExit(); // this probably does nothing, but maybe...
HttpDownloader httpDownloader = new HttpDownloader(url, destFile, "wrongusername", "supersecretpassword"); HttpDownloader httpDownloader = new HttpDownloader(url, destFile, "wrongusername", "supersecretpassword");
httpDownloader.download(); httpDownloader.download();
assertFalse(destFile.exists()); assertFalse(destFile.exists());
destFile.deleteOnExit();
} }
@Test @Test
public void downloadThenCancel() throws IOException, InterruptedException { public void downloadThenCancel() throws IOException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(5); final CountDownLatch latch = new CountDownLatch(2);
URL url = new URL("https://f-droid.org/repo/index.jar"); URL url = new URL("https://f-droid.org/repo/index.jar");
File destFile = File.createTempFile("dl-", ""); File destFile = File.createTempFile("dl-", "");
destFile.deleteOnExit(); // this probably does nothing, but maybe...
final HttpDownloader httpDownloader = new HttpDownloader(url, destFile); final HttpDownloader httpDownloader = new HttpDownloader(url, destFile);
httpDownloader.setListener(new Downloader.DownloaderProgressListener() { httpDownloader.setListener(new Downloader.DownloaderProgressListener() {
@Override @Override
@ -121,8 +120,9 @@ public class HttpDownloaderTest {
} }
} }
}.start(); }.start();
latch.await(100, TimeUnit.SECONDS); // either 5 progress reports or 100 seconds latch.await(100, TimeUnit.SECONDS); // either 2 progress reports or 100 seconds
httpDownloader.cancelDownload(); httpDownloader.cancelDownload();
assertTrue(receivedProgress); assertTrue(receivedProgress);
destFile.deleteOnExit();
} }
} }