001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
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: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.util;
016:
017: import junit.framework.TestSuite;
018:
019: /* ------------------------------------------------------------ */
020: /** Util meta Tests.
021: * @author Greg Wilkins (gregw)
022: */
023: public class URITest extends junit.framework.TestCase {
024: public URITest(String name) {
025: super (name);
026: }
027:
028: public static junit.framework.Test suite() {
029: TestSuite suite = new TestSuite(URITest.class);
030: return suite;
031: }
032:
033: /* ------------------------------------------------------------ */
034: /** main.
035: */
036: public static void main(String[] args) {
037: junit.textui.TestRunner.run(suite());
038: }
039:
040: /* ------------------------------------------------------------ */
041: public void testEncodePath() {
042: // test basic encode/decode
043: StringBuffer buf = new StringBuffer();
044:
045: buf.setLength(0);
046: URIUtil.encodePath(buf, "/foo%23+;,:=/b a r/?info ");
047: assertEquals("/foo%2523+%3B,:=/b%20a%20r/%3Finfo%20", buf
048: .toString());
049:
050: assertEquals("/foo%2523+%3B,:=/b%20a%20r/%3Finfo%20", URIUtil
051: .encodePath("/foo%23+;,:=/b a r/?info "));
052:
053: buf.setLength(0);
054: URIUtil.encodeString(buf, "foo%23;,:=b a r", ";,= ");
055: assertEquals("foo%2523%3b%2c:%3db%20a%20r", buf.toString());
056:
057: }
058:
059: /* ------------------------------------------------------------ */
060: public void testDecodePath() {
061: assertEquals("foo%23;,:=b a r", URIUtil
062: .decodePath("foo%2523%3b%2c:%3db%20a%20r"));
063: assertEquals("foo%23;,:=b a r", URIUtil.decodePath(
064: "xxxfoo%2523%3b%2c:%3db%20a%20rxxx".getBytes(), 3, 27));
065: assertEquals("fää%23;,:=b a r", URIUtil
066: .decodePath("fää%2523%3b%2c:%3db%20a%20r"));
067: assertEquals("f\u0629\u0629%23;,:=b a r", URIUtil
068: .decodePath("f%d8%a9%d8%a9%2523%3b%2c:%3db%20a%20r"));
069: }
070:
071: /* ------------------------------------------------------------ */
072: public void testAddPaths() {
073: assertEquals("null+null", URIUtil.addPaths(null, null), null);
074: assertEquals("null+", URIUtil.addPaths(null, ""), "");
075: assertEquals("null+bbb", URIUtil.addPaths(null, "bbb"), "bbb");
076: assertEquals("null+/", URIUtil.addPaths(null, "/"), "/");
077: assertEquals("null+/bbb", URIUtil.addPaths(null, "/bbb"),
078: "/bbb");
079:
080: assertEquals("+null", URIUtil.addPaths("", null), "");
081: assertEquals("+", URIUtil.addPaths("", ""), "");
082: assertEquals("+bbb", URIUtil.addPaths("", "bbb"), "bbb");
083: assertEquals("+/", URIUtil.addPaths("", "/"), "/");
084: assertEquals("+/bbb", URIUtil.addPaths("", "/bbb"), "/bbb");
085:
086: assertEquals("aaa+null", URIUtil.addPaths("aaa", null), "aaa");
087: assertEquals("aaa+", URIUtil.addPaths("aaa", ""), "aaa");
088: assertEquals("aaa+bbb", URIUtil.addPaths("aaa", "bbb"),
089: "aaa/bbb");
090: assertEquals("aaa+/", URIUtil.addPaths("aaa", "/"), "aaa/");
091: assertEquals("aaa+/bbb", URIUtil.addPaths("aaa", "/bbb"),
092: "aaa/bbb");
093:
094: assertEquals("/+null", URIUtil.addPaths("/", null), "/");
095: assertEquals("/+", URIUtil.addPaths("/", ""), "/");
096: assertEquals("/+bbb", URIUtil.addPaths("/", "bbb"), "/bbb");
097: assertEquals("/+/", URIUtil.addPaths("/", "/"), "/");
098: assertEquals("/+/bbb", URIUtil.addPaths("/", "/bbb"), "/bbb");
099:
100: assertEquals("aaa/+null", URIUtil.addPaths("aaa/", null),
101: "aaa/");
102: assertEquals("aaa/+", URIUtil.addPaths("aaa/", ""), "aaa/");
103: assertEquals("aaa/+bbb", URIUtil.addPaths("aaa/", "bbb"),
104: "aaa/bbb");
105: assertEquals("aaa/+/", URIUtil.addPaths("aaa/", "/"), "aaa/");
106: assertEquals("aaa/+/bbb", URIUtil.addPaths("aaa/", "/bbb"),
107: "aaa/bbb");
108:
109: assertEquals(";JS+null", URIUtil.addPaths(";JS", null), ";JS");
110: assertEquals(";JS+", URIUtil.addPaths(";JS", ""), ";JS");
111: assertEquals(";JS+bbb", URIUtil.addPaths(";JS", "bbb"),
112: "bbb;JS");
113: assertEquals(";JS+/", URIUtil.addPaths(";JS", "/"), "/;JS");
114: assertEquals(";JS+/bbb", URIUtil.addPaths(";JS", "/bbb"),
115: "/bbb;JS");
116:
117: assertEquals("aaa;JS+null", URIUtil.addPaths("aaa;JS", null),
118: "aaa;JS");
119: assertEquals("aaa;JS+", URIUtil.addPaths("aaa;JS", ""),
120: "aaa;JS");
121: assertEquals("aaa;JS+bbb", URIUtil.addPaths("aaa;JS", "bbb"),
122: "aaa/bbb;JS");
123: assertEquals("aaa;JS+/", URIUtil.addPaths("aaa;JS", "/"),
124: "aaa/;JS");
125: assertEquals("aaa;JS+/bbb", URIUtil.addPaths("aaa;JS", "/bbb"),
126: "aaa/bbb;JS");
127:
128: assertEquals("aaa;JS+null", URIUtil.addPaths("aaa/;JS", null),
129: "aaa/;JS");
130: assertEquals("aaa;JS+", URIUtil.addPaths("aaa/;JS", ""),
131: "aaa/;JS");
132: assertEquals("aaa;JS+bbb", URIUtil.addPaths("aaa/;JS", "bbb"),
133: "aaa/bbb;JS");
134: assertEquals("aaa;JS+/", URIUtil.addPaths("aaa/;JS", "/"),
135: "aaa/;JS");
136: assertEquals("aaa;JS+/bbb",
137: URIUtil.addPaths("aaa/;JS", "/bbb"), "aaa/bbb;JS");
138:
139: assertEquals("?A=1+null", URIUtil.addPaths("?A=1", null),
140: "?A=1");
141: assertEquals("?A=1+", URIUtil.addPaths("?A=1", ""), "?A=1");
142: assertEquals("?A=1+bbb", URIUtil.addPaths("?A=1", "bbb"),
143: "bbb?A=1");
144: assertEquals("?A=1+/", URIUtil.addPaths("?A=1", "/"), "/?A=1");
145: assertEquals("?A=1+/bbb", URIUtil.addPaths("?A=1", "/bbb"),
146: "/bbb?A=1");
147:
148: assertEquals("aaa?A=1+null", URIUtil.addPaths("aaa?A=1", null),
149: "aaa?A=1");
150: assertEquals("aaa?A=1+", URIUtil.addPaths("aaa?A=1", ""),
151: "aaa?A=1");
152: assertEquals("aaa?A=1+bbb", URIUtil.addPaths("aaa?A=1", "bbb"),
153: "aaa/bbb?A=1");
154: assertEquals("aaa?A=1+/", URIUtil.addPaths("aaa?A=1", "/"),
155: "aaa/?A=1");
156: assertEquals("aaa?A=1+/bbb", URIUtil
157: .addPaths("aaa?A=1", "/bbb"), "aaa/bbb?A=1");
158:
159: assertEquals("aaa?A=1+null",
160: URIUtil.addPaths("aaa/?A=1", null), "aaa/?A=1");
161: assertEquals("aaa?A=1+", URIUtil.addPaths("aaa/?A=1", ""),
162: "aaa/?A=1");
163: assertEquals("aaa?A=1+bbb",
164: URIUtil.addPaths("aaa/?A=1", "bbb"), "aaa/bbb?A=1");
165: assertEquals("aaa?A=1+/", URIUtil.addPaths("aaa/?A=1", "/"),
166: "aaa/?A=1");
167: assertEquals("aaa?A=1+/bbb", URIUtil.addPaths("aaa/?A=1",
168: "/bbb"), "aaa/bbb?A=1");
169:
170: assertEquals(";JS?A=1+null", URIUtil.addPaths(";JS?A=1", null),
171: ";JS?A=1");
172: assertEquals(";JS?A=1+", URIUtil.addPaths(";JS?A=1", ""),
173: ";JS?A=1");
174: assertEquals(";JS?A=1+bbb", URIUtil.addPaths(";JS?A=1", "bbb"),
175: "bbb;JS?A=1");
176: assertEquals(";JS?A=1+/", URIUtil.addPaths(";JS?A=1", "/"),
177: "/;JS?A=1");
178: assertEquals(";JS?A=1+/bbb", URIUtil
179: .addPaths(";JS?A=1", "/bbb"), "/bbb;JS?A=1");
180:
181: assertEquals("aaa;JS?A=1+null", URIUtil.addPaths("aaa;JS?A=1",
182: null), "aaa;JS?A=1");
183: assertEquals("aaa;JS?A=1+", URIUtil.addPaths("aaa;JS?A=1", ""),
184: "aaa;JS?A=1");
185: assertEquals("aaa;JS?A=1+bbb", URIUtil.addPaths("aaa;JS?A=1",
186: "bbb"), "aaa/bbb;JS?A=1");
187: assertEquals("aaa;JS?A=1+/", URIUtil
188: .addPaths("aaa;JS?A=1", "/"), "aaa/;JS?A=1");
189: assertEquals("aaa;JS?A=1+/bbb", URIUtil.addPaths("aaa;JS?A=1",
190: "/bbb"), "aaa/bbb;JS?A=1");
191:
192: assertEquals("aaa;JS?A=1+null", URIUtil.addPaths("aaa/;JS?A=1",
193: null), "aaa/;JS?A=1");
194: assertEquals("aaa;JS?A=1+",
195: URIUtil.addPaths("aaa/;JS?A=1", ""), "aaa/;JS?A=1");
196: assertEquals("aaa;JS?A=1+bbb", URIUtil.addPaths("aaa/;JS?A=1",
197: "bbb"), "aaa/bbb;JS?A=1");
198: assertEquals("aaa;JS?A=1+/", URIUtil.addPaths("aaa/;JS?A=1",
199: "/"), "aaa/;JS?A=1");
200: assertEquals("aaa;JS?A=1+/bbb", URIUtil.addPaths("aaa/;JS?A=1",
201: "/bbb"), "aaa/bbb;JS?A=1");
202:
203: }
204:
205: /* ------------------------------------------------------------ */
206: public void testParentPath() {
207: assertEquals("parent /aaa/bbb/", "/aaa/", URIUtil
208: .parentPath("/aaa/bbb/"));
209: assertEquals("parent /aaa/bbb", "/aaa/", URIUtil
210: .parentPath("/aaa/bbb"));
211: assertEquals("parent /aaa/", "/", URIUtil.parentPath("/aaa/"));
212: assertEquals("parent /aaa", "/", URIUtil.parentPath("/aaa"));
213: assertEquals("parent /", null, URIUtil.parentPath("/"));
214: assertEquals("parent null", null, URIUtil.parentPath(null));
215:
216: }
217:
218: /* ------------------------------------------------------------ */
219: public void testCanonicalPath() {
220: String[][] canonical = { { "/aaa/bbb/", "/aaa/bbb/" },
221: { "/aaa//bbb/", "/aaa//bbb/" },
222: { "/aaa///bbb/", "/aaa///bbb/" },
223: { "/aaa/./bbb/", "/aaa/bbb/" },
224: { "/aaa/../bbb/", "/bbb/" },
225: { "/aaa/./../bbb/", "/bbb/" },
226: { "/aaa/bbb/ccc/../../ddd/", "/aaa/ddd/" },
227: { "./bbb/", "bbb/" }, { "./aaa/../bbb/", "bbb/" },
228: { "./", "" }, { ".//", ".//" }, { ".///", ".///" },
229: { "/.", "/" }, { "//.", "//" }, { "///.", "///" },
230: { "/", "/" }, { "aaa/bbb", "aaa/bbb" },
231: { "aaa/", "aaa/" }, { "aaa", "aaa" },
232: { "/aaa/bbb", "/aaa/bbb" },
233: { "/aaa//bbb", "/aaa//bbb" },
234: { "/aaa/./bbb", "/aaa/bbb" },
235: { "/aaa/../bbb", "/bbb" }, { "/aaa/./../bbb", "/bbb" },
236: { "./bbb", "bbb" }, { "./aaa/../bbb", "bbb" },
237: { "aaa/bbb/..", "aaa/" }, { "aaa/bbb/../", "aaa/" },
238: { "./", "" }, { ".", "" }, { "", "" }, { "..", null },
239: { "./..", null }, { "aaa/../..", null },
240: { "/foo/bar/../../..", null }, { "/../foo", null },
241: { "/foo/.", "/foo/" }, { "a", "a" }, { "a/", "a/" },
242: { "a/.", "a/" }, { "a/..", "" }, { "a/../..", null },
243: { "/foo/../bar//", "/bar//" }, };
244:
245: for (int t = 0; t < canonical.length; t++)
246: assertEquals("canonical " + canonical[t][0],
247: canonical[t][1], URIUtil
248: .canonicalPath(canonical[t][0]));
249:
250: }
251:
252: }
|