OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / core / tests / coretests / src / android / os / FileUtilsTest.java
1 /*
2  * Copyright (C) 2008 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
17 package android.os;
18
19 import android.content.Context;
20 import android.os.FileUtils;
21 import android.os.FileUtils.FileStatus;
22 import android.test.AndroidTestCase;
23 import android.test.suitebuilder.annotation.LargeTest;
24 import android.test.suitebuilder.annotation.MediumTest;
25 import android.test.suitebuilder.annotation.SmallTest;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.FileWriter;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33
34 import junit.framework.Assert;
35
36 public class FileUtilsTest extends AndroidTestCase {
37     private static final String TEST_DATA =
38             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
39
40     private File mTestFile;
41     private File mCopyFile;
42
43     @Override
44     protected void setUp() throws Exception {
45         super.setUp();
46         File testDir = getContext().getDir("testing", Context.MODE_PRIVATE);
47         mTestFile = new File(testDir, "test.file");
48         mCopyFile = new File(testDir, "copy.file");
49         FileWriter writer = new FileWriter(mTestFile);
50         try {
51             writer.write(TEST_DATA, 0, TEST_DATA.length());
52         } finally {
53             writer.close();
54         }
55     }
56
57     @Override
58     protected void tearDown() throws Exception {
59         if (mTestFile.exists()) mTestFile.delete();
60         if (mCopyFile.exists()) mCopyFile.delete();
61     }
62
63     @LargeTest
64     public void testGetFileStatus() {
65         final byte[] MAGIC = { 0xB, 0xE, 0x0, 0x5 };
66
67         try {
68             // truncate test file and write MAGIC (4 bytes) to it.
69             FileOutputStream os = new FileOutputStream(mTestFile, false);
70             os.write(MAGIC, 0, 4);
71             os.flush();
72             os.close();
73         } catch (FileNotFoundException e) {
74             Assert.fail("File was removed durning test" + e);
75         } catch (IOException e) {
76             Assert.fail("Unexpected IOException: " + e);
77         }
78         
79         Assert.assertTrue(mTestFile.exists());
80         Assert.assertTrue(FileUtils.getFileStatus(mTestFile.getPath(), null));
81         
82         FileStatus status1 = new FileStatus();
83         FileUtils.getFileStatus(mTestFile.getPath(), status1);
84         
85         Assert.assertEquals(4, status1.size);
86         
87         // Sleep for at least one second so that the modification time will be different.
88         try {
89             Thread.sleep(1000);
90         } catch (InterruptedException e) {
91         }
92
93         try {
94             // append so we don't change the creation time.
95             FileOutputStream os = new FileOutputStream(mTestFile, true);
96             os.write(MAGIC, 0, 4);
97             os.flush();
98             os.close();
99         } catch (FileNotFoundException e) {
100             Assert.fail("File was removed durning test" + e);
101         } catch (IOException e) {
102             Assert.fail("Unexpected IOException: " + e);
103         }
104         
105         FileStatus status2 = new FileStatus();
106         FileUtils.getFileStatus(mTestFile.getPath(), status2);
107         
108         Assert.assertEquals(8, status2.size);
109         Assert.assertTrue(status2.mtime > status1.mtime);
110         
111         mTestFile.delete();
112         
113         Assert.assertFalse(mTestFile.exists());
114         Assert.assertFalse(FileUtils.getFileStatus(mTestFile.getPath(), null));
115     }
116
117     // TODO: test setPermissions(), getPermissions()
118
119     @MediumTest
120     public void testCopyFile() throws Exception {
121         assertFalse(mCopyFile.exists());
122         FileUtils.copyFile(mTestFile, mCopyFile);
123         assertTrue(mCopyFile.exists());
124         assertEquals(TEST_DATA, FileUtils.readTextFile(mCopyFile, 0, null));
125     }
126
127     @MediumTest
128     public void testCopyToFile() throws Exception {
129         final String s = "Foo Bar";
130         assertFalse(mCopyFile.exists());
131         FileUtils.copyToFile(new ByteArrayInputStream(s.getBytes()), mCopyFile);        assertTrue(mCopyFile.exists());
132         assertEquals(s, FileUtils.readTextFile(mCopyFile, 0, null));
133     }
134
135     @MediumTest
136     public void testIsFilenameSafe() throws Exception {
137         assertTrue(FileUtils.isFilenameSafe(new File("foobar")));
138         assertTrue(FileUtils.isFilenameSafe(new File("a_b-c=d.e/0,1+23")));
139         assertFalse(FileUtils.isFilenameSafe(new File("foo*bar")));
140         assertFalse(FileUtils.isFilenameSafe(new File("foo\nbar")));
141     }
142
143     @MediumTest
144     public void testReadTextFile() throws Exception {
145         assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 0, null));
146
147         assertEquals("ABCDE", FileUtils.readTextFile(mTestFile, 5, null));
148         assertEquals("ABCDE<>", FileUtils.readTextFile(mTestFile, 5, "<>"));
149         assertEquals(TEST_DATA.substring(0, 51) + "<>",
150                 FileUtils.readTextFile(mTestFile, 51, "<>"));
151         assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 52, "<>"));
152         assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 100, "<>"));
153
154         assertEquals("vwxyz", FileUtils.readTextFile(mTestFile, -5, null));
155         assertEquals("<>vwxyz", FileUtils.readTextFile(mTestFile, -5, "<>"));
156         assertEquals("<>" + TEST_DATA.substring(1, 52),
157                 FileUtils.readTextFile(mTestFile, -51, "<>"));
158         assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -52, "<>"));
159         assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -100, "<>"));
160     }
161
162     @MediumTest
163     public void testReadTextFileWithZeroLengthFile() throws Exception {
164         new FileOutputStream(mTestFile).close();  // Zero out the file
165         assertEquals("", FileUtils.readTextFile(mTestFile, 0, null));
166         assertEquals("", FileUtils.readTextFile(mTestFile, 1, "<>"));
167         assertEquals("", FileUtils.readTextFile(mTestFile, 10, "<>"));
168         assertEquals("", FileUtils.readTextFile(mTestFile, -1, "<>"));
169         assertEquals("", FileUtils.readTextFile(mTestFile, -10, "<>"));
170     }
171 }