KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > ProviderDeleteTests


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.test;
17
18 import org.apache.commons.vfs.Capability;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSelectInfo;
21 import org.apache.commons.vfs.FileSelector;
22 import org.apache.commons.vfs.FileType;
23 import org.apache.commons.vfs.FileTypeSelector;
24 import org.apache.commons.vfs.Selectors;
25
26 /**
27  * File system test that do some delete operations.
28  *
29  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
30  */

31 public class ProviderDeleteTests
32     extends AbstractProviderTestCase
33 {
34     private class FileNameSelector implements FileSelector
35     {
36         final String JavaDoc basename;
37
38         private FileNameSelector(String JavaDoc basename)
39         {
40             this.basename = basename;
41         }
42
43         public boolean includeFile(FileSelectInfo fileInfo) throws Exception JavaDoc
44         {
45             return this.basename.equals(fileInfo.getFile().getName().getBaseName());
46         }
47
48         public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception JavaDoc
49         {
50             return true;
51         }
52     }
53
54     /**
55      * Returns the capabilities required by the tests of this test case.
56      */

57     protected Capability[] getRequiredCaps()
58     {
59         return new Capability[]
60         {
61             Capability.CREATE,
62             Capability.DELETE,
63             Capability.GET_TYPE,
64             Capability.LIST_CHILDREN,
65         };
66     }
67
68     /**
69      * Sets up a scratch folder for the test to use.
70      */

71     protected FileObject createScratchFolder() throws Exception JavaDoc
72     {
73         FileObject scratchFolder = getWriteFolder();
74
75         // Make sure the test folder is empty
76
scratchFolder.delete(Selectors.EXCLUDE_SELF);
77         scratchFolder.createFolder();
78
79         final FileObject dir1 = scratchFolder.resolveFile("dir1");
80         dir1.createFolder();
81         final FileObject dir1file1 = dir1.resolveFile("a.txt");
82         dir1file1.createFile();
83         final FileObject dir2 = scratchFolder.resolveFile("dir2");
84         dir2.createFolder();
85         final FileObject dir2file1 = dir2.resolveFile("b.txt");
86         dir2file1.createFile();
87
88         return scratchFolder;
89     }
90
91     /**
92      * deletes the complete structure
93      */

94     public void testDeleteFiles() throws Exception JavaDoc
95     {
96         final FileObject scratchFolder = createScratchFolder();
97
98         assertEquals(scratchFolder.delete(Selectors.EXCLUDE_SELF), 4);
99     }
100
101     /**
102      * deletes a single file
103      */

104     public void testDeleteFile() throws Exception JavaDoc
105     {
106         final FileObject scratchFolder = createScratchFolder();
107
108         final FileObject file = scratchFolder.resolveFile("dir1/a.txt");
109
110         assertTrue(file.delete());
111     }
112
113     /**
114      * Deletes a non existent file
115      */

116     public void testDeleteNonExistantFile() throws Exception JavaDoc
117     {
118         final FileObject scratchFolder = createScratchFolder();
119
120         final FileObject file = scratchFolder.resolveFile("dir1/aa.txt");
121
122         assertFalse(file.delete());
123     }
124
125     /**
126      * deletes files
127      */

128     public void testDeleteAllFiles() throws Exception JavaDoc
129     {
130         final FileObject scratchFolder = createScratchFolder();
131
132         assertEquals(scratchFolder.delete(new FileTypeSelector(FileType.FILE)), 2);
133     }
134
135     /**
136      * deletes a.txt
137      */

138     public void testDeleteOneFiles() throws Exception JavaDoc
139     {
140         final FileObject scratchFolder = createScratchFolder();
141
142         assertEquals(scratchFolder.delete(new FileNameSelector("a.txt")), 1);
143     }
144 }
145
Popular Tags