1 17 18 package org.apache.tools.ant.util; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 24 import junit.framework.TestCase; 25 26 import org.apache.tools.ant.BuildException; 27 import org.apache.tools.ant.taskdefs.condition.Os; 28 29 33 public class FileUtilsTest extends TestCase { 34 35 private FileUtils fu; 36 private File removeThis; 37 private String root; 38 39 public FileUtilsTest(String name) { 40 super(name); 41 } 42 43 public void setUp() { 44 fu = FileUtils.newFileUtils(); 45 root = new File (File.separator).getAbsolutePath().toUpperCase(); 47 } 48 49 public void tearDown() { 50 if (removeThis != null && removeThis.exists()) { 51 removeThis.delete(); 52 } 53 } 54 55 public void testSetLastModified() throws IOException { 56 removeThis = new File ("dummy"); 57 FileOutputStream fos = new FileOutputStream (removeThis); 58 fos.write(new byte[0]); 59 fos.close(); 60 long modTime = removeThis.lastModified(); 61 assertTrue(modTime != 0); 62 63 68 try { 69 Thread.sleep(5000); 70 } catch (InterruptedException ie) { 71 fail(ie.getMessage()); 72 } 73 74 fu.setFileLastModified(removeThis, -1); 75 long secondModTime = removeThis.lastModified(); 76 assertTrue(secondModTime > modTime); 77 78 final int millisperday=24 * 3600 * 1000; 80 fu.setFileLastModified(removeThis, secondModTime + millisperday); 84 long thirdModTime = removeThis.lastModified(); 85 91 assertTrue(thirdModTime != secondModTime); 92 } 93 94 public void testResolveFile() { 95 98 assertEquals(File.separator, 99 fu.resolveFile(null, "/").getPath()); 100 assertEquals(File.separator, 101 fu.resolveFile(null, "\\").getPath()); 102 103 if (!Os.isFamily("unix")) { 104 107 String driveSpec = "C:"; 108 assertEquals(driveSpec + "\\", 109 fu.resolveFile(null, driveSpec + "/").getPath()); 110 assertEquals(driveSpec + "\\", 111 fu.resolveFile(null, driveSpec + "\\").getPath()); 112 String driveSpecLower = "c:"; 113 assertEquals(driveSpec + "\\", 114 fu.resolveFile(null, driveSpecLower + "/").getPath()); 115 assertEquals(driveSpec + "\\", 116 fu.resolveFile(null, driveSpecLower + "\\").getPath()); 117 120 assertEquals(driveSpec + "\\", 121 fu.resolveFile(null, driveSpec + "/////").getPath()); 122 assertEquals(driveSpec + "\\", 123 fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); 124 } 125 126 if (Os.isFamily("netware")) { 127 130 String driveSpec = "SYS:"; 131 assertEquals(driveSpec, 132 fu.resolveFile(null, driveSpec + "/").getPath()); 133 assertEquals(driveSpec, 134 fu.resolveFile(null, driveSpec + "\\").getPath()); 135 String driveSpecLower = "sys:"; 136 assertEquals(driveSpec, 137 fu.resolveFile(null, driveSpecLower + "/").getPath()); 138 assertEquals(driveSpec, 139 fu.resolveFile(null, driveSpecLower + "\\").getPath()); 140 143 assertEquals(driveSpec, 144 fu.resolveFile(null, driveSpec + "/////").getPath()); 145 assertEquals(driveSpec, 146 fu.resolveFile(null, driveSpec + "\\\\\\\\\\\\").getPath()); 147 } 148 149 152 assertEquals(localize("/1/2/3/4"), 153 fu.resolveFile(new File (localize("/1/2/3")), "4").getPath()); 154 assertEquals(localize("/1/2/3/4"), 155 fu.resolveFile(new File (localize("/1/2/3")), "./4").getPath()); 156 assertEquals(localize("/1/2/3/4"), 157 fu.resolveFile(new File (localize("/1/2/3")), ".\\4").getPath()); 158 assertEquals(localize("/1/2/3/4"), 159 fu.resolveFile(new File (localize("/1/2/3")), "./.\\4").getPath()); 160 assertEquals(localize("/1/2/3/4"), 161 fu.resolveFile(new File (localize("/1/2/3")), "../3/4").getPath()); 162 assertEquals(localize("/1/2/3/4"), 163 fu.resolveFile(new File (localize("/1/2/3")), "..\\3\\4").getPath()); 164 assertEquals(localize("/1/2/3/4"), 165 fu.resolveFile(new File (localize("/1/2/3")), "../../5/.././2/./3/6/../4").getPath()); 166 assertEquals(localize("/1/2/3/4"), 167 fu.resolveFile(new File (localize("/1/2/3")), "..\\../5/..\\./2/./3/6\\../4").getPath()); 168 169 try { 170 fu.resolveFile(new File (localize("/1")), "../../b"); 171 fail("successfully crawled beyond the filesystem root"); 172 } catch (BuildException e) { 173 } 175 176 } 177 178 public void testNormalize() { 179 182 assertEquals(File.separator, 183 fu.normalize("/").getPath()); 184 assertEquals(File.separator, 185 fu.normalize("\\").getPath()); 186 187 if (!Os.isFamily("unix")) { 188 191 String driveSpec = "C:"; 192 assertEquals(driveSpec, 193 fu.normalize(driveSpec).getPath()); 194 assertEquals(driveSpec + "\\", 195 fu.normalize(driveSpec + "/").getPath()); 196 assertEquals(driveSpec + "\\", 197 fu.normalize(driveSpec + "\\").getPath()); 198 String driveSpecLower = "c:"; 199 assertEquals(driveSpec + "\\", 200 fu.normalize(driveSpecLower + "/").getPath()); 201 assertEquals(driveSpec + "\\", 202 fu.normalize(driveSpecLower + "\\").getPath()); 203 206 assertEquals(driveSpec + "\\", 207 fu.normalize(driveSpec + "/////").getPath()); 208 assertEquals(driveSpec + "\\", 209 fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); 210 } 211 if (Os.isFamily("netware")) { 212 215 String driveSpec = "SYS:"; 216 assertEquals(driveSpec, 217 fu.normalize(driveSpec).getPath()); 218 assertEquals(driveSpec, 219 fu.normalize(driveSpec + "/").getPath()); 220 assertEquals(driveSpec, 221 fu.normalize(driveSpec + "\\").getPath()); 222 String driveSpecLower = "sys:"; 223 assertEquals(driveSpec, 224 fu.normalize(driveSpecLower).getPath()); 225 assertEquals(driveSpec, 226 fu.normalize(driveSpecLower + "/").getPath()); 227 assertEquals(driveSpec, 228 fu.normalize(driveSpecLower + "\\").getPath()); 229 assertEquals(driveSpec + "\\junk", 230 fu.normalize(driveSpecLower + "\\junk").getPath()); 231 234 assertEquals(driveSpec, 235 fu.normalize(driveSpec + "/////").getPath()); 236 assertEquals(driveSpec, 237 fu.normalize(driveSpec + "\\\\\\\\\\\\").getPath()); 238 } 239 240 243 assertEquals(localize("/1/2/3/4"), 244 fu.normalize(localize("/1/2/3/4")).getPath()); 245 assertEquals(localize("/1/2/3/4"), 246 fu.normalize(localize("/1/2/3/./4")).getPath()); 247 assertEquals(localize("/1/2/3/4"), 248 fu.normalize(localize("/1/2/3/.\\4")).getPath()); 249 assertEquals(localize("/1/2/3/4"), 250 fu.normalize(localize("/1/2/3/./.\\4")).getPath()); 251 assertEquals(localize("/1/2/3/4"), 252 fu.normalize(localize("/1/2/3/../3/4")).getPath()); 253 assertEquals(localize("/1/2/3/4"), 254 fu.normalize(localize("/1/2/3/..\\3\\4")).getPath()); 255 assertEquals(localize("/1/2/3/4"), 256 fu.normalize(localize("/1/2/3/../../5/.././2/./3/6/../4")).getPath()); 257 assertEquals(localize("/1/2/3/4"), 258 fu.normalize(localize("/1/2/3/..\\../5/..\\./2/./3/6\\../4")).getPath()); 259 260 try { 261 fu.normalize("foo"); 262 fail("foo is not an absolute path"); 263 } catch (BuildException e) { 264 } 266 267 try { 268 fu.normalize(localize("/1/../../b")); 269 fail("successfully crawled beyond the filesystem root"); 270 } catch (BuildException e) { 271 } 273 } 274 275 278 public void testNullArgs() { 279 try { 280 fu.normalize(null); 281 fail("successfully normalized a null-file"); 282 } catch (NullPointerException npe) { 283 } 285 286 File f = fu.resolveFile(null, "a"); 287 assertEquals(f, new File ("a")); 288 } 289 290 293 public void testCreateTempFile() { 294 File parent = new File ((new File ("/tmp")).getAbsolutePath()); 295 File tmp1 = fu.createTempFile("pre", ".suf", parent); 296 assertTrue("new file", !tmp1.exists()); 297 298 String name = tmp1.getName(); 299 assertTrue("starts with pre", name.startsWith("pre")); 300 assertTrue("ends with .suf", name.endsWith(".suf")); 301 assertEquals("is inside parent dir", 302 parent.getAbsolutePath(), 303 tmp1.getParent()); 304 305 File tmp2 = fu.createTempFile("pre", ".suf", parent); 306 assertTrue("files are different", 307 !tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath())); 308 309 File tmp3 = fu.createTempFile("pre", ".suf", null); 311 String tmploc = System.getProperty("java.io.tmpdir"); 312 assertEquals((new File (tmploc, tmp3.getName())).getAbsolutePath(), 313 tmp3.getAbsolutePath()); 314 } 315 316 319 public void testContentEquals() throws IOException { 320 assertTrue("Non existing files", fu.contentEquals(new File ("foo"), 321 new File ("bar"))); 322 assertTrue("One exists, the other one doesn\'t", 323 !fu.contentEquals(new File ("foo"), new File ("build.xml"))); 324 assertTrue("Don\'t compare directories", 325 !fu.contentEquals(new File ("src"), new File ("src"))); 326 assertTrue("File equals itself", 327 fu.contentEquals(new File ("build.xml"), 328 new File ("build.xml"))); 329 assertTrue("Files are different", 330 !fu.contentEquals(new File ("build.xml"), 331 new File ("docs.xml"))); 332 } 333 334 337 public void testCreateNewFile() throws IOException { 338 removeThis = new File ("dummy"); 339 assertTrue(!removeThis.exists()); 340 fu.createNewFile(removeThis); 341 assertTrue(removeThis.exists()); 342 } 343 344 347 public void testRemoveLeadingPath() { 348 assertEquals("bar", fu.removeLeadingPath(new File ("/foo"), 349 new File ("/foo/bar"))); 350 assertEquals("bar", fu.removeLeadingPath(new File ("/foo/"), 351 new File ("/foo/bar"))); 352 assertEquals("bar", fu.removeLeadingPath(new File ("\\foo"), 353 new File ("\\foo\\bar"))); 354 assertEquals("bar", fu.removeLeadingPath(new File ("\\foo\\"), 355 new File ("\\foo\\bar"))); 356 assertEquals("bar", fu.removeLeadingPath(new File ("c:/foo"), 357 new File ("c:/foo/bar"))); 358 assertEquals("bar", fu.removeLeadingPath(new File ("c:/foo/"), 359 new File ("c:/foo/bar"))); 360 assertEquals("bar", fu.removeLeadingPath(new File ("c:\\foo"), 361 new File ("c:\\foo\\bar"))); 362 assertEquals("bar", fu.removeLeadingPath(new File ("c:\\foo\\"), 363 new File ("c:\\foo\\bar"))); 364 assertEqualsIgnoreDriveCase(fu.normalize("/bar").getAbsolutePath(), 365 fu.removeLeadingPath(new File ("/foo"), new File ("/bar"))); 366 assertEqualsIgnoreDriveCase(fu.normalize("/foobar").getAbsolutePath(), 367 fu.removeLeadingPath(new File ("/foo"), new File ("/foobar"))); 368 assertEquals("", fu.removeLeadingPath(new File ("/foo/bar"), 370 new File ("/foo/bar"))); 371 assertEquals("", fu.removeLeadingPath(new File ("/foo/bar"), 372 new File ("/foo/bar/"))); 373 assertEquals("", fu.removeLeadingPath(new File ("/foo/bar/"), 374 new File ("/foo/bar/"))); 375 assertEquals("", fu.removeLeadingPath(new File ("/foo/bar/"), 376 new File ("/foo/bar"))); 377 378 String expected = "foo/bar".replace('\\', File.separatorChar) 379 .replace('/', File.separatorChar); 380 assertEquals(expected, fu.removeLeadingPath(new File ("/"), 381 new File ("/foo/bar"))); 382 assertEquals(expected, fu.removeLeadingPath(new File ("c:/"), 383 new File ("c:/foo/bar"))); 384 assertEquals(expected, fu.removeLeadingPath(new File ("c:\\"), 385 new File ("c:\\foo\\bar"))); 386 } 387 388 391 public void testToURI() { 392 String dosRoot = null; 393 if (Os.isFamily("dos")) { 394 dosRoot = System.getProperty("user.dir").charAt(0) + ":/"; 395 } 396 else 397 { 398 dosRoot = ""; 399 } 400 if (Os.isFamily("dos")) { 401 assertEquals("file:///C:/foo", fu.toURI("c:\\foo")); 402 } 403 if (Os.isFamily("netware")) { 404 assertEquals("file:///SYS:/foo", fu.toURI("sys:\\foo")); 405 } 406 assertEquals("file:///" + dosRoot + "foo", fu.toURI("/foo")); 407 assertEquals("file:./foo", fu.toURI("./foo")); 408 assertEquals("file:///" + dosRoot + "foo", fu.toURI("\\foo")); 409 assertEquals("file:./foo", fu.toURI(".\\foo")); 410 assertEquals("file:///" + dosRoot + "foo%20bar", fu.toURI("/foo bar")); 411 assertEquals("file:///" + dosRoot + "foo%20bar", fu.toURI("\\foo bar")); 412 assertEquals("file:///" + dosRoot + "foo%23bar", fu.toURI("/foo#bar")); 413 assertEquals("file:///" + dosRoot + "foo%23bar", fu.toURI("\\foo#bar")); 414 } 415 416 419 public void testFromURI() { 420 if (Os.isFamily("netware")) { 421 assertEqualsIgnoreDriveCase("SYS:\\foo", fu.fromURI("file:///sys:/foo")); 422 } 423 if (Os.isFamily("dos")) { 424 assertEqualsIgnoreDriveCase("C:\\foo", fu.fromURI("file:///c:/foo")); 425 } 426 assertEqualsIgnoreDriveCase(localize("/foo"), fu.fromURI("file:///foo")); 427 assertEquals("." + File.separator + "foo", 428 fu.fromURI("file:./foo")); 429 assertEqualsIgnoreDriveCase(localize("/foo bar"), fu.fromURI("file:///foo%20bar")); 430 assertEqualsIgnoreDriveCase(localize("/foo#bar"), fu.fromURI("file:///foo%23bar")); 431 } 432 433 436 private String localize(String path) { 437 path = root + path.substring(1); 438 return path.replace('\\', File.separatorChar).replace('/', File.separatorChar); 439 } 440 446 private void assertEqualsIgnoreDriveCase(String s1, String s2) { 447 if (Os.isFamily("dos") && s1.length()>=1 && s2.length()>=1) { 448 StringBuffer sb1= new StringBuffer (s1); 449 StringBuffer sb2= new StringBuffer (s2); 450 sb1.setCharAt(0,Character.toUpperCase(s1.charAt(0))); 451 sb2.setCharAt(0,Character.toUpperCase(s2.charAt(0))); 452 assertEquals(sb1.toString(),sb2.toString()); 453 } else { 454 assertEquals(s1,s2); 455 } 456 } 457 } 458 | Popular Tags |