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