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  import org.junit.jupiter.api.condition.DisabledOnOs;
21  import org.junit.jupiter.api.condition.EnabledOnOs;
22  import org.junit.jupiter.api.condition.OS;
23  
24  import static org.junit.jupiter.api.Assertions.assertEquals;
25  
26  /**
27   * <p>PathToolTest class.</p>
28   *
29   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
30   * @since 3.4.0
31   */
32  class PathToolTest {
33  
34      @Test
35      void getRelativePath() {
36          assertEquals("", PathTool.getRelativePath(null, null));
37          assertEquals("", PathTool.getRelativePath(null, "/usr/local/java/bin"));
38          assertEquals("", PathTool.getRelativePath("/usr/local/", null));
39          assertEquals("..", PathTool.getRelativePath("/usr/local/", "/usr/local/java/bin"));
40          assertEquals("../..", PathTool.getRelativePath("/usr/local/", "/usr/local/java/bin/java.sh"));
41          assertEquals("", PathTool.getRelativePath("/usr/local/java/bin/java.sh", "/usr/local/"));
42      }
43  
44      @Test
45      void getDirectoryComponent() {
46          assertEquals("", PathTool.getDirectoryComponent(null));
47          assertEquals("/usr/local/java", PathTool.getDirectoryComponent("/usr/local/java/bin"));
48          assertEquals("/usr/local/java/bin", PathTool.getDirectoryComponent("/usr/local/java/bin/"));
49          assertEquals("/usr/local/java/bin", PathTool.getDirectoryComponent("/usr/local/java/bin/java.sh"));
50      }
51  
52      @Test
53      void calculateLink() {
54          assertEquals("../../index.html", PathTool.calculateLink("/index.html", "../.."));
55          assertEquals(
56                  "http://plexus.codehaus.org/plexus-utils/index.html",
57                  PathTool.calculateLink("http://plexus.codehaus.org/plexus-utils/index.html", "../.."));
58          assertEquals(
59                  "../../usr/local/java/bin/java.sh", PathTool.calculateLink("/usr/local/java/bin/java.sh", "../.."));
60          assertEquals(
61                  "/usr/local/java/bin/../index.html", PathTool.calculateLink("../index.html", "/usr/local/java/bin"));
62          assertEquals(
63                  "http://plexus.codehaus.org/plexus-utils/../index.html",
64                  PathTool.calculateLink("../index.html", "http://plexus.codehaus.org/plexus-utils"));
65      }
66  
67      @Test
68      void getRelativeWebPath() {
69          assertEquals("", PathTool.getRelativeWebPath(null, null));
70          assertEquals("", PathTool.getRelativeWebPath(null, "http://plexus.codehaus.org/"));
71          assertEquals("", PathTool.getRelativeWebPath("http://plexus.codehaus.org/", null));
72          assertEquals(
73                  "plexus-utils/index.html",
74                  PathTool.getRelativeWebPath(
75                          "http://plexus.codehaus.org/", "http://plexus.codehaus.org/plexus-utils/index.html"));
76          assertEquals(
77                  "../../",
78                  PathTool.getRelativeWebPath(
79                          "http://plexus.codehaus.org/plexus-utils/index.html", "http://plexus.codehaus.org/"));
80      }
81  
82      @Test
83      @EnabledOnOs(OS.WINDOWS)
84      void getRelativeFilePathOnWindows() {
85          assertEquals("", PathTool.getRelativeFilePath(null, null));
86          assertEquals("", PathTool.getRelativeFilePath(null, "c:\\tools\\java\\bin"));
87          assertEquals("", PathTool.getRelativeFilePath("c:\\tools\\java", null));
88          assertEquals("java\\bin", PathTool.getRelativeFilePath("c:\\tools", "c:\\tools\\java\\bin"));
89          assertEquals("java\\bin\\", PathTool.getRelativeFilePath("c:\\tools", "c:\\tools\\java\\bin\\"));
90          assertEquals("..\\..", PathTool.getRelativeFilePath("c:\\tools\\java\\bin", "c:\\tools"));
91          assertEquals(
92                  "java\\bin\\java.exe", PathTool.getRelativeFilePath("c:\\tools\\", "c:\\tools\\java\\bin\\java.exe"));
93          assertEquals("..\\..\\..", PathTool.getRelativeFilePath("c:\\tools\\java\\bin\\java.sh", "c:\\tools"));
94          assertEquals("..\\bin", PathTool.getRelativeFilePath("c:\\tools", "c:\\bin"));
95          assertEquals("..\\tools", PathTool.getRelativeFilePath("c:\\bin", "c:\\tools"));
96          assertEquals("", PathTool.getRelativeFilePath("c:\\bin", "c:\\bin"));
97      }
98  
99      @Test
100     @DisabledOnOs(OS.WINDOWS)
101     void getRelativeFilePath() {
102         assertEquals("", PathTool.getRelativeFilePath(null, null));
103         assertEquals("", PathTool.getRelativeFilePath(null, "/usr/local/java/bin"));
104         assertEquals("", PathTool.getRelativeFilePath("/usr/local", null));
105         assertEquals("java/bin", PathTool.getRelativeFilePath("/usr/local", "/usr/local/java/bin"));
106         assertEquals("java/bin/", PathTool.getRelativeFilePath("/usr/local", "/usr/local/java/bin/"));
107         assertEquals("../../", PathTool.getRelativeFilePath("/usr/local/java/bin", "/usr/local/"));
108         assertEquals("java/bin/java.sh", PathTool.getRelativeFilePath("/usr/local/", "/usr/local/java/bin/java.sh"));
109         assertEquals("../../../", PathTool.getRelativeFilePath("/usr/local/java/bin/java.sh", "/usr/local/"));
110         assertEquals("../../bin", PathTool.getRelativeFilePath("/usr/local/", "/bin"));
111         assertEquals("../usr/local", PathTool.getRelativeFilePath("/bin", "/usr/local"));
112         assertEquals("", PathTool.getRelativeFilePath("/bin", "/bin"));
113     }
114 }