1 16 package org.apache.commons.io; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.io.RandomAccessFile ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 import org.apache.commons.io.testtools.FileBasedTestCase; 27 28 38 public class FileCleanerTestCase extends FileBasedTestCase { 39 40 private File testFile; 41 42 public static void main(String [] args) { 43 TestRunner.run(suite()); 44 } 45 46 public static Test suite() { 47 return new TestSuite(FileCleanerTestCase.class); 48 } 49 50 public FileCleanerTestCase(String name) throws IOException { 51 super(name); 52 53 testFile = new File (getTestDirectory(), "file-test.txt"); 54 } 55 56 57 protected void setUp() throws Exception { 58 getTestDirectory().mkdirs(); 59 } 60 61 62 protected void tearDown() throws Exception { 63 FileUtils.deleteDirectory(getTestDirectory()); 64 } 65 66 69 public void testFileCleaner() throws Exception { 70 String path = testFile.getPath(); 71 72 assertFalse("File does not exist", testFile.exists()); 73 RandomAccessFile r = new RandomAccessFile (testFile, "rw"); 74 assertTrue("File exists", testFile.exists()); 75 76 assertTrue("No files tracked", FileCleaner.getTrackCount() == 0); 77 FileCleaner.track(path, r); 78 assertTrue("One file tracked", FileCleaner.getTrackCount() == 1); 79 80 r.close(); 81 testFile = null; 82 r = null; 83 84 while (FileCleaner.getTrackCount() != 0) { 85 System.gc(); 86 } 87 88 assertTrue("No files tracked", FileCleaner.getTrackCount() == 0); 89 assertFalse("File does not exist", new File (path).exists()); 90 } 91 } 92 | Popular Tags |