~ubuntu-branches/ubuntu/karmic/commons-io/karmic

« back to all changes in this revision

Viewing changes to src/test/org/apache/commons/io/FileCleanerTestCase.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2007-04-13 08:20:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070413082049-2hd3s5ixtxsgvnvm
Tags: 1.3.1.dfsg.1-1
* New upstream (closes: #418973)
* java-gcj-compat-dev and cdbs transition
* Removed junit at the moment (closes: #397567)
* No javadoc at the moment

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
 
 *
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 * 
8
9
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
 
10
 * 
10
11
 * Unless required by applicable law or agreed to in writing, software
11
12
 * distributed under the License is distributed on an "AS IS" BASIS,
12
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
19
import java.io.File;
19
20
import java.io.IOException;
20
21
import java.io.RandomAccessFile;
 
22
import java.lang.ref.ReferenceQueue;
 
23
import java.util.Vector;
21
24
 
22
25
import junit.framework.Test;
23
26
import junit.framework.TestSuite;
31
34
 * @author Noel Bergman
32
35
 * @author Martin Cooper
33
36
 *
34
 
 * @version $Id: FileCleanerTestCase.java,v 1.1 2004/03/18 06:04:14 martinc Exp $
 
37
 * @version $Id: FileCleanerTestCase.java 482437 2006-12-05 01:13:05Z scolebourne $
35
38
 
36
39
 * @see FileCleaner
37
40
 */
61
64
    /** @see junit.framework.TestCase#tearDown() */
62
65
    protected void tearDown() throws Exception {
63
66
        FileUtils.deleteDirectory(getTestDirectory());
64
 
    }
65
 
 
66
 
    /**
67
 
     *  Test the FileCleaner implementation.
68
 
     */
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
 
 
 
67
        
 
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;
 
73
    }
 
74
 
 
75
    //-----------------------------------------------------------------------
 
76
    public void testFileCleanerFile() throws Exception {
 
77
        String path = testFile.getPath();
 
78
        
 
79
        assertEquals(false, testFile.exists());
 
80
        RandomAccessFile r = new RandomAccessFile(testFile, "rw");
 
81
        assertEquals(true, testFile.exists());
 
82
        
 
83
        assertEquals(0, FileCleaner.getTrackCount());
 
84
        FileCleaner.track(path, r);
 
85
        assertEquals(1, FileCleaner.getTrackCount());
 
86
        
 
87
        r.close();
 
88
        testFile = null;
 
89
        r = null;
 
90
 
 
91
        waitUntilTrackCount();
 
92
        
 
93
        assertEquals(0, FileCleaner.getTrackCount());
 
94
        assertEquals(false, new File(path).exists());
 
95
    }
 
96
 
 
97
    public void testFileCleanerDirectory() throws Exception {
 
98
        createFile(testFile, 100);
 
99
        assertEquals(true, testFile.exists());
 
100
        assertEquals(true, getTestDirectory().exists());
 
101
        
 
102
        Object obj = new Object();
 
103
        assertEquals(0, FileCleaner.getTrackCount());
 
104
        FileCleaner.track(getTestDirectory(), obj);
 
105
        assertEquals(1, FileCleaner.getTrackCount());
 
106
        
 
107
        obj = null;
 
108
 
 
109
        waitUntilTrackCount();
 
110
 
 
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
 
114
    }
 
115
 
 
116
    public void testFileCleanerDirectory_NullStrategy() throws Exception {
 
117
        createFile(testFile, 100);
 
118
        assertEquals(true, testFile.exists());
 
119
        assertEquals(true, getTestDirectory().exists());
 
120
        
 
121
        Object obj = new Object();
 
122
        assertEquals(0, FileCleaner.getTrackCount());
 
123
        FileCleaner.track(getTestDirectory(), obj, (FileDeleteStrategy) null);
 
124
        assertEquals(1, FileCleaner.getTrackCount());
 
125
        
 
126
        obj = null;
 
127
 
 
128
        waitUntilTrackCount();
 
129
        
 
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
 
133
    }
 
134
 
 
135
    public void testFileCleanerDirectory_ForceStrategy() throws Exception {
 
136
        createFile(testFile, 100);
 
137
        assertEquals(true, testFile.exists());
 
138
        assertEquals(true, getTestDirectory().exists());
 
139
        
 
140
        Object obj = new Object();
 
141
        assertEquals(0, FileCleaner.getTrackCount());
 
142
        FileCleaner.track(getTestDirectory(), obj, FileDeleteStrategy.FORCE);
 
143
        assertEquals(1, FileCleaner.getTrackCount());
 
144
        
 
145
        obj = null;
 
146
 
 
147
        waitUntilTrackCount();
 
148
        
 
149
        assertEquals(0, FileCleaner.getTrackCount());
 
150
        assertEquals(false, testFile.exists());
 
151
        assertEquals(false, testFile.getParentFile().exists());
 
152
    }
 
153
 
 
154
    public void testFileCleanerNull() throws Exception {
 
155
        try {
 
156
            FileCleaner.track((File) null, new Object());
 
157
            fail();
 
158
        } catch (NullPointerException ex) {
 
159
            // expected
 
160
        }
 
161
        try {
 
162
            FileCleaner.track((File) null, new Object(), FileDeleteStrategy.NORMAL);
 
163
            fail();
 
164
        } catch (NullPointerException ex) {
 
165
            // expected
 
166
        }
 
167
        try {
 
168
            FileCleaner.track((String) null, new Object());
 
169
            fail();
 
170
        } catch (NullPointerException ex) {
 
171
            // expected
 
172
        }
 
173
        try {
 
174
            FileCleaner.track((String) null, new Object(), FileDeleteStrategy.NORMAL);
 
175
            fail();
 
176
        } catch (NullPointerException ex) {
 
177
            // expected
 
178
        }
 
179
    }
 
180
 
 
181
    public void testFileCleanerExitWhenFinishedFirst() throws Exception {
 
182
        assertEquals(false, FileCleaner.exitWhenFinished);
 
183
        FileCleaner.exitWhenFinished();
 
184
        assertEquals(true, FileCleaner.exitWhenFinished);
 
185
        assertEquals(null, FileCleaner.reaper);
 
186
        
 
187
        waitUntilTrackCount();
 
188
        
 
189
        assertEquals(0, FileCleaner.getTrackCount());
 
190
        assertEquals(true, FileCleaner.exitWhenFinished);
 
191
        assertEquals(null, FileCleaner.reaper);
 
192
    }
 
193
 
 
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);
 
199
        
 
200
        String path = testFile.getPath();
 
201
        Object marker = new Object();
 
202
        try {
 
203
            FileCleaner.track(path, marker);
 
204
            fail();
 
205
        } catch (IllegalStateException ex) {
 
206
            // expected
 
207
        }
 
208
        assertEquals(true, FileCleaner.exitWhenFinished);
 
209
        assertEquals(null, FileCleaner.reaper);
 
210
    }
 
211
 
 
212
    public void testFileCleanerExitWhenFinished1() throws Exception {
 
213
        String path = testFile.getPath();
 
214
        
 
215
        assertEquals(false, testFile.exists());
 
216
        RandomAccessFile r = new RandomAccessFile(testFile, "rw");
 
217
        assertEquals(true, testFile.exists());
 
218
        
 
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());
 
224
        
 
225
        assertEquals(false, FileCleaner.exitWhenFinished);
 
226
        FileCleaner.exitWhenFinished();
 
227
        assertEquals(true, FileCleaner.exitWhenFinished);
 
228
        assertEquals(true, FileCleaner.reaper.isAlive());
 
229
        
 
230
        r.close();
 
231
        testFile = null;
 
232
        r = null;
 
233
 
 
234
        waitUntilTrackCount();
 
235
        
 
236
        assertEquals(0, FileCleaner.getTrackCount());
 
237
        assertEquals(false, new File(path).exists());
 
238
        assertEquals(true, FileCleaner.exitWhenFinished);
 
239
        assertEquals(false, FileCleaner.reaper.isAlive());
 
240
    }
 
241
 
 
242
    public void testFileCleanerExitWhenFinished2() throws Exception {
 
243
        String path = testFile.getPath();
 
244
        
 
245
        assertEquals(false, testFile.exists());
 
246
        RandomAccessFile r = new RandomAccessFile(testFile, "rw");
 
247
        assertEquals(true, testFile.exists());
 
248
        
 
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());
 
254
        
 
255
        r.close();
 
256
        testFile = null;
 
257
        r = null;
 
258
 
 
259
        waitUntilTrackCount();
 
260
        
 
261
        assertEquals(0, FileCleaner.getTrackCount());
 
262
        assertEquals(false, new File(path).exists());
 
263
        assertEquals(false, FileCleaner.exitWhenFinished);
 
264
        assertEquals(true, FileCleaner.reaper.isAlive());
 
265
        
 
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
 
270
        }
 
271
        assertEquals(true, FileCleaner.exitWhenFinished);
 
272
        assertEquals(false, FileCleaner.reaper.isAlive());
 
273
    }
 
274
 
 
275
    //-----------------------------------------------------------------------
 
276
    private void waitUntilTrackCount() {
84
277
        while (FileCleaner.getTrackCount() != 0) {
85
 
            System.gc();
 
278
            int total = 0;
 
279
            while (FileCleaner.getTrackCount() != 0) {
 
280
                byte[] b = new byte[1024 * 1024];
 
281
                b[0] = (byte) System.currentTimeMillis();
 
282
                total = total + b[0];
 
283
                System.gc();
 
284
            }
86
285
        }
87
 
 
88
 
        assertTrue("No files tracked", FileCleaner.getTrackCount() == 0);
89
 
        assertFalse("File does not exist", new File(path).exists());
90
286
    }
91
287
}