OldURLStreamHandlerTest.java revision 2925824515c1758fcb60354a06320017b1ebc34c
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.InetAddress;
21import java.net.MalformedURLException;
22import java.net.Proxy;
23import java.net.URL;
24import java.net.URLConnection;
25import java.net.URLStreamHandler;
26import java.net.UnknownHostException;
27import junit.framework.TestCase;
28
29public class OldURLStreamHandlerTest extends TestCase {
30
31    MockURLStreamHandler handler = null;
32
33    public void test_equalsLjava_net_URLLjava_net_URL() throws MalformedURLException {
34        URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1");
35        URL url2 = new URL("http://test_url/test?a=b&c=%D0+%D1");
36        assertFalse(url1.equals(url2));
37
38        new URL("http://test_url+/test?a=b&c=%D0+%D1");
39        assertFalse(handler.equals(url1,url2));
40
41        try {
42            assertFalse(handler.equals(null, url1));
43            fail("NullPointerException was not thrown.");
44        } catch(NullPointerException npe) {
45            //expected
46        }
47    }
48
49    public void test_getDefaultPort() {
50        assertEquals(-1, handler.getDefaultPort());
51    }
52
53    public void test_getHostAddress() throws MalformedURLException, UnknownHostException {
54        URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1");
55        assertNull(handler.getHostAddress(url1));
56
57        URL url2 = new URL("http://test:pwd@fakehostname.fakedomain/test?a=b&c=%D0+%D1");
58        assertNull(handler.getHostAddress(url2));
59
60        URL url3 = new URL("http://localhost/test");
61        assertEquals(InetAddress.getByName("localhost"), handler.getHostAddress(url3));
62    }
63
64    public void test_hashCodeLjava_net_URL() throws MalformedURLException {
65        URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1");
66        URL url2 = new URL("http://test_url/test?a=b&c=%D0+%D1");
67        assertTrue(handler.hashCode(url1) != handler.hashCode(url2));
68
69        new URL("http://test_url+/test?a=b&c=%D0+%D1");
70        assertFalse(handler.equals(url1,url2));
71
72        try {
73            handler.hashCode(null);
74            fail("NullPointerException was not thrown.");
75        } catch(NullPointerException expected) {
76        }
77    }
78
79    public void test_hostsEqualLjava_net_URLLjava_net_URL() throws MalformedURLException {
80        URL url1 = new URL("ftp://localhost:21/*test");
81        URL url2 = new URL("http://127.0.0.1/_test");
82        assertTrue(handler.hostsEqual(url1, url2));
83
84        URL url3 = new URL("http://foo/_test_goo");
85        assertFalse(handler.hostsEqual(url1, url3));
86    }
87
88    public void test_openConnectionLjava_net_URL() throws IOException {
89        // abstract method, it doesn't check anything
90        assertNull(handler.openConnection(null));
91    }
92
93    public void test_openConnectionLjava_net_URLLjava_net_Proxy() {
94        try {
95            handler.openConnection(null, null);
96            fail("UnsupportedOperationException was not thrown.");
97        } catch(UnsupportedOperationException  uoe) {
98            //expected
99        } catch (IOException e) {
100            fail("IOException was thrown.");
101        }
102    }
103
104    public void test_parseURLLjava_net_URLLjava_lang_StringII()
105                                                throws MalformedURLException {
106        String str  = "http://test.org/foo?a=123&b=%D5D6D7&c=++&d=";
107        URL url = new URL("http://test.org");
108
109        try {
110            handler.parseURL(url, str, 0, str.length());
111            fail("SecurityException should be thrown.");
112        } catch(SecurityException se) {
113            //SecurityException is expected
114        }
115    }
116
117    public void test_sameFile() throws MalformedURLException {
118        URL url1  = new URL("http://test:pwd@localhost:80/foo/foo1.c");
119        URL url2  = new URL("http://test:pwd@127.0.01:80/foo/foo1.c");
120        URL url3  = new URL("http://test:pwd@127.0.01:80/foo/foo2.c");
121        URL url4  = new URL("ftp://test:pwd@127.0.01:21/foo/foo2.c");
122        URL url5  = new URL("ftp://test:pwd@127.0.01:21/foo/foo1/foo2.c");
123        URL url6  = new URL("http://test/foo/foo1.c");
124
125        assertTrue("Test case 1", handler.sameFile(url1, url2));
126        assertFalse("Test case 2", handler.sameFile(url3, url2));
127        assertFalse("Test case 3", handler.sameFile(url3, url4));
128        assertFalse("Test case 4", handler.sameFile(url4, url5));
129        assertFalse("Test case 5", handler.sameFile(url1, url6));
130    }
131
132    public void test_setURL1() throws MalformedURLException {
133        URL url = new URL("http://test.org");
134
135        try {
136            handler.setURL(url, "http", "localhost", 80, "foo.c", "ref");
137            fail("SecurityException should be thrown.");
138        } catch(SecurityException expected) {
139        }
140    }
141
142    public void test_setURL2() throws MalformedURLException {
143        URL url = new URL("http://test.org");
144
145        try {
146            handler.setURL(url, "http", "localhost", 80, "authority",
147                    "user", "foo.c", "query", "ref");
148            fail("SecurityException should be thrown.");
149        } catch(SecurityException expected) {
150        }
151    }
152
153    public void test_toExternalForm() throws MalformedURLException {
154        URL [] urls = { new URL("ftp://test_url/test?a=b&c=%D0+%D1"),
155                        new URL("http://test_url/test?a=b&c=%D0+%D1"),
156                        new URL("http://test:pwd@localhost:80/foo/foo1.c")};
157
158        for(URL url : urls) {
159            assertEquals("Test case for " + url.toString(),
160                    url.toString(), handler.toExternalForm(url));
161        }
162    }
163
164    public void test_Constructor() {
165        MockURLStreamHandler msh = new MockURLStreamHandler();
166        assertEquals(-1, msh.getDefaultPort());
167    }
168
169    public void setUp() {
170        handler = new MockURLStreamHandler();
171    }
172
173    class MockURLStreamHandler extends URLStreamHandler {
174
175        @Override
176        protected URLConnection openConnection(URL arg0) throws IOException {
177            return null;
178        }
179
180        public boolean equals(URL u1, URL u2) {
181            return super.equals(u1, u2);
182        }
183
184        public int getDefaultPort() {
185            return super.getDefaultPort();
186        }
187
188        public InetAddress getHostAddress(URL u) {
189            return super.getHostAddress(u);
190        }
191
192        public int hashCode(URL u) {
193            return super.hashCode(u);
194        }
195
196        public boolean hostsEqual(URL u1, URL u2) {
197            return super.hostsEqual(u1, u2);
198        }
199
200        public URLConnection openConnection(URL u, Proxy p) throws IOException {
201            return super.openConnection(u, p);
202        }
203
204        public void parseURL(URL u, String spec, int start, int limit) {
205            super.parseURL(u, spec, start, limit);
206        }
207
208        public boolean sameFile(URL u1, URL u2) {
209            return super.sameFile(u1, u2);
210        }
211
212        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        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        public String toExternalForm(URL u) {
235            return super.toExternalForm(u);
236        }
237    }
238}
239