Source Code Cross Referenced for TestDirectoryUtil.java in  » ESB » open-esb » com » sun » jbi » management » internal » support » 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 » ESB » open esb » com.sun.jbi.management.internal.support 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * BEGIN_HEADER - DO NOT EDIT
003:         *
004:         * The contents of this file are subject to the terms
005:         * of the Common Development and Distribution License
006:         * (the "License").  You may not use this file except
007:         * in compliance with the License.
008:         *
009:         * You can obtain a copy of the license at
010:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011:         * See the License for the specific language governing
012:         * permissions and limitations under the License.
013:         *
014:         * When distributing Covered Code, include this CDDL
015:         * HEADER in each file and include the License file at
016:         * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017:         * If applicable add the following below this CDDL HEADER,
018:         * with the fields enclosed by brackets "[]" replaced with
019:         * your own identifying information: Portions Copyright
020:         * [year] [name of copyright owner]
021:         */
022:
023:        /*
024:         * @(#)TestDirectoryUtil.java
025:         * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026:         *
027:         * END_HEADER - DO NOT EDIT
028:         */
029:        package com.sun.jbi.management.internal.support;
030:
031:        import java.io.File;
032:        import java.io.FileInputStream;
033:
034:        public class TestDirectoryUtil extends junit.framework.TestCase {
035:            private final static String TEST_ROOT = System
036:                    .getProperty("junit.srcroot")
037:                    + "/runtime/manage/bld/test-classes/dir-util";
038:
039:            private final static String CHILD_DIR_PATH = "parent/child";
040:            private final static String TEST_FILE_NAME = "foo.txt";
041:
042:            private File mTestRoot;
043:
044:            public TestDirectoryUtil(String aTestName) throws Exception {
045:                super (aTestName);
046:                mTestRoot = new File(TEST_ROOT);
047:            }
048:
049:            public void setUp() throws Exception {
050:                super .setUp();
051:                mTestRoot.mkdir();
052:            }
053:
054:            public void tearDown() throws Exception {
055:                try {
056:                    DirectoryUtil.deleteDir(TEST_ROOT);
057:                } catch (Exception ex) {
058:                    System.out
059:                            .println("tearDown failed to remove " + TEST_ROOT);
060:                }
061:            }
062:
063:            /** Happy path for deleteDir(). 
064:             */
065:            public void testDeleteDir() throws Exception {
066:                File dir = createTestDir("testDeleteDir");
067:
068:                // sanity check
069:                assertTrue(dir.exists());
070:
071:                // delete the directory
072:                DirectoryUtil.deleteDir(dir.getAbsolutePath());
073:
074:                // it should be gone now
075:                assertFalse(dir.exists());
076:            }
077:
078:            /** Failure path for deleteDir() - file handle still open.  I'm pretty sure
079:             *  this will only fail on windows, so this test is kind of tricky.  If
080:             *  the deletion failed, an exception should be thrown.  If the deletion
081:             *  succeeded, then an exception should not be thrown.
082:             */
083:            public void testDeleteDirBad() throws Exception {
084:                File dir = createTestDir("testDeleteDirBad");
085:                FileInputStream fis = openFileStream(dir.getAbsolutePath());
086:                boolean deletionFailed = false;
087:
088:                // sanity check
089:                assertTrue(dir.exists());
090:
091:                // delete the directory (this should fail on windows)
092:                try {
093:                    DirectoryUtil.deleteDir(dir.getAbsolutePath());
094:                } catch (Exception ex) {
095:                    deletionFailed = true;
096:                    // print the exception so we can see what's going on in the junit output
097:                    System.out.println("testDeleteDirBad: " + ex.toString());
098:                }
099:
100:                if (deletionFailed) {
101:                    // Deletion failed, so the directory should still exist
102:                    assertTrue(dir.exists());
103:                } else {
104:                    // No exception thrown, so the directory should be toast
105:                    assertFalse(dir.exists());
106:                }
107:
108:                try {
109:                    fis.close();
110:                } catch (Exception ex) { /* unimportant */
111:                }
112:            }
113:
114:            /** Happy path for removeDir(). 
115:             */
116:            public void testRemoveDir() throws Exception {
117:                File dir = createTestDir("testRemoveDir");
118:
119:                // sanity check
120:                assertTrue(dir.exists());
121:
122:                // delete the directory
123:                DirectoryUtil.removeDir(dir.getAbsolutePath());
124:
125:                // it should be gone now
126:                assertFalse(dir.exists());
127:            }
128:
129:            /** Failure path for removeDir() - file handle still open.  I'm pretty sure
130:             *  this will only fail on windows, so this test is kind of tricky.  If
131:             *  the deletion failed, the directory should be marked for deletion.  If 
132:             *  the deletion succeeded, then an exception should not be thrown.
133:             */
134:            public void testRemoveDirBad() throws Exception {
135:                File dir = createTestDir("testRemoveDirBad");
136:                FileInputStream fis = openFileStream(dir.getAbsolutePath());
137:                boolean removed;
138:
139:                // sanity check
140:                assertTrue(dir.exists());
141:
142:                // remove the directory (this should fail on windows)
143:                removed = DirectoryUtil.removeDir(dir.getAbsolutePath());
144:
145:                if (removed) {
146:                    // It should be removed
147:                    assertFalse(dir.exists());
148:                } else {
149:                    // Removal failed, so the directory should still exist
150:                    assertTrue(dir.exists());
151:                    // And it should be marked now
152:                    assertTrue(DirectoryUtil.isMarked(dir));
153:                }
154:
155:                try {
156:                    fis.close();
157:                } catch (Exception ex) { /* unimportant */
158:                }
159:            }
160:
161:            /** Happy-path tests for markDir() and isMarked(). */
162:            public void testMarkDir() throws Exception {
163:                File dir = createTestDir("testMarkDir");
164:
165:                // sanity check
166:                assertTrue(dir.exists());
167:
168:                // mark the directory and test it
169:                DirectoryUtil.markDir(dir.getAbsolutePath());
170:                assertTrue(DirectoryUtil.isMarked(dir));
171:            }
172:
173:            /** Negative tests for markDir() and isMarked(). */
174:            public void testMarkDirBad() throws Exception {
175:                File dir = createTestDir("testMarkDirBad");
176:
177:                // sanity check
178:                assertTrue(dir.exists());
179:
180:                // don't mark the directory and test it
181:                assertFalse(DirectoryUtil.isMarked(dir));
182:            }
183:
184:            /** Happy-path test for removeMarkedDirs().  Test to make sure it deletes
185:             *  what it's supposed to and leaves the other stuff alone.
186:             */
187:            public void testRemoveMarkedDirs() throws Exception {
188:                File topDir = new File(mTestRoot, "testRemoveMarkedDirs");
189:                File markedDir = createTestDir("testRemoveMarkedDirs/marked");
190:                File unmarkedDir = createTestDir("testRemoveMarkedDirs/unmarked");
191:
192:                // sanity check
193:                assertTrue(topDir.exists());
194:                assertTrue(markedDir.exists());
195:                assertTrue(unmarkedDir.exists());
196:
197:                // mark one directory and run cleanup
198:                DirectoryUtil.markDir(markedDir.getAbsolutePath());
199:                DirectoryUtil.removeMarkedDirs(topDir.getAbsolutePath());
200:
201:                // make sure the right stuff was removed
202:                assertTrue(topDir.exists());
203:                assertTrue(unmarkedDir.exists());
204:                assertFalse(markedDir.exists());
205:            }
206:
207:            /** Various tests for testGetFolderNameListing() */
208:            public void testGetFolderNameListing() throws Exception {
209:                String[] list;
210:
211:                // test an empty dir
212:                File emptyDir = new File(mTestRoot, "testGetFolderNameListing");
213:                emptyDir.mkdir();
214:                list = DirectoryUtil.getFolderNameListing(emptyDir);
215:                assertTrue(list != null && list.length == 0);
216:
217:                // create the test dir which has one immediate child directory
218:                File fullDir = createTestDir("testGetFolderNameListing");
219:                list = DirectoryUtil.getFolderNameListing(fullDir);
220:                assertTrue(list != null && list.length == 1);
221:            }
222:
223:            /** ################  HELPER METHODS ###################### */
224:
225:            /** Creates a set of test directories with some dummy files.
226:             *  @param dirName the directory root name under TEST_ROOT
227:             */
228:            private File createTestDir(String dirName) throws Exception {
229:                File root = new File(mTestRoot, dirName);
230:                File childDir = new File(root, CHILD_DIR_PATH);
231:                childDir.mkdirs();
232:
233:                // create test files
234:                new File(root, TEST_FILE_NAME).createNewFile();
235:                new File(childDir, TEST_FILE_NAME).createNewFile();
236:
237:                return root;
238:            }
239:
240:            /** Opens a stream to a newly-created file in a test directory.  This should
241:             *  prevent deletion on windows-based platforms.
242:             */
243:            private FileInputStream openFileStream(String rootPath)
244:                    throws Exception {
245:                return new FileInputStream(rootPath + "/" + CHILD_DIR_PATH
246:                        + "/" + TEST_FILE_NAME);
247:            }
248:
249:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.