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: }
|