1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.net;
19
20import java.io.File;
21import java.io.IOException;
22import java.io.InputStream;
23import java.net.InetAddress;
24import java.net.InetSocketAddress;
25import java.net.MalformedURLException;
26import java.net.Proxy;
27import java.net.Proxy.Type;
28import java.net.ProxySelector;
29import java.net.SocketAddress;
30import java.net.URI;
31import java.net.URL;
32import java.net.URLConnection;
33import java.net.URLStreamHandler;
34import java.net.URLStreamHandlerFactory;
35import java.util.ArrayList;
36import java.util.List;
37import junit.framework.TestCase;
38import tests.support.Support_Configuration;
39import tests.support.resource.Support_Resources;
40
41public class URLTest extends TestCase {
42
43    public static class MyHandler extends URLStreamHandler {
44        protected URLConnection openConnection(URL u)
45                throws IOException {
46            return null;
47        }
48    }
49
50    URL u;
51
52    URL u1;
53
54    URL u2;
55
56    URL u3;
57
58    URL u4;
59
60    URL u5;
61
62    URL u6;
63
64    boolean caught = false;
65
66    static boolean isSelectCalled;
67
68    /**
69     * @tests java.net.URL#URL(java.lang.String)
70     */
71    public void test_ConstructorLjava_lang_String() throws IOException {
72        // Tests for multiple URL instantiation basic parsing test
73        u = new URL(
74                "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
75        assertEquals("u returns a wrong protocol", "http", u.getProtocol());
76        assertEquals("u returns a wrong host", "www.yahoo1.com", u.getHost());
77        assertEquals("u returns a wrong port", 8080, u.getPort());
78        assertEquals("u returns a wrong file",
79                "/dir1/dir2/test.cgi?point1.html", u.getFile());
80        assertEquals("u returns a wrong anchor", "anchor1", u.getRef());
81
82        // test for no file
83        u1 = new URL("http://www.yahoo2.com:9999");
84        assertEquals("u1 returns a wrong protocol", "http", u1.getProtocol());
85        assertEquals("u1 returns a wrong host", "www.yahoo2.com", u1.getHost());
86        assertEquals("u1 returns a wrong port", 9999, u1.getPort());
87        assertTrue("u1 returns a wrong file", u1.getFile().equals(""));
88        assertNull("u1 returns a wrong anchor", u1.getRef());
89
90        // test for no port
91        u2 = new URL(
92                "http://www.yahoo3.com/dir1/dir2/test.cgi?point1.html#anchor1");
93        assertEquals("u2 returns a wrong protocol", "http", u2.getProtocol());
94        assertEquals("u2 returns a wrong host", "www.yahoo3.com", u2.getHost());
95        assertEquals("u2 returns a wrong port", -1, u2.getPort());
96        assertEquals("u2 returns a wrong file",
97                "/dir1/dir2/test.cgi?point1.html", u2.getFile());
98        assertEquals("u2 returns a wrong anchor", "anchor1", u2.getRef());
99
100        // test for no port
101        URL u2a = new URL("file://www.yahoo3.com/dir1/dir2/test.cgi#anchor1");
102        assertEquals("u2a returns a wrong protocol", "file", u2a.getProtocol());
103        assertEquals("u2a returns a wrong host", "www.yahoo3.com", u2a
104                .getHost());
105        assertEquals("u2a returns a wrong port", -1, u2a.getPort());
106        assertEquals("u2a returns a wrong file", "/dir1/dir2/test.cgi", u2a
107                .getFile());
108        assertEquals("u2a returns a wrong anchor", "anchor1", u2a.getRef());
109
110        // test for no file, no port
111        u3 = new URL("http://www.yahoo4.com/");
112        assertEquals("u3 returns a wrong protocol", "http", u3.getProtocol());
113        assertEquals("u3 returns a wrong host", "www.yahoo4.com", u3.getHost());
114        assertEquals("u3 returns a wrong port", -1, u3.getPort());
115        assertEquals("u3 returns a wrong file", "/", u3.getFile());
116        assertNull("u3 returns a wrong anchor", u3.getRef());
117
118        // test for no file, no port
119        URL u3a = new URL("file://www.yahoo4.com/");
120        assertEquals("u3a returns a wrong protocol", "file", u3a.getProtocol());
121        assertEquals("u3a returns a wrong host", "www.yahoo4.com", u3a
122                .getHost());
123        assertEquals("u3a returns a wrong port", -1, u3a.getPort());
124        assertEquals("u3a returns a wrong file", "/", u3a.getFile());
125        assertNull("u3a returns a wrong anchor", u3a.getRef());
126
127        // test for no file, no port
128        URL u3b = new URL("file://www.yahoo4.com");
129        assertEquals("u3b returns a wrong protocol", "file", u3b.getProtocol());
130        assertEquals("u3b returns a wrong host", "www.yahoo4.com", u3b
131                .getHost());
132        assertEquals("u3b returns a wrong port", -1, u3b.getPort());
133        assertTrue("u3b returns a wrong file", u3b.getFile().equals(""));
134        assertNull("u3b returns a wrong anchor", u3b.getRef());
135
136        // test for non-port ":" and wierd characters occurrences
137        u4 = new URL(
138                "http://www.yahoo5.com/di!@$%^&*()_+r1/di:::r2/test.cgi?point1.html#anchor1");
139        assertEquals("u4 returns a wrong protocol", "http", u4.getProtocol());
140        assertEquals("u4 returns a wrong host", "www.yahoo5.com", u4.getHost());
141        assertEquals("u4 returns a wrong port", -1, u4.getPort());
142        assertEquals("u4 returns a wrong file",
143                "/di!@$%^&*()_+r1/di:::r2/test.cgi?point1.html", u4.getFile());
144        assertEquals("u4 returns a wrong anchor", "anchor1", u4.getRef());
145
146        u5 = new URL("file:/testing.tst");
147        assertEquals("u5 returns a wrong protocol", "file", u5.getProtocol());
148        assertTrue("u5 returns a wrong host", u5.getHost().equals(""));
149        assertEquals("u5 returns a wrong port", -1, u5.getPort());
150        assertEquals("u5 returns a wrong file", "/testing.tst", u5.getFile());
151        assertNull("u5 returns a wrong anchor", u5.getRef());
152
153        URL u5a = new URL("file:testing.tst");
154        assertEquals("u5a returns a wrong protocol", "file", u5a.getProtocol());
155        assertTrue("u5a returns a wrong host", u5a.getHost().equals(""));
156        assertEquals("u5a returns a wrong port", -1, u5a.getPort());
157        assertEquals("u5a returns a wrong file", "testing.tst", u5a.getFile());
158        assertNull("u5a returns a wrong anchor", u5a.getRef());
159
160        URL u6 = new URL("http://host:/file");
161        assertEquals("u6 return a wrong port", -1, u6.getPort());
162
163        URL u7 = new URL("file:../../file.txt");
164        assertTrue("u7 returns a wrong file: " + u7.getFile(), u7.getFile()
165                .equals("../../file.txt"));
166
167        URL u8 = new URL("http://[fec0::1:20d:60ff:fe24:7410]:35/file.txt");
168        assertTrue("u8 returns a wrong protocol " + u8.getProtocol(), u8
169                .getProtocol().equals("http"));
170        assertTrue("u8 returns a wrong host " + u8.getHost(), u8.getHost()
171                .equals("[fec0::1:20d:60ff:fe24:7410]"));
172        assertTrue("u8 returns a wrong port " + u8.getPort(),
173                u8.getPort() == 35);
174        assertTrue("u8 returns a wrong file " + u8.getFile(), u8.getFile()
175                .equals("/file.txt"));
176        assertNull("u8 returns a wrong anchor " + u8.getRef(), u8.getRef());
177
178        URL u9 = new URL("file://[fec0::1:20d:60ff:fe24:7410]/file.txt#sogood");
179        assertTrue("u9 returns a wrong protocol " + u9.getProtocol(), u9
180                .getProtocol().equals("file"));
181        assertTrue("u9 returns a wrong host " + u9.getHost(), u9.getHost()
182                .equals("[fec0::1:20d:60ff:fe24:7410]"));
183        assertTrue("u9 returns a wrong port " + u9.getPort(),
184                u9.getPort() == -1);
185        assertTrue("u9 returns a wrong file " + u9.getFile(), u9.getFile()
186                .equals("/file.txt"));
187        assertTrue("u9 returns a wrong anchor " + u9.getRef(), u9.getRef()
188                .equals("sogood"));
189
190        URL u10 = new URL("file://[fec0::1:20d:60ff:fe24:7410]");
191        assertTrue("u10 returns a wrong protocol " + u10.getProtocol(), u10
192                .getProtocol().equals("file"));
193        assertTrue("u10 returns a wrong host " + u10.getHost(), u10.getHost()
194                .equals("[fec0::1:20d:60ff:fe24:7410]"));
195        assertTrue("u10 returns a wrong port " + u10.getPort(),
196                u10.getPort() == -1);
197
198		URL u11 = new URL("file:////file.txt");
199        assertNull("u11 returns a wrong authority " + u11.getAuthority(), u11
200                .getAuthority());
201        assertTrue("u11 returns a wrong file " + u11.getFile(), u11.getFile()
202                .equals("////file.txt"));
203
204        URL u12 = new URL("file:///file.txt");
205        assertTrue("u12 returns a wrong authority", u12.getAuthority().equals(
206                ""));
207        assertTrue("u12 returns a wrong file " + u12.getFile(), u12.getFile()
208                .equals("/file.txt"));
209
210
211        // test for error catching
212
213        // Bad HTTP format - no "//"
214        u = new URL(
215                "http:www.yahoo5.com::22/dir1/di:::r2/test.cgi?point1.html#anchor1");
216
217        caught = false;
218        try {
219            u = new URL(
220                    "http://www.yahoo5.com::22/dir1/di:::r2/test.cgi?point1.html#anchor1");
221        } catch (MalformedURLException e) {
222            caught = true;
223        }
224        assertTrue("Should have throw MalformedURLException", caught);
225
226        // unknown protocol
227        try {
228            u = new URL("myProtocol://www.yahoo.com:22");
229        } catch (MalformedURLException e) {
230            caught = true;
231        }
232        assertTrue("3 Failed to throw MalformedURLException", caught);
233
234        caught = false;
235        // no protocol
236        try {
237            u = new URL("www.yahoo.com");
238        } catch (MalformedURLException e) {
239            caught = true;
240        }
241        assertTrue("4 Failed to throw MalformedURLException", caught);
242
243        caught = false;
244
245        URL u1 = null;
246        try {
247            // No leading or trailing spaces.
248            u1 = new URL("file:/some/path");
249            assertEquals("5 got wrong file length1", 10, u1.getFile().length());
250
251            // Leading spaces.
252            u1 = new URL("  file:/some/path");
253            assertEquals("5 got wrong file length2", 10, u1.getFile().length());
254
255            // Trailing spaces.
256            u1 = new URL("file:/some/path  ");
257            assertEquals("5 got wrong file length3", 10, u1.getFile().length());
258
259            // Leading and trailing.
260            u1 = new URL("  file:/some/path ");
261            assertEquals("5 got wrong file length4", 10, u1.getFile().length());
262
263            // in-place spaces.
264            u1 = new URL("  file:  /some/path ");
265            assertEquals("5 got wrong file length5", 12, u1.getFile().length());
266
267        } catch (MalformedURLException e) {
268            fail("5 Did not expect the exception " + e);
269        }
270
271        // testing jar protocol with relative path
272        // to make sure it's not canonicalized
273        try {
274            String file = "file:/a!/b/../d";
275
276            u = new URL("jar:" + file);
277            assertEquals("Wrong file (jar protocol, relative path)", file, u
278                    .getFile());
279        } catch (MalformedURLException e) {
280            fail("Unexpected exception (jar protocol, relative path)" + e);
281        }
282    }
283
284    /**
285     * @tests java.net.URL#URL(java.net.URL, java.lang.String)
286     */
287    public void test_ConstructorLjava_net_URLLjava_lang_String()
288            throws Exception {
289        // Test for method java.net.URL(java.net.URL, java.lang.String)
290        u = new URL("http://www.yahoo.com");
291        URL uf = new URL("file://www.yahoo.com");
292        // basic ones
293        u1 = new URL(u, "file.java");
294        assertEquals("1 returns a wrong protocol", "http", u1.getProtocol());
295        assertEquals("1 returns a wrong host", "www.yahoo.com", u1.getHost());
296        assertEquals("1 returns a wrong port", -1, u1.getPort());
297        assertEquals("1 returns a wrong file", "/file.java", u1.getFile());
298        assertNull("1 returns a wrong anchor", u1.getRef());
299
300        URL u1f = new URL(uf, "file.java");
301        assertEquals("1f returns a wrong protocol", "file", u1f.getProtocol());
302        assertEquals("1f returns a wrong host", "www.yahoo.com", u1f.getHost());
303        assertEquals("1f returns a wrong port", -1, u1f.getPort());
304        assertEquals("1f returns a wrong file", "/file.java", u1f.getFile());
305        assertNull("1f returns a wrong anchor", u1f.getRef());
306
307        u1 = new URL(u, "dir1/dir2/../file.java");
308        assertEquals("3 returns a wrong protocol", "http", u1.getProtocol());
309        assertTrue("3 returns a wrong host: " + u1.getHost(), u1.getHost()
310                .equals("www.yahoo.com"));
311        assertEquals("3 returns a wrong port", -1, u1.getPort());
312        assertEquals("3 returns a wrong file", "/dir1/dir2/../file.java", u1
313                .getFile());
314        assertNull("3 returns a wrong anchor", u1.getRef());
315
316        u1 = new URL(u, "http:dir1/dir2/../file.java");
317        assertEquals("3a returns a wrong protocol", "http", u1.getProtocol());
318        assertTrue("3a returns a wrong host: " + u1.getHost(), u1.getHost()
319                .equals(""));
320        assertEquals("3a returns a wrong port", -1, u1.getPort());
321        assertEquals("3a returns a wrong file", "dir1/dir2/../file.java", u1
322                .getFile());
323        assertNull("3a returns a wrong anchor", u1.getRef());
324
325        u = new URL("http://www.apache.org/testing/");
326        u1 = new URL(u, "file.java");
327        assertEquals("4 returns a wrong protocol", "http", u1.getProtocol());
328        assertEquals("4 returns a wrong host", "www.apache.org", u1.getHost());
329        assertEquals("4 returns a wrong port", -1, u1.getPort());
330        assertEquals("4 returns a wrong file", "/testing/file.java", u1
331                .getFile());
332        assertNull("4 returns a wrong anchor", u1.getRef());
333
334        uf = new URL("file://www.apache.org/testing/");
335        u1f = new URL(uf, "file.java");
336        assertEquals("4f returns a wrong protocol", "file", u1f.getProtocol());
337        assertEquals("4f returns a wrong host", "www.apache.org", u1f.getHost());
338        assertEquals("4f returns a wrong port", -1, u1f.getPort());
339        assertEquals("4f returns a wrong file", "/testing/file.java", u1f
340                .getFile());
341        assertNull("4f returns a wrong anchor", u1f.getRef());
342
343        uf = new URL("file:/testing/");
344        u1f = new URL(uf, "file.java");
345        assertEquals("4fa returns a wrong protocol", "file", u1f.getProtocol());
346        assertTrue("4fa returns a wrong host", u1f.getHost().equals(""));
347        assertEquals("4fa returns a wrong port", -1, u1f.getPort());
348        assertEquals("4fa returns a wrong file", "/testing/file.java", u1f
349                .getFile());
350        assertNull("4fa returns a wrong anchor", u1f.getRef());
351
352        uf = new URL("file:testing/");
353        u1f = new URL(uf, "file.java");
354        assertEquals("4fb returns a wrong protocol", "file", u1f.getProtocol());
355        assertTrue("4fb returns a wrong host", u1f.getHost().equals(""));
356        assertEquals("4fb returns a wrong port", -1, u1f.getPort());
357        assertEquals("4fb returns a wrong file", "testing/file.java", u1f
358                .getFile());
359        assertNull("4fb returns a wrong anchor", u1f.getRef());
360
361        u1f = new URL(uf, "file:file.java");
362        assertEquals("4fc returns a wrong protocol", "file", u1f.getProtocol());
363        assertTrue("4fc returns a wrong host", u1f.getHost().equals(""));
364        assertEquals("4fc returns a wrong port", -1, u1f.getPort());
365        assertEquals("4fc returns a wrong file", "file.java", u1f.getFile());
366        assertNull("4fc returns a wrong anchor", u1f.getRef());
367
368        u1f = new URL(uf, "file:");
369        assertEquals("4fd returns a wrong protocol", "file", u1f.getProtocol());
370        assertTrue("4fd returns a wrong host", u1f.getHost().equals(""));
371        assertEquals("4fd returns a wrong port", -1, u1f.getPort());
372        assertTrue("4fd returns a wrong file", u1f.getFile().equals(""));
373        assertNull("4fd returns a wrong anchor", u1f.getRef());
374
375        u = new URL("http://www.apache.org/testing");
376        u1 = new URL(u, "file.java");
377        assertEquals("5 returns a wrong protocol", "http", u1.getProtocol());
378        assertEquals("5 returns a wrong host", "www.apache.org", u1.getHost());
379        assertEquals("5 returns a wrong port", -1, u1.getPort());
380        assertEquals("5 returns a wrong file", "/file.java", u1.getFile());
381        assertNull("5 returns a wrong anchor", u1.getRef());
382
383        uf = new URL("file://www.apache.org/testing");
384        u1f = new URL(uf, "file.java");
385        assertEquals("5f returns a wrong protocol", "file", u1f.getProtocol());
386        assertEquals("5f returns a wrong host", "www.apache.org", u1f.getHost());
387        assertEquals("5f returns a wrong port", -1, u1f.getPort());
388        assertEquals("5f returns a wrong file", "/file.java", u1f.getFile());
389        assertNull("5f returns a wrong anchor", u1f.getRef());
390
391        uf = new URL("file:/testing");
392        u1f = new URL(uf, "file.java");
393        assertEquals("5fa returns a wrong protocol", "file", u1f.getProtocol());
394        assertTrue("5fa returns a wrong host", u1f.getHost().equals(""));
395        assertEquals("5fa returns a wrong port", -1, u1f.getPort());
396        assertEquals("5fa returns a wrong file", "/file.java", u1f.getFile());
397        assertNull("5fa returns a wrong anchor", u1f.getRef());
398
399        uf = new URL("file:testing");
400        u1f = new URL(uf, "file.java");
401        assertEquals("5fb returns a wrong protocol", "file", u1f.getProtocol());
402        assertTrue("5fb returns a wrong host", u1f.getHost().equals(""));
403        assertEquals("5fb returns a wrong port", -1, u1f.getPort());
404        assertEquals("5fb returns a wrong file", "file.java", u1f.getFile());
405        assertNull("5fb returns a wrong anchor", u1f.getRef());
406
407        u = new URL("http://www.apache.org/testing/foobaz");
408        u1 = new URL(u, "/file.java");
409        assertEquals("6 returns a wrong protocol", "http", u1.getProtocol());
410        assertEquals("6 returns a wrong host", "www.apache.org", u1.getHost());
411        assertEquals("6 returns a wrong port", -1, u1.getPort());
412        assertEquals("6 returns a wrong file", "/file.java", u1.getFile());
413        assertNull("6 returns a wrong anchor", u1.getRef());
414
415        uf = new URL("file://www.apache.org/testing/foobaz");
416        u1f = new URL(uf, "/file.java");
417        assertEquals("6f returns a wrong protocol", "file", u1f.getProtocol());
418        assertEquals("6f returns a wrong host", "www.apache.org", u1f.getHost());
419        assertEquals("6f returns a wrong port", -1, u1f.getPort());
420        assertEquals("6f returns a wrong file", "/file.java", u1f.getFile());
421        assertNull("6f returns a wrong anchor", u1f.getRef());
422
423        u = new URL("http://www.apache.org:8000/testing/foobaz");
424        u1 = new URL(u, "/file.java");
425        assertEquals("7 returns a wrong protocol", "http", u1.getProtocol());
426        assertEquals("7 returns a wrong host", "www.apache.org", u1.getHost());
427        assertEquals("7 returns a wrong port", 8000, u1.getPort());
428        assertEquals("7 returns a wrong file", "/file.java", u1.getFile());
429        assertNull("7 returns a wrong anchor", u1.getRef());
430
431        u = new URL("http://www.apache.org/index.html");
432        u1 = new URL(u, "#bar");
433        assertEquals("8 returns a wrong host", "www.apache.org", u1.getHost());
434        assertEquals("8 returns a wrong file", "/index.html", u1.getFile());
435        assertEquals("8 returns a wrong anchor", "bar", u1.getRef());
436
437        u = new URL("http://www.apache.org/index.html#foo");
438        u1 = new URL(u, "http:#bar");
439        assertEquals("9 returns a wrong host", "www.apache.org", u1.getHost());
440        assertEquals("9 returns a wrong file", "/index.html", u1.getFile());
441        assertEquals("9 returns a wrong anchor", "bar", u1.getRef());
442
443        u = new URL("http://www.apache.org/index.html");
444        u1 = new URL(u, "");
445        assertEquals("10 returns a wrong host", "www.apache.org", u1.getHost());
446        assertEquals("10 returns a wrong file", "/index.html", u1.getFile());
447        assertNull("10 returns a wrong anchor", u1.getRef());
448
449        uf = new URL("file://www.apache.org/index.html");
450        u1f = new URL(uf, "");
451        assertEquals("10f returns a wrong host", "www.apache.org", u1.getHost());
452        assertEquals("10f returns a wrong file", "/index.html", u1.getFile());
453        assertNull("10f returns a wrong anchor", u1.getRef());
454
455        u = new URL("http://www.apache.org/index.html");
456        u1 = new URL(u, "http://www.apache.org");
457        assertEquals("11 returns a wrong host", "www.apache.org", u1.getHost());
458        assertTrue("11 returns a wrong file", u1.getFile().equals(""));
459        assertNull("11 returns a wrong anchor", u1.getRef());
460
461        // test for question mark processing
462        u = new URL("http://www.foo.com/d0/d1/d2/cgi-bin?foo=bar/baz");
463
464        // test for relative file and out of bound "/../" processing
465        u1 = new URL(u, "../dir1/./dir2/../file.java");
466        assertTrue("A) returns a wrong file: " + u1.getFile(), u1.getFile()
467                .equals("/d0/d1/dir1/file.java"));
468
469        // test for absolute and relative file processing
470        u1 = new URL(u, "/../dir1/./dir2/../file.java");
471        assertEquals("B) returns a wrong file", "/../dir1/./dir2/../file.java",
472                u1.getFile());
473
474        try {
475            // u should raise a MalFormedURLException because u, the context is
476            // null
477            u = null;
478            u1 = new URL(u, "file.java");
479            fail("didn't throw the expected MalFormedURLException");
480        } catch (MalformedURLException e) {
481            // valid
482        }
483
484        // Regression test for HARMONY-3258
485        // testing jar context url with relative file
486        try {
487            // check that relative path with null context is not canonicalized
488            String spec = "jar:file:/a!/b/../d";
489            URL ctx = null;
490            u = new URL(ctx, spec);
491            assertEquals("1 Wrong file (jar protocol, relative path)", spec, u
492                    .toString());
493
494            spec = "../d";
495            ctx = new URL("jar:file:/a!/b");
496            u = new URL(ctx, spec);
497            assertEquals("2 Wrong file (jar protocol, relative path)",
498                    "file:/a!/d", u.getFile());
499
500            spec = "../d";
501            ctx = new URL("jar:file:/a!/b/c");
502            u = new URL(ctx, spec);
503            assertEquals("3 Wrong file (jar protocol, relative path)",
504                    "file:/a!/d", u.getFile());
505
506            spec = "../d";
507            ctx = new URL("jar:file:/a!/b/c/d");
508            u = new URL(ctx, spec);
509            assertEquals("4 Wrong file (jar protocol, relative path)",
510                    "file:/a!/b/d", u.getFile());
511
512            // added the real example
513            spec = "../pdf/PDF.settings";
514            ctx = new URL(
515                    "jar:file:/C:/Program%20Files/Netbeans-5.5/ide7/modules/org-netbeans-modules-utilities.jar!/org/netbeans/modules/utilities/Layer.xml");
516            u = new URL(ctx, spec);
517            assertEquals(
518                    "5 Wrong file (jar protocol, relative path)",
519                    "file:/C:/Program%20Files/Netbeans-5.5/ide7/modules/org-netbeans-modules-utilities.jar!/org/netbeans/modules/pdf/PDF.settings",
520                    u.getFile());
521        } catch (MalformedURLException e) {
522            fail("Testing jar protocol, relative path failed: " + e);
523        }
524    }
525
526    /**
527     * @tests java.net.URL#URL(java.net.URL, java.lang.String,
528     *        java.net.URLStreamHandler)
529     */
530    public void test_ConstructorLjava_net_URLLjava_lang_StringLjava_net_URLStreamHandler()
531            throws Exception {
532        // Test for method java.net.URL(java.net.URL, java.lang.String,
533        // java.net.URLStreamHandler)
534        u = new URL("http://www.yahoo.com");
535        // basic ones
536        u1 = new URL(u, "file.java", new MyHandler());
537        assertEquals("1 returns a wrong protocol", "http", u1.getProtocol());
538        assertEquals("1 returns a wrong host", "www.yahoo.com", u1.getHost());
539        assertEquals("1 returns a wrong port", -1, u1.getPort());
540        assertEquals("1 returns a wrong file", "/file.java", u1.getFile());
541        assertNull("1 returns a wrong anchor", u1.getRef());
542
543        u1 = new URL(u, "systemresource:/+/FILE0/test.java", new MyHandler());
544        assertEquals("2 returns a wrong protocol", "systemresource", u1
545                .getProtocol());
546        assertTrue("2 returns a wrong host", u1.getHost().equals(""));
547        assertEquals("2 returns a wrong port", -1, u1.getPort());
548        assertEquals("2 returns a wrong file", "/+/FILE0/test.java", u1
549                .getFile());
550        assertNull("2 returns a wrong anchor", u1.getRef());
551
552        u1 = new URL(u, "dir1/dir2/../file.java", null);
553        assertEquals("3 returns a wrong protocol", "http", u1.getProtocol());
554        assertEquals("3 returns a wrong host", "www.yahoo.com", u1.getHost());
555        assertEquals("3 returns a wrong port", -1, u1.getPort());
556        assertEquals("3 returns a wrong file", "/dir1/dir2/../file.java", u1
557                .getFile());
558        assertNull("3 returns a wrong anchor", u1.getRef());
559
560        // test for question mark processing
561        u = new URL("http://www.foo.com/d0/d1/d2/cgi-bin?foo=bar/baz");
562
563        // test for relative file and out of bound "/../" processing
564        u1 = new URL(u, "../dir1/dir2/../file.java", new MyHandler());
565        assertTrue("A) returns a wrong file: " + u1.getFile(), u1.getFile()
566                .equals("/d0/d1/dir1/file.java"));
567
568        // test for absolute and relative file processing
569        u1 = new URL(u, "/../dir1/dir2/../file.java", null);
570        assertEquals("B) returns a wrong file", "/../dir1/dir2/../file.java",
571                u1.getFile());
572
573        URL one;
574        try {
575            one = new URL("http://www.ibm.com");
576        } catch (MalformedURLException ex) {
577            // Should not happen.
578            throw new RuntimeException(ex.getMessage());
579        }
580        try {
581            new URL(one, (String) null);
582            fail("Specifying null spec on URL constructor should throw MalformedURLException");
583        } catch (MalformedURLException e) {
584            // expected
585        }
586
587        try {
588            // u should raise a MalFormedURLException because u, the context is
589            // null
590            u = null;
591            u1 = new URL(u, "file.java", new MyHandler());
592        } catch (MalformedURLException e) {
593            return;
594        }
595        fail("didn't throw expected MalFormedURLException");
596    }
597
598    /**
599     * @tests java.net.URL#URL(java.lang.String, java.lang.String,
600     *        java.lang.String)
601     */
602    public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String()
603            throws MalformedURLException {
604
605        u = new URL("http", "www.yahoo.com", "test.html#foo");
606        assertEquals("http", u.getProtocol());
607        assertEquals("www.yahoo.com", u.getHost());
608        assertEquals(-1, u.getPort());
609        assertEquals("test.html", u.getFile());
610        assertEquals("foo", u.getRef());
611
612        // Strange behavior in reference, the hostname contains a ':' so it gets
613        // wrapped in '[', ']'
614        URL testURL = new URL("http", "www.apache.org:8080", "test.html#anch");
615        assertEquals("wrong protocol", "http", testURL.getProtocol());
616        assertEquals("wrong host", "[www.apache.org:8080]", testURL.getHost());
617        assertEquals("wrong port", -1, testURL.getPort());
618        assertEquals("wrong file", "test.html", testURL.getFile());
619        assertEquals("wrong anchor", "anch", testURL.getRef());
620    }
621
622    /**
623     * @tests java.net.URL#URL(java.lang.String, java.lang.String, int,
624     *        java.lang.String)
625     */
626    public void test_ConstructorLjava_lang_StringLjava_lang_StringILjava_lang_String()
627            throws MalformedURLException {
628        u = new URL("http", "www.yahoo.com", 8080, "test.html#foo");
629        assertEquals("SSIS returns a wrong protocol", "http", u.getProtocol());
630        assertEquals("SSIS returns a wrong host", "www.yahoo.com", u.getHost());
631        assertEquals("SSIS returns a wrong port", 8080, u.getPort());
632        assertEquals("SSIS returns a wrong file", "test.html", u.getFile());
633        assertTrue("SSIS returns a wrong anchor: " + u.getRef(), u.getRef()
634                .equals("foo"));
635
636        // Regression for HARMONY-83
637        new URL("http", "apache.org", 123456789, "file");
638        try {
639            new URL("http", "apache.org", -123, "file");
640            fail("Assert 0: Negative port should throw exception");
641        } catch (MalformedURLException e) {
642            // expected
643        }
644
645    }
646
647    /**
648     * @tests java.net.URL#URL(java.lang.String, java.lang.String, int,
649     *        java.lang.String, java.net.URLStreamHandler)
650     */
651    public void test_ConstructorLjava_lang_StringLjava_lang_StringILjava_lang_StringLjava_net_URLStreamHandler()
652            throws Exception {
653        // Test for method java.net.URL(java.lang.String, java.lang.String, int,
654        // java.lang.String, java.net.URLStreamHandler)
655        u = new URL("http", "www.yahoo.com", 8080, "test.html#foo", null);
656        assertEquals("SSISH1 returns a wrong protocol", "http", u.getProtocol());
657        assertEquals("SSISH1 returns a wrong host", "www.yahoo.com", u
658                .getHost());
659        assertEquals("SSISH1 returns a wrong port", 8080, u.getPort());
660        assertEquals("SSISH1 returns a wrong file", "test.html", u.getFile());
661        assertTrue("SSISH1 returns a wrong anchor: " + u.getRef(), u.getRef()
662                .equals("foo"));
663
664        u = new URL("http", "www.yahoo.com", 8080, "test.html#foo",
665                new MyHandler());
666        assertEquals("SSISH2 returns a wrong protocol", "http", u.getProtocol());
667        assertEquals("SSISH2 returns a wrong host", "www.yahoo.com", u
668                .getHost());
669        assertEquals("SSISH2 returns a wrong port", 8080, u.getPort());
670        assertEquals("SSISH2 returns a wrong file", "test.html", u.getFile());
671        assertTrue("SSISH2 returns a wrong anchor: " + u.getRef(), u.getRef()
672                .equals("foo"));
673    }
674
675    /**
676     * @tests java.net.URL#equals(java.lang.Object)
677     */
678    public void test_equalsLjava_lang_Object() throws MalformedURLException {
679        u = new URL("http://www.apache.org:8080/dir::23??????????test.html");
680        u1 = new URL("http://www.apache.org:8080/dir::23??????????test.html");
681        assertTrue("A) equals returns false for two identical URLs", u
682                .equals(u1));
683        assertTrue("return true for null comparison", !u1.equals(null));
684        u = new URL("ftp://www.apache.org:8080/dir::23??????????test.html");
685        assertTrue("Returned true for non-equal URLs", !u.equals(u1));
686
687        // Regression for HARMONY-6556
688        u = new URL("file", null, 0, "/test.txt");
689        u1 = new URL("file", null, 0, "/test.txt");
690        assertEquals(u, u1);
691
692        u = new URL("file", "first.invalid", 0, "/test.txt");
693        u1 = new URL("file", "second.invalid", 0, "/test.txt");
694        assertFalse(u.equals(u1));
695
696        u = new URL("file", "harmony.apache.org", 0, "/test.txt");
697        u1 = new URL("file", "www.apache.org", 0, "/test.txt");
698        assertEquals(u, u1);
699    }
700
701    /**
702     * @tests java.net.URL#sameFile(java.net.URL)
703     */
704    public void test_sameFileLjava_net_URL() throws Exception {
705        // Test for method boolean java.net.URL.sameFile(java.net.URL)
706        u = new URL("http://www.yahoo.com");
707        u1 = new URL("http", "www.yahoo.com", "");
708        assertTrue("Should be the same1", u.sameFile(u1));
709        u = new URL("http://www.yahoo.com/dir1/dir2/test.html#anchor1");
710        u1 = new URL("http://www.yahoo.com/dir1/dir2/test.html#anchor2");
711        assertTrue("Should be the same ", u.sameFile(u1));
712
713        // regression test for Harmony-1040
714        u = new URL("file", null, -1, "/d:/somedir/");
715        u1 = new URL("file:/d:/somedir/");
716        assertFalse(u.sameFile(u1));
717
718        // regression test for Harmony-2136
719        URL url1 = new URL("file:///anyfile");
720        URL url2 = new URL("file://localhost/anyfile");
721        assertTrue(url1.sameFile(url2));
722
723        url1 = new URL("http:///anyfile");
724        url2 = new URL("http://localhost/anyfile");
725        assertFalse(url1.sameFile(url2));
726
727        url1 = new URL("ftp:///anyfile");
728        url2 = new URL("ftp://localhost/anyfile");
729        assertFalse(url1.sameFile(url2));
730
731        url1 = new URL("jar:file:///anyfile.jar!/");
732        url2 = new URL("jar:file://localhost/anyfile.jar!/");
733        assertFalse(url1.sameFile(url2));
734    }
735
736    /**
737     * @tests java.net.URL#getContent()
738     */
739    public void test_getContent() {
740        // Test for method java.lang.Object java.net.URL.getContent()
741        byte[] ba;
742        InputStream is;
743        String s;
744        File resources = Support_Resources.createTempFolder();
745        try {
746            Support_Resources.copyFile(resources, null, "hyts_htmltest.html");
747            u = new URL("file", "", resources.getAbsolutePath()
748                    + "/hyts_htmltest.html");
749            u.openConnection();
750            is = (InputStream) u.getContent();
751            is.read(ba = new byte[4096]);
752            s = new String(ba);
753            assertTrue(
754                    "Incorrect content "
755                            + u
756                            + " does not contain: \" A Seemingly Non Important String \"",
757                    s.indexOf("A Seemingly Non Important String") >= 0);
758        } catch (IOException e) {
759            fail("IOException thrown : " + e.getMessage());
760        } finally {
761            // Support_Resources.deleteTempFolder(resources);
762        }
763    }
764
765    /**
766     * @tests java.net.URL#getContent(class[])
767     */
768    public void test_getContent_LJavaLangClass() throws Exception {
769        byte[] ba;
770        InputStream is;
771        String s;
772
773        File resources = Support_Resources.createTempFolder();
774
775        Support_Resources.copyFile(resources, null, "hyts_htmltest.html");
776        u = new URL("file", "", resources.getAbsolutePath()
777                + "/hyts_htmltest.html");
778        u.openConnection();
779
780        is = (InputStream) u.getContent(new Class[] { Object.class });
781        is.read(ba = new byte[4096]);
782        s = new String(ba);
783        assertTrue("Incorrect content " + u
784                + " does not contain: \" A Seemingly Non Important String \"",
785                s.indexOf("A Seemingly Non Important String") >= 0);
786
787    }
788
789    /**
790     * @tests java.net.URL#openStream()
791     */
792    public void test_openStream() throws Exception {
793        // Regression test for Harmony-1700
794        URL BASE = URLTest.class.getClassLoader().getResource(
795                URLTest.class.getPackage().getName().replace('.',
796                        File.separatorChar)
797                        + "/lf.jar");
798        URL url = new URL("jar:" + BASE + "!/foo.jar!/Bugs/HelloWorld.class");
799        try {
800            url.openStream();
801            fail("should throw FNFE.");
802        } catch (java.io.FileNotFoundException e) {
803            // Expected
804        }
805
806        // Test for method java.io.InputStream java.net.URL.openStream()
807        File resources = Support_Resources.createTempFolder();
808        Support_Resources.copyFile(resources, null, "hyts_htmltest.html");
809        u = new URL("file", "", resources.getAbsolutePath()
810                + "/hyts_htmltest.html");
811        // HTTP connection
812        InputStream is1 = u.openStream();
813        assertTrue("Unable to read from stream", is1.read() != 0);
814        is1.close();
815
816        boolean exception = false;
817        try {
818            u = new URL("file:///nonexistenttestdir/tstfile");
819            u.openStream();
820        } catch (IOException e) {
821            // Correct behaviour
822            exception = true;
823        }
824        assertTrue("openStream succeeded for non existent resource", exception);
825    }
826
827    /**
828     * @tests java.net.URL#openConnection()
829     */
830    public void test_openConnection() {
831        // Test for method java.net.URLConnection java.net.URL.openConnection()
832        try {
833            u = new URL("systemresource:/FILE4/+/types.properties");
834            URLConnection uConn = u.openConnection();
835            assertNotNull("u.openConnection() returns null", uConn);
836        } catch (Exception e) {
837        }
838    }
839
840    /**
841     * @tests java.net.URL#toString()
842     */
843    public void test_toString() {
844        // Test for method java.lang.String java.net.URL.toString()
845        try {
846            u1 = new URL("http://www.yahoo2.com:9999");
847            u = new URL(
848                    "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
849            assertEquals(
850                    "a) Does not return the right url string",
851
852                    "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1",
853                    u.toString());
854            assertEquals("b) Does not return the right url string",
855                    "http://www.yahoo2.com:9999", u1.toString());
856            assertTrue("c) Does not return the right url string", u
857                    .equals(new URL(u.toString())));
858        } catch (Exception e) {
859        }
860    }
861
862    /**
863     * @tests java.net.URL#toExternalForm()
864     */
865    public void test_toExternalForm() {
866        // Test for method java.lang.String java.net.URL.toExternalForm()
867        try {
868            u1 = new URL("http://www.yahoo2.com:9999");
869            u = new URL(
870                    "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
871            assertEquals(
872                    "a) Does not return the right url string",
873
874                    "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1",
875                    u.toString());
876            assertEquals("b) Does not return the right url string",
877                    "http://www.yahoo2.com:9999", u1.toString());
878            assertTrue("c) Does not return the right url string", u
879                    .equals(new URL(u.toString())));
880
881            u = new URL("http:index");
882            assertEquals("2 wrong external form", "http:index", u
883                    .toExternalForm());
884
885            u = new URL("http", null, "index");
886            assertEquals("2 wrong external form", "http:index", u
887                    .toExternalForm());
888        } catch (Exception e) {
889        }
890    }
891
892    /**
893     * @tests java.net.URL#getFile()
894     */
895    public void test_getFile() throws Exception {
896        // Test for method java.lang.String java.net.URL.getFile()
897        u = new URL("http", "www.yahoo.com:8080", 1233,
898                "test/!@$%^&*/test.html#foo");
899        assertEquals("returns a wrong file", "test/!@$%^&*/test.html", u
900                .getFile());
901        u = new URL("http", "www.yahoo.com:8080", 1233, "");
902        assertTrue("returns a wrong file", u.getFile().equals(""));
903    }
904
905    /**
906     * @tests java.net.URL#getHost()
907     */
908    public void test_getHost() throws MalformedURLException {
909        // Regression for HARMONY-60
910        String ipv6Host = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210";
911        URL url = new URL("http", ipv6Host, -1, "myfile");
912        assertEquals(("[" + ipv6Host + "]"), url.getHost());
913    }
914
915    /**
916     * @tests java.net.URL#getPort()
917     */
918    public void test_getPort() throws Exception {
919        // Test for method int java.net.URL.getPort()
920        u = new URL("http://member12.c++.com:9999");
921        assertTrue("return wrong port number " + u.getPort(),
922                u.getPort() == 9999);
923        u = new URL("http://member12.c++.com:9999/");
924        assertEquals("return wrong port number", 9999, u.getPort());
925    }
926
927    /**
928     * @throws MalformedURLException
929     * @tests java.net.URL#getDefaultPort()
930     */
931    public void test_getDefaultPort() throws MalformedURLException {
932        u = new URL("http://member12.c++.com:9999");
933        assertEquals(80, u.getDefaultPort());
934        u = new URL("ftp://member12.c++.com:9999/");
935        assertEquals(21, u.getDefaultPort());
936    }
937
938    /**
939     * @tests java.net.URL#getProtocol()
940     */
941    public void test_getProtocol() throws Exception {
942        // Test for method java.lang.String java.net.URL.getProtocol()
943        u = new URL("http://www.yahoo2.com:9999");
944        assertTrue("u returns a wrong protocol: " + u.getProtocol(), u
945                .getProtocol().equals("http"));
946    }
947
948    /**
949     * @tests java.net.URL#getRef()
950     */
951    public void test_getRef() {
952        // Test for method java.lang.String java.net.URL.getRef()
953        try {
954            u1 = new URL("http://www.yahoo2.com:9999");
955            u = new URL(
956                    "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
957            assertEquals("returns a wrong anchor1", "anchor1", u.getRef());
958            assertNull("returns a wrong anchor2", u1.getRef());
959            u1 = new URL("http://www.yahoo2.com#ref");
960            assertEquals("returns a wrong anchor3", "ref", u1.getRef());
961            u1 = new URL("http://www.yahoo2.com/file#ref1#ref2");
962            assertEquals("returns a wrong anchor4", "ref1#ref2", u1.getRef());
963        } catch (MalformedURLException e) {
964            fail("Incorrect URL format : " + e.getMessage());
965        }
966    }
967
968    /**
969     * @tests java.net.URL#getAuthority()
970     */
971    public void test_getAuthority() throws MalformedURLException {
972        URL testURL = new URL("http", "hostname", 80, "/java?q1#ref");
973        assertEquals("hostname:80", testURL.getAuthority());
974        assertEquals("hostname", testURL.getHost());
975        assertNull(testURL.getUserInfo());
976        assertEquals("/java?q1", testURL.getFile());
977        assertEquals("/java", testURL.getPath());
978        assertEquals("q1", testURL.getQuery());
979        assertEquals("ref", testURL.getRef());
980
981        testURL = new URL("http", "u:p@home", 80, "/java?q1#ref");
982        assertEquals("[u:p@home]:80", testURL.getAuthority());
983        assertEquals("[u:p@home]", testURL.getHost());
984        assertNull(testURL.getUserInfo());
985        assertEquals("/java?q1", testURL.getFile());
986        assertEquals("/java", testURL.getPath());
987        assertEquals("q1", testURL.getQuery());
988        assertEquals("ref", testURL.getRef());
989
990        testURL = new URL("http", "home", -1, "/java");
991        assertEquals("wrong authority2", "home", testURL.getAuthority());
992        assertNull("wrong userInfo2", testURL.getUserInfo());
993        assertEquals("wrong host2", "home", testURL.getHost());
994        assertEquals("wrong file2", "/java", testURL.getFile());
995        assertEquals("wrong path2", "/java", testURL.getPath());
996        assertNull("wrong query2", testURL.getQuery());
997        assertNull("wrong ref2", testURL.getRef());
998    }
999
1000    /**
1001     * @tests java.net.URL#toURL()
1002     */
1003    public void test_toURI() throws Exception {
1004        u = new URL("http://www.apache.org");
1005        URI uri = u.toURI();
1006        assertTrue(u.equals(uri.toURL()));
1007    }
1008
1009    /**
1010     * @tests java.net.URL#openConnection()
1011     */
1012    public void test_openConnection_SelectorCalled()
1013            throws MalformedURLException {
1014        URL httpUrl = new URL("http://"
1015                + Support_Configuration.ProxyServerTestHost
1016                + "/cgi-bin/test.pl");
1017        URL ftpUrl = new URL("ftp://" + Support_Configuration.FTPTestAddress
1018                + "/nettest.txt");
1019        URL[] urlList = { httpUrl, ftpUrl };
1020        ProxySelector originalSelector = ProxySelector.getDefault();
1021        ProxySelector.setDefault(new MockProxySelector());
1022        try {
1023            for (int i = 0; i < urlList.length; ++i) {
1024                try {
1025                    isSelectCalled = false;
1026                    URLConnection conn = urlList[i].openConnection();
1027                    conn.getInputStream();
1028                } catch (Exception e) {
1029                    // ignore
1030                }
1031                assertTrue(
1032                        "openConnection should call ProxySelector.select(), url = "
1033                                + urlList[i], isSelectCalled);
1034            }
1035        } finally {
1036            ProxySelector.setDefault(originalSelector);
1037        }
1038    }
1039
1040    /**
1041     * @throws IOException
1042     * @tests java.net.URL#openConnection(Proxy)
1043     */
1044    public void test_openConnection_proxy_SelectorCalled() throws IOException {
1045        URL httpUrl = new URL("http://"
1046                + Support_Configuration.ProxyServerTestHost
1047                + "/cgi-bin/test.pl");
1048
1049        try {
1050            httpUrl.openConnection(null);
1051            fail("should throw IllegalArgumentException");
1052        } catch (IllegalArgumentException e) {
1053            // expected
1054        }
1055
1056        URLConnection uc = httpUrl.openConnection(new Proxy(Type.SOCKS,
1057                new InetSocketAddress(InetAddress.getLocalHost(), 80)));
1058        assertEquals(uc.getURL(), httpUrl);
1059    }
1060
1061    /**
1062     * @tests java.net.URL#openConnection()
1063     */
1064    public void test_openConnection_FileProtocal() throws Exception {
1065        // Regression test for Harmony-5779
1066        String basedir = new File("temp.java").getAbsolutePath();
1067        String fileUrlString = "file://localhost/" + basedir;
1068        URLConnection conn  = new URL(fileUrlString).openConnection();
1069        assertEquals("file",conn.getURL().getProtocol());
1070        assertEquals(new File(basedir),new File(conn.getURL().getFile()));
1071
1072        String nonLocalUrlString = "file://anything/" + basedir;
1073        conn  = new URL(nonLocalUrlString).openConnection();
1074        assertEquals("ftp",conn.getURL().getProtocol());
1075        assertEquals(new File(basedir),new File(conn.getURL().getFile()));
1076    }
1077
1078    /**
1079     * URLStreamHandler implementation class necessary for tests.
1080     */
1081    private class TestURLStreamHandler extends URLStreamHandler {
1082        public URLConnection openConnection(URL arg0) throws IOException {
1083            try {
1084                return arg0.openConnection();
1085            } catch (Throwable e) {
1086                return null;
1087            }
1088        }
1089
1090        public URLConnection openConnection(URL arg0, Proxy proxy)
1091                throws IOException {
1092            return super.openConnection(u, proxy);
1093        }
1094    }
1095
1096    /**
1097     * Check UnsupportedOperationException in openConnection(URL arg0, Proxy
1098     * proxy)
1099     */
1100    public void test_openConnection_URL_Proxy() throws Exception {
1101        // Regression for HARMONY-1131
1102        TestURLStreamHandler lh = new TestURLStreamHandler();
1103        URL httpUrl = new URL("http://"
1104                + Support_Configuration.ProxyServerTestHost
1105                + "/cgi-bin/test.pl");
1106        Proxy proxy = new Proxy(Type.SOCKS, new InetSocketAddress(InetAddress
1107                .getLocalHost(), 80));
1108
1109        try {
1110            lh.openConnection(null, null);
1111            fail("UnsupportedOperationException expected, but nothing was thrown!");
1112        } catch (UnsupportedOperationException e) {
1113            // Expected
1114        }
1115
1116        try {
1117            lh.openConnection(httpUrl, proxy);
1118            fail("UnsupportedOperationException expected, but nothing was thrown!");
1119        } catch (UnsupportedOperationException e) {
1120            // Expected
1121        }
1122    }
1123
1124    /**
1125     * Check NPE throwing in constructor when protocol argument is null and
1126     * URLStreamHandler argument is initialized.
1127     */
1128    public void test_ConstructorLnullLjava_lang_StringILjava_lang_StringLjava_net_URLStreamHandler()
1129            throws Exception {
1130        // Regression for HARMONY-1131
1131        TestURLStreamHandler lh = new TestURLStreamHandler();
1132
1133        try {
1134            new URL(null, "1", 0, "file", lh);
1135            fail("NullPointerException expected, but nothing was thrown!");
1136        } catch (NullPointerException e) {
1137            // Expected NullPointerException
1138        }
1139
1140    }
1141
1142    /**
1143     * Check NPE throwing in constructor when protocol argument is null and
1144     * URLStreamHandler argument is null.
1145     */
1146    public void test_ConstructorLnullLjava_lang_StringILjava_lang_StringLnull()
1147            throws Exception {
1148        // Regression for HARMONY-1131
1149        try {
1150            new URL(null, "1", 0, "file", null);
1151            fail("NullPointerException expected, but nothing was thrown!");
1152        } catch (NullPointerException e) {
1153            // Expected NullPointerException
1154        }
1155    }
1156
1157    /**
1158     * Check NPE throwing in constructor with 4 params when protocol argument is
1159     * null.
1160     */
1161    public void test_ConstructorLnullLjava_lang_StringILjava_lang_String()
1162            throws Exception {
1163        // Regression for HARMONY-1131
1164        try {
1165            new URL(null, "1", 0, "file");
1166            fail("NullPointerException expected, but nothing was thrown!");
1167        } catch (NullPointerException e) {
1168            // Expected NullPointerException
1169        }
1170    }
1171
1172    /**
1173     * Check NPE throwing in constructor with 3 params when protocol argument is
1174     * null.
1175     */
1176    public void test_ConstructorLnullLjava_lang_StringLjava_lang_String()
1177            throws Exception {
1178        // Regression for HARMONY-1131
1179        try {
1180            new URL(null, "1", "file");
1181            fail("NullPointerException expected, but nothing was thrown!");
1182        } catch (NullPointerException e) {
1183            // Expected NullPointerException
1184        }
1185    }
1186
1187    public void test_toExternalForm_Absolute() throws MalformedURLException {
1188        String strURL = "http://localhost?name=value";
1189        URL url = new URL(strURL);
1190        assertEquals(strURL, url.toExternalForm());
1191
1192        strURL = "http://localhost?name=value/age=12";
1193        url = new URL(strURL);
1194        assertEquals(strURL, url.toExternalForm());
1195    }
1196
1197    public void test_toExternalForm_Relative() throws MalformedURLException {
1198        String strURL = "http://a/b/c/d;p?q";
1199        String ref = "?y";
1200        URL url = new URL(new URL(strURL), ref);
1201        assertEquals("http://a/b/c/?y", url.toExternalForm());
1202    }
1203
1204    // Regression test for HARMONY-6254
1205
1206    // Bogus handler forces file part of URL to be null
1207    static class MyHandler2 extends URLStreamHandler {
1208
1209        @Override
1210        protected URLConnection openConnection(URL arg0) throws IOException {
1211            return null;
1212        }
1213
1214        @Override
1215        protected void setURL(URL u, String protocol, String host, int port,
1216                String authority, String userInfo, String file, String query,
1217                String ref) {
1218            super.setURL(u, protocol, host, port, authority, userInfo,
1219                    (String) null, query, ref);
1220        }
1221    }
1222
1223    // Test special case of external form with null file part (HARMONY-6254)
1224    public void test_toExternalForm_Null() throws IOException {
1225        URLStreamHandler myHandler = new MyHandler2();
1226        URL url = new URL(null, "foobar://example.com/foobar", myHandler);
1227        String s = url.toExternalForm();
1228        assertEquals("Got wrong URL external form", "foobar://example.com", s);
1229    }
1230
1231    static class MockProxySelector extends ProxySelector {
1232
1233        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
1234            System.out.println("connection failed");
1235        }
1236
1237        public List<Proxy> select(URI uri) {
1238            isSelectCalled = true;
1239            ArrayList<Proxy> proxyList = new ArrayList<Proxy>(1);
1240            proxyList.add(Proxy.NO_PROXY);
1241            return proxyList;
1242        }
1243    }
1244
1245    static class MyURLStreamHandler extends URLStreamHandler {
1246
1247        @Override
1248        protected URLConnection openConnection(URL arg0) throws IOException {
1249            return null;
1250        }
1251
1252        public void parse(URL url, String spec, int start, int end) {
1253            parseURL(url, spec, start, end);
1254        }
1255    }
1256
1257    static class MyURLStreamHandlerFactory implements URLStreamHandlerFactory {
1258
1259        public static MyURLStreamHandler handler = new MyURLStreamHandler();
1260
1261        public URLStreamHandler createURLStreamHandler(String arg0) {
1262            handler = new MyURLStreamHandler();
1263            return handler;
1264        }
1265
1266    }
1267
1268    // Regression test for harmony-2941
1269    public void test_URLStreamHandler_parseURL() throws MalformedURLException {
1270        URL url = new URL("http://localhost");
1271        MyURLStreamHandler handler = MyURLStreamHandlerFactory.handler;
1272        try {
1273            handler.parse(url, "//", 0, Integer.MIN_VALUE);
1274            fail("Should throw SIOOBE.");
1275        } catch (StringIndexOutOfBoundsException e) {
1276            // expected;
1277        }
1278        try {
1279            handler.parse(url, "1234//", 4, Integer.MIN_VALUE);
1280            fail("Should throw SIOOBE.");
1281        } catch (StringIndexOutOfBoundsException e) {
1282            // expected;
1283        }
1284        try {
1285            handler.parse(url, "1", -1, 0);
1286            fail("Should throw SIOOBE.");
1287        } catch (StringIndexOutOfBoundsException e) {
1288            // expected;
1289        }
1290        try {
1291            handler.parse(url, "1", 3, 2);
1292            fail("Should throw SecurityException.");
1293        } catch (SecurityException e) {
1294            // expected;
1295        }
1296
1297        try {
1298            handler.parse(url, "11", 1, Integer.MIN_VALUE);
1299            fail("Should throw SecurityException.");
1300        } catch (SecurityException e) {
1301            // expected;
1302        }
1303
1304        // Regression tests for HARMONY-6499
1305        try {
1306            handler.parse(url, "any", 10, Integer.MIN_VALUE);
1307            fail("Should throw StringIndexOutOfBoundsException");
1308        } catch (StringIndexOutOfBoundsException e) {
1309            // expected;
1310        }
1311
1312        try {
1313            handler.parse(url, "any", 10, Integer.MIN_VALUE+1);
1314            fail("Should throw StringIndexOutOfBoundsException");
1315        } catch (StringIndexOutOfBoundsException e) {
1316            // expected;
1317        }
1318
1319        try {
1320            handler.parse(url, "any", Integer.MIN_VALUE, Integer.MIN_VALUE);
1321            fail("Should throw StringIndexOutOfBoundsException");
1322        } catch (StringIndexOutOfBoundsException e) {
1323            // expected;
1324        }
1325
1326        try {
1327            handler.parse(url, "any", Integer.MIN_VALUE, 2);
1328            fail("Should throw StringIndexOutOfBoundsException");
1329        } catch (StringIndexOutOfBoundsException e) {
1330            // expected;
1331        }
1332
1333        try {
1334            handler.parse(url, "any", -1, 2);
1335            fail("Should throw StringIndexOutOfBoundsException");
1336        } catch (StringIndexOutOfBoundsException e) {
1337            // expected;
1338        }
1339
1340        try {
1341            handler.parse(url, "any", -1, -1);
1342            fail("Should throw SecurityException");
1343        } catch (SecurityException e) {
1344            // expected;
1345        }
1346    }
1347
1348    /**
1349     * @tests java.net.URL#URL(String, String, String)
1350     */
1351    public void test_java_protocol_handler_pkgs_prop()
1352            throws MalformedURLException {
1353        // Regression test for Harmony-3094
1354        final String HANDLER_PKGS = "java.protocol.handler.pkgs";
1355        String pkgs = System.getProperty(HANDLER_PKGS);
1356        System.setProperty(HANDLER_PKGS,
1357                "fake|org.apache.harmony.luni.tests.java.net");
1358
1359        try {
1360            new URL("test_protocol", "", "fake.jar");
1361        } finally {
1362            if (pkgs == null) {
1363                System.clearProperty(HANDLER_PKGS);
1364            } else {
1365                System.setProperty(HANDLER_PKGS, pkgs);
1366            }
1367        }
1368    }
1369}
1370