View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils;
20  
21  import java.io.File;
22  
23  import org.hamcrest.CoreMatchers;
24  import org.junit.Assume;
25  import org.junit.Rule;
26  import org.junit.Test;
27  import org.junit.rules.TemporaryFolder;
28  
29  import static org.hamcrest.CoreMatchers.is;
30  import static org.hamcrest.MatcherAssert.assertThat;
31  
32  /**
33   * Test the {@link PathTool} class.
34   *
35   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
36   */
37  public class PathToolTest {
38  
39      @Rule
40      public TemporaryFolder tempFolder = new TemporaryFolder();
41  
42      @Test
43      // Keep in sync with testGetRelativeFilePath_Windows()
44      public void testGetRelativeFilePath_NonWindows() {
45          Assume.assumeThat(File.separatorChar, is('/'));
46  
47          assertThat(PathTool.getRelativeFilePath(null, null), is(""));
48  
49          assertThat(PathTool.getRelativeFilePath(null, "/usr/local/java/bin"), is(""));
50  
51          assertThat(PathTool.getRelativeFilePath("/usr/local", null), is(""));
52  
53          assertThat(PathTool.getRelativeFilePath("/usr/local", "/usr/local/java/bin"), is("java/bin"));
54  
55          assertThat(PathTool.getRelativeFilePath("/usr/local", "/usr/local/java/bin/"), is("java/bin/"));
56  
57          assertThat(PathTool.getRelativeFilePath("/usr/local/java/bin", "/usr/local/"), is("../../"));
58  
59          assertThat(PathTool.getRelativeFilePath("/usr/local/", "/usr/local/java/bin/java.sh"), is("java/bin/java.sh"));
60  
61          assertThat(PathTool.getRelativeFilePath("/usr/local/java/bin/java.sh", "/usr/local/"), is("../../../"));
62  
63          assertThat(PathTool.getRelativeFilePath("/usr/local/", "/bin"), is("../../bin"));
64  
65          assertThat(PathTool.getRelativeFilePath("/bin", "/usr/local/"), is("../usr/local/"));
66      }
67  
68      @Test
69      // Keep in sync with testGetRelativeFilePath_NonWindows()
70      public void testGetRelativeFilePath_Windows() {
71          Assume.assumeThat(File.separatorChar, is('\\'));
72  
73          assertThat(PathTool.getRelativeFilePath(null, null), is(""));
74  
75          assertThat(PathTool.getRelativeFilePath(null, "c:\\usr\\local\\java\\bin"), is(""));
76  
77          assertThat(PathTool.getRelativeFilePath("c:\\usr\\local", null), is(""));
78  
79          assertThat(PathTool.getRelativeFilePath("c:\\usr\\local", "c:\\usr\\local\\java\\bin"), is("java\\bin"));
80  
81          assertThat(PathTool.getRelativeFilePath("c:\\usr\\local", "c:\\usr\\local\\java\\bin\\"), is("java\\bin\\"));
82  
83          assertThat(PathTool.getRelativeFilePath("c:\\usr\\local\\java\\bin", "c:\\usr\\local\\"), is("..\\..\\"));
84  
85          assertThat(
86                  PathTool.getRelativeFilePath("c:\\usr\\local\\", "c:\\usr\\local\\java\\bin\\java.sh"),
87                  is("java\\bin\\java.sh"));
88  
89          assertThat(
90                  PathTool.getRelativeFilePath("c:\\usr\\local\\java\\bin\\java.sh", "c:\\usr\\local\\"),
91                  is("..\\..\\..\\"));
92  
93          assertThat(PathTool.getRelativeFilePath("c:\\usr\\local\\", "c:\\bin"), is("..\\..\\bin"));
94  
95          assertThat(PathTool.getRelativeFilePath("c:\\bin", "c:\\usr\\local\\"), is("..\\usr\\local\\"));
96      }
97  
98      @Test
99      public void testGetRelativePath_2parm() {
100         assertThat(PathTool.getRelativePath(null, null), is(""));
101 
102         assertThat(PathTool.getRelativePath(null, "/usr/local/java/bin"), is(""));
103 
104         assertThat(PathTool.getRelativePath("/usr/local/", null), is(""));
105 
106         assertThat(PathTool.getRelativePath("/usr/local/", "/usr/local/java/bin"), is(".."));
107 
108         assertThat(PathTool.getRelativePath("/usr/local/", "/usr/local/java/bin/java.sh"), is("../.."));
109 
110         assertThat(PathTool.getRelativePath("/usr/local/java/bin/java.sh", "/usr/local/"), is(""));
111     }
112 
113     @Test
114     public void testUppercaseDrive() {
115         assertThat(PathTool.uppercaseDrive(null), CoreMatchers.nullValue());
116 
117         assertThat(PathTool.uppercaseDrive("d:"), is("D:"));
118 
119         assertThat(PathTool.uppercaseDrive("D:"), is("D:"));
120 
121         assertThat(PathTool.uppercaseDrive("/notadrive"), is("/notadrive"));
122     }
123 }