FileTest.java revision fa2ad81d0b839083c711f5f0ef17030af3000f69
1/*
2 * Copyright (C) 2009 The Android Open Source Project
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
17package java.io;
18
19import java.util.UUID;
20
21import junit.framework.Test;
22import junit.framework.TestSuite;
23
24public class FileTest extends junit.framework.TestCase {
25    private static File createTemporaryDirectory() throws Exception {
26        String base = System.getProperty("java.io.tmpdir");
27        File directory = new File(base, UUID.randomUUID().toString());
28        assertTrue(directory.mkdirs());
29        return directory;
30    }
31
32    private static String longString(int n) {
33        StringBuilder result = new StringBuilder();
34        for (int i = 0; i < n; ++i) {
35            result.append('x');
36        }
37        return result.toString();
38    }
39
40    private static File createDeepStructure(File base) throws Exception {
41        // ext has a limit of around 256 characters for each path entry.
42        // 128 characters should be safe for everything but FAT.
43        String longString = longString(128);
44        // Keep creating subdirectories until the path length is greater than 1KiB.
45        // Ubuntu 8.04's kernel is happy up to about 4KiB.
46        File f = base;
47        for (int i = 0; f.toString().length() <= 1024; ++i) {
48            f = new File(f, longString);
49            assertTrue(f.mkdir());
50        }
51        return f;
52    }
53
54    // Rather than test all methods, assume that if createTempFile creates a long path and
55    // exists can see it, the code for coping with long paths (shared by all methods) works.
56    public void test_longPath() throws Exception {
57        File base = createTemporaryDirectory();
58        assertTrue(createDeepStructure(base).exists());
59    }
60
61    // readlink(2) is a special case,.
62    public void test_longReadlink() throws Exception {
63        File base = createTemporaryDirectory();
64        File target = createDeepStructure(base);
65        File source = new File(base, "source");
66        assertFalse(source.exists());
67        assertTrue(target.exists());
68        assertTrue(target.getCanonicalPath().length() > 1024);
69        Runtime.getRuntime().exec(new String[] { "ln", "-s", target.toString(), source.toString() }).waitFor();
70        assertTrue(source.exists());
71        assertEquals(target.getCanonicalPath(), source.getCanonicalPath());
72    }
73
74    // TODO: File.list is a special case too, but I haven't fixed it yet, and the new code,
75    // like the old code, will die of a native buffer overrun if we exercise it.
76
77    public void test_emptyFilename() throws Exception {
78        // The behavior of the empty filename is an odd mixture.
79        File f = new File("");
80        // Mostly it behaves like an invalid path...
81        assertFalse(f.canRead());
82        assertFalse(f.canWrite());
83        try {
84            f.createNewFile();
85            fail("expected IOException");
86        } catch (IOException expected) {
87        }
88        assertFalse(f.delete());
89        f.deleteOnExit();
90        assertFalse(f.exists());
91        assertEquals("", f.getName());
92        assertEquals(null, f.getParent());
93        assertEquals(null, f.getParentFile());
94        assertEquals("", f.getPath());
95        assertFalse(f.isAbsolute());
96        assertFalse(f.isDirectory());
97        assertFalse(f.isFile());
98        assertFalse(f.isHidden());
99        assertEquals(0, f.lastModified());
100        assertEquals(0, f.length());
101        assertEquals(null, f.list());
102        assertEquals(null, f.list(null));
103        assertEquals(null, f.listFiles());
104        assertEquals(null, f.listFiles((FileFilter) null));
105        assertEquals(null, f.listFiles((FilenameFilter) null));
106        assertFalse(f.mkdir());
107        assertFalse(f.mkdirs());
108        assertFalse(f.renameTo(f));
109        assertFalse(f.setLastModified(123));
110        assertFalse(f.setReadOnly());
111        // ...but sometimes it behaves like "user.dir".
112        String cwd = System.getProperty("user.dir");
113        assertEquals(new File(cwd), f.getAbsoluteFile());
114        assertEquals(cwd, f.getAbsolutePath());
115        assertEquals(new File(cwd), f.getCanonicalFile());
116        assertEquals(cwd, f.getCanonicalPath());
117    }
118
119    // http://b/2486943 - between eclair and froyo, we added a call to
120    // isAbsolute from the File constructor, potentially breaking subclasses.
121    public void test_subclassing() throws Exception {
122        class MyFile extends File {
123            private String field;
124            MyFile(String s) {
125                super(s);
126                field = "";
127            }
128            @Override public boolean isAbsolute() {
129                field.length();
130                return super.isAbsolute();
131            }
132        }
133        new MyFile("");
134    }
135}
136