KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > source > test > SourceUtilTestCase


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

17 package org.apache.excalibur.source.test;
18
19 import org.apache.excalibur.source.SourceUtil;
20
21 import junit.framework.TestCase;
22
23 /**
24  * Test case for SourceUtil.
25  *
26  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27  * @version $Id: SourceUtilTestCase.java,v 1.4 2004/02/28 11:47:22 cziegeler Exp $
28  */

29 public class SourceUtilTestCase extends TestCase
30 {
31     public SourceUtilTestCase()
32     {
33         this("SourceUtil");
34     }
35
36     public SourceUtilTestCase(String JavaDoc name)
37     {
38         super(name);
39     }
40
41     public void testNominalScheme() throws Exception JavaDoc
42     {
43         String JavaDoc uri = "http://foo";
44         assertEquals(4, SourceUtil.indexOfSchemeColon(uri));
45         assertEquals("http", SourceUtil.getScheme(uri));
46         assertEquals("//foo", SourceUtil.getSpecificPart(uri));
47     }
48
49     public void testDoubleColon() throws Exception JavaDoc
50     {
51         assertEquals(4, SourceUtil.indexOfSchemeColon("file:foo:bar"));
52     }
53
54     public void testSpecialScheme() throws Exception JavaDoc
55     {
56         String JavaDoc uri = "a-+.:foo"; // Strange, but valid !
57
assertEquals(4, SourceUtil.indexOfSchemeColon(uri));
58         assertEquals("a-+.", SourceUtil.getScheme(uri));
59         assertEquals("foo", SourceUtil.getSpecificPart(uri));
60     }
61
62     public void testSpecialPart() throws Exception JavaDoc
63     {
64         String JavaDoc uri = "bar:";
65         assertEquals(3, SourceUtil.indexOfSchemeColon(uri));
66         assertEquals("bar", SourceUtil.getScheme(uri));
67         assertEquals("", SourceUtil.getSpecificPart(uri));
68     }
69
70     public void testInvalidScheme() throws Exception JavaDoc
71     {
72         String JavaDoc uri = "2foo:bar";
73         assertEquals(-1, SourceUtil.indexOfSchemeColon(uri));
74         assertEquals(null, SourceUtil.getScheme(uri));
75         assertEquals(null, SourceUtil.getSpecificPart(uri));
76
77         // Invalid character before any of the allowed ones
78
assertEquals(-1, SourceUtil.indexOfSchemeColon("h ttp:foo"));
79         assertEquals(-1, SourceUtil.indexOfSchemeColon(" http:foo"));
80         assertEquals(-1, SourceUtil.indexOfSchemeColon("http :foo"));
81
82        // Invalid character between allowed ranges
83
assertEquals(-1, SourceUtil.indexOfSchemeColon("h_ttp:foo"));
84         assertEquals(-1, SourceUtil.indexOfSchemeColon("_http:foo"));
85         assertEquals(-1, SourceUtil.indexOfSchemeColon("http_:foo"));
86
87         // Invalid character after any of the allowed ones
88
assertEquals(-1, SourceUtil.indexOfSchemeColon("h~ttp:foo"));
89         assertEquals(-1, SourceUtil.indexOfSchemeColon("~http:foo"));
90         assertEquals(-1, SourceUtil.indexOfSchemeColon("http~:foo"));
91     }
92
93     public void testAbsolutize()
94     {
95         String JavaDoc base = "http://a/b/c/d;p?q";
96
97         //
98
// Test examples from RFC 2396
99
//
100

101         // normal cases
102
assertEquals("g:h", SourceUtil.absolutize(base, "g:h"));
103         assertEquals("http://a/b/c/g", SourceUtil.absolutize(base, "g"));
104         assertEquals("http://a/b/c/g", SourceUtil.absolutize(base, "./g"));
105         assertEquals("http://a/b/c/g/", SourceUtil.absolutize(base, "g/"));
106         assertEquals("http://a/g", SourceUtil.absolutize(base, "/g"));
107         assertEquals("http://g", SourceUtil.absolutize(base, "//g"));
108         assertEquals("http://a/b/c/?y", SourceUtil.absolutize(base, "?y"));
109         assertEquals("http://a/b/c/g?y", SourceUtil.absolutize(base, "g?y"));
110         assertEquals("http://a/b/c/d;p?q#s", SourceUtil.absolutize(base, "#s"));
111         assertEquals("http://a/b/c/g#s", SourceUtil.absolutize(base, "g#s"));
112         assertEquals("http://a/b/c/g?y#s", SourceUtil.absolutize(base, "g?y#s"));
113         assertEquals("http://a/b/c/;x", SourceUtil.absolutize(base, ";x"));
114         assertEquals("http://a/b/c/g;x", SourceUtil.absolutize(base, "g;x"));
115         assertEquals("http://a/b/c/g;x?y#s", SourceUtil.absolutize(base, "g;x?y#s"));
116         assertEquals("http://a/b/c/", SourceUtil.absolutize(base, "."));
117         assertEquals("http://a/b/c/", SourceUtil.absolutize(base, "./"));
118         assertEquals("http://a/b/", SourceUtil.absolutize(base, ".."));
119         assertEquals("http://a/b/", SourceUtil.absolutize(base, "../"));
120         assertEquals("http://a/b/g", SourceUtil.absolutize(base, "../g"));
121         assertEquals("http://a/", SourceUtil.absolutize(base, "../.."));
122         assertEquals("http://a/", SourceUtil.absolutize(base, "../../"));
123         assertEquals("http://a/g", SourceUtil.absolutize(base, "../../g"));
124
125         // abnormal cases
126
assertEquals("http://a/../g", SourceUtil.absolutize(base, "../../../g"));
127         assertEquals("http://a/../../g", SourceUtil.absolutize(base, "../../../../g"));
128
129         assertEquals("http://a/./g", SourceUtil.absolutize(base, "/./g"));
130         assertEquals("http://a/../g", SourceUtil.absolutize(base, "/../g"));
131         assertEquals("http://a/b/c/g.", SourceUtil.absolutize(base, "g."));
132         assertEquals("http://a/b/c/.g", SourceUtil.absolutize(base, ".g"));
133         assertEquals("http://a/b/c/g..", SourceUtil.absolutize(base, "g.."));
134         assertEquals("http://a/b/c/..g", SourceUtil.absolutize(base, "..g"));
135
136         assertEquals("http://a/b/g", SourceUtil.absolutize(base, "./../g"));
137         assertEquals("http://a/b/c/g/", SourceUtil.absolutize(base, "./g/."));
138         assertEquals("http://a/b/c/g/h", SourceUtil.absolutize(base, "g/./h"));
139         assertEquals("http://a/b/c/h", SourceUtil.absolutize(base, "g/../h"));
140         assertEquals("http://a/b/c/g;x=1/y", SourceUtil.absolutize(base, "g;x=1/./y"));
141         assertEquals("http://a/b/c/y", SourceUtil.absolutize(base, "g;x=1/../y"));
142
143         assertEquals("http://a/b/c/g?y/./x", SourceUtil.absolutize(base, "g?y/./x"));
144         assertEquals("http://a/b/c/g?y/../x", SourceUtil.absolutize(base, "g?y/../x"));
145         assertEquals("http://a/b/c/g#s/./x", SourceUtil.absolutize(base, "g#s/./x"));
146         assertEquals("http://a/b/c/g#s/../x", SourceUtil.absolutize(base, "g#s/../x"));
147
148         //
149
// other tests
150
//
151

152         // if there's a scheme, url is absolute
153
assertEquals("http://a", SourceUtil.absolutize("", "http://a"));
154         assertEquals("cocoon:/a", SourceUtil.absolutize("", "cocoon:/a", true));
155
156         // handle null base
157
assertEquals("a", SourceUtil.absolutize(null, "a"));
158
159         // handle network reference
160
assertEquals("http://a/b", SourceUtil.absolutize("http://myhost", "//a/b"));
161
162         // handle empty authority
163
assertEquals("http:///a/b", SourceUtil.absolutize("http:///a/", "b"));
164
165         // cocoon and context protocols
166
assertEquals("cocoon://a/b/c", SourceUtil.absolutize("cocoon://a/b/", "c", true));
167         assertEquals("cocoon:/a/b/c", SourceUtil.absolutize("cocoon:/a/b/", "c", true));
168         assertEquals("cocoon://c", SourceUtil.absolutize("cocoon://a", "c", true));
169         assertEquals("cocoon://c", SourceUtil.absolutize("cocoon://a/b/", "../../c", true));
170
171         // Test relative File URI
172
assertEquals("file://C:/projects/avalon-excalibur/build/docs/framework/api/index.html%3Ffoo=bar",
173                 SourceUtil.absolutize("file://C:/projects/avalon-excalibur/build/docs/framework/", "api/index.html%3Ffoo=bar"));
174         assertEquals( "file://C:/foo/api/", SourceUtil.absolutize( "file://C:/foo/", "api/" ) );
175     }
176 }
177
Popular Tags