KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > FileCleanerTestCase


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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 package org.apache.commons.io;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.RandomAccessFile JavaDoc;
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 /**
29  * This is used to test FileCleaner for correctness.
30  *
31  * @author Noel Bergman
32  * @author Martin Cooper
33  *
34  * @version $Id: FileCleanerTestCase.java,v 1.1 2004/03/18 06:04:14 martinc Exp $
35
36  * @see FileCleaner
37  */

38 public class FileCleanerTestCase extends FileBasedTestCase {
39
40     private File JavaDoc testFile;
41
42     public static void main(String JavaDoc[] args) {
43         TestRunner.run(suite());
44     }
45
46     public static Test suite() {
47         return new TestSuite(FileCleanerTestCase.class);
48     }
49
50     public FileCleanerTestCase(String JavaDoc name) throws IOException JavaDoc {
51         super(name);
52
53         testFile = new File JavaDoc(getTestDirectory(), "file-test.txt");
54     }
55
56     /** @see junit.framework.TestCase#setUp() */
57     protected void setUp() throws Exception JavaDoc {
58         getTestDirectory().mkdirs();
59     }
60
61     /** @see junit.framework.TestCase#tearDown() */
62     protected void tearDown() throws Exception JavaDoc {
63         FileUtils.deleteDirectory(getTestDirectory());
64     }
65
66     /**
67      * Test the FileCleaner implementation.
68      */

69     public void testFileCleaner() throws Exception JavaDoc {
70         String JavaDoc path = testFile.getPath();
71
72         assertFalse("File does not exist", testFile.exists());
73         RandomAccessFile JavaDoc r = new RandomAccessFile JavaDoc(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 JavaDoc(path).exists());
90     }
91 }
92
Popular Tags