001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.util;
018:
019: import junit.framework.TestCase;
020:
021: public class TestPathUtil extends TestCase {
022: public void testPath() {
023: Path path = new Path(
024: "/root/sub1/sub2/file.html?foo=bar&name=bob");
025:
026: Path path2 = new Path(
027: "/root/sub1/sub2/file.html?foo=bar&name=bob");
028:
029: assertEquals(path, path2);
030:
031: assertEquals(".html", path.getFileExtension());
032: assertEquals("foo=bar&name=bob", path.getQueryString());
033: assertEquals("file.html", path.getFileName());
034: assertEquals("file", path.getBaseName());
035:
036: assertEquals(4, path.length());
037:
038: assertEquals("root", path.getSegment(0));
039: assertEquals("sub1", path.getSegment(1));
040: assertEquals("sub2", path.getSegment(2));
041: assertEquals("file.html", path.getSegment(3));
042: // assertEquals("/root/sub1/sub2/file.html", path.pathOnly());
043:
044: assertEquals("/sub1/sub2/file.html", path.getSubPath(1)
045: .toString());
046:
047: path = new Path("file.html");
048: assertEquals(".html", path.getFileExtension());
049: assertEquals("file.html", path.getFileName());
050: assertEquals("file", path.getBaseName());
051:
052: assertNull(path.getQueryString());
053:
054: assertEquals(1, path.length());
055: assertEquals("file.html", path.getSegment(0));
056:
057: path = new Path("file");
058:
059: assertNull(path.getBaseName());
060:
061: Path pathNoFile = new Path("/root/sub1/sub2?abc");
062: assertEquals("root", pathNoFile.getSegment(0));
063: assertEquals("sub1", pathNoFile.getSegment(1));
064: assertEquals("sub2", pathNoFile.getSegment(2));
065:
066: assertEquals("/sub1/sub2", pathNoFile.getSubPath(1).toString());
067:
068: assertEquals("/root/sub1/sub2/abc", pathNoFile.getChild("abc")
069: .toString());
070: assertEquals("/root/sub1/sub2/abc/def", pathNoFile.getChild(
071: new Path("abc/def")).toString());
072: assertEquals("/root/sub1?abc", pathNoFile
073: .removeLastPathSegment().toString());
074: assertEquals("/root?abc", pathNoFile.removeLastPathSegment()
075: .removeLastPathSegment().toString());
076: assertEquals("?abc", pathNoFile.removeLastPathSegment()
077: .removeLastPathSegment().removeLastPathSegment()
078: .toString());
079: assertEquals("?abc", pathNoFile.removeLastPathSegment()
080: .removeLastPathSegment().removeLastPathSegment()
081: .removeLastPathSegment().toString());
082:
083: Path pathFile = new Path("/root/sub1/sub2/test.html?123");
084: assertEquals("/root/sub1/sub2/abc", pathFile.getChild("abc")
085: .toString());
086: assertEquals("/root/sub1/sub2/abc/def/test123.html", pathFile
087: .getChild(new Path("abc/def/test123.html")).toString());
088: assertEquals("/root/sub1/test.html?123", pathFile
089: .removeLastPathSegment().toString());
090: assertEquals("/root/test.html?123", pathFile
091: .removeLastPathSegment().removeLastPathSegment()
092: .toString());
093: assertEquals("/test.html?123", pathFile.removeLastPathSegment()
094: .removeLastPathSegment().removeLastPathSegment()
095: .toString());
096: assertEquals("/test.html?123", pathFile.removeLastPathSegment()
097: .removeLastPathSegment().removeLastPathSegment()
098: .removeLastPathSegment().toString());
099: }
100: }
|