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