Source Code Cross Referenced for FileCleaningTrackerTestCase.java in  » Library » apache-common-IO » org » apache » commons » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common IO » org.apache.commons.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.