1 package org.apache.maven.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25 /**
26 * Unit test for <code>DVSLPathTool</code>.
27 *
28 * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
29 * @version $Id: DVSLPathToolTest.java 517014 2007-03-11 21:15:50Z ltheussl $
30 */
31 public class DVSLPathToolTest
32 extends TestCase
33 {
34 /**
35 * Create the test case
36 *
37 * @param testName name of the test case
38 */
39 public DVSLPathToolTest( String testName )
40 {
41 super( testName );
42 }
43
44 /**
45 * @return the suite of tests being tested
46 */
47 public static Test suite()
48 {
49 return new TestSuite( DVSLPathToolTest.class );
50 }
51
52 /**
53 * Tests <code>DVSLPathTool.getDirectoryComponent</code>.
54 */
55 public void testGetDirectoryComponent()
56 {
57 String[] test = { "file1.xml", "ref/file1", "/home/test/file2.vm", "d1\\d2\\d3\\file", "/d1/d2/" };
58
59 String[] expected = { ".", "ref", "/home/test", "d1\\d2\\d3", "/d1/d2" };
60
61 for ( int i = 0; i < test.length; i++ )
62 {
63 assertEquals( expected[i], DVSLPathTool.getDirectoryComponent( test[i] ) );
64 }
65 }
66
67 /**
68 * Tests <code>DVSLPathTool.getRelativePath</code>.
69 */
70 public void testGetRelativePath()
71 {
72 TestArgs[] tests = {
73 new TestArgs( "", "/home/www/html", "", "../.." ),
74 new TestArgs( "/", "/home/www/html", "../..", "../.." ),
75 new TestArgs( "", "/home/www/html/", "", "../.." ),
76 new TestArgs( "/", "/home/www/html/", "../..", "../.." ),
77 new TestArgs( "", "home/www/html", "", "../.." ),
78 new TestArgs( "/", "home/www/html", "", "../.." ),
79 new TestArgs( "", "home/www/html/", "", "../.." ),
80 new TestArgs( "/", "home/www/html/", "", "../.." ),
81 new TestArgs( "/", "/home", ".", "." ),
82 new TestArgs( "/usr/local", null, "", "" ),
83 new TestArgs( null, "/usr/local", "", ".." ),
84 new TestArgs( null, null, "", "" ),
85 new TestArgs( "/home/kaz", "/home/kaz", "", ".." ),
86 new TestArgs( "/home/kaz", "/home/kaz/", "", ".." ),
87 new TestArgs( "/home/kaz/", "/home/kaz", "", ".." ),
88 new TestArgs( "/home/kaz/", "/home/kaz/", "", ".." ),
89 new TestArgs( "/home/kaz", "/home/kaz/file.html", ".", "../.." ),
90 new TestArgs( "/home/kaz/", "/home/kaz/file.html", ".", "../.." ),
91 new TestArgs( "/home/kaz", "/home/kaz/howto/jeff.html", "..", "../../.." ),
92 new TestArgs( "/home/", "/home/kaz/howto/images/", "../..", "../../.." ),
93 new TestArgs( "/home/", "/home/kaz/howto/jeff.html", "../..", "../../.." ),
94 new TestArgs( "/home/", "/home/kaz/howto/u/y/z/jeff.html", "../../../../..", "../../../../../.." ),
95 new TestArgs( "/home/boston", "/usr/local/where/am/i", "", "../../../.." ),
96 new TestArgs( "home/boston", "/usr/local/where/am/i", "", "../../../.." ),
97 new TestArgs( "home/boston", "home/boston/where/am/i", "../..", "../../../.." ),
98 new TestArgs( "", "\\home\\www\\html", "", "../.." ),
99 new TestArgs( "\\", "\\home\\www\\html", "../..", "../.." ),
100 new TestArgs( "", "\\home\\www\\html\\", "", "../.." ),
101 new TestArgs( "\\", "\\home\\www\\html\\", "../..", "../.." ),
102 new TestArgs( "", "home\\www\\html", "", "../.." ),
103 new TestArgs( "\\", "home\\www\\html", "", "../.." ),
104 new TestArgs( "", "home\\www\\html\\", "", "../.." ),
105 new TestArgs( "\\", "home\\www\\html\\", "", "../.." ),
106 new TestArgs( "\\", "\\home", ".", "." ),
107 new TestArgs( "\\usr\\local", null, "", "" ),
108 new TestArgs( null, "\\usr\\local", "", ".." ),
109 new TestArgs( null, null, "", "" ),
110 new TestArgs( "\\home\\kaz", "\\home\\kaz", "", ".." ),
111 new TestArgs( "\\home\\kaz", "\\home\\kaz\\", "", ".." ),
112 new TestArgs( "\\home\\kaz\\", "\\home\\kaz", "", ".." ),
113 new TestArgs( "\\home\\kaz\\", "\\home\\kaz\\", "", ".." ),
114 new TestArgs( "\\home\\kaz", "\\home\\kaz\\file.html", ".", "../.." ),
115 new TestArgs( "\\home\\kaz\\", "\\home\\kaz\\file.html", ".", "../.." ),
116 new TestArgs( "\\home\\kaz", "\\home\\kaz\\howto\\jeff.html", "..", "../../.." ),
117 new TestArgs( "\\home\\", "\\home\\kaz\\howto\\images\\", "../..", "../../.." ),
118 new TestArgs( "\\home\\", "\\home\\kaz\\howto\\jeff.html", "../..", "../../.." ),
119 new TestArgs( "\\home\\", "\\home\\kaz\\howto\\u\\y\\z\\jeff.html", "../../../../..", "../../../../../.." ),
120 new TestArgs( "\\home\\boston", "\\usr\\local\\where\\am\\i", "", "../../../.." ),
121 new TestArgs( "home\\boston", "\\usr\\local\\where\\am\\i", "", "../../../.." ),
122 new TestArgs( "home\\boston", "home\\boston\\where\\am\\i", "../..", "../../../.." ),
123 new TestArgs( "/x/y/z/", "/x/y/z/a\\b\\c", ".", "../../.." ),
124 new TestArgs( "/x/y/z/", "\\x\\y\\z\\a\\b\\c", "", "../../../../.." ),
125 new TestArgs( "\\x\\y\\z", "\\x\\y\\z\\a/b\\c", "..", "../../../.." ),
126 new TestArgs( "\\x\\y\\z/", "\\x\\y\\z/\\a/b\\c", "..", "../../../.." ),
127 new TestArgs( "D:/a/b", "D:/a/b/c", ".", "../../.." ),
128 new TestArgs( "d:/a/b", "D:/a/b/c.html", ".", "../../.." ), };
129
130 for ( int i = 0; i < tests.length; i++ )
131 {
132 assertEquals( tests[i].toString(), tests[i].expected, DVSLPathTool.getRelativePath( tests[i].basedir,
133 tests[i].filename ) );
134 }
135
136 for ( int i = 0; i < tests.length; i++ )
137 {
138 assertEquals( tests[i].toString(), tests[i].expectedNoBase, DVSLPathTool
139 .getRelativePath( tests[i].filename ) );
140 }
141 }
142
143 /**
144 * Value object for a relative path test case. This class is used
145 * when testing the <code>getRelativePath(basedir, filename)</code>
146 * and <code>getRelativePath(filename)</code> methods. The expected
147 * results for each method are different which is the reason that
148 * both expected values are stored as part of the class.
149 *
150 * @author <a href="mailto:pete-apache-dev@kazmier.com">Pete Kazmier</a>
151 * @version
152 * $Id: DVSLPathToolTest.java 517014 2007-03-11 21:15:50Z ltheussl $
153 */
154 private static class TestArgs
155 {
156 /** base directory for tests */
157 private String basedir;
158
159 /** the file to test*/
160 private String filename;
161
162 /**
163 * expected result when calling <code>getRelativePath(basedir,
164 * filename)</code>.
165 */
166 private String expected;
167
168 /**
169 * expected result when calling <code>getRelativePath(filename)</code>.
170 */
171 private String expectedNoBase;
172
173 /**
174 * Constructor.
175 *
176 * @param b The base directory.
177 * @param f The filename.
178 * @param e The expected result when calling
179 * <code>getRelativePath(basedir, filename)</code>.
180 * @param enb The expected result when calling
181 * <code>getRelativePath(filename)</code>.
182 */
183 TestArgs( String b, String f, String e, String enb )
184 {
185 basedir = b;
186 filename = f;
187 expected = e;
188 expectedNoBase = enb;
189 }
190
191 /**
192 * Provide detailed information regarding the test case which
193 * can be used as part of the JUnit error message in the event
194 * of a unit test failure.
195 *
196 * @return A string describing the test parameters.
197 */
198 public String toString()
199 {
200 StringBuffer sb = new StringBuffer();
201 sb.append( "Basedir: " );
202 sb.append( basedir );
203 sb.append( " Filename: " );
204 sb.append( filename );
205 return sb.toString();
206 }
207 }
208
209 public void testUppercaseDrive()
210 {
211 assertEquals( "c:/b/", "C:/b/", DVSLPathTool.uppercaseDrive( "c:/b/" ) );
212 assertEquals( "c:", "C:", DVSLPathTool.uppercaseDrive( "c:" ) );
213 assertEquals( ".", ".", DVSLPathTool.uppercaseDrive( "." ) );
214 assertEquals( "null", null, DVSLPathTool.uppercaseDrive( null ) );
215
216 }
217
218 public void testCalculateLink( String expected, String link, String relativePath )
219 {
220 String actual = DVSLPathTool.calculateLink( link, relativePath );
221 String function = "DVSLPathTool.calculateLink(" + link + "," + relativePath + ")";
222 if ( !expected.equals( actual ) )
223 {
224 System.out.println( "Function:" + function );
225 System.out.println( "Actual:" + actual );
226 System.out.println( "Expected:" + expected );
227 }
228 assertEquals( function, expected, actual );
229 }
230
231 public void testCalculateLink()
232 {
233 testCalculateLink( "/bob.html", "/absolute//bob.html", ".." );
234 testCalculateLink( "http://crazyhorse.com", "/absolute/http://crazyhorse.com", ".." );
235 testCalculateLink( "http://crazyhorse.com", "http://crazyhorse.com", ".." );
236 testCalculateLink( "../index.html", "/index.html", ".." );
237 testCalculateLink( "index.html", "/index.html", "." );
238 testCalculateLink( "../other-module/index.html", "../other-module/index.html", "." );
239 testCalculateLink( "../../other-module/index.html", "../other-module/index.html", ".." );
240 }
241 }