OldURLStreamHandlerTest.java revision 4ce2ff6e19315b5ba071fc1739e071e585145dd4
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
188        protected URLConnection openConnection(URL arg0) throws IOException {
189            return null;
190        }
191
192        public boolean equals(URL u1, URL u2) {
193            return super.equals(u1, u2);
194        }
195
196        public int getDefaultPort() {
197            return super.getDefaultPort();
198        }
199
200        public InetAddress getHostAddress(URL u) {
201            return super.getHostAddress(u);
202        }
203
204        public int hashCode(URL u) {
205            return super.hashCode(u);
206        }
207
208        public boolean hostsEqual(URL u1, URL u2) {
209            return super.hostsEqual(u1, u2);
210        }
211
212        public URLConnection openConnection(URL u, Proxy p) throws IOException {
213            return super.openConnection(u, p);
214        }
215
216        public void parseURL(URL u, String spec, int start, int limit) {
217            super.parseURL(u, spec, start, limit);
218        }
219
220        public boolean sameFile(URL u1, URL u2) {
221            return super.sameFile(u1, u2);
222        }
223
224        public void setURL(URL u,
225                String protocol,
226                String host,
227                int port,
228                String file,
229                String ref) {
230            super.setURL(u, protocol, host, port, file, ref);
231        }
232
233        public void setURL(URL u,
234                String protocol,
235                String host,
236                int port,
237                String authority,
238                String userInfo,
239                String path,
240                String query,
241                String ref) {
242            super.setURL(u, protocol, host, port, authority,
243                    userInfo, path, query, ref);
244        }
245
246        public String toExternalForm(URL u) {
247            return super.toExternalForm(u);
248        }
249    }
250}
251