Skip to content

Commit 3b47ce5

Browse files
committed
UrlKey to be used as cachekey instead of toString
1 parent 886e5ec commit 3b47ce5

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package net.sourceforge.jnlp.util;
2+
3+
import net.adoptopenjdk.icedteaweb.StringUtils;
4+
5+
import java.net.URL;
6+
import java.util.Locale;
7+
import java.util.Objects;
8+
9+
public class UrlKey {
10+
private String protocol;
11+
private String host;
12+
private String file;
13+
private int port;
14+
private String ref;
15+
16+
public UrlKey(final URL url) {
17+
this.protocol = url.getProtocol() != null ? url.getProtocol().toLowerCase(Locale.ENGLISH) : null;
18+
this.host = url.getHost();
19+
this.port = url.getPort();
20+
this.file = url.getFile();
21+
this.ref = url.getRef();
22+
}
23+
24+
@Override
25+
public boolean equals(final Object obj) {
26+
if (!(obj instanceof UrlKey)) {
27+
return false;
28+
}
29+
UrlKey other = ((UrlKey) obj);
30+
return Objects.equals(protocol, other.protocol) &&
31+
Objects.equals(host, other.host) &&
32+
samePort(port, other.port) &&
33+
Objects.equals(file, other.file) &&
34+
Objects.equals(ref, other.ref);
35+
}
36+
37+
private boolean samePort(int port, int other) {
38+
if (port == other) {
39+
return true;
40+
}
41+
final int defaultPort = getDefaultPort();
42+
return (port == defaultPort || port == -1) && (other == defaultPort || other == -1);
43+
}
44+
45+
private int getDefaultPort() {
46+
if ("https".equalsIgnoreCase(protocol)) {
47+
return 443;
48+
}
49+
if ("http".equalsIgnoreCase(protocol)) {
50+
return 80;
51+
}
52+
if ("ftp".equalsIgnoreCase(protocol)) {
53+
return 21;
54+
}
55+
return -1;
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return Objects.hash(protocol, host, file, ref);
61+
}
62+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package net.sourceforge.jnlp.util;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.net.URL;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.Objects;
9+
10+
public class UrlKeyTest extends TestCase {
11+
12+
public void testEqualsHttp() throws Exception {
13+
URL url1 = new URL("http://www.example.com:80/index.html?a=b#3");
14+
List<URL> urlList = Arrays.asList(
15+
new URL("http://www.example.com/index.html?a=b#3"),
16+
new URL("Http://www.example.com/index.html?a=b#3"),
17+
new URL("http://www.example.com:80/index.html?a=b#3"));
18+
19+
for (final URL url : urlList) {
20+
UrlKey key1 = new UrlKey(url1);
21+
UrlKey key2 = new UrlKey(url);
22+
23+
assertEquals(key1.hashCode(), key2.hashCode());
24+
assertEquals(key1, key2);
25+
assertEquals(key2, key1);
26+
}
27+
}
28+
29+
public void testNotEqualsHttp() throws Exception {
30+
URL url1 = new URL("http://www.example.com:80/index.html?a=b#3");
31+
List<URL> urlList = Arrays.asList(
32+
new URL("http://www.Example.com:80/index.html?a=b#3"),
33+
new URL("http://www.example.com:80/index2.html?a=b#3"),
34+
new URL("http://www.example.com:80/index.html#3"),
35+
new URL("https://www.example.com:80/index.html?a=b#3"),
36+
new URL("http://www.example.com:80/index.html?a=c#3"),
37+
new URL("http://www.example.com:80/index.html?a=b#4"),
38+
new URL("http://www.example.com:80/index.html?a=b"),
39+
new URL("http://www.example.com:80/#3"),
40+
new URL("http://:80/index.html?a=b#3")
41+
);
42+
43+
for (final URL url : urlList) {
44+
UrlKey key1 = new UrlKey(url1);
45+
UrlKey key2 = new UrlKey(url);
46+
47+
assertNotEquals(key1.hashCode(), key2.hashCode());
48+
assertNotEquals(key1, key2);
49+
assertNotEquals(key2, key1);
50+
}
51+
}
52+
53+
public void testNotEqualsDifferentPorts() throws Exception {
54+
URL url1 = new URL("http://www.example.com:80/index.html?a=b#3");
55+
URL url = new URL("http://www.example.com:81/index.html?a=b#3");
56+
57+
UrlKey key1 = new UrlKey(url1);
58+
UrlKey key2 = new UrlKey(url);
59+
60+
assertEquals(key1.hashCode(), key2.hashCode());
61+
assertNotEquals(key1, key2);
62+
assertNotEquals(key2, key1);
63+
}
64+
65+
private void assertNotEquals(Object o1, Object o2) {
66+
assertFalse(Objects.equals(o1, o2));
67+
}
68+
69+
public void testEqualsHttps() throws Exception {
70+
URL url1 = new URL("https://www.example.com:443/index.html");
71+
URL url2 = new URL("https://www.example.com/index.html");
72+
73+
UrlKey key1 = new UrlKey(url1);
74+
UrlKey key2 = new UrlKey(url2);
75+
76+
assertEquals(key1.hashCode(), key2.hashCode());
77+
assertEquals(key1, key2);
78+
assertEquals(key2, key1);
79+
}
80+
81+
public void testEqualsFtp() throws Exception {
82+
URL url1 = new URL("ftp://www.example.com:21/index.html");
83+
URL url2 = new URL("ftp://www.example.com/index.html");
84+
85+
UrlKey key1 = new UrlKey(url1);
86+
UrlKey key2 = new UrlKey(url2);
87+
88+
assertEquals(key1.hashCode(), key2.hashCode());
89+
assertEquals(key1, key2);
90+
assertEquals(key2, key1);
91+
}
92+
93+
}
94+

0 commit comments

Comments
 (0)