KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > URLTestCase


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.catalina.util;
19
20
21 import java.net.MalformedURLException JavaDoc;
22 import junit.framework.Test;
23 import junit.framework.TestCase;
24 import junit.framework.TestSuite;
25
26
27 /**
28  * Unit tests for the <code>org.apache.catalina.util.URL</code> class.
29  *
30  * @author Craig R. McClanahan
31  * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:55 $
32  */

33
34 public class URLTestCase extends TestCase {
35
36
37     // ----------------------------------------------------- Instance Variables
38

39
40     // ----------------------------------------------------------- Constructors
41

42
43     /**
44      * Construct a new instance of this test case.
45      *
46      * @param name Name of the test case
47      */

48     public URLTestCase(String JavaDoc name) {
49
50         super(name);
51
52     }
53
54
55     // --------------------------------------------------- Overall Test Methods
56

57
58     /**
59      * Set up instance variables required by this test case.
60      */

61     public void setUp() {
62
63         ; // No action required
64

65     }
66
67
68     /**
69      * Return the tests included in this test suite.
70      */

71     public static Test suite() {
72
73         return (new TestSuite(URLTestCase.class));
74
75     }
76
77
78     /**
79      * Tear down instance variables required by this test case.
80      */

81     public void tearDown() {
82
83         ; // No action required
84

85     }
86
87
88     // ------------------------------------------------ Individual Test Methods
89

90
91     /**
92      * Negative tests for absolute URL strings in various patterns. Each of
93      * these should throw <code>MalformedURLException</code>.
94      */

95     public void testNegativeAbsolute() {
96
97         negative("index.html");
98         negative("index.html#ref");
99         negative("index.html?name=value");
100         negative("index.html?name=value#ref");
101
102         negative("/index.html");
103         negative("/index.html#ref");
104         negative("/index.html?name=value");
105         negative("/index.html?name=value#ref");
106
107     }
108
109
110     /**
111      * Negative tests for <code>normalize()</code>. Attempts to normalize
112      * these legal URLs should throw <code>MalformedURLException</code>.
113      */

114     public void testNegativeNormalize() {
115
116         normalize("http://localhost/..");
117         normalize("http://localhost/..#ref");
118         normalize("http://localhost/..?name=value");
119         normalize("http://localhost/..?name=value#ref");
120
121         normalize("http://localhost:8080/..");
122         normalize("http://localhost:8080/..#ref");
123         normalize("http://localhost:8080/..?name=value");
124         normalize("http://localhost:8080/..?name=value#ref");
125  
126         normalize("http://localhost/../");
127         normalize("http://localhost/../#ref");
128         normalize("http://localhost/../?name=value");
129         normalize("http://localhost/../?name=value#ref");
130
131         normalize("http://localhost:8080/../");
132         normalize("http://localhost:8080/../#ref");
133         normalize("http://localhost:8080/../?name=value");
134         normalize("http://localhost:8080/../?name=value#ref");
135  
136         normalize("http://localhost/index.html/../../foo.html");
137         normalize("http://localhost/index.html/../../foo.html#ref");
138         normalize("http://localhost/index.html/../../foo.html?name=value");
139         normalize("http://localhost/index.html/../../foo.html?name=value#ref");
140
141         normalize("http://localhost:8080/index.html/../../foo.html");
142         normalize("http://localhost:8080/index.html/../../foo.html#ref");
143         normalize("http://localhost:8080/index.html/../../foo.html?name=value");
144         normalize("http://localhost:8080/index.html/../../foo.html?name=value#ref");
145  
146     }
147
148
149     /**
150      * Negative tests for relative URL strings in various patterns. Each of
151      * these should throw <code>MalformedURLException</code>.
152      */

153     public void testNegativeRelative() {
154
155         // Commented out because java.net.URL ignores extraneous "../"
156
// negative("http://a/b/c/d;p?q", "../../../g");
157
// negative("http://a/b/c/d;p?q", "/../g");
158

159     }
160
161
162     /**
163      * Positive tests for absolute URL strings in various patterns.
164      */

165     public void testPositiveAbsolute() {
166
167         positive("http://a/b/c/d;p?q");
168
169         positive("http://localhost/index.html");
170         positive("http://localhost/index.html#ref");
171         positive("http://localhost/index.html?name=value");
172         positive("http://localhost/index.html?name=value#ref");
173
174         positive("http://localhost:8080/index.html");
175         positive("http://localhost:8080/index.html#ref");
176         positive("http://localhost:8080/index.html?name=value");
177         positive("http://localhost:8080/index.html?name=value#ref");
178
179         positive("http://localhost/index.html/.");
180         positive("http://localhost/index.html/.#ref");
181         positive("http://localhost/index.html/.?name=value");
182         positive("http://localhost/index.html/.?name=value#ref");
183
184         positive("http://localhost:8080/index.html/.");
185         positive("http://localhost:8080/index.html/.#ref");
186         positive("http://localhost:8080/index.html/.?name=value");
187         positive("http://localhost:8080/index.html/.?name=value#ref");
188
189         positive("http://localhost/index.html/foo/..");
190         positive("http://localhost/index.html/foo/..#ref");
191         positive("http://localhost/index.html/foo/..?name=value");
192         positive("http://localhost/index.html/foo/..?name=value#ref");
193
194         positive("http://localhost:8080/index.html/foo/..");
195         positive("http://localhost:8080/index.html/foo/..#ref");
196         positive("http://localhost:8080/index.html/foo/..?name=value");
197         positive("http://localhost:8080/index.html/foo/..?name=value#ref");
198
199         positive("http://localhost/index.html/../foo.html");
200         positive("http://localhost/index.html/../foo.html#ref");
201         positive("http://localhost/index.html/../foo.html?name=value");
202         positive("http://localhost/index.html/../foo.html?name=value#ref");
203
204         positive("http://localhost:8080/index.html/../foo.html");
205         positive("http://localhost:8080/index.html/../foo.html#ref");
206         positive("http://localhost:8080/index.html/../foo.html?name=value");
207         positive("http://localhost:8080/index.html/../foo.html?name=value#ref");
208
209         positive("http://localhost");
210         positive("http://localhost#ref");
211         positive("http://localhost?name=value");
212         positive("http://localhost?name=value#ref");
213
214         positive("http://localhost:8080");
215         positive("http://localhost:8080#ref");
216         positive("http://localhost:8080?name=value");
217         positive("http://localhost:8080?name=value#ref");
218
219         positive("http://localhost/");
220         positive("http://localhost/#ref");
221         positive("http://localhost/?name=value");
222         positive("http://localhost/?name=value#ref");
223
224         positive("http://localhost:8080/");
225         positive("http://localhost:8080/#ref");
226         positive("http://localhost:8080/?name=value");
227         positive("http://localhost:8080/?name=value#ref");
228
229     }
230
231
232     /**
233      * Positive tests for normalizing absolute URL strings in various patterns.
234      */

235     public void testPositiveNormalize() {
236
237         normalize("http://a/b/c/d;p?q",
238                   "http://a/b/c/d;p?q");
239
240         normalize("http://localhost/index.html",
241                   "http://localhost/index.html");
242         normalize("http://localhost/index.html#ref",
243                   "http://localhost/index.html#ref");
244         normalize("http://localhost/index.html?name=value",
245                   "http://localhost/index.html?name=value");
246         normalize("http://localhost/index.html?name=value#ref",
247                   "http://localhost/index.html?name=value#ref");
248
249         normalize("http://localhost:8080/index.html",
250                   "http://localhost:8080/index.html");
251         normalize("http://localhost:8080/index.html#ref",
252                   "http://localhost:8080/index.html#ref");
253         normalize("http://localhost:8080/index.html?name=value",
254                   "http://localhost:8080/index.html?name=value");
255         normalize("http://localhost:8080/index.html?name=value#ref",
256                   "http://localhost:8080/index.html?name=value#ref");
257
258         normalize("http://localhost/./index.html",
259                   "http://localhost/index.html");
260         normalize("http://localhost/./index.html#ref",
261                   "http://localhost/index.html#ref");
262         normalize("http://localhost/./index.html?name=value",
263                   "http://localhost/index.html?name=value");
264         normalize("http://localhost/./index.html?name=value#ref",
265                   "http://localhost/index.html?name=value#ref");
266
267         normalize("http://localhost:8080/./index.html",
268                   "http://localhost:8080/index.html");
269         normalize("http://localhost:8080/./index.html#ref",
270                   "http://localhost:8080/index.html#ref");
271         normalize("http://localhost:8080/./index.html?name=value",
272                   "http://localhost:8080/index.html?name=value");
273         normalize("http://localhost:8080/./index.html?name=value#ref",
274                   "http://localhost:8080/index.html?name=value#ref");
275
276         normalize("http://localhost/index.html/.",
277                   "http://localhost/index.html/");
278         normalize("http://localhost/index.html/.#ref",
279                   "http://localhost/index.html/#ref");
280         normalize("http://localhost/index.html/.?name=value",
281                   "http://localhost/index.html/?name=value");
282         normalize("http://localhost/index.html/.?name=value#ref",
283                   "http://localhost/index.html/?name=value#ref");
284
285         normalize("http://localhost:8080/index.html/.",
286                   "http://localhost:8080/index.html/");
287         normalize("http://localhost:8080/index.html/.#ref",
288                   "http://localhost:8080/index.html/#ref");
289         normalize("http://localhost:8080/index.html/.?name=value",
290                   "http://localhost:8080/index.html/?name=value");
291         normalize("http://localhost:8080/index.html/.?name=value#ref",
292                   "http://localhost:8080/index.html/?name=value#ref");
293
294         normalize("http://localhost/index.html/./",
295                   "http://localhost/index.html/");
296         normalize("http://localhost/index.html/./#ref",
297                   "http://localhost/index.html/#ref");
298         normalize("http://localhost/index.html/./?name=value",
299                   "http://localhost/index.html/?name=value");
300         normalize("http://localhost/index.html/./?name=value#ref",
301                   "http://localhost/index.html/?name=value#ref");
302
303         normalize("http://localhost:8080/index.html/./",
304                   "http://localhost:8080/index.html/");
305         normalize("http://localhost:8080/index.html/./#ref",
306                   "http://localhost:8080/index.html/#ref");
307         normalize("http://localhost:8080/index.html/./?name=value",
308                   "http://localhost:8080/index.html/?name=value");
309         normalize("http://localhost:8080/index.html/./?name=value#ref",
310                   "http://localhost:8080/index.html/?name=value#ref");
311
312         normalize("http://localhost/foo.html/../index.html",
313                   "http://localhost/index.html");
314         normalize("http://localhost/foo.html/../index.html#ref",
315                   "http://localhost/index.html#ref");
316         normalize("http://localhost/foo.html/../index.html?name=value",
317                   "http://localhost/index.html?name=value");
318         normalize("http://localhost/foo.html/../index.html?name=value#ref",
319                   "http://localhost/index.html?name=value#ref");
320
321         normalize("http://localhost:8080/foo.html/../index.html",
322                   "http://localhost:8080/index.html");
323         normalize("http://localhost:8080/foo.html/../index.html#ref",
324                   "http://localhost:8080/index.html#ref");
325         normalize("http://localhost:8080/foo.html/../index.html?name=value",
326                   "http://localhost:8080/index.html?name=value");
327         normalize("http://localhost:8080/foo.html/../index.html?name=value#ref",
328                   "http://localhost:8080/index.html?name=value#ref");
329
330         normalize("http://localhost/index.html/foo.html/..",
331                   "http://localhost/index.html/");
332         normalize("http://localhost/index.html/foo.html/..#ref",
333                   "http://localhost/index.html/#ref");
334         normalize("http://localhost/index.html/foo.html/..?name=value",
335                   "http://localhost/index.html/?name=value");
336         normalize("http://localhost/index.html/foo.html/..?name=value#ref",
337                   "http://localhost/index.html/?name=value#ref");
338
339         normalize("http://localhost:8080/index.html/foo.html/..",
340                   "http://localhost:8080/index.html/");
341         normalize("http://localhost:8080/index.html/foo.html/..#ref",
342                   "http://localhost:8080/index.html/#ref");
343         normalize("http://localhost:8080/index.html/foo.html/..?name=value",
344                   "http://localhost:8080/index.html/?name=value");
345         normalize("http://localhost:8080/index.html/foo.html/..?name=value#ref",
346                   "http://localhost:8080/index.html/?name=value#ref");
347
348         normalize("http://localhost/index.html/foo.html/../",
349                   "http://localhost/index.html/");
350         normalize("http://localhost/index.html/foo.html/../#ref",
351                   "http://localhost/index.html/#ref");
352         normalize("http://localhost/index.html/foo.html/../?name=value",
353                   "http://localhost/index.html/?name=value");
354         normalize("http://localhost/index.html/foo.html/../?name=value#ref",
355                   "http://localhost/index.html/?name=value#ref");
356
357         normalize("http://localhost:8080/index.html/foo.html/../",
358                   "http://localhost:8080/index.html/");
359         normalize("http://localhost:8080/index.html/foo.html/../#ref",
360                   "http://localhost:8080/index.html/#ref");
361         normalize("http://localhost:8080/index.html/foo.html/../?name=value",
362                   "http://localhost:8080/index.html/?name=value");
363         normalize("http://localhost:8080/index.html/foo.html/../?name=value#ref",
364                   "http://localhost:8080/index.html/?name=value#ref");
365
366     }
367
368
369     /**
370      * Positive tests for relative URL strings in various patterns.
371      */

372     public void testPositiveRelative() {
373
374         // Test cases based on RFC 2396, Appendix C
375
positive("http://a/b/c/d;p?q", "http:h");
376         positive("http://a/b/c/d;p?q", "g");
377         positive("http://a/b/c/d;p?q", "./g");
378         positive("http://a/b/c/d;p?q", "g/");
379         positive("http://a/b/c/d;p?q", "/g");
380         // positive("http://a/b/c/d;p?q", "//g");
381
positive("http://a/b/c/d;p?q", "?y");
382         positive("http://a/b/c/d;p?q", "g?y");
383         // positive("http://a/b/c/d;p?q", "#s");
384
positive("http://a/b/c/d;p?q", "g#s");
385         positive("http://a/b/c/d;p?q", "g?y#s");
386         positive("http://a/b/c/d;p?q", ";x");
387         positive("http://a/b/c/d;p?q", "g;x");
388         positive("http://a/b/c/d;p?q", "g;x?y#s");
389         positive("http://a/b/c/d;p?q", ".");
390         positive("http://a/b/c/d;p?q", "./");
391         positive("http://a/b/c/d;p?q", "..");
392         positive("http://a/b/c/d;p?q", "../");
393         positive("http://a/b/c/d;p?q", "../g");
394         positive("http://a/b/c/d;p?q", "../..");
395         positive("http://a/b/c/d;p?q", "../../");
396         positive("http://a/b/c/d;p?q", "../../g");
397         // Commented because java.net.URL doesn't normalize out the "/./"????
398
// positive("http://a/b/c/d;p?q", "/./g");
399
positive("http://a/b/c/d;p?q", "g.");
400         positive("http://a/b/c/d;p?q", ".g");
401         positive("http://a/b/c/d;p?q", "g..");
402         positive("http://a/b/c/d;p?q", "..g");
403         positive("http://a/b/c/d;p?q", "./../g");
404         positive("http://a/b/c/d;p?q", "./g/.");
405         positive("http://a/b/c/d;p?q", "g/./h");
406         positive("http://a/b/c/d;p?q", "g/../h");
407         positive("http://a/b/c/d;p?q", "g;x=1/./y");
408         positive("http://a/b/c/d;p?q", "g;x=1/../y");
409         positive("http://a/b/c/d;p?q", "g?y/./x");
410         positive("http://a/b/c/d;p?q", "g?y/../x");
411         positive("http://a/b/c/d;p?q", "g#s/./x");
412         positive("http://a/b/c/d;p?q", "g#s/../x");
413         positive("http://a/b", "c");
414         positive("http://a/b/", "c");
415
416     }
417
418
419     // -------------------------------------------------------- Private Methods
420

421
422     /**
423      * Check that both our URL class and <code>java.net.URL</code> throw
424      * <code>MalformedURLException</code> on an absolute URL specification.
425      *
426      * @param spec Absolute URL specification to be checked
427      */

428     private void negative(String JavaDoc spec) {
429
430         try {
431             java.net.URL JavaDoc url = new java.net.URL JavaDoc(spec);
432             fail(spec + " should have failed on java.net.URL " +
433                  "but returned " + url.toExternalForm());
434         } catch (MalformedURLException JavaDoc e) {
435             ; // Expected response
436
}
437
438         try {
439             URL url = new URL(spec);
440             fail(spec + " should have failed on tested URL " +
441                  "but returned " + url.toExternalForm());
442         } catch (MalformedURLException JavaDoc e) {
443             ; // Expected response
444
}
445
446     }
447
448
449     /**
450      * Check that both our URL class and <code>java.net.URL</code> throw
451      * <code>MalformedURLException</code> on an absolute URL specification
452      * plus the corresponding relative URL specification.
453      *
454      * @param abs Absolute URL specification to be checked
455      * @param rel Relative URL specification to be checked
456      */

457     private void negative(String JavaDoc abs, String JavaDoc rel) {
458
459         java.net.URL JavaDoc baseNet = null;
460         URL baseUrl = null;
461
462         try {
463             baseNet = new java.net.URL JavaDoc(abs);
464         } catch (MalformedURLException JavaDoc e) {
465             fail(abs + " net URL threw " + e);
466         }
467
468         try {
469             baseUrl = new URL(abs);
470         } catch (MalformedURLException JavaDoc e) {
471             fail(abs + " url URL threw " + e);
472         }
473
474         try {
475             java.net.URL JavaDoc url = new java.net.URL JavaDoc(baseNet, rel);
476             fail(rel + " should have failed on java.net.URL " +
477                  "but returned " + url.toExternalForm());
478         } catch (MalformedURLException JavaDoc e) {
479             ; // Expected response
480
}
481
482         try {
483             URL url = new URL(baseUrl, rel);
484             fail(rel + " should have failed on tested URL " +
485                  "but returned " + url.toExternalForm());
486         } catch (MalformedURLException JavaDoc e) {
487             ; // Expected response
488
}
489
490     }
491
492
493     /**
494      * Attempts to normalize the specified URL should throw
495      * MalformedURLException.
496      *
497      * @param spec Unnormalized version of the URL specification
498      */

499     private void normalize(String JavaDoc spec) {
500
501         URL url = null;
502         try {
503             url = new URL(spec);
504         } catch (Throwable JavaDoc t) {
505             fail(spec + " should not have thrown " + t);
506         }
507
508         try {
509             url.normalize();
510             fail(spec + ".normalize() should have thrown MUE");
511         } catch (MalformedURLException JavaDoc e) {
512             ; // Expected result
513
}
514
515     }
516
517
518     /**
519      * It should be possible to normalize the specified URL into the
520      * specified normalized form.
521      *
522      * @param spec Unnormalized version of the URL specification
523      * @param norm Normalized version of the URL specification
524      */

525     private void normalize(String JavaDoc spec, String JavaDoc norm) {
526
527         try {
528             URL url = new URL(spec);
529             url.normalize();
530             assertEquals(spec + ".normalize()", norm, url.toExternalForm());
531         } catch (Throwable JavaDoc t) {
532             fail(spec + ".normalize() threw " + t);
533         }
534
535     }
536
537
538     /**
539      * Check the details of our URL class against <code>java.net.URL</code>
540      * for an absolute URL specification.
541      *
542      * @param spec Absolute URL specification to be checked
543      */

544     private void positive(String JavaDoc spec) {
545
546         // Compare results with what java.net.URL returns
547
try {
548             URL url = new URL(spec);
549             java.net.URL JavaDoc net = new java.net.URL JavaDoc(spec);
550             assertEquals(spec + " toExternalForm()",
551                          net.toExternalForm(),
552                          url.toExternalForm());
553             assertEquals(spec + ".getAuthority()",
554                          net.getAuthority(),
555                          url.getAuthority());
556             assertEquals(spec + ".getFile()",
557                          net.getFile(),
558                          url.getFile());
559             assertEquals(spec + ".getHost()",
560                          net.getHost(),
561                          url.getHost());
562             assertEquals(spec + ".getPath()",
563                          net.getPath(),
564                          url.getPath());
565             assertEquals(spec + ".getPort()",
566                          net.getPort(),
567                          url.getPort());
568             assertEquals(spec + ".getProtocol()",
569                          net.getProtocol(),
570                          url.getProtocol());
571             assertEquals(spec + ".getQuery()",
572                          net.getQuery(),
573                          url.getQuery());
574             assertEquals(spec + ".getRef()",
575                          net.getRef(),
576                          url.getRef());
577             assertEquals(spec + ".getUserInfo()",
578                          net.getUserInfo(),
579                          url.getUserInfo());
580         } catch (Throwable JavaDoc t) {
581             fail(spec + " positive test threw " + t);
582         }
583
584     }
585
586
587     /**
588      * Check the details of our URL class against <code>java.net.URL</code>
589      * for a relative URL specification.
590      *
591      * @param abs Absolute URL specification for base reference
592      * @param rel Relative URL specification to resolve
593      */

594     private void positive(String JavaDoc abs, String JavaDoc rel) {
595
596         // Compare results with what java.net.URL returns
597
try {
598             URL urlBase = new URL(abs);
599             java.net.URL JavaDoc netBase = new java.net.URL JavaDoc(abs);
600             URL url = new URL(urlBase, rel);
601             java.net.URL JavaDoc net = new java.net.URL JavaDoc(netBase, rel);
602             assertEquals(rel + " toExternalForm()",
603                          net.toExternalForm(),
604                          url.toExternalForm());
605             assertEquals(rel + ".getAuthority()",
606                          net.getAuthority(),
607                          url.getAuthority());
608             assertEquals(rel + ".getFile()",
609                          net.getFile(),
610                          url.getFile());
611             assertEquals(rel + ".getHost()",
612                          net.getHost(),
613                          url.getHost());
614             assertEquals(rel + ".getPath()",
615                          net.getPath(),
616                          url.getPath());
617             assertEquals(rel + ".getPort()",
618                          net.getPort(),
619                          url.getPort());
620             assertEquals(rel + ".getProtocol()",
621                          net.getProtocol(),
622                          url.getProtocol());
623             assertEquals(rel + ".getQuery()",
624                          net.getQuery(),
625                          url.getQuery());
626             assertEquals(rel + ".getRef()",
627                          net.getRef(),
628                          url.getRef());
629             assertEquals(rel + ".getUserInfo()",
630                          net.getUserInfo(),
631                          url.getUserInfo());
632         } catch (Throwable JavaDoc t) {
633             fail(rel + " positive test threw " + t);
634         }
635
636     }
637
638
639 }
640
Popular Tags