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 org.junit.jupiter.api.Test;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  /**
24   * <p>PathToolTest class.</p>
25   *
26   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
27   * @version $Id: $Id
28   * @since 3.4.0
29   */
30  public class PathToolTest {
31      /**
32       * <p>testGetRelativePath.</p>
33       *
34       * @throws java.lang.Exception
35       */
36      @org.junit.jupiter.api.Test
37      public void testGetRelativePath() throws Exception {
38          assertEquals(PathTool.getRelativePath(null, null), "");
39          assertEquals(PathTool.getRelativePath(null, "/usr/local/java/bin"), "");
40          assertEquals(PathTool.getRelativePath("/usr/local/", null), "");
41          assertEquals(PathTool.getRelativePath("/usr/local/", "/usr/local/java/bin"), "..");
42          assertEquals(PathTool.getRelativePath("/usr/local/", "/usr/local/java/bin/java.sh"), "../..");
43          assertEquals(PathTool.getRelativePath("/usr/local/java/bin/java.sh", "/usr/local/"), "");
44      }
45  
46      /**
47       * <p>testGetDirectoryComponent.</p>
48       *
49       * @throws java.lang.Exception
50       */
51      @Test
52      public void testGetDirectoryComponent() throws Exception {
53          assertEquals(PathTool.getDirectoryComponent(null), "");
54          assertEquals(PathTool.getDirectoryComponent("/usr/local/java/bin"), "/usr/local/java");
55          assertEquals(PathTool.getDirectoryComponent("/usr/local/java/bin/"), "/usr/local/java/bin");
56          assertEquals(PathTool.getDirectoryComponent("/usr/local/java/bin/java.sh"), "/usr/local/java/bin");
57      }
58  
59      /**
60       * <p>testCalculateLink.</p>
61       *
62       * @throws java.lang.Exception
63       */
64      @org.junit.jupiter.api.Test
65      public void testCalculateLink() throws Exception {
66          assertEquals(PathTool.calculateLink("/index.html", "../.."), "../../index.html");
67          assertEquals(
68                  PathTool.calculateLink("http://plexus.codehaus.org/plexus-utils/index.html", "../.."),
69                  "http://plexus.codehaus.org/plexus-utils/index.html");
70          assertEquals(
71                  PathTool.calculateLink("/usr/local/java/bin/java.sh", "../.."), "../../usr/local/java/bin/java.sh");
72          assertEquals(
73                  PathTool.calculateLink("../index.html", "/usr/local/java/bin"), "/usr/local/java/bin/../index.html");
74          assertEquals(
75                  PathTool.calculateLink("../index.html", "http://plexus.codehaus.org/plexus-utils"),
76                  "http://plexus.codehaus.org/plexus-utils/../index.html");
77      }
78  
79      /**
80       * <p>testGetRelativeWebPath.</p>
81       *
82       * @throws java.lang.Exception
83       */
84      @Test
85      public void testGetRelativeWebPath() throws Exception {
86          assertEquals(PathTool.getRelativeWebPath(null, null), "");
87          assertEquals(PathTool.getRelativeWebPath(null, "http://plexus.codehaus.org/"), "");
88          assertEquals(PathTool.getRelativeWebPath("http://plexus.codehaus.org/", null), "");
89          assertEquals(
90                  PathTool.getRelativeWebPath(
91                          "http://plexus.codehaus.org/", "http://plexus.codehaus.org/plexus-utils/index.html"),
92                  "plexus-utils/index.html");
93          assertEquals(
94                  PathTool.getRelativeWebPath(
95                          "http://plexus.codehaus.org/plexus-utils/index.html", "http://plexus.codehaus.org/"),
96                  "../../");
97      }
98  
99      /**
100      * <p>testGetRelativeFilePath.</p>
101      *
102      * @throws java.lang.Exception
103      */
104     @Test
105     public void testGetRelativeFilePath() throws Exception {
106         if (Os.isFamily(Os.FAMILY_WINDOWS)) {
107             assertEquals(PathTool.getRelativeFilePath(null, null), "");
108             assertEquals(PathTool.getRelativeFilePath(null, "c:\\tools\\java\\bin"), "");
109             assertEquals(PathTool.getRelativeFilePath("c:\\tools\\java", null), "");
110             assertEquals(PathTool.getRelativeFilePath("c:\\tools", "c:\\tools\\java\\bin"), "java\\bin");
111             assertEquals(PathTool.getRelativeFilePath("c:\\tools", "c:\\tools\\java\\bin\\"), "java\\bin\\");
112             assertEquals(PathTool.getRelativeFilePath("c:\\tools\\java\\bin", "c:\\tools"), "..\\..");
113             assertEquals(
114                     PathTool.getRelativeFilePath("c:\\tools\\", "c:\\tools\\java\\bin\\java.exe"),
115                     "java\\bin\\java.exe");
116             assertEquals(PathTool.getRelativeFilePath("c:\\tools\\java\\bin\\java.sh", "c:\\tools"), "..\\..\\..");
117             assertEquals(PathTool.getRelativeFilePath("c:\\tools", "c:\\bin"), "..\\bin");
118             assertEquals(PathTool.getRelativeFilePath("c:\\bin", "c:\\tools"), "..\\tools");
119             assertEquals(PathTool.getRelativeFilePath("c:\\bin", "c:\\bin"), "");
120         } else {
121             assertEquals(PathTool.getRelativeFilePath(null, null), "");
122             assertEquals(PathTool.getRelativeFilePath(null, "/usr/local/java/bin"), "");
123             assertEquals(PathTool.getRelativeFilePath("/usr/local", null), "");
124             assertEquals(PathTool.getRelativeFilePath("/usr/local", "/usr/local/java/bin"), "java/bin");
125             assertEquals(PathTool.getRelativeFilePath("/usr/local", "/usr/local/java/bin/"), "java/bin/");
126             assertEquals(PathTool.getRelativeFilePath("/usr/local/java/bin", "/usr/local/"), "../../");
127             assertEquals(
128                     PathTool.getRelativeFilePath("/usr/local/", "/usr/local/java/bin/java.sh"), "java/bin/java.sh");
129             assertEquals(PathTool.getRelativeFilePath("/usr/local/java/bin/java.sh", "/usr/local/"), "../../../");
130             assertEquals(PathTool.getRelativeFilePath("/usr/local/", "/bin"), "../../bin");
131             assertEquals(PathTool.getRelativeFilePath("/bin", "/usr/local"), "../usr/local");
132             assertEquals(PathTool.getRelativeFilePath("/bin", "/bin"), "");
133         }
134     }
135 }