001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.RandomAccessFile;
022: import java.lang.ref.ReferenceQueue;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026: import junit.textui.TestRunner;
027:
028: import org.apache.commons.io.testtools.FileBasedTestCase;
029:
030: /**
031: * This is used to test {@link FileCleaningTracker} for correctness.
032: *
033: * @author Noel Bergman
034: * @author Martin Cooper
035: *
036: * @version $Id: FileCleanerTestCase.java 482437 2006-12-05 01:13:05Z scolebourne $
037:
038: * @see FileCleaner
039: */
040: public class FileCleaningTrackerTestCase extends FileBasedTestCase {
041: protected FileCleaningTracker newInstance() {
042: return new FileCleaningTracker();
043: }
044:
045: private File testFile;
046: private FileCleaningTracker theInstance;
047:
048: public static void main(String[] args) {
049: TestRunner.run(suite());
050: }
051:
052: public static Test suite() {
053: return new TestSuite(FileCleaningTrackerTestCase.class);
054: }
055:
056: public FileCleaningTrackerTestCase(String name) throws IOException {
057: super (name);
058:
059: testFile = new File(getTestDirectory(), "file-test.txt");
060: }
061:
062: /** @see junit.framework.TestCase#setUp() */
063: protected void setUp() throws Exception {
064: theInstance = newInstance();
065: getTestDirectory().mkdirs();
066: }
067:
068: /** @see junit.framework.TestCase#tearDown() */
069: protected void tearDown() throws Exception {
070: FileUtils.deleteDirectory(getTestDirectory());
071:
072: // reset file cleaner class, so as not to break other tests
073:
074: /**
075: * The following block of code can possibly be removed when the
076: * deprecated {@link FileCleaner} is gone. The question is, whether
077: * we want to support reuse of {@link FileCleaningTracker} instances,
078: * which we should, IMO, not.
079: */
080: {
081: theInstance.q = new ReferenceQueue();
082: theInstance.trackers.clear();
083: theInstance.exitWhenFinished = false;
084: theInstance.reaper = null;
085: }
086:
087: theInstance = null;
088: }
089:
090: //-----------------------------------------------------------------------
091: public void testFileCleanerFile() throws Exception {
092: String path = testFile.getPath();
093:
094: assertEquals(false, testFile.exists());
095: RandomAccessFile r = new RandomAccessFile(testFile, "rw");
096: assertEquals(true, testFile.exists());
097:
098: assertEquals(0, theInstance.getTrackCount());
099: theInstance.track(path, r);
100: assertEquals(1, theInstance.getTrackCount());
101:
102: r.close();
103: testFile = null;
104: r = null;
105:
106: waitUntilTrackCount();
107:
108: assertEquals(0, theInstance.getTrackCount());
109: assertEquals(false, new File(path).exists());
110: }
111:
112: public void testFileCleanerDirectory() throws Exception {
113: createFile(testFile, 100);
114: assertEquals(true, testFile.exists());
115: assertEquals(true, getTestDirectory().exists());
116:
117: Object obj = new Object();
118: assertEquals(0, theInstance.getTrackCount());
119: theInstance.track(getTestDirectory(), obj);
120: assertEquals(1, theInstance.getTrackCount());
121:
122: obj = null;
123:
124: waitUntilTrackCount();
125:
126: assertEquals(0, theInstance.getTrackCount());
127: assertEquals(true, testFile.exists()); // not deleted, as dir not empty
128: assertEquals(true, testFile.getParentFile().exists()); // not deleted, as dir not empty
129: }
130:
131: public void testFileCleanerDirectory_NullStrategy()
132: throws Exception {
133: createFile(testFile, 100);
134: assertEquals(true, testFile.exists());
135: assertEquals(true, getTestDirectory().exists());
136:
137: Object obj = new Object();
138: assertEquals(0, theInstance.getTrackCount());
139: theInstance.track(getTestDirectory(), obj,
140: (FileDeleteStrategy) null);
141: assertEquals(1, theInstance.getTrackCount());
142:
143: obj = null;
144:
145: waitUntilTrackCount();
146:
147: assertEquals(0, theInstance.getTrackCount());
148: assertEquals(true, testFile.exists()); // not deleted, as dir not empty
149: assertEquals(true, testFile.getParentFile().exists()); // not deleted, as dir not empty
150: }
151:
152: public void testFileCleanerDirectory_ForceStrategy()
153: throws Exception {
154: createFile(testFile, 100);
155: assertEquals(true, testFile.exists());
156: assertEquals(true, getTestDirectory().exists());
157:
158: Object obj = new Object();
159: assertEquals(0, theInstance.getTrackCount());
160: theInstance.track(getTestDirectory(), obj,
161: FileDeleteStrategy.FORCE);
162: assertEquals(1, theInstance.getTrackCount());
163:
164: obj = null;
165:
166: waitUntilTrackCount();
167:
168: assertEquals(0, theInstance.getTrackCount());
169: assertEquals(false, testFile.exists());
170: assertEquals(false, testFile.getParentFile().exists());
171: }
172:
173: public void testFileCleanerNull() throws Exception {
174: try {
175: theInstance.track((File) null, new Object());
176: fail();
177: } catch (NullPointerException ex) {
178: // expected
179: }
180: try {
181: theInstance.track((File) null, new Object(),
182: FileDeleteStrategy.NORMAL);
183: fail();
184: } catch (NullPointerException ex) {
185: // expected
186: }
187: try {
188: theInstance.track((String) null, new Object());
189: fail();
190: } catch (NullPointerException ex) {
191: // expected
192: }
193: try {
194: theInstance.track((String) null, new Object(),
195: FileDeleteStrategy.NORMAL);
196: fail();
197: } catch (NullPointerException ex) {
198: // expected
199: }
200: }
201:
202: public void testFileCleanerExitWhenFinishedFirst() throws Exception {
203: assertEquals(false, theInstance.exitWhenFinished);
204: theInstance.exitWhenFinished();
205: assertEquals(true, theInstance.exitWhenFinished);
206: assertEquals(null, theInstance.reaper);
207:
208: waitUntilTrackCount();
209:
210: assertEquals(0, theInstance.getTrackCount());
211: assertEquals(true, theInstance.exitWhenFinished);
212: assertEquals(null, theInstance.reaper);
213: }
214:
215: public void testFileCleanerExitWhenFinished_NoTrackAfter()
216: throws Exception {
217: assertEquals(false, theInstance.exitWhenFinished);
218: theInstance.exitWhenFinished();
219: assertEquals(true, theInstance.exitWhenFinished);
220: assertEquals(null, theInstance.reaper);
221:
222: String path = testFile.getPath();
223: Object marker = new Object();
224: try {
225: theInstance.track(path, marker);
226: fail();
227: } catch (IllegalStateException ex) {
228: // expected
229: }
230: assertEquals(true, theInstance.exitWhenFinished);
231: assertEquals(null, theInstance.reaper);
232: }
233:
234: public void testFileCleanerExitWhenFinished1() throws Exception {
235: String path = testFile.getPath();
236:
237: assertEquals(false, testFile.exists());
238: RandomAccessFile r = new RandomAccessFile(testFile, "rw");
239: assertEquals(true, testFile.exists());
240:
241: assertEquals(0, theInstance.getTrackCount());
242: theInstance.track(path, r);
243: assertEquals(1, theInstance.getTrackCount());
244: assertEquals(false, theInstance.exitWhenFinished);
245: assertEquals(true, theInstance.reaper.isAlive());
246:
247: assertEquals(false, theInstance.exitWhenFinished);
248: theInstance.exitWhenFinished();
249: assertEquals(true, theInstance.exitWhenFinished);
250: assertEquals(true, theInstance.reaper.isAlive());
251:
252: r.close();
253: testFile = null;
254: r = null;
255:
256: waitUntilTrackCount();
257:
258: assertEquals(0, theInstance.getTrackCount());
259: assertEquals(false, new File(path).exists());
260: assertEquals(true, theInstance.exitWhenFinished);
261: assertEquals(false, theInstance.reaper.isAlive());
262: }
263:
264: public void testFileCleanerExitWhenFinished2() throws Exception {
265: String path = testFile.getPath();
266:
267: assertEquals(false, testFile.exists());
268: RandomAccessFile r = new RandomAccessFile(testFile, "rw");
269: assertEquals(true, testFile.exists());
270:
271: assertEquals(0, theInstance.getTrackCount());
272: theInstance.track(path, r);
273: assertEquals(1, theInstance.getTrackCount());
274: assertEquals(false, theInstance.exitWhenFinished);
275: assertEquals(true, theInstance.reaper.isAlive());
276:
277: r.close();
278: testFile = null;
279: r = null;
280:
281: waitUntilTrackCount();
282:
283: assertEquals(0, theInstance.getTrackCount());
284: assertEquals(false, new File(path).exists());
285: assertEquals(false, theInstance.exitWhenFinished);
286: assertEquals(true, theInstance.reaper.isAlive());
287:
288: assertEquals(false, theInstance.exitWhenFinished);
289: theInstance.exitWhenFinished();
290: for (int i = 0; i < 20 && theInstance.reaper.isAlive(); i++) {
291: Thread.sleep(500L); // allow reaper thread to die
292: }
293: assertEquals(true, theInstance.exitWhenFinished);
294: assertEquals(false, theInstance.reaper.isAlive());
295: }
296:
297: //-----------------------------------------------------------------------
298: private void waitUntilTrackCount() {
299: while (theInstance.getTrackCount() != 0) {
300: int total = 0;
301: while (theInstance.getTrackCount() != 0) {
302: byte[] b = new byte[1024 * 1024];
303: b[0] = (byte) System.currentTimeMillis();
304: total = total + b[0];
305: System.gc();
306: }
307: }
308: }
309: }
|