1 4 5 9 10 package org.openlaszlo.utils; 11 import java.io.*; 12 import java.util.*; 13 import junit.framework.*; 14 15 24 25 public class FileUtils_Test extends TestCase { 26 public FileUtils_Test(String 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 assertEquals("split 1", 56 Arrays.asList(StringUtils.split("", "/")), 57 Arrays.asList(new String [] {})); 58 assertEquals("split 2", 59 Arrays.asList(StringUtils.split("a", "/")), 60 Arrays.asList(new String [] {"a"})); 61 assertEquals("split 3", 62 Arrays.asList(StringUtils.split("a/b", "/")), 63 Arrays.asList(new String [] {"a", "b"})); 64 assertEquals("split 4", 65 Arrays.asList(StringUtils.split("/a", "/")), 66 Arrays.asList(new String [] {"", "a"})); 67 assertEquals("split 5", 68 Arrays.asList(StringUtils.split("a/", "/")), 69 Arrays.asList(new String [] {"a", ""})); 70 assertEquals("split 6", 71 Arrays.asList(StringUtils.split("/a/", "/")), 72 Arrays.asList(new String [] {"", "a", ""})); 73 assertEquals("split 7", 74 Arrays.asList(StringUtils.split("a//b", "/")), 75 Arrays.asList(new String [] {"a", "", "b"})); 76 assertEquals("split 7b", 77 Arrays.asList(StringUtils.split("a///b", "/")), 78 Arrays.asList(new String [] {"a", "", "", "b"})); 79 assertEquals("split 8", 80 Arrays.asList(StringUtils.split("//a", "/")), 81 Arrays.asList(new String [] {"", "", "a"})); 82 assertEquals("split 9", 83 Arrays.asList(StringUtils.split("a//", "/")), 84 Arrays.asList(new String [] {"a", "", ""})); 85 for (Iterator iter = Arrays.asList(new String [] { 86 "", "a", "/a", "a/b", "/a/b", 87 "/", "a/", "/a/", "a/b/", "/a/b/"}) 88 .iterator(); iter.hasNext();) { 89 String s = (String ) iter.next(); 90 assertEquals("split/join(" + s + ")", 91 StringUtils.join(StringUtils.split(s, "/"), "/"), 92 s); 93 if (s.equals("/") || !s.endsWith("/")) { 95 assertEquals("toURLPath(" + s + ")", 96 FileUtils.toURLPath(new File(s)), s); 97 } 98 } 99 String tests[] = { 100 "", "", "base.ext", "base.ext", "from", "to", "base.ext", "../to/base.ext", "from1/from2", "to1/to2", "base.ext", "../../to1/to2/base.ext", "from1", "to1/to2", "base.ext", "../to1/to2/base.ext", "from1/from2", "to", "base.ext", "../../to/base.ext", "/a/b/c", "/a/b/c", "base.ext", "base.ext", "/a/b/c", "/a/d/e", "base.ext", "../../d/e/base.ext", "/a/b", "/a/d/e", "base.ext", "../d/e/base.ext", "/a/b/c", "/a/d", "base.ext", "../../d/base.ext", "c:/a/b/c", "c:/a/b/c", "base.ext", "base.ext", "c:/a/b/c", "c:/a/d/e", "base.ext", "../../d/e/base.ext", "c:/a/b", "c:/a/d/e", "base.ext", "../d/e/base.ext", "c:/a/b/c", "c:/a/d", "base.ext", "../../d/base.ext", 115 ".", "", "base.ext", "base.ext", "", ".", "base.ext", "base.ext", ".", ".", "base.ext", "base.ext", }; 119 int i = 0; 120 while (i < tests.length) { 121 String fromdir = tests[i++]; 122 String todir = tests[i++]; 123 String fromfile = tests[i++]; 124 String tofile = tests[i++]; 125 for (int j = 0; j < 3; j++) { 126 String fromdir_ = fromdir; 127 if ((j & 1) != 0) 128 fromdir_ += "/"; 129 String 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 if (false) { 143 String errTests[] = { 144 "c:/", "d:/", 145 "c:/", "", 146 "", "d:/" 147 }; 148 i = 0; 149 while (i < errTests.length) { 150 String sourcedir = errTests[i++]; 151 String destdir = errTests[i++]; 152 try { 153 FileUtils.adjustRelativePath( 154 "base.ext", sourcedir, destdir); 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 |