KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > PathTest


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

17
18 package org.apache.tools.ant.types;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.taskdefs.condition.Os;
23
24 import junit.framework.TestCase;
25 import junit.framework.AssertionFailedError;
26
27 import java.io.File JavaDoc;
28 import java.util.Locale JavaDoc;
29
30 /**
31  * JUnit 3 testcases for org.apache.tools.ant.types.Path
32  *
33  */

34
35 public class PathTest extends TestCase {
36
37     public static boolean isUnixStyle = File.pathSeparatorChar == ':';
38     public static boolean isNetWare = Os.isFamily("netware");
39
40     private Project project;
41
42     public PathTest(String JavaDoc name) {
43         super(name);
44     }
45
46     public void setUp() {
47         project = new Project();
48         project.setBasedir(".");
49     }
50
51     // actually tests constructor as well as setPath
52
public void testConstructorUnixStyle() {
53         Path p = new Path(project, "/a:/b");
54         String JavaDoc[] l = p.list();
55         assertEquals("two items, Unix style", 2, l.length);
56         if (isUnixStyle) {
57             assertEquals("/a", l[0]);
58             assertEquals("/b", l[1]);
59         } else if (isNetWare) {
60             assertEquals("\\a", l[0]);
61             assertEquals("\\b", l[1]);
62         } else {
63             assertEquals(":\\a", l[0].substring(1));
64             assertEquals(":\\b", l[1].substring(1));
65         }
66     }
67
68     public void testRelativePathUnixStyle() {
69         project.setBasedir("src/etc");
70         Path p = new Path(project, "..:testcases");
71         String JavaDoc[] l = p.list();
72         assertEquals("two items, Unix style", 2, l.length);
73         if (isUnixStyle) {
74            assertTrue("test resolved relative to src/etc",
75                  l[0].endsWith("/src"));
76            assertTrue("test resolved relative to src/etc",
77                  l[1].endsWith("/src/etc/testcases"));
78         } else if (isNetWare) {
79            assertTrue("test resolved relative to src/etc",
80                  l[0].endsWith("\\src"));
81            assertTrue("test resolved relative to src/etc",
82                  l[1].endsWith("\\src\\etc\\testcases"));
83         } else {
84            assertTrue("test resolved relative to src/etc",
85                  l[0].endsWith("\\src"));
86            assertTrue("test resolved relative to src/etc",
87                  l[1].endsWith("\\src\\etc\\testcases"));
88         }
89     }
90
91     public void testConstructorWindowsStyle() {
92         Path p = new Path(project, "\\a;\\b");
93         String JavaDoc[] l = p.list();
94         assertEquals("two items, DOS style", 2, l.length);
95         if (isUnixStyle) {
96             assertEquals("/a", l[0]);
97             assertEquals("/b", l[1]);
98         } else if (isNetWare) {
99             assertEquals("\\a", l[0]);
100             assertEquals("\\b", l[1]);
101         } else {
102             assertEquals(":\\a", l[0].substring(1));
103             assertEquals(":\\b", l[1].substring(1));
104         }
105
106         p = new Path(project, "c:\\test");
107         l = p.list();
108         if (isUnixStyle) {
109             assertEquals("no drives on Unix", 2, l.length);
110             assertTrue("c resolved relative to project\'s basedir",
111                    l[0].endsWith("/c"));
112             assertEquals("/test", l[1]);
113         } else if (isNetWare) {
114             assertEquals("volumes on NetWare", 1, l.length);
115             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
116         } else {
117             assertEquals("drives on DOS", 1, l.length);
118             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
119         }
120
121         p = new Path(project, "c:\\test;d:\\programs");
122         l = p.list();
123         if (isUnixStyle) {
124             assertEquals("no drives on Unix", 4, l.length);
125             assertTrue("c resolved relative to project\'s basedir",
126                    l[0].endsWith("/c"));
127             assertEquals("/test", l[1]);
128             assertTrue("d resolved relative to project\'s basedir",
129                    l[2].endsWith("/d"));
130             assertEquals("/programs", l[3]);
131         } else if (isNetWare) {
132             assertEquals("volumes on NetWare", 2, l.length);
133             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
134             assertEquals("d:\\programs", l[1].toLowerCase(Locale.US));
135         } else {
136             assertEquals("drives on DOS", 2, l.length);
137             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
138             assertEquals("d:\\programs", l[1].toLowerCase(Locale.US));
139         }
140
141         p = new Path(project, "c:/test");
142         l = p.list();
143         if (isUnixStyle) {
144             assertEquals("no drives on Unix", 2, l.length);
145             assertTrue("c resolved relative to project\'s basedir",
146                    l[0].endsWith("/c"));
147             assertEquals("/test", l[1]);
148         } else if (isNetWare) {
149             assertEquals("volumes on NetWare", 1, l.length);
150             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
151         } else {
152             assertEquals("drives on DOS", 1, l.length);
153             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
154         }
155
156         p = new Path(project, "c:/test;d:/programs");
157         l = p.list();
158         if (isUnixStyle) {
159             assertEquals("no drives on Unix", 4, l.length);
160             assertTrue("c resolved relative to project\'s basedir",
161                    l[0].endsWith("/c"));
162             assertEquals("/test", l[1]);
163             assertTrue("d resolved relative to project\'s basedir",
164                    l[2].endsWith("/d"));
165             assertEquals("/programs", l[3]);
166         } else if (isNetWare) {
167             assertEquals("volumes on NetWare", 2, l.length);
168             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
169             assertEquals("d:\\programs", l[1].toLowerCase(Locale.US));
170         } else {
171             assertEquals("drives on DOS", 2, l.length);
172             assertEquals("c:\\test", l[0].toLowerCase(Locale.US));
173             assertEquals("d:\\programs", l[1].toLowerCase(Locale.US));
174         }
175     }
176
177     public void testConstructorNetWareStyle() {
178         // try a netware-volume length path, see how it is handled
179
Path p = new Path(project, "sys:\\test");
180         String JavaDoc[] l = p.list();
181         if (isUnixStyle) {
182             assertEquals("no drives on Unix", 2, l.length);
183             assertTrue("sys resolved relative to project\'s basedir",
184                    l[0].endsWith("/sys"));
185             assertEquals("/test", l[1]);
186         } else if (isNetWare) {
187             assertEquals("sys:\\test", l[0].toLowerCase(Locale.US));
188             assertEquals("volumes on NetWare", 1, l.length);
189         } else {
190             assertEquals("no multiple character-length volumes on Windows", 2, l.length);
191             assertTrue("sys resolved relative to project\'s basedir",
192                    l[0].endsWith("\\sys"));
193             assertTrue("test resolved relative to project\'s basedir",
194                    l[1].endsWith("\\test"));
195         }
196
197         // try a multi-part netware-volume length path, see how it is handled
198
p = new Path(project, "sys:\\test;dev:\\temp");
199         l = p.list();
200         if (isUnixStyle) {
201             assertEquals("no drives on Unix", 4, l.length);
202             assertTrue("sys resolved relative to project\'s basedir",
203                    l[0].endsWith("/sys"));
204             assertEquals("/test", l[1]);
205             assertTrue("dev resolved relative to project\'s basedir",
206                    l[2].endsWith("/dev"));
207             assertEquals("/temp", l[3]);
208         } else if (isNetWare) {
209             assertEquals("volumes on NetWare", 2, l.length);
210             assertEquals("sys:\\test", l[0].toLowerCase(Locale.US));
211             assertEquals("dev:\\temp", l[1].toLowerCase(Locale.US));
212         } else {
213             assertEquals("no multiple character-length volumes on Windows", 4, l.length);
214             assertTrue("sys resolved relative to project\'s basedir",
215                    l[0].endsWith("\\sys"));
216             assertTrue("test resolved relative to project\'s basedir",
217                    l[1].endsWith("\\test"));
218             assertTrue("dev resolved relative to project\'s basedir",
219                    l[2].endsWith("\\dev"));
220             assertTrue("temp resolved relative to project\'s basedir",
221                    l[3].endsWith("\\temp"));
222         }
223
224         // try a netware-volume length path w/forward slash, see how it is handled
225
p = new Path(project, "sys:/test");
226         l = p.list();
227         if (isUnixStyle) {
228             assertEquals("no drives on Unix", 2, l.length);
229             assertTrue("sys resolved relative to project\'s basedir",
230                    l[0].endsWith("/sys"));
231             assertEquals("/test", l[1]);
232         } else if (isNetWare) {
233             assertEquals("volumes on NetWare", 1, l.length);
234             assertEquals("sys:\\test", l[0].toLowerCase(Locale.US));
235         } else {
236             assertEquals("no multiple character-length volumes on Windows", 2, l.length);
237             assertTrue("sys resolved relative to project\'s basedir",
238                    l[0].endsWith("\\sys"));
239             assertTrue("test resolved relative to project\'s basedir",
240                    l[1].endsWith("\\test"));
241         }
242
243         // try a multi-part netware-volume length path w/forward slash, see how it is handled
244
p = new Path(project, "sys:/test;dev:/temp");
245         l = p.list();
246         if (isUnixStyle) {
247             assertEquals("no drives on Unix", 4, l.length);
248             assertTrue("sys resolved relative to project\'s basedir",
249                    l[0].endsWith("/sys"));
250             assertEquals("/test", l[1]);
251             assertTrue("dev resolved relative to project\'s basedir",
252                    l[2].endsWith("/dev"));
253             assertEquals("/temp", l[3]);
254         } else if (isNetWare) {
255             assertEquals("volumes on NetWare", 2, l.length);
256             assertEquals("sys:\\test", l[0].toLowerCase(Locale.US));
257             assertEquals("dev:\\temp", l[1].toLowerCase(Locale.US));
258         } else {
259             assertEquals("no multiple character-length volumes on Windows", 4, l.length);
260             assertTrue("sys resolved relative to project\'s basedir",
261                    l[0].endsWith("\\sys"));
262             assertTrue("test resolved relative to project\'s basedir",
263                    l[1].endsWith("\\test"));
264             assertTrue("dev resolved relative to project\'s basedir",
265                    l[2].endsWith("\\dev"));
266             assertTrue("temp resolved relative to project\'s basedir",
267                    l[3].endsWith("\\temp"));
268          }
269
270         // try a multi-part netware-volume length path with UNIX
271
// separator (this testcase if from an actual bug that was
272
// found, in AvailableTest, which uses PathTokenizer)
273
p = new Path(project,
274                      "SYS:\\JAVA/lib/rt.jar:SYS:\\JAVA/lib/classes.zip");
275         l = p.list();
276         if (isUnixStyle) {
277             assertEquals("no drives on Unix", 3, l.length);
278             assertTrue("sys resolved relative to project\'s basedir",
279                    l[0].endsWith("/SYS"));
280             assertEquals("/JAVA/lib/rt.jar", l[1]);
281             assertEquals("/JAVA/lib/classes.zip", l[2]);
282         } else if (isNetWare) {
283             assertEquals("volumes on NetWare", 2, l.length);
284             assertEquals("sys:\\java\\lib\\rt.jar", l[0].toLowerCase(Locale.US));
285             assertEquals("sys:\\java\\lib\\classes.zip", l[1].toLowerCase(Locale.US));
286         } else {
287             assertEquals("no multiple character-length volumes on Windows", 3, l.length);
288             assertTrue("sys resolved relative to project\'s basedir",
289                    l[0].endsWith("\\SYS"));
290             assertTrue("java/lib/rt.jar resolved relative to project\'s basedir",
291                    l[1].endsWith("\\JAVA\\lib\\rt.jar"));
292             assertTrue("java/lib/classes.zip resolved relative to project\'s basedir",
293                    l[2].endsWith("\\JAVA\\lib\\classes.zip"));
294         }
295     }
296
297     public void testConstructorMixedStyle() {
298         Path p = new Path(project, "\\a;\\b:/c");
299         String JavaDoc[] l = p.list();
300         assertEquals("three items, mixed style", 3, l.length);
301         if (isUnixStyle) {
302             assertEquals("/a", l[0]);
303             assertEquals("/b", l[1]);
304             assertEquals("/c", l[2]);
305         } else if (isNetWare) {
306             assertEquals("\\a", l[0]);
307             assertEquals("\\b", l[1]);
308             assertEquals("\\c", l[2]);
309         } else {
310             assertEquals(":\\a", l[0].substring(1));
311             assertEquals(":\\b", l[1].substring(1));
312             assertEquals(":\\c", l[2].substring(1));
313         }
314     }
315
316     public void testSetLocation() {
317         Path p = new Path(project);
318         p.setLocation(new File JavaDoc(File.separatorChar+"a"));
319         String JavaDoc[] l = p.list();
320         if (isUnixStyle) {
321             assertEquals(1, l.length);
322             assertEquals("/a", l[0]);
323         } else if (isNetWare) {
324             assertEquals(1, l.length);
325             assertEquals("\\a", l[0]);
326         } else {
327             assertEquals(1, l.length);
328             assertEquals(":\\a", l[0].substring(1));
329         }
330     }
331
332     public void testAppending() {
333         Path p = new Path(project, "/a:/b");
334         String JavaDoc[] l = p.list();
335         assertEquals("2 after construction", 2, l.length);
336         p.setLocation(new File JavaDoc("/c"));
337         l = p.list();
338         assertEquals("3 after setLocation", 3, l.length);
339         p.setPath("\\d;\\e");
340         l = p.list();
341         assertEquals("5 after setPath", 5, l.length);
342         p.append(new Path(project, "\\f"));
343         l = p.list();
344         assertEquals("6 after append", 6, l.length);
345         p.createPath().setLocation(new File JavaDoc("/g"));
346         l = p.list();
347         assertEquals("7 after append", 7, l.length);
348     }
349
350     public void testEmpyPath() {
351         Path p = new Path(project, "");
352         String JavaDoc[] l = p.list();
353         assertEquals("0 after construction", 0, l.length);
354         p.setPath("");
355         l = p.list();
356         assertEquals("0 after setPath", 0, l.length);
357         p.append(new Path(project));
358         l = p.list();
359         assertEquals("0 after append", 0, l.length);
360         p.createPath();
361         l = p.list();
362         assertEquals("0 after append", 0, l.length);
363     }
364
365     public void testUnique() {
366         Path p = new Path(project, "/a:/a");
367         String JavaDoc[] l = p.list();
368         assertEquals("1 after construction", 1, l.length);
369         p.setLocation(new File JavaDoc(File.separatorChar+"a"));
370         l = p.list();
371         assertEquals("1 after setLocation", 1, l.length);
372         p.setPath("\\a;/a");
373         l = p.list();
374         assertEquals("1 after setPath", 1, l.length);
375         p.append(new Path(project, "/a;\\a:\\a"));
376         l = p.list();
377         assertEquals("1 after append", 1, l.length);
378         p.createPath().setPath("\\a:/a");
379         l = p.list();
380         assertEquals("1 after append", 1, l.length);
381     }
382
383     public void testEmptyElementIfIsReference() {
384         Path p = new Path(project, "/a:/a");
385         try {
386             p.setRefid(new Reference("dummyref"));
387             fail("Can add reference to Path with elements from constructor");
388         } catch (BuildException be) {
389             assertEquals("You must not specify more than one attribute when using refid",
390                          be.getMessage());
391         }
392
393         p = new Path(project);
394         p.setLocation(new File JavaDoc("/a"));
395         try {
396             p.setRefid(new Reference("dummyref"));
397             fail("Can add reference to Path with elements from setLocation");
398         } catch (BuildException be) {
399             assertEquals("You must not specify more than one attribute when using refid",
400                          be.getMessage());
401         }
402
403         Path another = new Path(project, "/a:/a");
404         project.addReference("dummyref", another);
405         p = new Path(project);
406         p.setRefid(new Reference("dummyref"));
407         try {
408             p.setLocation(new File JavaDoc("/a"));
409             fail("Can set location in Path that is a reference.");
410         } catch (BuildException be) {
411             assertEquals("You must not specify more than one attribute when using refid",
412                          be.getMessage());
413         }
414
415         try {
416             p.setPath("/a;\\a");
417             fail("Can set path in Path that is a reference.");
418         } catch (BuildException be) {
419             assertEquals("You must not specify more than one attribute when using refid",
420                          be.getMessage());
421         }
422
423         try {
424             p.createPath();
425             fail("Can create nested Path in Path that is a reference.");
426         } catch (BuildException be) {
427             assertEquals("You must not specify nested elements when using refid",
428                          be.getMessage());
429         }
430
431         try {
432             p.createPathElement();
433             fail("Can create nested PathElement in Path that is a reference.");
434         } catch (BuildException be) {
435             assertEquals("You must not specify nested elements when using refid",
436                          be.getMessage());
437         }
438
439         try {
440             p.addFileset(new FileSet());
441             fail("Can add nested FileSet in Path that is a reference.");
442         } catch (BuildException be) {
443             assertEquals("You must not specify nested elements when using refid",
444                          be.getMessage());
445         }
446
447         try {
448             p.addFilelist(new FileList());
449             fail("Can add nested FileList in Path that is a reference.");
450         } catch (BuildException be) {
451             assertEquals("You must not specify nested elements when using refid",
452                          be.getMessage());
453         }
454
455         try {
456             p.addDirset(new DirSet());
457             fail("Can add nested Dirset in Path that is a reference.");
458         } catch (BuildException be) {
459             assertEquals("You must not specify nested elements when using refid",
460                          be.getMessage());
461         }
462     }
463
464     public void testCircularReferenceCheck() {
465         Path p = new Path(project);
466         project.addReference("dummy", p);
467         p.setRefid(new Reference("dummy"));
468         try {
469             p.list();
470             fail("Can make Path a Reference to itself.");
471         } catch (BuildException be) {
472             assertEquals("This data type contains a circular reference.",
473                          be.getMessage());
474         }
475
476         // dummy1 --> dummy2 --> dummy3 --> dummy1
477
Path p1 = new Path(project);
478         project.addReference("dummy1", p1);
479         Path p2 = p1.createPath();
480         project.addReference("dummy2", p2);
481         Path p3 = p2.createPath();
482         project.addReference("dummy3", p3);
483         p3.setRefid(new Reference("dummy1"));
484         try {
485             p1.list();
486             fail("Can make circular reference.");
487         } catch (BuildException be) {
488             assertEquals("This data type contains a circular reference.",
489                          be.getMessage());
490         }
491
492         // dummy1 --> dummy2 --> dummy3 (with Path "/a")
493
p1 = new Path(project);
494         project.addReference("dummy1", p1);
495         p2 = p1.createPath();
496         project.addReference("dummy2", p2);
497         p3 = p2.createPath();
498         project.addReference("dummy3", p3);
499         p3.setLocation(new File JavaDoc("/a"));
500         String JavaDoc[] l = p1.list();
501         assertEquals("One element buried deep inside a nested path structure",
502                      1, l.length);
503         if (isUnixStyle) {
504             assertEquals("/a", l[0]);
505         } else if (isNetWare) {
506             assertEquals("\\a", l[0]);
507         } else {
508             assertEquals(":\\a", l[0].substring(1));
509         }
510     }
511
512     public void testFileList() {
513         Path p = new Path(project);
514         FileList f = new FileList();
515         f.setProject(project);
516         f.setDir(project.resolveFile("."));
517         f.setFiles("build.xml");
518         p.addFilelist(f);
519         String JavaDoc[] l = p.list();
520         assertEquals(1, l.length);
521         assertEquals(project.resolveFile("build.xml").getAbsolutePath(), l[0]);
522     }
523
524     public void testFileSet() {
525         Path p = new Path(project);
526         FileSet f = new FileSet();
527         f.setProject(project);
528         f.setDir(project.resolveFile("."));
529         f.setIncludes("build.xml");
530         p.addFileset(f);
531         String JavaDoc[] l = p.list();
532         assertEquals(1, l.length);
533         assertEquals(project.resolveFile("build.xml").getAbsolutePath(), l[0]);
534     }
535
536     public void testDirSet() {
537         Path p = new Path(project);
538         DirSet d = new DirSet();
539         d.setProject(project);
540         d.setDir(project.resolveFile("."));
541         d.setIncludes("build");
542         p.addDirset(d);
543         String JavaDoc[] l = p.list();
544         assertEquals(1, l.length);
545         assertEquals(project.resolveFile("build").getAbsolutePath(), l[0]);
546     }
547
548 }
549
Popular Tags