1package tests.api.java.net;
2
3import dalvik.annotation.TestLevel;
4import dalvik.annotation.TestTargetClass;
5import dalvik.annotation.TestTargetNew;
6
7import junit.framework.TestCase;
8
9import java.io.IOException;
10import java.net.InetAddress;
11import java.net.MalformedURLException;
12import java.net.Proxy;
13import java.net.URL;
14import java.net.URLConnection;
15import java.net.URLStreamHandler;
16import java.net.UnknownHostException;
17
18@TestTargetClass(URLStreamHandler.class)
19public class URLStreamHandlerTest extends TestCase {
20
21    MockURLStreamHandler handler = null;
22
23    @TestTargetNew(
24        level = TestLevel.COMPLETE,
25        notes = "",
26        method = "equals",
27        args = {URL.class, URL.class}
28    )
29    public void test_equalsLjava_net_URLLjava_net_URL() {
30        try {
31            URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1");
32            URL url2 = new URL("http://test_url/test?a=b&c=%D0+%D1");
33            assertFalse(url1.equals(url2));
34
35            URL url3 = new URL("http://test_url+/test?a=b&c=%D0+%D1");
36            assertFalse(handler.equals(url1,url2));
37
38            try {
39                assertFalse(handler.equals(null, url1));
40                fail("NullPointerException was not thrown.");
41            } catch(NullPointerException npe) {
42                //expected
43            }
44        } catch (MalformedURLException e) {
45            fail("MalformedURLException was thrown.");
46        }
47    }
48
49    @TestTargetNew(
50        level = TestLevel.COMPLETE,
51        notes = "",
52        method = "getDefaultPort",
53        args = {}
54    )
55    public void test_getDefaultPort() {
56        assertEquals(-1, handler.getDefaultPort());
57    }
58
59    @TestTargetNew(
60        level = TestLevel.COMPLETE,
61        notes = "",
62        method = "getHostAddress",
63        args = {URL.class}
64    )
65    public void test_getHostAddress() throws MalformedURLException,
66                                        UnknownHostException {
67        URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1");
68        assertNull(handler.getHostAddress(url1));
69
70        URL url2 = new URL("http://test:pwd@host/test?a=b&c=%D0+%D1");
71        assertNull("testHost", handler.getHostAddress(url2));handler.getHostAddress(url2);
72
73        URL url3 = new URL("http://localhost/test");
74        assertEquals(InetAddress.getLocalHost(), handler.getHostAddress(url3));
75    }
76
77    @TestTargetNew(
78        level = TestLevel.COMPLETE,
79        notes = "",
80        method = "hashCode",
81        args = {URL.class}
82    )
83    public void test_hashCodeLjava_net_URL() {
84        try {
85            URL url1 = new URL("ftp://test_url/test?a=b&c=%D0+%D1");
86            URL url2 = new URL("http://test_url/test?a=b&c=%D0+%D1");
87            assertTrue(handler.hashCode(url1) != handler.hashCode(url2));
88
89            URL url3 = new URL("http://test_url+/test?a=b&c=%D0+%D1");
90            assertFalse(handler.equals(url1,url2));
91
92            try {
93                handler.hashCode(null);
94                fail("NullPointerException was not thrown.");
95            } catch(NullPointerException npe) {
96                //expected
97            }
98        } catch (MalformedURLException e) {
99            fail("MalformedURLException was thrown.");
100        }
101    }
102
103    @TestTargetNew(
104        level = TestLevel.COMPLETE,
105        notes = "",
106        method = "hostsEqual",
107        args = {URL.class, URL.class}
108    )
109    public void test_hostsEqualLjava_net_URLLjava_net_URL() throws
110                                                    MalformedURLException {
111        URL url1 = new URL("ftp://localhost:21/*test");
112        URL url2 = new URL("http://127.0.0.1/_test");
113        assertTrue(handler.hostsEqual(url1, url2));
114
115        URL url3 = new URL("http://foo/_test_goo");
116        assertFalse(handler.hostsEqual(url1, url3));
117    }
118
119    @TestTargetNew(
120        level = TestLevel.COMPLETE,
121        notes = "",
122        method = "openConnection",
123        args = { URL.class }
124    )
125    public void test_openConnectionLjava_net_URL() throws IOException {
126        // abstract method, it doesn't check anything
127        assertNull(handler.openConnection(null));
128    }
129
130    @TestTargetNew(
131        level = TestLevel.COMPLETE,
132        notes = "",
133        method = "openConnection",
134        args = {URL.class, Proxy.class}
135    )
136    public void test_openConnectionLjava_net_URLLjava_net_Proxy() {
137        try {
138            handler.openConnection(null, null);
139            fail("UnsupportedOperationException was not thrown.");
140        } catch(UnsupportedOperationException  uoe) {
141            //expected
142        } catch (IOException e) {
143            fail("IOException was thrown.");
144        }
145    }
146
147    @TestTargetNew(
148        level = TestLevel.SUFFICIENT,
149        notes = "Completed testing of this method requres set up " +
150                "URLStreamHandlerFactory that can be done at most once.",
151        method = "parseURL",
152        args = {URL.class, String.class, int.class, int.class}
153    )
154    public void test_parseURLLjava_net_URLLjava_lang_StringII()
155                                                throws MalformedURLException {
156        String str  = "http://test.org/foo?a=123&b=%D5D6D7&c=++&d=";
157        URL url = new URL("http://test.org");
158
159        try {
160            handler.parseURL(url, str, 0, str.length());
161            fail("SecurityException should be thrown.");
162        } catch(SecurityException se) {
163            //SecurityException is expected
164        }
165    }
166
167    @TestTargetNew(
168        level = TestLevel.COMPLETE,
169        notes = "",
170        method = "sameFile",
171        args = {URL.class, URL.class}
172    )
173    public void test_sameFile() throws MalformedURLException {
174        URL url1  = new URL("http://test:pwd@localhost:80/foo/foo1.c");
175        URL url2  = new URL("http://test:pwd@127.0.01:80/foo/foo1.c");
176        URL url3  = new URL("http://test:pwd@127.0.01:80/foo/foo2.c");
177        URL url4  = new URL("ftp://test:pwd@127.0.01:21/foo/foo2.c");
178        URL url5  = new URL("ftp://test:pwd@127.0.01:21/foo/foo1/foo2.c");
179        URL url6  = new URL("http://test/foo/foo1.c");
180
181        assertTrue("Test case 1", handler.sameFile(url1, url2));
182        assertFalse("Test case 2", handler.sameFile(url3, url2));
183        assertFalse("Test case 3", handler.sameFile(url3, url4));
184        assertFalse("Test case 4", handler.sameFile(url4, url5));
185        assertFalse("Test case 5", handler.sameFile(url1, url6));
186    }
187
188    @TestTargetNew(
189        level = TestLevel.SUFFICIENT,
190        notes = "Completed testing of this method requres set up " +
191                "URLStreamHandlerFactory that can be done at most once.",
192        method = "setURL",
193        args = {java.net.URL.class, java.lang.String.class,
194                java.lang.String.class, int.class, java.lang.String.class,
195                java.lang.String.class}
196    )
197    public void test_setURL1() throws MalformedURLException {
198        URL url = new URL("http://test.org");
199
200        try {
201            handler.setURL(url, "http", "localhost", 80, "foo.c", "ref");
202            fail("SecurityException should be thrown.");
203        } catch(SecurityException se) {
204            //SecurityException is expected
205        }
206    }
207
208    @TestTargetNew(
209        level = TestLevel.SUFFICIENT,
210        notes = "Completed testing of this method requres set up " +
211                 "URLStreamHandlerFactory that can be done at most once.",
212        method = "setURL",
213        args = {java.net.URL.class, java.lang.String.class,
214                java.lang.String.class, int.class, java.lang.String.class,
215                java.lang.String.class, java.lang.String.class,
216                java.lang.String.class, java.lang.String.class}
217    )
218    public void test_setURL2() throws MalformedURLException {
219        URL url = new URL("http://test.org");
220
221        try {
222            handler.setURL(url, "http", "localhost", 80, "authority",
223                    "user", "foo.c", "query", "ref");
224            fail("SecurityException should be thrown.");
225        } catch(SecurityException se) {
226            //SecurityException is expected
227        }
228    }
229
230    @TestTargetNew(
231        level = TestLevel.COMPLETE,
232        notes = "",
233        method = "toExternalForm",
234        args = {URL.class}
235    )
236    public void test_toExternalForm() throws MalformedURLException {
237        URL [] urls = { new URL("ftp://test_url/test?a=b&c=%D0+%D1"),
238                        new URL("http://test_url/test?a=b&c=%D0+%D1"),
239                        new URL("http://test:pwd@localhost:80/foo/foo1.c")};
240
241        for(URL url:urls) {
242            assertEquals("Test case for " + url.toString(),
243                    url.toString(), handler.toExternalForm(url));
244        }
245    }
246
247    @TestTargetNew(
248        level = TestLevel.COMPLETE,
249        notes = "",
250        method = "URLStreamHandler",
251        args = {}
252    )
253    public void test_Constructor() {
254        MockURLStreamHandler msh = new MockURLStreamHandler();
255        assertEquals(-1, msh.getDefaultPort());
256    }
257
258    public void setUp() {
259        handler = new MockURLStreamHandler();
260    }
261
262    class MockURLStreamHandler extends URLStreamHandler {
263
264        @Override
265        protected URLConnection openConnection(URL arg0) throws IOException {
266            // TODO Auto-generated method stub
267            return null;
268        }
269
270        public boolean equals(URL u1, URL u2) {
271            return super.equals(u1, u2);
272        }
273
274        public int getDefaultPort() {
275            return super.getDefaultPort();
276        }
277
278        public InetAddress getHostAddress(URL u) {
279            return super.getHostAddress(u);
280        }
281
282        public int hashCode(URL u) {
283            return super.hashCode(u);
284        }
285
286        public boolean hostsEqual(URL u1, URL u2) {
287            return super.hostsEqual(u1, u2);
288        }
289
290        public URLConnection openConnection(URL u, Proxy p) throws IOException {
291            return super.openConnection(u, p);
292        }
293
294        public void parseURL(URL u, String spec, int start, int limit) {
295            super.parseURL(u, spec, start, limit);
296        }
297
298        public boolean sameFile(URL u1, URL u2) {
299            return super.sameFile(u1, u2);
300        }
301
302        public void setURL(URL u,
303                String protocol,
304                String host,
305                int port,
306                String file,
307                String ref) {
308            super.setURL(u, protocol, host, port, file, ref);
309        }
310
311        public void setURL(URL u,
312                String protocol,
313                String host,
314                int port,
315                String authority,
316                String userInfo,
317                String path,
318                String query,
319                String ref) {
320            super.setURL(u, protocol, host, port, authority,
321                    userInfo, path, query, ref);
322        }
323
324        public String toExternalForm(URL u) {
325            return super.toExternalForm(u);
326        }
327    }
328}
329