View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  
21  /**
22   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
23   * @version $Id$
24   */
25  public class PathToolTest
26      extends TestCase
27  {
28      /**
29       * @throws Exception
30       */
31      public void testGetRelativePath()
32          throws Exception
33      {
34          assertEquals( PathTool.getRelativePath( null, null ), "" );
35          assertEquals( PathTool.getRelativePath( null, "/usr/local/java/bin" ), "" );
36          assertEquals( PathTool.getRelativePath( "/usr/local/", null ), "" );
37          assertEquals( PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin" ), ".." );
38          assertEquals( PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin/java.sh" ), "../.." );
39          assertEquals( PathTool.getRelativePath( "/usr/local/java/bin/java.sh", "/usr/local/" ), "" );
40      }
41  
42      /**
43       * @throws Exception
44       */
45      public void testGetDirectoryComponent()
46          throws Exception
47      {
48          assertEquals( PathTool.getDirectoryComponent( null ), "" );
49          assertEquals( PathTool.getDirectoryComponent( "/usr/local/java/bin" ), "/usr/local/java" );
50          assertEquals( PathTool.getDirectoryComponent( "/usr/local/java/bin/" ), "/usr/local/java/bin" );
51          assertEquals( PathTool.getDirectoryComponent( "/usr/local/java/bin/java.sh" ), "/usr/local/java/bin" );
52      }
53  
54      /**
55       * @throws Exception
56       */
57      public void testCalculateLink()
58          throws Exception
59      {
60          assertEquals( PathTool.calculateLink( "/index.html", "../.." ), "../../index.html" );
61          assertEquals( PathTool.calculateLink( "http://plexus.codehaus.org/plexus-utils/index.html", "../.." ),
62                        "http://plexus.codehaus.org/plexus-utils/index.html" );
63          assertEquals( PathTool.calculateLink( "/usr/local/java/bin/java.sh", "../.." ),
64                        "../../usr/local/java/bin/java.sh" );
65          assertEquals( PathTool.calculateLink( "../index.html", "/usr/local/java/bin" ),
66                        "/usr/local/java/bin/../index.html" );
67          assertEquals( PathTool.calculateLink( "../index.html", "http://plexus.codehaus.org/plexus-utils" ),
68                        "http://plexus.codehaus.org/plexus-utils/../index.html" );
69      }
70  
71      /**
72       * @throws Exception
73       */
74      public void testGetRelativeWebPath()
75          throws Exception
76      {
77          assertEquals( PathTool.getRelativeWebPath( null, null ), "" );
78          assertEquals( PathTool.getRelativeWebPath( null, "http://plexus.codehaus.org/" ), "" );
79          assertEquals( PathTool.getRelativeWebPath( "http://plexus.codehaus.org/", null ), "" );
80          assertEquals( PathTool.getRelativeWebPath( "http://plexus.codehaus.org/",
81                                                     "http://plexus.codehaus.org/plexus-utils/index.html" ),
82                        "plexus-utils/index.html" );
83          assertEquals( PathTool.getRelativeWebPath( "http://plexus.codehaus.org/plexus-utils/index.html",
84                                                     "http://plexus.codehaus.org/" ),
85                        "../../" );
86      }
87  
88      /**
89       * @throws Exception
90       */
91      public void testGetRelativeFilePath()
92          throws Exception
93      {
94          if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
95          {
96              assertEquals( PathTool.getRelativeFilePath( null, null ), "" );
97              assertEquals( PathTool.getRelativeFilePath( null, "c:\\tools\\java\\bin" ), "" );
98              assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\java", null ), "" );
99              assertEquals( PathTool.getRelativeFilePath( "c:\\tools", "c:\\tools\\java\\bin" ), "java\\bin" );
100             assertEquals( PathTool.getRelativeFilePath( "c:\\tools", "c:\\tools\\java\\bin\\" ), "java\\bin\\" );
101             assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\java\\bin", "c:\\tools" ), "..\\.." );
102             assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\", "c:\\tools\\java\\bin\\java.exe" ),
103                           "java\\bin\\java.exe" );
104             assertEquals( PathTool.getRelativeFilePath( "c:\\tools\\java\\bin\\java.sh", "c:\\tools" ), "..\\..\\.." );
105             assertEquals( PathTool.getRelativeFilePath( "c:\\tools", "c:\\bin" ), "..\\bin" );
106             assertEquals( PathTool.getRelativeFilePath( "c:\\bin", "c:\\tools" ), "..\\tools" );
107             assertEquals( PathTool.getRelativeFilePath( "c:\\bin", "c:\\bin" ), "" );
108         }
109         else
110         {
111             assertEquals( PathTool.getRelativeFilePath( null, null ), "" );
112             assertEquals( PathTool.getRelativeFilePath( null, "/usr/local/java/bin" ), "" );
113             assertEquals( PathTool.getRelativeFilePath( "/usr/local", null ), "" );
114             assertEquals( PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin" ), "java/bin" );
115             assertEquals( PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin/" ), "java/bin/" );
116             assertEquals( PathTool.getRelativeFilePath( "/usr/local/java/bin", "/usr/local/" ), "../../" );
117             assertEquals( PathTool.getRelativeFilePath( "/usr/local/", "/usr/local/java/bin/java.sh" ),
118                           "java/bin/java.sh" );
119             assertEquals( PathTool.getRelativeFilePath( "/usr/local/java/bin/java.sh", "/usr/local/" ), "../../../" );
120             assertEquals( PathTool.getRelativeFilePath( "/usr/local/", "/bin" ), "../../bin" );
121             assertEquals( PathTool.getRelativeFilePath( "/bin", "/usr/local" ), "../usr/local" );
122             assertEquals( PathTool.getRelativeFilePath( "/bin", "/bin" ), "" );
123         }
124     }
125 }