61
64
/** @see junit.framework.TestCase#tearDown() */
62
65
protected void tearDown() throws Exception {
63
66
FileUtils.deleteDirectory(getTestDirectory());
67
* Test the FileCleaner implementation.
69
public void testFileCleaner() throws Exception {
70
String path = testFile.getPath();
72
assertFalse("File does not exist", testFile.exists());
73
RandomAccessFile r = new RandomAccessFile(testFile, "rw");
74
assertTrue("File exists", testFile.exists());
76
assertTrue("No files tracked", FileCleaner.getTrackCount() == 0);
77
FileCleaner.track(path, r);
78
assertTrue("One file tracked", FileCleaner.getTrackCount() == 1);
68
// reset file cleaner class, so as not to break other tests
69
FileCleaner.q = new ReferenceQueue();
70
FileCleaner.trackers = new Vector();
71
FileCleaner.exitWhenFinished = false;
72
FileCleaner.reaper = null;
75
//-----------------------------------------------------------------------
76
public void testFileCleanerFile() throws Exception {
77
String path = testFile.getPath();
79
assertEquals(false, testFile.exists());
80
RandomAccessFile r = new RandomAccessFile(testFile, "rw");
81
assertEquals(true, testFile.exists());
83
assertEquals(0, FileCleaner.getTrackCount());
84
FileCleaner.track(path, r);
85
assertEquals(1, FileCleaner.getTrackCount());
91
waitUntilTrackCount();
93
assertEquals(0, FileCleaner.getTrackCount());
94
assertEquals(false, new File(path).exists());
97
public void testFileCleanerDirectory() throws Exception {
98
createFile(testFile, 100);
99
assertEquals(true, testFile.exists());
100
assertEquals(true, getTestDirectory().exists());
102
Object obj = new Object();
103
assertEquals(0, FileCleaner.getTrackCount());
104
FileCleaner.track(getTestDirectory(), obj);
105
assertEquals(1, FileCleaner.getTrackCount());
109
waitUntilTrackCount();
111
assertEquals(0, FileCleaner.getTrackCount());
112
assertEquals(true, testFile.exists()); // not deleted, as dir not empty
113
assertEquals(true, testFile.getParentFile().exists()); // not deleted, as dir not empty
116
public void testFileCleanerDirectory_NullStrategy() throws Exception {
117
createFile(testFile, 100);
118
assertEquals(true, testFile.exists());
119
assertEquals(true, getTestDirectory().exists());
121
Object obj = new Object();
122
assertEquals(0, FileCleaner.getTrackCount());
123
FileCleaner.track(getTestDirectory(), obj, (FileDeleteStrategy) null);
124
assertEquals(1, FileCleaner.getTrackCount());
128
waitUntilTrackCount();
130
assertEquals(0, FileCleaner.getTrackCount());
131
assertEquals(true, testFile.exists()); // not deleted, as dir not empty
132
assertEquals(true, testFile.getParentFile().exists()); // not deleted, as dir not empty
135
public void testFileCleanerDirectory_ForceStrategy() throws Exception {
136
createFile(testFile, 100);
137
assertEquals(true, testFile.exists());
138
assertEquals(true, getTestDirectory().exists());
140
Object obj = new Object();
141
assertEquals(0, FileCleaner.getTrackCount());
142
FileCleaner.track(getTestDirectory(), obj, FileDeleteStrategy.FORCE);
143
assertEquals(1, FileCleaner.getTrackCount());
147
waitUntilTrackCount();
149
assertEquals(0, FileCleaner.getTrackCount());
150
assertEquals(false, testFile.exists());
151
assertEquals(false, testFile.getParentFile().exists());
154
public void testFileCleanerNull() throws Exception {
156
FileCleaner.track((File) null, new Object());
158
} catch (NullPointerException ex) {
162
FileCleaner.track((File) null, new Object(), FileDeleteStrategy.NORMAL);
164
} catch (NullPointerException ex) {
168
FileCleaner.track((String) null, new Object());
170
} catch (NullPointerException ex) {
174
FileCleaner.track((String) null, new Object(), FileDeleteStrategy.NORMAL);
176
} catch (NullPointerException ex) {
181
public void testFileCleanerExitWhenFinishedFirst() throws Exception {
182
assertEquals(false, FileCleaner.exitWhenFinished);
183
FileCleaner.exitWhenFinished();
184
assertEquals(true, FileCleaner.exitWhenFinished);
185
assertEquals(null, FileCleaner.reaper);
187
waitUntilTrackCount();
189
assertEquals(0, FileCleaner.getTrackCount());
190
assertEquals(true, FileCleaner.exitWhenFinished);
191
assertEquals(null, FileCleaner.reaper);
194
public void testFileCleanerExitWhenFinished_NoTrackAfter() throws Exception {
195
assertEquals(false, FileCleaner.exitWhenFinished);
196
FileCleaner.exitWhenFinished();
197
assertEquals(true, FileCleaner.exitWhenFinished);
198
assertEquals(null, FileCleaner.reaper);
200
String path = testFile.getPath();
201
Object marker = new Object();
203
FileCleaner.track(path, marker);
205
} catch (IllegalStateException ex) {
208
assertEquals(true, FileCleaner.exitWhenFinished);
209
assertEquals(null, FileCleaner.reaper);
212
public void testFileCleanerExitWhenFinished1() throws Exception {
213
String path = testFile.getPath();
215
assertEquals(false, testFile.exists());
216
RandomAccessFile r = new RandomAccessFile(testFile, "rw");
217
assertEquals(true, testFile.exists());
219
assertEquals(0, FileCleaner.getTrackCount());
220
FileCleaner.track(path, r);
221
assertEquals(1, FileCleaner.getTrackCount());
222
assertEquals(false, FileCleaner.exitWhenFinished);
223
assertEquals(true, FileCleaner.reaper.isAlive());
225
assertEquals(false, FileCleaner.exitWhenFinished);
226
FileCleaner.exitWhenFinished();
227
assertEquals(true, FileCleaner.exitWhenFinished);
228
assertEquals(true, FileCleaner.reaper.isAlive());
234
waitUntilTrackCount();
236
assertEquals(0, FileCleaner.getTrackCount());
237
assertEquals(false, new File(path).exists());
238
assertEquals(true, FileCleaner.exitWhenFinished);
239
assertEquals(false, FileCleaner.reaper.isAlive());
242
public void testFileCleanerExitWhenFinished2() throws Exception {
243
String path = testFile.getPath();
245
assertEquals(false, testFile.exists());
246
RandomAccessFile r = new RandomAccessFile(testFile, "rw");
247
assertEquals(true, testFile.exists());
249
assertEquals(0, FileCleaner.getTrackCount());
250
FileCleaner.track(path, r);
251
assertEquals(1, FileCleaner.getTrackCount());
252
assertEquals(false, FileCleaner.exitWhenFinished);
253
assertEquals(true, FileCleaner.reaper.isAlive());
259
waitUntilTrackCount();
261
assertEquals(0, FileCleaner.getTrackCount());
262
assertEquals(false, new File(path).exists());
263
assertEquals(false, FileCleaner.exitWhenFinished);
264
assertEquals(true, FileCleaner.reaper.isAlive());
266
assertEquals(false, FileCleaner.exitWhenFinished);
267
FileCleaner.exitWhenFinished();
268
for (int i = 0; i < 20 && FileCleaner.reaper.isAlive(); i++) {
269
Thread.sleep(500L); // allow reaper thread to die
271
assertEquals(true, FileCleaner.exitWhenFinished);
272
assertEquals(false, FileCleaner.reaper.isAlive());
275
//-----------------------------------------------------------------------
276
private void waitUntilTrackCount() {
84
277
while (FileCleaner.getTrackCount() != 0) {
279
while (FileCleaner.getTrackCount() != 0) {
280
byte[] b = new byte[1024 * 1024];
281
b[0] = (byte) System.currentTimeMillis();
282
total = total + b[0];
88
assertTrue("No files tracked", FileCleaner.getTrackCount() == 0);
89
assertFalse("File does not exist", new File(path).exists());