|
| 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