OldURLStreamHandlerTest.java revision 7e00db4156e50ce5f20fefb820dca339299134d3
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        String loopback = getLoopbackAddressAsHost();
82
83        URL url1 = new URL("ftp://localhost:21/*test");
84        URL url2 = new URL("http://" + loopback + "/_test");
85        assertTrue(handler.hostsEqual(url1, url2));
86
87        URL url3 = new URL("http://foo/_test_goo");
88        assertFalse(handler.hostsEqual(url1, url3));
89    }
90
91    public void test_openConnectionLjava_net_URL() throws IOException {
92        // abstract method, it doesn't check anything
93        assertNull(handler.openConnection(null));
94    }
95
96    public void test_openConnectionLjava_net_URLLjava_net_Proxy() {
97        try {
98            handler.openConnection(null, null);
99            fail("UnsupportedOperationException was not thrown.");
100        } catch(UnsupportedOperationException  uoe) {
101            //expected
102        } catch (IOException e) {
103            fail("IOException was thrown.");
104        }
105    }
106
107    public void test_parseURLLjava_net_URLLjava_lang_StringII()
108                                                throws MalformedURLException {
109        String str  = "http://test.org/foo?a=123&b=%D5D6D7&c=++&d=";
110        URL url = new URL("http://test.org");
111
112        try {
113            handler.parseURL(url, str, 0, str.length());
114            fail("SecurityException should be thrown.");
115        } catch(SecurityException se) {
116            //SecurityException is expected
117        }
118    }
119
120    public void test_sameFile() throws Exception {
121        String loopback = getLoopbackAddressAsHost();
122
123        URL url1  = new URL("http://test:pwd@localhost:80/foo/foo1.c");
124        URL url2  = new URL("http://test:pwd@" + loopback + ":80/foo/foo1.c");
125        URL url3  = new URL("http://test:pwd@" + loopback + ":80/foo/foo2.c");
126        URL url4  = new URL("ftp://test:pwd@" + loopback + ":21/foo/foo2.c");
127        URL url5  = new URL("ftp://test:pwd@" + loopback + ":21/foo/foo1/foo2.c");
128        URL url6  = new URL("http://test/foo/foo1.c");
129
130        assertTrue("Test case 1", handler.sameFile(url1, url2));
131        assertFalse("Test case 2", handler.sameFile(url3, url2));
132        assertFalse("Test case 3", handler.sameFile(url3, url4));
133        assertFalse("Test case 4", handler.sameFile(url4, url5));
134        assertFalse("Test case 5", handler.sameFile(url1, url6));
135    }
136
137    public void test_setURL1() throws MalformedURLException {
138        URL url = new URL("http://test.org");
139
140        try {
141            handler.setURL(url, "http", "localhost", 80, "foo.c", "ref");
142            fail("SecurityException should be thrown.");
143        } catch(SecurityException expected) {
144        }
145    }
146
147    public void test_setURL2() throws MalformedURLException {
148        URL url = new URL("http://test.org");
149
150        try {
151            handler.setURL(url, "http", "localhost", 80, "authority",
152                    "user", "foo.c", "query", "ref");
153            fail("SecurityException should be thrown.");
154        } catch(SecurityException expected) {
155        }
156    }
157
158    public void test_toExternalForm() throws MalformedURLException {
159        URL [] urls = { new URL("ftp://test_url/test?a=b&c=%D0+%D1"),
160                        new URL("http://test_url/test?a=b&c=%D0+%D1"),
161                        new URL("http://test:pwd@localhost:80/foo/foo1.c")};
162
163        for(URL url : urls) {
164            assertEquals("Test case for " + url.toString(),
165                    url.toString(), handler.toExternalForm(url));
166        }
167    }
168
169    public void test_Constructor() {
170        MockURLStreamHandler msh = new MockURLStreamHandler();
171        assertEquals(-1, msh.getDefaultPort());
172    }
173
174    public void setUp() {
175        handler = new MockURLStreamHandler();
176    }
177
178    private String getLoopbackAddressAsHost() throws UnknownHostException {
179        InetAddress localhost = InetAddress.getByName("localhost");
180        return localhost instanceof Inet6Address
181                ? "[" + localhost.getHostAddress() + "]"
182                : localhost.getHostAddress();
183    }
184
185    class MockURLStreamHandler extends URLStreamHandler {
186
187        @Override protected URLConnection openConnection(URL arg0) throws IOException {
188            return null;
189        }
190
191        @Override public boolean equals(URL a, URL b) {
192            return super.equals(a, b);
193        }
194
195        @Override public int getDefaultPort() {
196            return super.getDefaultPort();
197        }
198
199        @Override public InetAddress getHostAddress(URL u) {
200            return super.getHostAddress(u);
201        }
202
203        @Override public int hashCode(URL u) {
204            return super.hashCode(u);
205        }
206
207        @Override public boolean hostsEqual(URL a, URL b) {
208            return super.hostsEqual(a, b);
209        }
210
211        @Override public URLConnection openConnection(URL u, Proxy p) throws IOException {
212            return super.openConnection(u, p);
213        }
214
215        @Override public void parseURL(URL u, String spec, int start, int limit) {
216            super.parseURL(u, spec, start, limit);
217        }
218
219        @Override public boolean sameFile(URL a, URL b) {
220            return super.sameFile(a, b);
221        }
222
223        @Override public void setURL(URL u,
224                String protocol,
225                String host,
226                int port,
227                String file,
228                String ref) {
229            super.setURL(u, protocol, host, port, file, ref);
230        }
231
232        @Override public void setURL(URL u,
233                String protocol,
234                String host,
235                int port,
236                String authority,
237                String userInfo,
238                String path,
239                String query,
240                String ref) {
241            super.setURL(u, protocol, host, port, authority,
242                    userInfo, path, query, ref);
243        }
244
245        @Override public String toExternalForm(URL u) {
246            return super.toExternalForm(u);
247        }
248    }
249}
250