FileUtilsTestpublic class FileUtilsTest extends android.test.AndroidTestCase
Fields Summary |
---|
private static final String | TEST_DATA | private File | mTestFile | private File | mCopyFile |
Methods Summary |
---|
protected void | setUp()
super.setUp();
File testDir = getContext().getDir("testing", Context.MODE_PRIVATE);
mTestFile = new File(testDir, "test.file");
mCopyFile = new File(testDir, "copy.file");
FileWriter writer = new FileWriter(mTestFile);
try {
writer.write(TEST_DATA, 0, TEST_DATA.length());
} finally {
writer.close();
}
| protected void | tearDown()
if (mTestFile.exists()) mTestFile.delete();
if (mCopyFile.exists()) mCopyFile.delete();
| public void | testCopyFile()
assertFalse(mCopyFile.exists());
FileUtils.copyFile(mTestFile, mCopyFile);
assertTrue(mCopyFile.exists());
assertEquals(TEST_DATA, FileUtils.readTextFile(mCopyFile, 0, null));
| public void | testCopyToFile()
final String s = "Foo Bar";
assertFalse(mCopyFile.exists());
FileUtils.copyToFile(new ByteArrayInputStream(s.getBytes()), mCopyFile); assertTrue(mCopyFile.exists());
assertEquals(s, FileUtils.readTextFile(mCopyFile, 0, null));
| public void | testGetFileStatus()
final byte[] MAGIC = { 0xB, 0xE, 0x0, 0x5 };
try {
// truncate test file and write MAGIC (4 bytes) to it.
FileOutputStream os = new FileOutputStream(mTestFile, false);
os.write(MAGIC, 0, 4);
os.flush();
os.close();
} catch (FileNotFoundException e) {
Assert.fail("File was removed durning test" + e);
} catch (IOException e) {
Assert.fail("Unexpected IOException: " + e);
}
Assert.assertTrue(mTestFile.exists());
Assert.assertTrue(FileUtils.getFileStatus(mTestFile.getPath(), null));
FileStatus status1 = new FileStatus();
FileUtils.getFileStatus(mTestFile.getPath(), status1);
Assert.assertEquals(4, status1.size);
// Sleep for at least one second so that the modification time will be different.
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
try {
// append so we don't change the creation time.
FileOutputStream os = new FileOutputStream(mTestFile, true);
os.write(MAGIC, 0, 4);
os.flush();
os.close();
} catch (FileNotFoundException e) {
Assert.fail("File was removed durning test" + e);
} catch (IOException e) {
Assert.fail("Unexpected IOException: " + e);
}
FileStatus status2 = new FileStatus();
FileUtils.getFileStatus(mTestFile.getPath(), status2);
Assert.assertEquals(8, status2.size);
Assert.assertTrue(status2.mtime > status1.mtime);
mTestFile.delete();
Assert.assertFalse(mTestFile.exists());
Assert.assertFalse(FileUtils.getFileStatus(mTestFile.getPath(), null));
| public void | testIsFilenameSafe()
assertTrue(FileUtils.isFilenameSafe(new File("foobar")));
assertTrue(FileUtils.isFilenameSafe(new File("a_b-c=d.e/0,1+23")));
assertFalse(FileUtils.isFilenameSafe(new File("foo*bar")));
assertFalse(FileUtils.isFilenameSafe(new File("foo\nbar")));
| public void | testReadTextFile()
assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 0, null));
assertEquals("ABCDE", FileUtils.readTextFile(mTestFile, 5, null));
assertEquals("ABCDE<>", FileUtils.readTextFile(mTestFile, 5, "<>"));
assertEquals(TEST_DATA.substring(0, 51) + "<>",
FileUtils.readTextFile(mTestFile, 51, "<>"));
assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 52, "<>"));
assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, 100, "<>"));
assertEquals("vwxyz", FileUtils.readTextFile(mTestFile, -5, null));
assertEquals("<>vwxyz", FileUtils.readTextFile(mTestFile, -5, "<>"));
assertEquals("<>" + TEST_DATA.substring(1, 52),
FileUtils.readTextFile(mTestFile, -51, "<>"));
assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -52, "<>"));
assertEquals(TEST_DATA, FileUtils.readTextFile(mTestFile, -100, "<>"));
| public void | testReadTextFileWithZeroLengthFile()
new FileOutputStream(mTestFile).close(); // Zero out the file
assertEquals("", FileUtils.readTextFile(mTestFile, 0, null));
assertEquals("", FileUtils.readTextFile(mTestFile, 1, "<>"));
assertEquals("", FileUtils.readTextFile(mTestFile, 10, "<>"));
assertEquals("", FileUtils.readTextFile(mTestFile, -1, "<>"));
assertEquals("", FileUtils.readTextFile(mTestFile, -10, "<>"));
|
|