/hg/icedtea-web: Fix regression in ResourceUrlCreator due to PR1...

aazores at icedtea.classpath.org aazores at icedtea.classpath.org
Thu Oct 3 06:25:44 PDT 2013


changeset cdd42af7c0b5 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=cdd42af7c0b5
author: Andrew Azores <aazores at redhat.com>
date: Thu Oct 03 09:25:35 2013 -0400

	Fix regression in ResourceUrlCreator due to PR1204 patch

	* netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java:
	(getVersionedUrl) fix regression in previous PR1204 patch. Refactor
	to not take Resource parameter, use instance's field instead.
	(uriPartToString) new method
	* tests/netx/unit/net/sourceforge/jnlp/cache/ResourceUrlCreatorTest.java:
	new tests for ResourceUrlCreator.getVersionedUrl


diffstat:

 ChangeLog                                                              |   10 +
 netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java                |   57 ++-
 tests/netx/unit/net/sourceforge/jnlp/cache/ResourceUrlCreatorTest.java |  136 +++++++--
 3 files changed, 148 insertions(+), 55 deletions(-)

diffs (297 lines):

diff -r c797a2c1329b -r cdd42af7c0b5 ChangeLog
--- a/ChangeLog	Thu Oct 03 14:54:25 2013 +0200
+++ b/ChangeLog	Thu Oct 03 09:25:35 2013 -0400
@@ -1,3 +1,13 @@
+2013-10-03  Andrew Azores  <aazores at redhat.com>
+
+	PR1204 patch regression fix
+	* netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java:
+	(getVersionedUrl) fix regression in previous PR1204 patch. Refactor
+	to not take Resource parameter, use instance's field instead.
+	(uriPartToString) new method
+	* tests/netx/unit/net/sourceforge/jnlp/cache/ResourceUrlCreatorTest.java:
+	new tests for ResourceUrlCreator.getVersionedUrl 
+
 2013-10-03  Jacob Wisor  <gitne at gmx.de>
 
 	* netx/net/sourceforge/jnlp/controlpanel/CachePane.java:
diff -r c797a2c1329b -r cdd42af7c0b5 netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java
--- a/netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java	Thu Oct 03 14:54:25 2013 +0200
+++ b/netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java	Thu Oct 03 09:25:35 2013 -0400
@@ -41,8 +41,11 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
 import java.util.LinkedList;
 import java.util.List;
+import java.io.UnsupportedEncodingException;
 
 import net.sourceforge.jnlp.DownloadOptions;
 
@@ -91,7 +94,7 @@
             }
         }
 
-        url = getVersionedUrl(resource);
+        url = getVersionedUrl();
         urls.add(url);
 
         urls.add(resource.getLocation());
@@ -148,29 +151,47 @@
     }
 
     /**
-     * Returns the URL for a resource, including the resource's version number in the query string
-     *
-     * @param resource the resource to get the url for
+     * Returns the URL for this resource, including the resource's version number in the query string
      */
-    protected URL getVersionedUrl(Resource resource) {
+    protected URL getVersionedUrl() {
         URL resourceUrl = resource.getLocation();
+        String protocol = uriPartToString(resourceUrl.getProtocol()) + "://";
+        String userInfo = uriPartToString(resourceUrl.getUserInfo());
+        if (!userInfo.isEmpty()) {
+            userInfo += "@";
+        }
+        String host = uriPartToString(resourceUrl.getHost());
+        String port;
+        if (resourceUrl.getPort() == -1) {
+            port = "";
+        } else {
+            port = ":" + String.valueOf(resourceUrl.getPort());
+        }
+        String path = uriPartToString(resourceUrl.getPath());
+        String query = uriPartToString(resourceUrl.getQuery());
+        if (!query.isEmpty()) {
+            query = "?" + query;
+        }
+        if (resource.requestVersion != null && resource.requestVersion.isVersionId()) {
+            if (!query.isEmpty()) {
+                query += "&";
+            } else {
+                query = "?" + query;
+            }
+            query += "version-id=" + resource.requestVersion;
+        }
         try {
-            String query = resourceUrl.getQuery(); // returns null if there was no query string
-            if (resource.requestVersion != null && resource.requestVersion.isVersionId()) {
-                if (query == null) {
-                    query = "";
-                } else {
-                    query += "&";
-                }
-                query += "version-id=" + resource.requestVersion;
-            }
-            URI uri = new URI(resourceUrl.getProtocol(), resourceUrl.getUserInfo(), resourceUrl.getHost(), resourceUrl.getPort(), resourceUrl.getPath(), query, null);
-            return uri.toURL();
+            URL url = new URL(protocol + userInfo + host + port + path + query);
+            return url;
         } catch (MalformedURLException e) {
             return resourceUrl;
-        } catch (URISyntaxException e) {
-            return resourceUrl;
         }
     }
 
+    private static String uriPartToString(String part) {
+        if (part == null)
+            return "";
+        return part;
+    }
+
 }
diff -r c797a2c1329b -r cdd42af7c0b5 tests/netx/unit/net/sourceforge/jnlp/cache/ResourceUrlCreatorTest.java
--- a/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceUrlCreatorTest.java	Thu Oct 03 14:54:25 2013 +0200
+++ b/tests/netx/unit/net/sourceforge/jnlp/cache/ResourceUrlCreatorTest.java	Thu Oct 03 09:25:35 2013 -0400
@@ -1,6 +1,8 @@
 package net.sourceforge.jnlp.cache;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -12,81 +14,141 @@
 
 public class ResourceUrlCreatorTest {
 
+    private static final Version VERSION_11 = new Version("1.1");
+    private static final Version VERSION_20 = new Version("2.0");
+    private static final Version VERSION_TWO = new Version("version two");
+    private static final DownloadOptions DLOPTS_NOPACK_USEVERSION = new DownloadOptions(false, true);
+    private static final DownloadOptions DLOPTS_NOPACK_NOVERSION = new DownloadOptions(false, false);
+
+    private URL getResultUrl(String url, Version version,
+            boolean usePack /*use pack.gz suffix*/, boolean useVersion /*use version suffix*/) throws MalformedURLException {
+        Resource resource = Resource.getResource(new URL(url), version, null);
+        return ResourceUrlCreator.getUrl(resource, usePack, useVersion);
+    }
+
+    private URL getResultUrl(String url, Version version, DownloadOptions downloadOptions) throws MalformedURLException {
+        Resource resource = Resource.getResource(new URL(url), version, null);
+        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, downloadOptions);
+        return ruc.getVersionedUrl();
+    }
+
     @Test
     public void testVersionEncode() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://test.jar"), new Version("1.1"), null);
-        URL result = ResourceUrlCreator.getUrl(resource, false /*don't use pack suffix*/, true /*use version suffix*/);
-        assertEquals("http://test__V1.1.jar", result.toString());
+        URL result = getResultUrl("http://example.com/versionEncode.jar", VERSION_11, false, true);
+        assertEquals("http://example.com/versionEncode__V1.1.jar", result.toString());
     }
 
     @Test
     public void testVersionWithPeriods() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://test.with.periods.jar"), new Version("1.1"), null);
-        URL result = ResourceUrlCreator.getUrl(resource, false /*don't use pack suffix*/, true /*use version suffix*/);
-
+        URL result = getResultUrl("http://example.com/test.version.with.periods.jar", VERSION_11, false, true);
         // A previous bug had this as "test__V1.1.with.periods.jar"
-        assertEquals("http://test.with.periods__V1.1.jar", result.toString());
+        assertEquals("http://example.com/test.version.with.periods__V1.1.jar", result.toString());
     }
 
     @Test
     public void testPackEncode() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://test.jar"), new Version("1.1"), null);
-        URL result = ResourceUrlCreator.getUrl(resource, true /*use pack suffix*/, false /*don't use version suffix*/);
-        assertEquals("http://test.jar.pack.gz", result.toString());
+        URL result = getResultUrl("http://example.com/packEncode.jar", VERSION_11, true, false);
+        assertEquals("http://example.com/packEncode.jar.pack.gz", result.toString());
     }
 
     @Test
     public void testVersionAndPackEncode() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://test.jar"), new Version("1.1"), null);
-        URL result = ResourceUrlCreator.getUrl(resource, true /*use pack suffix*/, true /*use version suffix*/);
-        assertEquals("http://test__V1.1.jar.pack.gz", result.toString());
+        URL result = getResultUrl("http://example.com/versionAndPackEncode.jar", VERSION_11, true, true);
+        assertEquals("http://example.com/versionAndPackEncode__V1.1.jar.pack.gz", result.toString());
     }
 
     @Test
     public void testGetVersionedUrl() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://foo.com/bar.jar"), new Version("1.1"), null);
-        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, new DownloadOptions(false, true));
-        URL result = ruc.getVersionedUrl(resource);
-        assertEquals("http://foo.com/bar.jar?version-id=1.1", result.toString());
+        URL result = getResultUrl("http://example.com/versionedUrl.jar", VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/versionedUrl.jar?version-id=1.1", result.toString());
     }
 
     @Test
     public void testGetNonVersionIdUrl() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://foo.com/some.jar"), new Version("version two"), null);
-        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, new DownloadOptions(false, true));
-        URL result = ruc.getVersionedUrl(resource);
-        assertEquals("http://foo.com/some.jar", result.toString());
+        URL result = getResultUrl("http://example.com/nonVersionIdUrl.jar", VERSION_TWO, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/nonVersionIdUrl.jar", result.toString());
     }
 
     @Test
     public void testGetVersionedUrlWithQuery() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://bar.com/bar.jar?i=1234abcd"), new Version("1.1"), null);
-        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, new DownloadOptions(false, true));
-        URL result = ruc.getVersionedUrl(resource);
-        assertEquals("http://bar.com/bar.jar?i=1234abcd&version-id=1.1", result.toString());
+        URL result = getResultUrl("http://example.com/versionedUrlWithQuery.jar?i=1234abcd", VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/versionedUrlWithQuery.jar?i=1234abcd&version-id=1.1", result.toString());
     }
 
     @Test
     public void testGetVersionedUrlWithoutVersion() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://baz.com/bar.jar"), null, null);
-        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, new DownloadOptions(false, false));
-        URL result = ruc.getVersionedUrl(resource);
-        assertEquals("http://baz.com/bar.jar", result.toString());
+        URL result = getResultUrl("http://example.com/versionedUrlWithoutVersion.jar", null, DLOPTS_NOPACK_NOVERSION);
+        assertEquals("http://example.com/versionedUrlWithoutVersion.jar", result.toString());
     }
 
     @Test
     public void testGetVersionedUrlWithoutVersionWithQuery() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://rhat.com/bar.jar?i=1234abcd"), null, null);
-        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, new DownloadOptions(false, false));
-        URL result = ruc.getVersionedUrl(resource);
-        assertEquals("http://rhat.com/bar.jar?i=1234abcd", result.toString());
+        URL result = getResultUrl("http://example.com/versionedUrlWithoutVersionWithQuery.jar?i=1234abcd", null, DLOPTS_NOPACK_NOVERSION);
+        assertEquals("http://example.com/versionedUrlWithoutVersionWithQuery.jar?i=1234abcd", result.toString());
     }
 
     @Test
     public void testGetVersionedUrlWithLongQuery() throws MalformedURLException {
-        Resource resource = Resource.getResource(new URL("http://yyz.com/bar.jar?i=1234&j=abcd"), new Version("2.0"), null);
-        ResourceUrlCreator ruc = new ResourceUrlCreator(resource, new DownloadOptions(false, true));
-        URL result = ruc.getVersionedUrl(resource);
-        assertEquals("http://yyz.com/bar.jar?i=1234&j=abcd&version-id=2.0", result.toString());
+        URL result = getResultUrl("http://example.com/versionedUrlWithLongQuery.jar?i=1234&j=abcd", VERSION_20, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/versionedUrlWithLongQuery.jar?i=1234&j=abcd&version-id=2.0", result.toString());
     }
+
+    @Test
+    public void testPercentEncoded() throws MalformedURLException {
+        URL result = getResultUrl("http://example.com/percent encoded.jar", null, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/percent encoded.jar", result.toString());
+    }
+
+    @Test
+    public void testPercentEncodedOnlyOnce() throws MalformedURLException {
+        URL result = getResultUrl("http://example.com/percent%20encoded%20once.jar", null, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/percent%20encoded%20once.jar", result.toString());
+    }
+
+    @Test
+    public void testPartiallyEncodedUrl() throws MalformedURLException {
+        URL result = getResultUrl("http://example.com/partially encoded%20url.jar", null, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/partially encoded%20url.jar", result.toString());
+    }
+
+    @Test
+    public void testVersionedEncodedUrl() throws MalformedURLException {
+        URL result = getResultUrl("http://example.com/versioned%20encoded.jar", VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/versioned%20encoded.jar?version-id=1.1", result.toString());
+    }
+
+    @Test
+    public void testInvalidVersionedUrl() throws MalformedURLException {
+        URL result = getResultUrl("http://example.com/invalid versioned url.jar", VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com/invalid versioned url.jar?version-id=1.1", result.toString());
+    }
+
+    @Test
+    public void testLongComplexUrl() throws MalformedURLException {
+        String URL =
+            "https://example.com/,DSID=64c19c5b657df383835706571a7c7216,DanaInfo=example.com,CT=java+JICAComponents/complexOne.jar";
+        URL result = getResultUrl(URL, null, DLOPTS_NOPACK_USEVERSION);
+        assertEquals(URL, result.toString());
+    }
+
+    @Test
+    public void testLongComplexVersionedUrl() throws MalformedURLException {
+        String URL =
+            "https://example.com/,DSID=64c19c5b657df383835706571a7c7216,DanaInfo=example.com,CT=java+JICAComponents/complexTwo.jar";
+        URL result = getResultUrl(URL, VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals(URL + "?version-id=" + VERSION_11, result.toString());
+    }
+
+    @Test
+    public void testUserInfoAndVersioning() throws MalformedURLException {
+        URL result = getResultUrl("http://foo:bar@example.com/userInfoAndVersion.jar", VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://foo:bar@example.com/userInfoAndVersion.jar?version-id=1.1", result.toString());
+    }
+
+    @Test
+    public void testPortAndVersioning() throws MalformedURLException {
+        URL result = getResultUrl("http://example.com:1234/portAndVersioning.jar", VERSION_11, DLOPTS_NOPACK_USEVERSION);
+        assertEquals("http://example.com:1234/portAndVersioning.jar?version-id=1.1", result.toString());
+    }
+
 }


More information about the distro-pkg-dev mailing list