package org.jabref.logic.net;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import org.junit.Assert;
import org.junit.Test;
public class URLDownloadTest {
@Test
public void testStringDownloadWithSetEncoding() throws IOException {
URLDownload dl = new URLDownload(new URL("http://www.google.com"));
Assert.assertTrue("google.com should contain google", dl.asString().contains("Google"));
}
@Test
public void testStringDownload() throws IOException {
URLDownload dl = new URLDownload(new URL("http://www.google.com"));
Assert.assertTrue("google.com should contain google",
dl.asString(StandardCharsets.UTF_8).contains("Google"));
}
@Test
public void testFileDownload() throws IOException {
File destination = File.createTempFile("jabref-test", ".html");
try {
URLDownload dl = new URLDownload(new URL("http://www.google.com"));
dl.toFile(destination.toPath());
Assert.assertTrue("file must exist", destination.exists());
} finally {
// cleanup
if (!destination.delete()) {
System.err.println("Cannot delete downloaded file");
}
}
}
@Test
public void testDetermineMimeType() throws IOException {
URLDownload dl = new URLDownload(new URL("http://www.google.com"));
Assert.assertTrue(dl.getMimeType().startsWith("text/html"));
}
@Test
public void downloadToTemporaryFilePathWithoutFileSavesAsTmpFile() throws IOException {
URLDownload google = new URLDownload(new URL("http://www.google.com"));
String path = google.toTemporaryFile().toString();
Assert.assertTrue(path, path.endsWith(".tmp"));
}
@Test
public void downloadToTemporaryFileKeepsName() throws IOException {
URLDownload google = new URLDownload(new URL("https://github.com/JabRef/jabref/blob/master/LICENSE.md"));
String path = google.toTemporaryFile().toString();
Assert.assertTrue(path, path.contains("LICENSE") && path.endsWith(".md"));
}
@Test
public void downloadOfFTPSucceeds() throws IOException {
URLDownload ftp = new URLDownload(new URL("ftp://ftp.informatik.uni-stuttgart.de/pub/library/ncstrl.ustuttgart_fi/INPROC-2016-15/INPROC-2016-15.pdf"));
Path path = ftp.toTemporaryFile();
Assert.assertNotNull(path);
}
@Test
public void downloadOfHttpSucceeds() throws IOException {
URLDownload ftp = new URLDownload(new URL("http://www.jabref.org"));
Path path = ftp.toTemporaryFile();
Assert.assertNotNull(path);
}
@Test
public void downloadOfHttpsSucceeds() throws IOException {
URLDownload ftp = new URLDownload(new URL("https://www.jabref.org"));
Path path = ftp.toTemporaryFile();
Assert.assertNotNull(path);
}
}