KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > utils > FileUtils_Test


1 /* ****************************************************************************
2  * FileUtils_Test.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.utils;
11 import java.io.*;
12 import java.util.*;
13 import junit.framework.*;
14
15 /*
16  * junit.awtui.TestRunner
17  * junit.swingui.TestRunner
18  * junit.textui.TestRunner
19  *
20  * java junit.textui.TestRunner org.openlaszlo.utils.FileUtils_Test
21  *
22  * @author Oliver Steele
23  */

24
25 public class FileUtils_Test extends TestCase {
26     public FileUtils_Test(String JavaDoc name) {
27         super(name);
28     }
29
30     public void setUp() {
31     }
32
33     public void testPathnameFunctions() {
34         assertEquals("getBase 1", FileUtils.getBase("base"), "base");
35         assertEquals("getBase 2", FileUtils.getBase("base.ext"), "base");
36         assertEquals("getBase 3", FileUtils.getBase("dir/base"), "dir/base");
37         assertEquals("getBase 4", FileUtils.getBase("dir/base.ext"), "dir/base");
38         assertEquals("getExtension 1", FileUtils.getExtension("base"), "");
39         assertEquals("getExtension 1", FileUtils.getExtension("base.ext"), "ext");
40         assertEquals("getExtension 2", FileUtils.getExtension("dir/base"), "");
41         assertEquals("getExtension 3", FileUtils.getExtension("dir/base.ext"), "ext");
42     }
43
44     public void textFixAbsWindowsPaths() {
45         assertEquals("fixAbs 1", FileUtils.fixAbsWindowsPaths("a/b/c"), "a/b/c");
46         assertEquals("fixAbs 2", FileUtils.fixAbsWindowsPaths("/a/b/c"), "/a/b/c");
47         assertEquals("fixAbs 3", FileUtils.fixAbsWindowsPaths("a:/b/c"), "a/b/c");
48         assertEquals("fixAbs 4", FileUtils.fixAbsWindowsPaths("A:/b/c"), "a/b/c");
49         assertEquals("fixAbs 5", FileUtils.fixAbsWindowsPaths("A:"), "");
50         assertEquals("fixAbs 6", FileUtils.fixAbsWindowsPaths("A"), "");
51     }
52
53     public void testAdjustRelativePath() {
54         // Test split first, since everything else depends on it.
55
assertEquals("split 1",
56                      Arrays.asList(StringUtils.split("", "/")),
57                      Arrays.asList(new String JavaDoc[] {}));
58         assertEquals("split 2",
59                      Arrays.asList(StringUtils.split("a", "/")),
60                      Arrays.asList(new String JavaDoc[] {"a"}));
61         assertEquals("split 3",
62                      Arrays.asList(StringUtils.split("a/b", "/")),
63                      Arrays.asList(new String JavaDoc[] {"a", "b"}));
64         assertEquals("split 4",
65                      Arrays.asList(StringUtils.split("/a", "/")),
66                      Arrays.asList(new String JavaDoc[] {"", "a"}));
67         assertEquals("split 5",
68                      Arrays.asList(StringUtils.split("a/", "/")),
69                      Arrays.asList(new String JavaDoc[] {"a", ""}));
70         assertEquals("split 6",
71                      Arrays.asList(StringUtils.split("/a/", "/")),
72                      Arrays.asList(new String JavaDoc[] {"", "a", ""}));
73         assertEquals("split 7",
74                      Arrays.asList(StringUtils.split("a//b", "/")),
75                      Arrays.asList(new String JavaDoc[] {"a", "", "b"}));
76         assertEquals("split 7b",
77                      Arrays.asList(StringUtils.split("a///b", "/")),
78                      Arrays.asList(new String JavaDoc[] {"a", "", "", "b"}));
79         assertEquals("split 8",
80                      Arrays.asList(StringUtils.split("//a", "/")),
81                      Arrays.asList(new String JavaDoc[] {"", "", "a"}));
82         assertEquals("split 9",
83                      Arrays.asList(StringUtils.split("a//", "/")),
84                      Arrays.asList(new String JavaDoc[] {"a", "", ""}));
85         for (Iterator iter = Arrays.asList(new String JavaDoc[] {
86             "", "a", "/a", "a/b", "/a/b",
87             "/", "a/", "/a/", "a/b/", "/a/b/"})
88                  .iterator(); iter.hasNext();) {
89             String JavaDoc s = (String JavaDoc) iter.next();
90             assertEquals("split/join(" + s + ")",
91                          StringUtils.join(StringUtils.split(s, "/"), "/"),
92                          s);
93             // File("a/") turns into File("a"), so don't even bother
94
if (s.equals("/") || !s.endsWith("/")) {
95                 assertEquals("toURLPath(" + s + ")",
96                              FileUtils.toURLPath(new File(s)), s);
97             }
98         }
99         String JavaDoc tests[] = {
100             // sourcedir, destdir, source, dest
101
"", "", "base.ext", "base.ext", // 1
102
"from", "to", "base.ext", "../to/base.ext", // 2
103
"from1/from2", "to1/to2", "base.ext", "../../to1/to2/base.ext", // 3
104
"from1", "to1/to2", "base.ext", "../to1/to2/base.ext", // 4
105
"from1/from2", "to", "base.ext", "../../to/base.ext", // 5
106
"/a/b/c", "/a/b/c", "base.ext", "base.ext", // 6
107
"/a/b/c", "/a/d/e", "base.ext", "../../d/e/base.ext", // 7
108
"/a/b", "/a/d/e", "base.ext", "../d/e/base.ext", // 8
109
"/a/b/c", "/a/d", "base.ext", "../../d/base.ext", // 9
110
"c:/a/b/c", "c:/a/b/c", "base.ext", "base.ext", // 10
111
"c:/a/b/c", "c:/a/d/e", "base.ext", "../../d/e/base.ext", // 11
112
"c:/a/b", "c:/a/d/e", "base.ext", "../d/e/base.ext", // 12
113
"c:/a/b/c", "c:/a/d", "base.ext", "../../d/base.ext", // 13
114

115             ".", "", "base.ext", "base.ext", // 14
116
"", ".", "base.ext", "base.ext", // 15
117
".", ".", "base.ext", "base.ext", // 16
118
};
119         int i = 0;
120         while (i < tests.length) {
121             String JavaDoc fromdir = tests[i++];
122             String JavaDoc todir = tests[i++];
123             String JavaDoc fromfile = tests[i++];
124             String JavaDoc tofile = tests[i++];
125             for (int j = 0; j < 3; j++) {
126                 String JavaDoc fromdir_ = fromdir;
127                 if ((j & 1) != 0)
128                     fromdir_ += "/";
129                 String JavaDoc todir_ = todir;
130                 if ((j & 2) != 0)
131                     todir_ += "/";
132                 if (i != 0 && (fromdir_.equals("/") || todir_.equals("/")))
133                     continue;
134                 assertEquals(""+ i/4 + " adjustRelativePath(" + fromfile +
135                              "," + fromdir_ + ", " + todir_ + ")",
136                              FileUtils.adjustRelativePath(
137                                  fromfile, fromdir_, todir_), tofile);
138             }
139         }
140
141         // TODO [ows 11/18/02] run this on Windows
142
if (false) {
143             String JavaDoc errTests[] = {
144                 "c:/", "d:/",
145                 "c:/", "",
146                 "", "d:/"
147             };
148             i = 0;
149             while (i < errTests.length) {
150                 String JavaDoc sourcedir = errTests[i++];
151                 String JavaDoc destdir = errTests[i++];
152                 try {
153                     FileUtils.adjustRelativePath(
154                         "base.ext", sourcedir, destdir); // for effect
155
fail("adjustRelativePath(..., " + sourcedir + ", " +
156                          destdir + ") " +
157                          "should have thrown an exception");
158                 } catch (FileUtils.RelativizationError e) {}
159             }
160         }
161     }
162
163     public void testRelativePath() {
164         assertEquals("relpath 1", FileUtils.relativePath("C:\\foo\\bar\\foobar", "C:\\foo\\bar"),
165                                   "/foobar");
166         assertEquals("relpath 2", FileUtils.relativePath("C:\\foo\\bar\\foobar", "C:\\foo\\bar\\"),
167                                   "/foobar");
168         assertEquals("relpath 2", FileUtils.relativePath("C:\\fxoo\\bar\\foobar", "C:\\foo\\bar\\"),
169                                   "C:/fxoo/bar/foobar");
170     }
171
172 }
173
Popular Tags