001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.catalina.util;
018:
019: import java.net.MalformedURLException;
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: /**
025: * Unit tests for the <code>org.apache.catalina.util.URL</code> class.
026: *
027: * @author Craig R. McClanahan
028: * @version $Revision: 1.2 $ $Date: 2004/02/27 14:58:55 $
029: */
030:
031: public class URLTestCase extends TestCase {
032:
033: // ----------------------------------------------------- Instance Variables
034:
035: // ----------------------------------------------------------- Constructors
036:
037: /**
038: * Construct a new instance of this test case.
039: *
040: * @param name Name of the test case
041: */
042: public URLTestCase(String name) {
043:
044: super (name);
045:
046: }
047:
048: // --------------------------------------------------- Overall Test Methods
049:
050: /**
051: * Set up instance variables required by this test case.
052: */
053: public void setUp() {
054:
055: ; // No action required
056:
057: }
058:
059: /**
060: * Return the tests included in this test suite.
061: */
062: public static Test suite() {
063:
064: return (new TestSuite(URLTestCase.class));
065:
066: }
067:
068: /**
069: * Tear down instance variables required by this test case.
070: */
071: public void tearDown() {
072:
073: ; // No action required
074:
075: }
076:
077: // ------------------------------------------------ Individual Test Methods
078:
079: /**
080: * Negative tests for absolute URL strings in various patterns. Each of
081: * these should throw <code>MalformedURLException</code>.
082: */
083: public void testNegativeAbsolute() {
084:
085: negative("index.html");
086: negative("index.html#ref");
087: negative("index.html?name=value");
088: negative("index.html?name=value#ref");
089:
090: negative("/index.html");
091: negative("/index.html#ref");
092: negative("/index.html?name=value");
093: negative("/index.html?name=value#ref");
094:
095: }
096:
097: /**
098: * Negative tests for <code>normalize()</code>. Attempts to normalize
099: * these legal URLs should throw <code>MalformedURLException</code>.
100: */
101: public void testNegativeNormalize() {
102:
103: normalize("http://localhost/..");
104: normalize("http://localhost/..#ref");
105: normalize("http://localhost/..?name=value");
106: normalize("http://localhost/..?name=value#ref");
107:
108: normalize("http://localhost:8080/..");
109: normalize("http://localhost:8080/..#ref");
110: normalize("http://localhost:8080/..?name=value");
111: normalize("http://localhost:8080/..?name=value#ref");
112:
113: normalize("http://localhost/../");
114: normalize("http://localhost/../#ref");
115: normalize("http://localhost/../?name=value");
116: normalize("http://localhost/../?name=value#ref");
117:
118: normalize("http://localhost:8080/../");
119: normalize("http://localhost:8080/../#ref");
120: normalize("http://localhost:8080/../?name=value");
121: normalize("http://localhost:8080/../?name=value#ref");
122:
123: normalize("http://localhost/index.html/../../foo.html");
124: normalize("http://localhost/index.html/../../foo.html#ref");
125: normalize("http://localhost/index.html/../../foo.html?name=value");
126: normalize("http://localhost/index.html/../../foo.html?name=value#ref");
127:
128: normalize("http://localhost:8080/index.html/../../foo.html");
129: normalize("http://localhost:8080/index.html/../../foo.html#ref");
130: normalize("http://localhost:8080/index.html/../../foo.html?name=value");
131: normalize("http://localhost:8080/index.html/../../foo.html?name=value#ref");
132:
133: }
134:
135: /**
136: * Negative tests for relative URL strings in various patterns. Each of
137: * these should throw <code>MalformedURLException</code>.
138: */
139: public void testNegativeRelative() {
140:
141: // Commented out because java.net.URL ignores extraneous "../"
142: // negative("http://a/b/c/d;p?q", "../../../g");
143: // negative("http://a/b/c/d;p?q", "/../g");
144:
145: }
146:
147: /**
148: * Positive tests for absolute URL strings in various patterns.
149: */
150: public void testPositiveAbsolute() {
151:
152: positive("http://a/b/c/d;p?q");
153:
154: positive("http://localhost/index.html");
155: positive("http://localhost/index.html#ref");
156: positive("http://localhost/index.html?name=value");
157: positive("http://localhost/index.html?name=value#ref");
158:
159: positive("http://localhost:8080/index.html");
160: positive("http://localhost:8080/index.html#ref");
161: positive("http://localhost:8080/index.html?name=value");
162: positive("http://localhost:8080/index.html?name=value#ref");
163:
164: positive("http://localhost/index.html/.");
165: positive("http://localhost/index.html/.#ref");
166: positive("http://localhost/index.html/.?name=value");
167: positive("http://localhost/index.html/.?name=value#ref");
168:
169: positive("http://localhost:8080/index.html/.");
170: positive("http://localhost:8080/index.html/.#ref");
171: positive("http://localhost:8080/index.html/.?name=value");
172: positive("http://localhost:8080/index.html/.?name=value#ref");
173:
174: positive("http://localhost/index.html/foo/..");
175: positive("http://localhost/index.html/foo/..#ref");
176: positive("http://localhost/index.html/foo/..?name=value");
177: positive("http://localhost/index.html/foo/..?name=value#ref");
178:
179: positive("http://localhost:8080/index.html/foo/..");
180: positive("http://localhost:8080/index.html/foo/..#ref");
181: positive("http://localhost:8080/index.html/foo/..?name=value");
182: positive("http://localhost:8080/index.html/foo/..?name=value#ref");
183:
184: positive("http://localhost/index.html/../foo.html");
185: positive("http://localhost/index.html/../foo.html#ref");
186: positive("http://localhost/index.html/../foo.html?name=value");
187: positive("http://localhost/index.html/../foo.html?name=value#ref");
188:
189: positive("http://localhost:8080/index.html/../foo.html");
190: positive("http://localhost:8080/index.html/../foo.html#ref");
191: positive("http://localhost:8080/index.html/../foo.html?name=value");
192: positive("http://localhost:8080/index.html/../foo.html?name=value#ref");
193:
194: positive("http://localhost");
195: positive("http://localhost#ref");
196: positive("http://localhost?name=value");
197: positive("http://localhost?name=value#ref");
198:
199: positive("http://localhost:8080");
200: positive("http://localhost:8080#ref");
201: positive("http://localhost:8080?name=value");
202: positive("http://localhost:8080?name=value#ref");
203:
204: positive("http://localhost/");
205: positive("http://localhost/#ref");
206: positive("http://localhost/?name=value");
207: positive("http://localhost/?name=value#ref");
208:
209: positive("http://localhost:8080/");
210: positive("http://localhost:8080/#ref");
211: positive("http://localhost:8080/?name=value");
212: positive("http://localhost:8080/?name=value#ref");
213:
214: }
215:
216: /**
217: * Positive tests for normalizing absolute URL strings in various patterns.
218: */
219: public void testPositiveNormalize() {
220:
221: normalize("http://a/b/c/d;p?q", "http://a/b/c/d;p?q");
222:
223: normalize("http://localhost/index.html",
224: "http://localhost/index.html");
225: normalize("http://localhost/index.html#ref",
226: "http://localhost/index.html#ref");
227: normalize("http://localhost/index.html?name=value",
228: "http://localhost/index.html?name=value");
229: normalize("http://localhost/index.html?name=value#ref",
230: "http://localhost/index.html?name=value#ref");
231:
232: normalize("http://localhost:8080/index.html",
233: "http://localhost:8080/index.html");
234: normalize("http://localhost:8080/index.html#ref",
235: "http://localhost:8080/index.html#ref");
236: normalize("http://localhost:8080/index.html?name=value",
237: "http://localhost:8080/index.html?name=value");
238: normalize("http://localhost:8080/index.html?name=value#ref",
239: "http://localhost:8080/index.html?name=value#ref");
240:
241: normalize("http://localhost/./index.html",
242: "http://localhost/index.html");
243: normalize("http://localhost/./index.html#ref",
244: "http://localhost/index.html#ref");
245: normalize("http://localhost/./index.html?name=value",
246: "http://localhost/index.html?name=value");
247: normalize("http://localhost/./index.html?name=value#ref",
248: "http://localhost/index.html?name=value#ref");
249:
250: normalize("http://localhost:8080/./index.html",
251: "http://localhost:8080/index.html");
252: normalize("http://localhost:8080/./index.html#ref",
253: "http://localhost:8080/index.html#ref");
254: normalize("http://localhost:8080/./index.html?name=value",
255: "http://localhost:8080/index.html?name=value");
256: normalize("http://localhost:8080/./index.html?name=value#ref",
257: "http://localhost:8080/index.html?name=value#ref");
258:
259: normalize("http://localhost/index.html/.",
260: "http://localhost/index.html/");
261: normalize("http://localhost/index.html/.#ref",
262: "http://localhost/index.html/#ref");
263: normalize("http://localhost/index.html/.?name=value",
264: "http://localhost/index.html/?name=value");
265: normalize("http://localhost/index.html/.?name=value#ref",
266: "http://localhost/index.html/?name=value#ref");
267:
268: normalize("http://localhost:8080/index.html/.",
269: "http://localhost:8080/index.html/");
270: normalize("http://localhost:8080/index.html/.#ref",
271: "http://localhost:8080/index.html/#ref");
272: normalize("http://localhost:8080/index.html/.?name=value",
273: "http://localhost:8080/index.html/?name=value");
274: normalize("http://localhost:8080/index.html/.?name=value#ref",
275: "http://localhost:8080/index.html/?name=value#ref");
276:
277: normalize("http://localhost/index.html/./",
278: "http://localhost/index.html/");
279: normalize("http://localhost/index.html/./#ref",
280: "http://localhost/index.html/#ref");
281: normalize("http://localhost/index.html/./?name=value",
282: "http://localhost/index.html/?name=value");
283: normalize("http://localhost/index.html/./?name=value#ref",
284: "http://localhost/index.html/?name=value#ref");
285:
286: normalize("http://localhost:8080/index.html/./",
287: "http://localhost:8080/index.html/");
288: normalize("http://localhost:8080/index.html/./#ref",
289: "http://localhost:8080/index.html/#ref");
290: normalize("http://localhost:8080/index.html/./?name=value",
291: "http://localhost:8080/index.html/?name=value");
292: normalize("http://localhost:8080/index.html/./?name=value#ref",
293: "http://localhost:8080/index.html/?name=value#ref");
294:
295: normalize("http://localhost/foo.html/../index.html",
296: "http://localhost/index.html");
297: normalize("http://localhost/foo.html/../index.html#ref",
298: "http://localhost/index.html#ref");
299: normalize("http://localhost/foo.html/../index.html?name=value",
300: "http://localhost/index.html?name=value");
301: normalize(
302: "http://localhost/foo.html/../index.html?name=value#ref",
303: "http://localhost/index.html?name=value#ref");
304:
305: normalize("http://localhost:8080/foo.html/../index.html",
306: "http://localhost:8080/index.html");
307: normalize("http://localhost:8080/foo.html/../index.html#ref",
308: "http://localhost:8080/index.html#ref");
309: normalize(
310: "http://localhost:8080/foo.html/../index.html?name=value",
311: "http://localhost:8080/index.html?name=value");
312: normalize(
313: "http://localhost:8080/foo.html/../index.html?name=value#ref",
314: "http://localhost:8080/index.html?name=value#ref");
315:
316: normalize("http://localhost/index.html/foo.html/..",
317: "http://localhost/index.html/");
318: normalize("http://localhost/index.html/foo.html/..#ref",
319: "http://localhost/index.html/#ref");
320: normalize("http://localhost/index.html/foo.html/..?name=value",
321: "http://localhost/index.html/?name=value");
322: normalize(
323: "http://localhost/index.html/foo.html/..?name=value#ref",
324: "http://localhost/index.html/?name=value#ref");
325:
326: normalize("http://localhost:8080/index.html/foo.html/..",
327: "http://localhost:8080/index.html/");
328: normalize("http://localhost:8080/index.html/foo.html/..#ref",
329: "http://localhost:8080/index.html/#ref");
330: normalize(
331: "http://localhost:8080/index.html/foo.html/..?name=value",
332: "http://localhost:8080/index.html/?name=value");
333: normalize(
334: "http://localhost:8080/index.html/foo.html/..?name=value#ref",
335: "http://localhost:8080/index.html/?name=value#ref");
336:
337: normalize("http://localhost/index.html/foo.html/../",
338: "http://localhost/index.html/");
339: normalize("http://localhost/index.html/foo.html/../#ref",
340: "http://localhost/index.html/#ref");
341: normalize(
342: "http://localhost/index.html/foo.html/../?name=value",
343: "http://localhost/index.html/?name=value");
344: normalize(
345: "http://localhost/index.html/foo.html/../?name=value#ref",
346: "http://localhost/index.html/?name=value#ref");
347:
348: normalize("http://localhost:8080/index.html/foo.html/../",
349: "http://localhost:8080/index.html/");
350: normalize("http://localhost:8080/index.html/foo.html/../#ref",
351: "http://localhost:8080/index.html/#ref");
352: normalize(
353: "http://localhost:8080/index.html/foo.html/../?name=value",
354: "http://localhost:8080/index.html/?name=value");
355: normalize(
356: "http://localhost:8080/index.html/foo.html/../?name=value#ref",
357: "http://localhost:8080/index.html/?name=value#ref");
358:
359: }
360:
361: /**
362: * Positive tests for relative URL strings in various patterns.
363: */
364: public void testPositiveRelative() {
365:
366: // Test cases based on RFC 2396, Appendix C
367: positive("http://a/b/c/d;p?q", "http:h");
368: positive("http://a/b/c/d;p?q", "g");
369: positive("http://a/b/c/d;p?q", "./g");
370: positive("http://a/b/c/d;p?q", "g/");
371: positive("http://a/b/c/d;p?q", "/g");
372: // positive("http://a/b/c/d;p?q", "//g");
373: positive("http://a/b/c/d;p?q", "?y");
374: positive("http://a/b/c/d;p?q", "g?y");
375: // positive("http://a/b/c/d;p?q", "#s");
376: positive("http://a/b/c/d;p?q", "g#s");
377: positive("http://a/b/c/d;p?q", "g?y#s");
378: positive("http://a/b/c/d;p?q", ";x");
379: positive("http://a/b/c/d;p?q", "g;x");
380: positive("http://a/b/c/d;p?q", "g;x?y#s");
381: positive("http://a/b/c/d;p?q", ".");
382: positive("http://a/b/c/d;p?q", "./");
383: positive("http://a/b/c/d;p?q", "..");
384: positive("http://a/b/c/d;p?q", "../");
385: positive("http://a/b/c/d;p?q", "../g");
386: positive("http://a/b/c/d;p?q", "../..");
387: positive("http://a/b/c/d;p?q", "../../");
388: positive("http://a/b/c/d;p?q", "../../g");
389: // Commented because java.net.URL doesn't normalize out the "/./"????
390: // positive("http://a/b/c/d;p?q", "/./g");
391: positive("http://a/b/c/d;p?q", "g.");
392: positive("http://a/b/c/d;p?q", ".g");
393: positive("http://a/b/c/d;p?q", "g..");
394: positive("http://a/b/c/d;p?q", "..g");
395: positive("http://a/b/c/d;p?q", "./../g");
396: positive("http://a/b/c/d;p?q", "./g/.");
397: positive("http://a/b/c/d;p?q", "g/./h");
398: positive("http://a/b/c/d;p?q", "g/../h");
399: positive("http://a/b/c/d;p?q", "g;x=1/./y");
400: positive("http://a/b/c/d;p?q", "g;x=1/../y");
401: positive("http://a/b/c/d;p?q", "g?y/./x");
402: positive("http://a/b/c/d;p?q", "g?y/../x");
403: positive("http://a/b/c/d;p?q", "g#s/./x");
404: positive("http://a/b/c/d;p?q", "g#s/../x");
405: positive("http://a/b", "c");
406: positive("http://a/b/", "c");
407:
408: }
409:
410: // -------------------------------------------------------- Private Methods
411:
412: /**
413: * Check that both our URL class and <code>java.net.URL</code> throw
414: * <code>MalformedURLException</code> on an absolute URL specification.
415: *
416: * @param spec Absolute URL specification to be checked
417: */
418: private void negative(String spec) {
419:
420: try {
421: java.net.URL url = new java.net.URL(spec);
422: fail(spec + " should have failed on java.net.URL "
423: + "but returned " + url.toExternalForm());
424: } catch (MalformedURLException e) {
425: ; // Expected response
426: }
427:
428: try {
429: URL url = new URL(spec);
430: fail(spec + " should have failed on tested URL "
431: + "but returned " + url.toExternalForm());
432: } catch (MalformedURLException e) {
433: ; // Expected response
434: }
435:
436: }
437:
438: /**
439: * Check that both our URL class and <code>java.net.URL</code> throw
440: * <code>MalformedURLException</code> on an absolute URL specification
441: * plus the corresponding relative URL specification.
442: *
443: * @param abs Absolute URL specification to be checked
444: * @param rel Relative URL specification to be checked
445: */
446: private void negative(String abs, String rel) {
447:
448: java.net.URL baseNet = null;
449: URL baseUrl = null;
450:
451: try {
452: baseNet = new java.net.URL(abs);
453: } catch (MalformedURLException e) {
454: fail(abs + " net URL threw " + e);
455: }
456:
457: try {
458: baseUrl = new URL(abs);
459: } catch (MalformedURLException e) {
460: fail(abs + " url URL threw " + e);
461: }
462:
463: try {
464: java.net.URL url = new java.net.URL(baseNet, rel);
465: fail(rel + " should have failed on java.net.URL "
466: + "but returned " + url.toExternalForm());
467: } catch (MalformedURLException e) {
468: ; // Expected response
469: }
470:
471: try {
472: URL url = new URL(baseUrl, rel);
473: fail(rel + " should have failed on tested URL "
474: + "but returned " + url.toExternalForm());
475: } catch (MalformedURLException e) {
476: ; // Expected response
477: }
478:
479: }
480:
481: /**
482: * Attempts to normalize the specified URL should throw
483: * MalformedURLException.
484: *
485: * @param spec Unnormalized version of the URL specification
486: */
487: private void normalize(String spec) {
488:
489: URL url = null;
490: try {
491: url = new URL(spec);
492: } catch (Throwable t) {
493: fail(spec + " should not have thrown " + t);
494: }
495:
496: try {
497: url.normalize();
498: fail(spec + ".normalize() should have thrown MUE");
499: } catch (MalformedURLException e) {
500: ; // Expected result
501: }
502:
503: }
504:
505: /**
506: * It should be possible to normalize the specified URL into the
507: * specified normalized form.
508: *
509: * @param spec Unnormalized version of the URL specification
510: * @param norm Normalized version of the URL specification
511: */
512: private void normalize(String spec, String norm) {
513:
514: try {
515: URL url = new URL(spec);
516: url.normalize();
517: assertEquals(spec + ".normalize()", norm, url
518: .toExternalForm());
519: } catch (Throwable t) {
520: fail(spec + ".normalize() threw " + t);
521: }
522:
523: }
524:
525: /**
526: * Check the details of our URL class against <code>java.net.URL</code>
527: * for an absolute URL specification.
528: *
529: * @param spec Absolute URL specification to be checked
530: */
531: private void positive(String spec) {
532:
533: // Compare results with what java.net.URL returns
534: try {
535: URL url = new URL(spec);
536: java.net.URL net = new java.net.URL(spec);
537: assertEquals(spec + " toExternalForm()", net
538: .toExternalForm(), url.toExternalForm());
539: assertEquals(spec + ".getAuthority()", net.getAuthority(),
540: url.getAuthority());
541: assertEquals(spec + ".getFile()", net.getFile(), url
542: .getFile());
543: assertEquals(spec + ".getHost()", net.getHost(), url
544: .getHost());
545: assertEquals(spec + ".getPath()", net.getPath(), url
546: .getPath());
547: assertEquals(spec + ".getPort()", net.getPort(), url
548: .getPort());
549: assertEquals(spec + ".getProtocol()", net.getProtocol(),
550: url.getProtocol());
551: assertEquals(spec + ".getQuery()", net.getQuery(), url
552: .getQuery());
553: assertEquals(spec + ".getRef()", net.getRef(), url.getRef());
554: assertEquals(spec + ".getUserInfo()", net.getUserInfo(),
555: url.getUserInfo());
556: } catch (Throwable t) {
557: fail(spec + " positive test threw " + t);
558: }
559:
560: }
561:
562: /**
563: * Check the details of our URL class against <code>java.net.URL</code>
564: * for a relative URL specification.
565: *
566: * @param abs Absolute URL specification for base reference
567: * @param rel Relative URL specification to resolve
568: */
569: private void positive(String abs, String rel) {
570:
571: // Compare results with what java.net.URL returns
572: try {
573: URL urlBase = new URL(abs);
574: java.net.URL netBase = new java.net.URL(abs);
575: URL url = new URL(urlBase, rel);
576: java.net.URL net = new java.net.URL(netBase, rel);
577: assertEquals(rel + " toExternalForm()", net
578: .toExternalForm(), url.toExternalForm());
579: assertEquals(rel + ".getAuthority()", net.getAuthority(),
580: url.getAuthority());
581: assertEquals(rel + ".getFile()", net.getFile(), url
582: .getFile());
583: assertEquals(rel + ".getHost()", net.getHost(), url
584: .getHost());
585: assertEquals(rel + ".getPath()", net.getPath(), url
586: .getPath());
587: assertEquals(rel + ".getPort()", net.getPort(), url
588: .getPort());
589: assertEquals(rel + ".getProtocol()", net.getProtocol(), url
590: .getProtocol());
591: assertEquals(rel + ".getQuery()", net.getQuery(), url
592: .getQuery());
593: assertEquals(rel + ".getRef()", net.getRef(), url.getRef());
594: assertEquals(rel + ".getUserInfo()", net.getUserInfo(), url
595: .getUserInfo());
596: } catch (Throwable t) {
597: fail(rel + " positive test threw " + t);
598: }
599:
600: }
601:
602: }
|