1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package libcore.java.net; 18 19import java.io.IOException; 20import java.net.Inet6Address; 21import java.net.InetAddress; 22import java.net.MalformedURLException; 23import java.net.Proxy; 24import java.net.URL; 25import java.net.URLConnection; 26import java.net.URLStreamHandler; 27import java.net.UnknownHostException; 28import junit.framework.TestCase; 29 30public class OldURLStreamHandlerTest extends TestCase { 31 32 MockURLStreamHandler handler = null; 33 34 public void test_equalsLjava_net_URLLjava_net_URL() throws MalformedURLException { 35 URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1"); 36 URL url2 = new URL("http://test_url/test?a=b&c=%D0+%D1"); 37 assertFalse(url1.equals(url2)); 38 39 new URL("http://test_url+/test?a=b&c=%D0+%D1"); 40 assertFalse(handler.equals(url1,url2)); 41 42 try { 43 assertFalse(handler.equals(null, url1)); 44 fail("NullPointerException was not thrown."); 45 } catch(NullPointerException npe) { 46 //expected 47 } 48 } 49 50 public void test_getDefaultPort() { 51 assertEquals(-1, handler.getDefaultPort()); 52 } 53 54 public void test_getHostAddress() throws MalformedURLException, UnknownHostException { 55 URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1"); 56 assertNull(handler.getHostAddress(url1)); 57 58 URL url2 = new URL("http://test:pwd@fakehostname.fakedomain/test?a=b&c=%D0+%D1"); 59 assertNull(handler.getHostAddress(url2)); 60 61 URL url3 = new URL("http://localhost/test"); 62 assertEquals(InetAddress.getByName("localhost"), handler.getHostAddress(url3)); 63 } 64 65 public void test_hashCodeLjava_net_URL() throws MalformedURLException { 66 URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1"); 67 URL url2 = new URL("http://test_url/test?a=b&c=%D0+%D1"); 68 assertTrue(handler.hashCode(url1) != handler.hashCode(url2)); 69 70 new URL("http://test_url+/test?a=b&c=%D0+%D1"); 71 assertFalse(handler.equals(url1,url2)); 72 73 try { 74 handler.hashCode(null); 75 fail("NullPointerException was not thrown."); 76 } catch(NullPointerException expected) { 77 } 78 } 79 80 public void test_hostsEqualLjava_net_URLLjava_net_URL() throws Exception { 81 URL url1 = new URL("ftp://localhost:21/*test"); 82 URL url2 = new URL("http://localhost/_test"); 83 assertTrue(handler.hostsEqual(url1, url2)); 84 85 URL url3 = new URL("http://foo/_test_goo"); 86 assertFalse(handler.hostsEqual(url1, url3)); 87 } 88 89 public void test_openConnectionLjava_net_URL() throws IOException { 90 // abstract method, it doesn't check anything 91 assertNull(handler.openConnection(null)); 92 } 93 94 public void test_openConnectionLjava_net_URLLjava_net_Proxy() { 95 try { 96 handler.openConnection(null, null); 97 fail("UnsupportedOperationException was not thrown."); 98 } catch(UnsupportedOperationException uoe) { 99 //expected 100 } catch (IOException e) { 101 fail("IOException was thrown."); 102 } 103 } 104 105 public void test_parseURLLjava_net_URLLjava_lang_StringII() 106 throws MalformedURLException { 107 String str = "http://test.org/foo?a=123&b=%D5D6D7&c=++&d="; 108 URL url = new URL("http://test.org"); 109 110 try { 111 handler.parseURL(url, str, 0, str.length()); 112 fail("SecurityException should be thrown."); 113 } catch(SecurityException se) { 114 //SecurityException is expected 115 } 116 } 117 118 public void test_sameFile() throws Exception { 119 URL url1 = new URL("http://test:pwd@localhost:80/foo/foo1.c"); 120 URL url2 = new URL("http://test:pwd@localhost:80/foo/foo1.c"); 121 URL url3 = new URL("http://test:pwd@localhost:80/foo/foo2.c"); 122 URL url4 = new URL("ftp://test:pwd@localhost:21/foo/foo2.c"); 123 URL url5 = new URL("ftp://test:pwd@localhost:21/foo/foo1/foo2.c"); 124 URL url6 = new URL("http://test/foo/foo1.c"); 125 126 assertTrue("Test case 1", handler.sameFile(url1, url2)); 127 assertFalse("Test case 2", handler.sameFile(url3, url2)); 128 assertFalse("Test case 3", handler.sameFile(url3, url4)); 129 assertFalse("Test case 4", handler.sameFile(url4, url5)); 130 assertFalse("Test case 5", handler.sameFile(url1, url6)); 131 } 132 133 public void test_setURL1() throws MalformedURLException { 134 URL url = new URL("http://test.org"); 135 136 try { 137 handler.setURL(url, "http", "localhost", 80, "foo.c", "ref"); 138 fail("SecurityException should be thrown."); 139 } catch(SecurityException expected) { 140 } 141 } 142 143 public void test_setURL2() throws MalformedURLException { 144 URL url = new URL("http://test.org"); 145 146 try { 147 handler.setURL(url, "http", "localhost", 80, "authority", 148 "user", "foo.c", "query", "ref"); 149 fail("SecurityException should be thrown."); 150 } catch(SecurityException expected) { 151 } 152 } 153 154 public void test_toExternalForm() throws MalformedURLException { 155 URL [] urls = { new URL("ftp://test_url/test?a=b&c=%D0+%D1"), 156 new URL("http://test_url/test?a=b&c=%D0+%D1"), 157 new URL("http://test:pwd@localhost:80/foo/foo1.c")}; 158 159 for(URL url : urls) { 160 assertEquals("Test case for " + url.toString(), 161 url.toString(), handler.toExternalForm(url)); 162 } 163 } 164 165 public void test_Constructor() { 166 MockURLStreamHandler msh = new MockURLStreamHandler(); 167 assertEquals(-1, msh.getDefaultPort()); 168 } 169 170 public void setUp() { 171 handler = new MockURLStreamHandler(); 172 } 173 174 class MockURLStreamHandler extends URLStreamHandler { 175 176 @Override protected URLConnection openConnection(URL arg0) throws IOException { 177 return null; 178 } 179 180 @Override public boolean equals(URL a, URL b) { 181 return super.equals(a, b); 182 } 183 184 @Override public int getDefaultPort() { 185 return super.getDefaultPort(); 186 } 187 188 @Override public InetAddress getHostAddress(URL u) { 189 return super.getHostAddress(u); 190 } 191 192 @Override public int hashCode(URL u) { 193 return super.hashCode(u); 194 } 195 196 @Override public boolean hostsEqual(URL a, URL b) { 197 return super.hostsEqual(a, b); 198 } 199 200 @Override public URLConnection openConnection(URL u, Proxy p) throws IOException { 201 return super.openConnection(u, p); 202 } 203 204 @Override public void parseURL(URL url, String spec, int start, int limit) { 205 super.parseURL(url, spec, start, limit); 206 } 207 208 @Override public boolean sameFile(URL a, URL b) { 209 return super.sameFile(a, b); 210 } 211 212 @Override public void setURL(URL u, 213 String protocol, 214 String host, 215 int port, 216 String file, 217 String ref) { 218 super.setURL(u, protocol, host, port, file, ref); 219 } 220 221 @Override public void setURL(URL u, 222 String protocol, 223 String host, 224 int port, 225 String authority, 226 String userInfo, 227 String path, 228 String query, 229 String ref) { 230 super.setURL(u, protocol, host, port, authority, 231 userInfo, path, query, ref); 232 } 233 234 @Override public String toExternalForm(URL u) { 235 return super.toExternalForm(u); 236 } 237 } 238} 239