1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.util;
18  
19  import junit.framework.TestCase;
20  
21  public class TestPathUtil extends TestCase
22  {
23      public void testPath()
24      {
25          Path path = new Path("/root/sub1/sub2/file.html?foo=bar&name=bob");
26          
27          Path path2 = new Path("/root/sub1/sub2/file.html?foo=bar&name=bob");
28          
29          assertEquals(path, path2);
30          
31          assertEquals(".html", path.getFileExtension());
32          assertEquals("foo=bar&name=bob", path.getQueryString());
33          assertEquals("file.html", path.getFileName());
34          assertEquals("file", path.getBaseName());
35          
36          assertEquals(4, path.length());
37          
38          assertEquals("root", path.getSegment(0));
39          assertEquals("sub1", path.getSegment(1));
40          assertEquals("sub2", path.getSegment(2));
41          assertEquals("file.html", path.getSegment(3));  
42  //        assertEquals("/root/sub1/sub2/file.html", path.pathOnly());
43          
44          assertEquals("/sub1/sub2/file.html", path.getSubPath(1).toString());
45          
46          path = new Path("file.html");
47          assertEquals(".html", path.getFileExtension());
48          assertEquals("file.html", path.getFileName());
49          assertEquals("file", path.getBaseName());
50          
51          assertNull(path.getQueryString());
52          
53          assertEquals(1, path.length());
54          assertEquals("file.html", path.getSegment(0));  
55          
56          path = new Path("file");
57          
58          assertNull(path.getBaseName());
59          
60          Path pathNoFile = new Path("/root/sub1/sub2?abc");
61          assertEquals("root", pathNoFile.getSegment(0));
62          assertEquals("sub1", pathNoFile.getSegment(1));
63          assertEquals("sub2", pathNoFile.getSegment(2));
64          
65          assertEquals("/sub1/sub2", pathNoFile.getSubPath(1).toString());
66          
67          assertEquals("/root/sub1/sub2/abc", pathNoFile.getChild("abc").toString());
68          assertEquals("/root/sub1/sub2/abc/def", pathNoFile.getChild(new Path("abc/def")).toString());
69          assertEquals("/root/sub1?abc", pathNoFile.removeLastPathSegment().toString());
70          assertEquals("/root?abc", pathNoFile.removeLastPathSegment().removeLastPathSegment().toString());
71          assertEquals("?abc", pathNoFile.removeLastPathSegment().removeLastPathSegment().removeLastPathSegment().toString());
72          assertEquals("?abc", pathNoFile.removeLastPathSegment().removeLastPathSegment().removeLastPathSegment().removeLastPathSegment().toString());
73  
74          Path pathFile = new Path("/root/sub1/sub2/test.html?123");
75          assertEquals("/root/sub1/sub2/abc", pathFile.getChild("abc").toString());
76          assertEquals("/root/sub1/sub2/abc/def/test123.html", pathFile.getChild(new Path("abc/def/test123.html")).toString());
77          assertEquals("/root/sub1/test.html?123", pathFile.removeLastPathSegment().toString());
78          assertEquals("/root/test.html?123", pathFile.removeLastPathSegment().removeLastPathSegment().toString());
79          assertEquals("/test.html?123", pathFile.removeLastPathSegment().removeLastPathSegment().removeLastPathSegment().toString());
80          assertEquals("/test.html?123", pathFile.removeLastPathSegment().removeLastPathSegment().removeLastPathSegment().removeLastPathSegment().toString());
81      }
82  }