KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > util > PathsTest


1 /*
2  * PathTest.java
3  * JUnit based test
4  *
5  * Created on 2. Maerz 2006, 20:26
6  */

7 /*
8  * Copyright 2006 Schlichtherle IT Services
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */

22
23 package de.schlichtherle.io.util;
24
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import junit.framework.*;
29
30 /**
31  * @author Christian Schlichtherle
32  * @since TrueZIP 5.1.4
33  * @version @version@
34  */

35 public class PathsTest extends TestCase {
36
37     public PathsTest(String JavaDoc testName) {
38         super(testName);
39     }
40
41     protected void setUp() throws Exception JavaDoc {
42     }
43
44     protected void tearDown() throws Exception JavaDoc {
45     }
46
47     public static Test suite() {
48         TestSuite suite = new TestSuite(PathsTest.class);
49
50         return suite;
51     }
52
53     public void testCutTrailingSeparators() {
54         String JavaDoc path;
55
56         path = "";
57         assertSame(path, Paths.cutTrailingSeparators(path, '/'));
58
59         path = "dir";
60         assertSame(path, Paths.cutTrailingSeparators(path, '/'));
61         assertEquals("dir", Paths.cutTrailingSeparators("dir/", '/'));
62         assertEquals("dir", Paths.cutTrailingSeparators("dir//", '/'));
63         assertEquals("dir", Paths.cutTrailingSeparators("dir///", '/'));
64
65         path = "/dir";
66         assertSame(path, Paths.cutTrailingSeparators(path, '/'));
67         assertEquals("/dir", Paths.cutTrailingSeparators("/dir/", '/'));
68         assertEquals("/dir", Paths.cutTrailingSeparators("/dir//", '/'));
69         assertEquals("/dir", Paths.cutTrailingSeparators("/dir///", '/'));
70
71         path = new String JavaDoc("/");
72         assertSame(path, Paths.cutTrailingSeparators(path, '/'));
73         assertEquals("/", Paths.cutTrailingSeparators("//", '/'));
74         assertEquals("/", Paths.cutTrailingSeparators("///", '/'));
75         assertEquals("/", Paths.cutTrailingSeparators("////", '/'));
76     }
77
78     public void testSplitPathName() {
79         final String JavaDoc fs = File.separator;
80
81         testSplit(fs + "dir" + fs + "file" + fs);
82         testSplit(fs + "dir" + fs + "file");
83         testSplit(fs + "dir" + fs);
84         testSplit(fs + "dir");
85         testSplit(fs);
86
87         testSplit("dir" + fs + "file" + fs);
88         testSplit("dir" + fs + "file");
89         testSplit("dir" + fs);
90         testSplit("dir");
91         testSplit("d");
92         testSplit("");
93
94         testSplit(".");
95         testSplit("..");
96         testSplit(fs);
97
98         testSplit("dir");
99         testSplit("dir" + fs);
100         testSplit("dir" + fs + fs);
101         testSplit("dir" + fs + fs + fs);
102
103         testSplit("dir" + fs + "file");
104         testSplit("dir" + fs + "file" + fs);
105         testSplit("dir" + fs + "file" + fs + fs);
106         testSplit("dir" + fs + "file" + fs + fs + fs);
107
108         testSplit("dir" + fs + fs + "file");
109         testSplit("dir" + fs + fs + fs + "file");
110         
111         testSplit("dir" + fs + fs + fs + "file" + fs);
112         testSplit("dir" + fs + fs + fs + "file" + fs + fs);
113         testSplit("dir" + fs + fs + fs + "file" + fs + fs + fs);
114         
115         if (File.separatorChar == '\\') { // Windoze?
116
testSplit("\\\\\\host");
117             testSplit("\\\\\\\\host");
118             testSplit("\\\\\\\\\\host");
119
120             testSplit("\\\\host\\share\\\\file\\\\");
121             testSplit("\\\\host\\share\\file\\");
122             testSplit("\\\\host\\share\\file");
123             testSplit("\\\\host\\share\\");
124             testSplit("\\\\host\\share");
125             testSplit("\\\\host\\");
126             testSplit("\\\\host");
127             testSplit("\\\\h");
128             testSplit("\\\\");
129
130             testSplit("C:\\dir\\\\file\\\\");
131             testSplit("C:\\dir\\file\\");
132             testSplit("C:\\dir\\file");
133             testSplit("C:\\dir\\");
134             testSplit("C:\\dir");
135             testSplit("C:\\d");
136             testSplit("C:\\");
137
138             testSplit("C:dir\\\\file\\\\");
139             testSplit("C:dir\\file\\");
140             testSplit("C:dir\\file");
141             testSplit("C:dir\\");
142             testSplit("C:dir");
143             testSplit("C:d");
144             testSplit("C:");
145         }
146     }
147
148     /**
149      * Test of split method, of class de.schlichtherle.io.util.Paths.
150      */

151     public void testSplit(final String JavaDoc path) {
152         final java.io.File JavaDoc file = new java.io.File JavaDoc(path);
153         final String JavaDoc parent = file.getParent();
154         final String JavaDoc base = file.getName();
155         //System.err.println(path + " -> (" + parent + ", " + base + ")");
156

157         final String JavaDoc[] split = Paths.split(path, File.separatorChar);
158         assertEquals(2, split.length);
159         assertEquals(parent, split[0]);
160         assertEquals(base, split[1]);
161     }
162
163     public void testNormalize() {
164         testNormalize(".", "");
165         testNormalize("a/b/c/d", "a/b/c/d");
166
167         testNormalize("a", "./a");
168
169         testNormalize(".", ".");
170         testNormalize("..", "./..");
171         testNormalize("../..", "./../..");
172         testNormalize("../../..", "./../../..");
173         testNormalize("../../..", "./.././.././..");
174         testNormalize("../../..", "././../././../././..");
175         testNormalize("../../..", "./././.././././.././././..");
176
177         testNormalize("..", "..");
178         testNormalize("../..", "../..");
179         testNormalize("../../..", "../../..");
180         testNormalize("../../../..", "../../../..");
181         testNormalize("../../../..", "../.././.././..");
182         testNormalize("../../../..", ".././../././../././..");
183         testNormalize("../../../..", "../././.././././.././././..");
184
185         testNormalize("a", "a");
186         testNormalize(".", "a/..");
187         testNormalize("..", "a/../..");
188         testNormalize("../..", "a/../../..");
189         testNormalize("../..", "a/./.././.././..");
190         testNormalize("../..", "a/././../././../././..");
191         testNormalize("../..", "a/./././.././././.././././..");
192
193         testNormalize("a/b", "a/b");
194         testNormalize("a", "a/b/..");
195         testNormalize(".", "a/b/../..");
196         testNormalize("..", "a/b/../../..");
197         testNormalize("..", "a/b/./.././.././..");
198         testNormalize("..", "a/b/././../././../././..");
199         testNormalize("..", "a/b/./././.././././.././././..");
200         
201         testNormalize("a/b/c", "a/b/c");
202         testNormalize("a/b", "a/b/c/..");
203         testNormalize("a", "a/b/c/../..");
204         testNormalize(".", "a/b/c/../../..");
205         testNormalize(".", "a/b/c/./.././.././..");
206         testNormalize(".", "a/b/c/././../././../././..");
207         testNormalize(".", "a/b/c/./././.././././.././././..");
208
209         testNormalize("a/b/c/d", "a/b/c/d");
210         testNormalize("a/b/c", "a/b/c/d/..");
211         testNormalize("a/b", "a/b/c/d/../..");
212         testNormalize("a", "a/b/c/d/../../..");
213         testNormalize("a", "a/b/c/d/./.././.././..");
214         testNormalize("a", "a/b/c/d/././../././../././..");
215         testNormalize("a", "a/b/c/d/./././.././././.././././..");
216
217         testNormalize("a/b/c/d", "a//b//c//d");
218         testNormalize("a/b/c/d", "a///b///c///d");
219         testNormalize("a/b/c/d", "a////b////c////d");
220         testNormalize("a/b/c", "a////b////c////d////..");
221         testNormalize("a/b", "a////b////c////d////..////..");
222         testNormalize("a/b", "a//.//b/.///c///./d//.//.././//..");
223         testNormalize("a/b", "a/////b/////c/////d/////../////..");
224
225         testNormalize("a", "x/../a");
226         testNormalize("a/b", "x/../a/y/../b");
227         testNormalize("a/b/c", "x/../a/y/../b/z/../c");
228
229         testNormalize("../a", "x/../../a");
230         testNormalize("../a/b", "x/../../a/y/../b");
231         testNormalize("../a/b/c", "x/../../a/y/../b/z/../c");
232
233         testNormalize("../a", "x/.././../a");
234         testNormalize("../a/b", "x/.././../a/y/../b");
235         testNormalize("../a/b/c", "x/.././../a/y/../b/z/../c");
236
237         testNormalize("../a", "x/..//../a");
238         testNormalize("../a/b", "x/..//../a/y/../b");
239         testNormalize("../a/b/c", "x/..//../a/y/../b/z/../c");
240
241         testNormalize("../../a", "x/../../../a");
242         testNormalize("../../a/b", "x/../../../a/y/../b");
243         testNormalize("../../a/b/c", "x/../../../a/y/../b/z/../c");
244
245         testNormalize("../../a", "x/.././.././../a");
246         testNormalize("../../a/b", "x/.././.././../a/y/../b");
247         testNormalize("../../a/b/c", "x/.././.././../a/y/../b/z/../c");
248
249         testNormalize("../../a", "x/..//..//../a");
250         testNormalize("../../a/b", "x/..//..//../a/y/../b");
251         testNormalize("../../a/b/c", "x/..//..//../a/y/../b/z/../c");
252
253         testNormalize("a", "x/x/../../a");
254         testNormalize("a/b", "x/x/../../a/y/y/../../b");
255         testNormalize("a/b/c", "x/x/../../a/y/y/../../b/z/z/../../c");
256
257         testNormalize("/", "/");
258         //testNormalize("/", "//");
259
testNormalize("/", "/.");
260         testNormalize("/", "/./");
261
262         testNormalize("/..", "/..");
263         testNormalize("/..", "/../.");
264         testNormalize("/../..", "/.././..");
265         testNormalize("/../..", "/.././../.");
266
267         testNormalize("\\\\host", "\\\\host", '\\');
268         testNormalize("\\\\host", "\\\\\\host", '\\');
269         testNormalize("\\\\host", "\\\\\\\\host", '\\');
270
271         testNormalize("C:\\dir", "C:\\dir", '\\');
272         testNormalize("C:\\dir", "C:\\\\dir", '\\');
273         testNormalize("C:\\dir", "C:\\\\\\dir", '\\');
274
275         testNormalize("C:dir", "C:dir", '\\');
276         testNormalize("C:dir", "C:dir\\.", '\\');
277         testNormalize("C:dir", "C:dir\\.\\.", '\\');
278
279         testNormalize("C:dir", "C:.\\dir", '\\');
280         testNormalize("C:dir", "C:.\\dir\\.", '\\');
281         testNormalize("C:dir", "C:.\\dir\\.\\.", '\\');
282
283         testNormalize(".", ".");
284         testNormalize("./", "./");
285         testNormalize("..", "..");
286         testNormalize("../", "../");
287         testNormalize("a", "./a");
288         testNormalize("a/", "./a/");
289         testNormalize("../a", "../a");
290         testNormalize("../a/", "../a/");
291         testNormalize("a/b", "./a/./b");
292         testNormalize("a/b/", "./a/./b/");
293         testNormalize("../a/b", "../a/./b");
294         testNormalize("../a/b/", "../a/./b/");
295         testNormalize("b", "./a/../b");
296         testNormalize("b/", "./a/../b/");
297         testNormalize("../b", "../a/../b");
298         testNormalize("../b/", "../a/../b/");
299
300         testNormalize("./", ".//");
301         testNormalize("./", ".///");
302         testNormalize("./", ".////");
303         testNormalize("../", "..//");
304         testNormalize("a/", ".//a//");
305         testNormalize("../a", "..//a");
306         testNormalize("../a/", "..//a//");
307         testNormalize("a/b", ".//a//.//b");
308         testNormalize("a/b/", ".//a//.//b//");
309         testNormalize("../a/b", "..//a//.//b");
310         testNormalize("../a/b/", "..//a//.//b//");
311         testNormalize("b/", ".//a//..//b//");
312         testNormalize("../b", "..//a//..//b");
313         testNormalize("../b/", "..//a//..//b//");
314     }
315     
316     private void testNormalize(String JavaDoc result, final String JavaDoc path) {
317         testNormalize(result, path, '/');
318     }
319     
320     private void testNormalize(String JavaDoc result, final String JavaDoc path, final char separatorChar) {
321         if (result == path)
322             assertSame(result, Paths.normalize(path, separatorChar));
323         else
324             assertEquals(result, Paths.normalize(path, separatorChar));
325         if (path.length() > 0 && !path.endsWith(separatorChar + "")) {
326             String JavaDoc appended = path;
327             for (int i = 0; i < 3; i++) {
328                 appended += separatorChar + ".";
329                 assertEquals(result, Paths.normalize(appended, separatorChar));
330             }
331         }
332     }
333 }
334
Popular Tags