diff --git a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java --- a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java +++ b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceTrackerTest.java @@ -39,6 +39,8 @@ import static net.sourceforge.jnlp.cache.Resource.Status.CONNECTED; import static net.sourceforge.jnlp.cache.Resource.Status.DOWNLOADING; import static net.sourceforge.jnlp.cache.Resource.Status.ERROR; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.ByteArrayOutputStream; import java.io.File; @@ -49,27 +51,30 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.util.Arrays; import java.util.EnumSet; import java.util.HashMap; import java.util.List; -import net.sourceforge.jnlp.ServerAccess; -import net.sourceforge.jnlp.ServerLauncher; -import net.sourceforge.jnlp.Version; -import net.sourceforge.jnlp.runtime.JNLPRuntime; -import net.sourceforge.jnlp.util.UrlUtils; -import net.sourceforge.jnlp.util.logging.OutputController; - import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import net.sourceforge.jnlp.ServerAccess; +import net.sourceforge.jnlp.ServerLauncher; +import net.sourceforge.jnlp.Version; +import net.sourceforge.jnlp.config.DeploymentConfiguration; +import net.sourceforge.jnlp.runtime.JNLPRuntime; +import net.sourceforge.jnlp.util.UrlUtils; +import net.sourceforge.jnlp.util.logging.OutputController; + public class ResourceTrackerTest { public static ServerLauncher testServer; public static ServerLauncher testServerWithBrokenHead; + public static ServerLauncher downloadServer; private static PrintStream[] backedUpStream = new PrintStream[4]; private static ByteArrayOutputStream currentErrorStream; private static final String nameStub1 = "itw-server"; @@ -147,7 +152,7 @@ Assert.assertNull("first url should be null", u[0]); Assert.assertNull("first normalized url should be null", n[0]); for (int i = 1; i < CHANGE_BORDER; i++) { - Assert.assertTrue("url " + i + " must be equals too normalized url " + i, u[i].equals(n[i])); + assertTrue("url " + i + " must be equals too normalized url " + i, u[i].equals(n[i])); } for (int i = CHANGE_BORDER; i < n.length; i++) { Assert.assertFalse("url " + i + " must be normalized (and so not equals) too normalized url " + i, u[i].equals(n[i])); @@ -424,19 +429,59 @@ } private void assertCommonComponentsOfUrl(URL u) { - Assert.assertTrue(u.getProtocol().equals("http")); - Assert.assertTrue(u.getHost().equals("localhost")); - Assert.assertTrue(u.getPath().contains(nameStub1)); - Assert.assertTrue(u.getPath().contains(nameStub2)); + assertTrue(u.getProtocol().equals("http")); + assertTrue(u.getHost().equals("localhost")); + assertTrue(u.getPath().contains(nameStub1)); + assertTrue(u.getPath().contains(nameStub2)); ServerAccess.logOutputReprint(u.toExternalForm()); } private void assertPort(URL u, int port) { - Assert.assertTrue(u.getPort() == port); + assertTrue(u.getPort() == port); } private void assertVersion(URL u) { - Assert.assertTrue(u.getPath().contains("-2.0")); - Assert.assertTrue(u.getQuery().contains("version-id=1.0")); + assertTrue(u.getPath().contains("-2.0")); + assertTrue(u.getQuery().contains("version-id=1.0")); + } + + @BeforeClass + public static void setupDownloadServer() throws IOException { + File dir = new File(System.getProperty("java.io.tmpdir"), "itw-down"); + dir.mkdirs(); + dir.deleteOnExit(); + redirectErr(); + downloadServer = ServerAccess.getIndependentInstance(dir.getAbsolutePath(), ServerAccess.findFreePort()); + redirectErrBack(); + + JNLPRuntime.getConfiguration().setProperty(DeploymentConfiguration.KEY_USER_CACHE_DIR, System.getProperty("java.io.tmpdir") + File.separator + "tempcache"); + } + + @AfterClass + public static void teardownDownloadServer() { + downloadServer.stop(); + + CacheUtil.clearCache(); + } + + @Test + public void testDownloadResource() throws IOException { + String s = "hello"; + File f = downloadServer.getDir(); + File temp = new File(f, "resource"); + temp.createNewFile(); + Files.write(temp.toPath(), s.getBytes()); + temp.deleteOnExit(); + + URL url = downloadServer.getUrl("resource"); + + ResourceTracker rt = new ResourceTracker(); + rt.addResource(url, null, null, UpdatePolicy.FORCE); + File downloadFile = rt.getCacheFile(url); + + assertTrue(downloadFile.exists() && downloadFile.isFile()); + + String output = new String(Files.readAllBytes(downloadFile.toPath())); + assertEquals(s, output); } }