1 package org.apache.maven.shared.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40 public class PathToolTest extends Assert
41 {
42
43 @Rule
44 public TemporaryFolder tempFolder = new TemporaryFolder();
45
46 @Test
47
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
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 }