1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37 public class PathToolTest {
38
39 @Rule
40 public TemporaryFolder tempFolder = new TemporaryFolder();
41
42 @Test
43
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
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 }