11326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes/* 21326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * Copyright (C) 2009 The Android Open Source Project 3f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 41326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 51326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * you may not use this file except in compliance with the License. 61326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * You may obtain a copy of the License at 7f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 81326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes * 101326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * Unless required by applicable law or agreed to in writing, software 111326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 121326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * See the License for the specific language governing permissions and 141326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes * limitations under the License. 151326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes */ 161326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.io; 181326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes 194557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.io.File; 204557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.io.FileFilter; 214557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.io.FilenameFilter; 224557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.io.IOException; 23c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughesimport java.util.UUID; 24f8c4009feecae2313dae4d1ada9574795af4fa07Elliott Hughesimport libcore.io.Libcore; 25c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes 261326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughespublic class FileTest extends junit.framework.TestCase { 271326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes private static File createTemporaryDirectory() throws Exception { 281326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes String base = System.getProperty("java.io.tmpdir"); 29c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes File directory = new File(base, UUID.randomUUID().toString()); 30c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertTrue(directory.mkdirs()); 31c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes return directory; 321326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 33f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 341326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes private static String longString(int n) { 351326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes StringBuilder result = new StringBuilder(); 361326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes for (int i = 0; i < n; ++i) { 371326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes result.append('x'); 381326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 391326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes return result.toString(); 401326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 41f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 421326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes private static File createDeepStructure(File base) throws Exception { 431326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // ext has a limit of around 256 characters for each path entry. 441326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // 128 characters should be safe for everything but FAT. 451326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes String longString = longString(128); 461326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // Keep creating subdirectories until the path length is greater than 1KiB. 471326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // Ubuntu 8.04's kernel is happy up to about 4KiB. 481326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes File f = base; 491326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes for (int i = 0; f.toString().length() <= 1024; ++i) { 501326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes f = new File(f, longString); 511326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertTrue(f.mkdir()); 521326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 531326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes return f; 541326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 55f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 561326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // Rather than test all methods, assume that if createTempFile creates a long path and 571326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // exists can see it, the code for coping with long paths (shared by all methods) works. 581326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes public void test_longPath() throws Exception { 591326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes File base = createTemporaryDirectory(); 601326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertTrue(createDeepStructure(base).exists()); 611326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 62f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 63dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath /* 64dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * readlink(2) is a special case,. 65dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * 66dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * This test assumes you can create symbolic links in the temporary directory. This 67dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * isn't true on Android if you're using /sdcard (which is used if this test is 68dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * run using vogar). It will work in /data/data/ though. 69dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath */ 701326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes public void test_longReadlink() throws Exception { 711326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes File base = createTemporaryDirectory(); 721326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes File target = createDeepStructure(base); 731326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes File source = new File(base, "source"); 741326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertFalse(source.exists()); 751326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertTrue(target.exists()); 761326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertTrue(target.getCanonicalPath().length() > 1024); 774d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes ln_s(target, source); 781326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertTrue(source.exists()); 791326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes assertEquals(target.getCanonicalPath(), source.getCanonicalPath()); 801326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes } 81f33eae7e84eb6d3b0f4e86b59605bb3de73009f3Elliott Hughes 821326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // TODO: File.list is a special case too, but I haven't fixed it yet, and the new code, 831326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes // like the old code, will die of a native buffer overrun if we exercise it. 84c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes 85c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes public void test_emptyFilename() throws Exception { 86c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes // The behavior of the empty filename is an odd mixture. 87c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes File f = new File(""); 88c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes // Mostly it behaves like an invalid path... 8908ec8fd5c950cb94e12aefa08c89d78762acf18aElliott Hughes assertFalse(f.canExecute()); 90c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.canRead()); 91c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.canWrite()); 92c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes try { 93c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes f.createNewFile(); 94c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes fail("expected IOException"); 95c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes } catch (IOException expected) { 96c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes } 97c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.delete()); 98c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes f.deleteOnExit(); 99c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.exists()); 100c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals("", f.getName()); 101c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.getParent()); 102c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.getParentFile()); 103c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals("", f.getPath()); 104c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.isAbsolute()); 105c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.isDirectory()); 106c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.isFile()); 107c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.isHidden()); 108c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(0, f.lastModified()); 109c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(0, f.length()); 110c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.list()); 111c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.list(null)); 112c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.listFiles()); 113c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.listFiles((FileFilter) null)); 114c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(null, f.listFiles((FilenameFilter) null)); 115c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.mkdir()); 116c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.mkdirs()); 117c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.renameTo(f)); 118c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.setLastModified(123)); 11908ec8fd5c950cb94e12aefa08c89d78762acf18aElliott Hughes assertFalse(f.setExecutable(true)); 120c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertFalse(f.setReadOnly()); 12108ec8fd5c950cb94e12aefa08c89d78762acf18aElliott Hughes assertFalse(f.setReadable(true)); 12208ec8fd5c950cb94e12aefa08c89d78762acf18aElliott Hughes assertFalse(f.setWritable(true)); 123c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes // ...but sometimes it behaves like "user.dir". 124c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes String cwd = System.getProperty("user.dir"); 125c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(new File(cwd), f.getAbsoluteFile()); 126c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes assertEquals(cwd, f.getAbsolutePath()); 1274d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes // TODO: how do we test these without hard-coding assumptions about where our temporary 1284d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes // directory is? (In practice, on Android, our temporary directory is accessed through 1294d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes // a symbolic link, so the canonical file/path will be different.) 1304d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes //assertEquals(new File(cwd), f.getCanonicalFile()); 1314d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes //assertEquals(cwd, f.getCanonicalPath()); 132c5c9c667028146ab5f5e446c44f911c2fdd7dd30Elliott Hughes } 133028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes 134028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes // http://b/2486943 - between eclair and froyo, we added a call to 135028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes // isAbsolute from the File constructor, potentially breaking subclasses. 136028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes public void test_subclassing() throws Exception { 137028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes class MyFile extends File { 138028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes private String field; 139028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes MyFile(String s) { 140028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes super(s); 141028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes field = ""; 142028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes } 143028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes @Override public boolean isAbsolute() { 144028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes field.length(); 145028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes return super.isAbsolute(); 146028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes } 147028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes } 148028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes new MyFile(""); 149028794d8a42840233d3cb316feb09b5593f19d1dElliott Hughes } 1504d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes 151dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath /* 152dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * http://b/3047893 - getCanonicalPath wasn't actually resolving symbolic links. 153dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * 154dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * This test assumes you can create symbolic links in the temporary directory. This 155dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * isn't true on Android if you're using /sdcard (which is used if this test is 156dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath * run using vogar). It will work in /data/data/ though. 157dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath */ 1584d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes public void test_getCanonicalPath() throws Exception { 1594d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes File base = createTemporaryDirectory(); 1604d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes File target = new File(base, "target"); 1614d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes target.createNewFile(); // The RI won't follow a dangling symlink, which seems like a bug! 1624d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes File linkName = new File(base, "link"); 1634d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes ln_s(target, linkName); 1644d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes assertEquals(target.getCanonicalPath(), linkName.getCanonicalPath()); 1654d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes 1664d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes // .../subdir/shorter -> .../target (using a link to ../target). 1674d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes File subdir = new File(base, "subdir"); 1684d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes assertTrue(subdir.mkdir()); 1694d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes linkName = new File(subdir, "shorter"); 1704d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes ln_s("../target", linkName.toString()); 1714d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes assertEquals(target.getCanonicalPath(), linkName.getCanonicalPath()); 1724d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes 1734d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes // .../l -> .../subdir/longer (using a relative link to subdir/longer). 1744d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes linkName = new File(base, "l"); 1754d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes ln_s("subdir/longer", linkName.toString()); 1764d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes File longer = new File(base, "subdir/longer"); 1774d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes longer.createNewFile(); // The RI won't follow a dangling symlink, which seems like a bug! 1784d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes assertEquals(longer.getCanonicalPath(), linkName.getCanonicalPath()); 1794d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes 1804d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes // .../double -> .../target (via a link into subdir and a link back out). 1814d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes linkName = new File(base, "double"); 1824d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes ln_s("subdir/shorter", linkName.toString()); 1834d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes assertEquals(target.getCanonicalPath(), linkName.getCanonicalPath()); 1844d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes } 1854d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes 1869b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes private static void ln_s(File target, File linkName) throws Exception { 1874d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes ln_s(target.toString(), linkName.toString()); 1884d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes } 1894d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes 1909b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes private static void ln_s(String target, String linkName) throws Exception { 191a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes Libcore.os.symlink(target, linkName); 1924d2023ebe0ead25681ce350a873728dfee86b577Elliott Hughes } 1932ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes 1942ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes public void test_createNewFile() throws Exception { 195a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes File f = File.createTempFile("FileTest", "tmp"); 1962ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes assertFalse(f.createNewFile()); // EEXIST -> false 1972ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes assertFalse(f.getParentFile().createNewFile()); // EEXIST -> false, even if S_ISDIR 1982ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes try { 1992ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes new File(f, "poop").createNewFile(); // ENOTDIR -> throw 2002ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes fail(); 2012ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes } catch (IOException expected) { 2022ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes } 2032ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes try { 2042ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes new File("").createNewFile(); // ENOENT -> throw 2052ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes fail(); 2062ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes } catch (IOException expected) { 2072ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes } 2082ffce92141c0b2a5f0543229f3ded34b0ee79ba1Elliott Hughes } 209a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes 210a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes public void test_rename() throws Exception { 211a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes File f = File.createTempFile("FileTest", "tmp"); 212a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes assertFalse(f.renameTo(new File(""))); 213a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes assertFalse(new File("").renameTo(f)); 214a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes assertFalse(f.renameTo(new File("."))); 215a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes assertTrue(f.renameTo(f)); 216a20cc6fca30d18e05db67ceeb0403b7b58ffd364Elliott Hughes } 2176c937923e20679d0752cea1ee5e2e969a094acdbElliott Hughes 2186c937923e20679d0752cea1ee5e2e969a094acdbElliott Hughes public void test_getAbsolutePath() throws Exception { 219dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath String userDir = System.getProperty("user.dir"); 220e10c61bb13373b95ba857cc3af60f94b9941ab9eNarayan Kamath if (!userDir.endsWith(File.separator)) { 221e10c61bb13373b95ba857cc3af60f94b9941ab9eNarayan Kamath userDir = userDir + File.separator; 222e10c61bb13373b95ba857cc3af60f94b9941ab9eNarayan Kamath } 223e10c61bb13373b95ba857cc3af60f94b9941ab9eNarayan Kamath 224dcac4295743e14811d1ab3282d07855ede17ccb2Narayan Kamath File f = new File("poop"); 225e10c61bb13373b95ba857cc3af60f94b9941ab9eNarayan Kamath assertEquals(userDir + "poop", f.getAbsolutePath()); 2266c937923e20679d0752cea1ee5e2e969a094acdbElliott Hughes } 2270ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughes 2280ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughes public void test_getSpace() throws Exception { 2290ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughes assertTrue(new File("/").getFreeSpace() >= 0); 2300ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughes assertTrue(new File("/").getTotalSpace() >= 0); 2310ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughes assertTrue(new File("/").getUsableSpace() >= 0); 2320ade5524d972b9dfd69b0126aa4c35319c1b7fa4Elliott Hughes } 233fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes 234fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes public void test_mkdirs() throws Exception { 235fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // Set up a directory to test in. 236fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File base = createTemporaryDirectory(); 237fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes 238fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // mkdirs returns true only if it _creates_ a directory. 239fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // So we get false for a directory that already exists... 240fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(base.exists()); 241fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(base.mkdirs()); 242fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // But true if we had to create something. 243fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File a = new File(base, "a"); 244fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(a.exists()); 245fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(a.mkdirs()); 246fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(a.exists()); 247fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes 248fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // Test the recursive case where we need to create multiple parents. 249fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File b = new File(a, "b"); 250fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File c = new File(b, "c"); 251fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File d = new File(c, "d"); 252fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(a.exists()); 253fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(b.exists()); 254fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(c.exists()); 255fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(d.exists()); 256fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(d.mkdirs()); 257fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(a.exists()); 258fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(b.exists()); 259fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(c.exists()); 260fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(d.exists()); 261fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes 262fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // Test the case where the 'directory' exists as a file. 263fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File existsAsFile = new File(base, "existsAsFile"); 264fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes existsAsFile.createNewFile(); 265fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(existsAsFile.exists()); 266fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(existsAsFile.mkdirs()); 267fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes 268fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes // Test the case where the parent exists as a file. 269fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes File badParent = new File(existsAsFile, "sub"); 270fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertTrue(existsAsFile.exists()); 271fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(badParent.exists()); 272fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes assertFalse(badParent.mkdirs()); 273fec51846c02116a6ac02fac8ecf8ffb705a74b36Elliott Hughes } 2741326cfc6f105f6c8fd5ffd83793223e1a797409dElliott Hughes} 275