KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > util > FileUtilTest


1 /*
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  *
9  * 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 JavaDoc;
20
21 import junit.framework.TestCase;
22
23 public class FileUtilTest extends TestCase {
24
25     private static File JavaDoc WORKDIR = new File JavaDoc("target/servicemix-test");
26     
27     protected void setUp() throws Exception JavaDoc {
28         FileUtil.deleteFile(WORKDIR);
29         WORKDIR.mkdirs();
30     }
31     
32     public void testDeleteFile() throws Exception JavaDoc {
33         File JavaDoc f = new File JavaDoc(WORKDIR, "test.txt");
34         assertFalse(f.exists());
35         assertTrue(f.createNewFile());
36         assertTrue(f.exists());
37         assertFalse(f.isDirectory());
38         assertTrue(f.isFile());
39         assertTrue(FileUtil.deleteFile(f));
40         assertFalse(f.exists());
41     }
42     
43     /*
44      * This test only works on windows, as
45      * writing to a file does not prevent its
46      * deletion on unix systems.
47      *
48     public void testDeleteLockedFile() throws Exception {
49         File f = new File(WORKDIR, "test.txt");
50         assertFalse(f.exists());
51         OutputStream os = new FileOutputStream(f);
52         try {
53             Writer w = new OutputStreamWriter(os);
54             w.write("hello");
55             w.flush();
56             assertTrue(f.exists());
57             assertFalse(FileUtil.deleteFile(f));
58             assertTrue(f.exists());
59         } finally {
60             os.close();
61         }
62         assertTrue(f.exists());
63         assertTrue(FileUtil.deleteFile(f));
64         assertFalse(f.exists());
65     }
66     */

67     
68     public void testDeleteDir() throws Exception JavaDoc {
69         File JavaDoc f = new File JavaDoc(WORKDIR, "testdir");
70         assertFalse(f.exists());
71         assertTrue(f.mkdir());
72         assertTrue(f.exists());
73         assertTrue(f.isDirectory());
74         assertFalse(f.isFile());
75         assertTrue(FileUtil.deleteFile(f));
76         assertFalse(f.exists());
77     }
78     
79     /*
80      * This test only works on windows, as
81      * writing to a file does not prevent its
82      * deletion on unix systems.
83      *
84     public void testDeleteDirWithLockedFile() throws Exception {
85         File f = new File(WORKDIR, "testdir");
86         assertFalse(f.exists());
87         assertTrue(f.mkdir());
88         assertTrue(f.exists());
89         assertTrue(f.isDirectory());
90         assertFalse(f.isFile());
91         File f2 = new File(f, "test.txt");
92         assertFalse(f2.exists());
93         File f3 = new File(f, "test2.txt");
94         assertFalse(f3.exists());
95         assertTrue(f3.createNewFile());
96         assertTrue(f3.exists());
97         OutputStream os = new FileOutputStream(f2);
98         try {
99             Writer w = new OutputStreamWriter(os);
100             w.write("hello");
101             w.flush();
102             assertTrue(f2.exists());
103             assertFalse(FileUtil.deleteFile(f));
104             assertTrue(f.exists());
105             assertTrue(f2.exists());
106         } finally {
107             os.close();
108         }
109         assertFalse(f3.exists());
110         assertTrue(f2.exists());
111         assertTrue(f.exists());
112         assertTrue(FileUtil.deleteFile(f));
113         assertFalse(f.exists());
114     }
115     */

116     
117 }
118
Popular Tags