View Javadoc

1   package org.apache.maven.shared.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.hamcrest.CoreMatchers;
23  import org.junit.Assume;
24  import org.junit.Rule;
25  import org.junit.Test;
26  import org.junit.Assert;
27  import org.junit.rules.TemporaryFolder;
28  
29  
30  import java.io.File;
31  
32  import static org.hamcrest.CoreMatchers.is;
33  
34  /**
35   * Test the {@link PathTool} class.
36   *
37   * We don't need to test this
38   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
39   */
40  public class PathToolTest extends Assert
41  {
42  
43      @Rule
44      public TemporaryFolder tempFolder = new TemporaryFolder();
45  
46      @Test
47      // Keep in sync with testGetRelativeFilePath_Windows()
48      public void testGetRelativeFilePath_NonWindows()
49      {
50          Assume.assumeThat( File.separatorChar, is( '/' ) );
51  
52          assertThat( PathTool.getRelativeFilePath( null, null )
53                    , is( "" ) );
54  
55          assertThat( PathTool.getRelativeFilePath( null, "/usr/local/java/bin" )
56                    , is( "" ) );
57  
58          assertThat( PathTool.getRelativeFilePath( "/usr/local", null )
59                    , is( "" ) );
60  
61          assertThat( PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin" )
62                    , is( "java/bin" ) );
63  
64          assertThat( PathTool.getRelativeFilePath( "/usr/local", "/usr/local/java/bin/" )
65                    , is( "java/bin/" ) );
66  
67          assertThat( PathTool.getRelativeFilePath( "/usr/local/java/bin", "/usr/local/" )
68                    , is( "../../" ) );
69  
70          assertThat( PathTool.getRelativeFilePath( "/usr/local/", "/usr/local/java/bin/java.sh" )
71                    , is( "java/bin/java.sh" ) );
72  
73          assertThat( PathTool.getRelativeFilePath( "/usr/local/java/bin/java.sh", "/usr/local/" )
74                    , is( "../../../" ) );
75  
76          assertThat( PathTool.getRelativeFilePath( "/usr/local/", "/bin" )
77                    , is( "../../bin" ) );
78  
79          assertThat( PathTool.getRelativeFilePath( "/bin", "/usr/local/" )
80                    , is( "../usr/local/" ) );
81      }
82      
83      @Test
84      // Keep in sync with testGetRelativeFilePath_NonWindows()
85      public void testGetRelativeFilePath_Windows()
86      {
87          Assume.assumeThat( File.separatorChar, is( '\\' ) );
88  
89          assertThat( PathTool.getRelativeFilePath( null, null )
90                    , is( "" ) );
91  
92          assertThat( PathTool.getRelativeFilePath( null, "c:\\usr\\local\\java\\bin" )
93                    , is( "" ) );
94  
95          assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local", null )
96                    , is( "" ) );
97  
98          assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local", "c:\\usr\\local\\java\\bin" )
99                    , is( "java\\bin" ) );
100 
101         assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local", "c:\\usr\\local\\java\\bin\\" )
102                   , is( "java\\bin\\" ) );
103 
104         assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local\\java\\bin", "c:\\usr\\local\\" )
105                   , is( "..\\..\\" ) );
106 
107         assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local\\", "c:\\usr\\local\\java\\bin\\java.sh" )
108                   , is( "java\\bin\\java.sh" ) );
109 
110         assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local\\java\\bin\\java.sh", "c:\\usr\\local\\" )
111                   , is( "..\\..\\..\\" ) );
112 
113         assertThat( PathTool.getRelativeFilePath( "c:\\usr\\local\\", "c:\\bin" )
114                   , is( "..\\..\\bin" ) );
115 
116         assertThat( PathTool.getRelativeFilePath( "c:\\bin", "c:\\usr\\local\\" )
117                   , is( "..\\usr\\local\\" ) );
118     }
119 
120     @Test
121     public void testGetRelativePath_2parm()
122     {
123         assertThat( PathTool.getRelativePath( null, null )
124                   , is( "" ) );
125 
126         assertThat( PathTool.getRelativePath( null, "/usr/local/java/bin" )
127                   , is( "" ) );
128 
129         assertThat( PathTool.getRelativePath( "/usr/local/", null )
130                   , is( "" ) );
131 
132         assertThat( PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin" )
133                   , is( ".." ) );
134 
135         assertThat( PathTool.getRelativePath( "/usr/local/", "/usr/local/java/bin/java.sh" )
136                   , is( "../.." ) );
137 
138         assertThat( PathTool.getRelativePath( "/usr/local/java/bin/java.sh", "/usr/local/" )
139                   , is( "" ) );
140     }
141 
142     @Test
143     public void testUppercaseDrive()
144     {
145         assertThat( PathTool.uppercaseDrive( null )
146                 , CoreMatchers.<Object>nullValue() );
147 
148         assertThat( PathTool.uppercaseDrive( "d:" )
149                 , is( "D:" ) );
150 
151         assertThat( PathTool.uppercaseDrive( "D:" )
152                 , is( "D:" ) );
153 
154         assertThat( PathTool.uppercaseDrive( "/notadrive" )
155                 , is( "/notadrive" ) );
156     }
157 
158 }