01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.jbi.util;
18:
19: import java.io.File;
20:
21: import junit.framework.TestCase;
22:
23: public class FileUtilTest extends TestCase {
24:
25: private static final File WORKDIR = new File(
26: "target/servicemix-test");
27:
28: protected void setUp() throws Exception {
29: FileUtil.deleteFile(WORKDIR);
30: WORKDIR.mkdirs();
31: }
32:
33: public void testDeleteFile() throws Exception {
34: File f = new File(WORKDIR, "test.txt");
35: assertFalse(f.exists());
36: assertTrue(f.createNewFile());
37: assertTrue(f.exists());
38: assertFalse(f.isDirectory());
39: assertTrue(f.isFile());
40: assertTrue(FileUtil.deleteFile(f));
41: assertFalse(f.exists());
42: }
43:
44: /*
45: * This test only works on windows, as writing to a file does not prevent
46: * its deletion on unix systems.
47: *
48: * public void testDeleteLockedFile() throws Exception { File f = new
49: * File(WORKDIR, "test.txt"); assertFalse(f.exists()); OutputStream os = new
50: * FileOutputStream(f); try { Writer w = new OutputStreamWriter(os);
51: * w.write("hello"); w.flush(); assertTrue(f.exists());
52: * assertFalse(FileUtil.deleteFile(f)); assertTrue(f.exists()); } finally {
53: * os.close(); } assertTrue(f.exists()); assertTrue(FileUtil.deleteFile(f));
54: * assertFalse(f.exists()); }
55: */
56:
57: public void testDeleteDir() throws Exception {
58: File f = new File(WORKDIR, "testdir");
59: assertFalse(f.exists());
60: assertTrue(f.mkdir());
61: assertTrue(f.exists());
62: assertTrue(f.isDirectory());
63: assertFalse(f.isFile());
64: assertTrue(FileUtil.deleteFile(f));
65: assertFalse(f.exists());
66: }
67:
68: /*
69: * This test only works on windows, as writing to a file does not prevent
70: * its deletion on unix systems.
71: *
72: * public void testDeleteDirWithLockedFile() throws Exception { File f = new
73: * File(WORKDIR, "testdir"); assertFalse(f.exists()); assertTrue(f.mkdir());
74: * assertTrue(f.exists()); assertTrue(f.isDirectory());
75: * assertFalse(f.isFile()); File f2 = new File(f, "test.txt");
76: * assertFalse(f2.exists()); File f3 = new File(f, "test2.txt");
77: * assertFalse(f3.exists()); assertTrue(f3.createNewFile());
78: * assertTrue(f3.exists()); OutputStream os = new FileOutputStream(f2); try {
79: * Writer w = new OutputStreamWriter(os); w.write("hello"); w.flush();
80: * assertTrue(f2.exists()); assertFalse(FileUtil.deleteFile(f));
81: * assertTrue(f.exists()); assertTrue(f2.exists()); } finally { os.close(); }
82: * assertFalse(f3.exists()); assertTrue(f2.exists());
83: * assertTrue(f.exists()); assertTrue(FileUtil.deleteFile(f));
84: * assertFalse(f.exists()); }
85: */
86:
87: }
|