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.commons.vfs.test;
18  
19  import org.apache.commons.vfs.Capability;
20  import org.apache.commons.vfs.FileObject;
21  import org.apache.commons.vfs.FileSelectInfo;
22  import org.apache.commons.vfs.FileSelector;
23  import org.apache.commons.vfs.FileType;
24  import org.apache.commons.vfs.FileTypeSelector;
25  import org.apache.commons.vfs.Selectors;
26  
27  /***
28   * File system test that do some delete operations.
29   *
30   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
31   */
32  public class ProviderDeleteTests
33      extends AbstractProviderTestCase
34  {
35      private class FileNameSelector implements FileSelector
36      {
37          final String basename;
38  
39          private FileNameSelector(String basename)
40          {
41              this.basename = basename;
42          }
43  
44          public boolean includeFile(FileSelectInfo fileInfo) throws Exception
45          {
46              return this.basename.equals(fileInfo.getFile().getName().getBaseName());
47          }
48  
49          public boolean traverseDescendents(FileSelectInfo fileInfo) throws Exception
50          {
51              return true;
52          }
53      }
54  
55      /***
56       * Returns the capabilities required by the tests of this test case.
57       */
58      protected Capability[] getRequiredCaps()
59      {
60          return new Capability[]
61          {
62              Capability.CREATE,
63              Capability.DELETE,
64              Capability.GET_TYPE,
65              Capability.LIST_CHILDREN,
66          };
67      }
68  
69      /***
70       * Sets up a scratch folder for the test to use.
71       */
72      protected FileObject createScratchFolder() throws Exception
73      {
74          FileObject scratchFolder = getWriteFolder();
75  
76          // Make sure the test folder is empty
77          scratchFolder.delete(Selectors.EXCLUDE_SELF);
78          scratchFolder.createFolder();
79  
80          final FileObject dir1 = scratchFolder.resolveFile("dir1");
81          dir1.createFolder();
82          final FileObject dir1file1 = dir1.resolveFile("a.txt");
83          dir1file1.createFile();
84          final FileObject dir2 = scratchFolder.resolveFile("dir2");
85          dir2.createFolder();
86          final FileObject dir2file1 = dir2.resolveFile("b.txt");
87          dir2file1.createFile();
88  
89          return scratchFolder;
90      }
91  
92      /***
93       * deletes the complete structure
94       */
95      public void testDeleteFiles() throws Exception
96      {
97          final FileObject scratchFolder = createScratchFolder();
98  
99          assertEquals(scratchFolder.delete(Selectors.EXCLUDE_SELF), 4);
100     }
101 
102     /***
103      * deletes a single file
104      */
105     public void testDeleteFile() throws Exception
106     {
107         final FileObject scratchFolder = createScratchFolder();
108 
109         final FileObject file = scratchFolder.resolveFile("dir1/a.txt");
110 
111         assertTrue(file.delete());
112     }
113 
114     /***
115      * Deletes a non existent file
116      */
117     public void testDeleteNonExistantFile() throws Exception
118     {
119         final FileObject scratchFolder = createScratchFolder();
120 
121         final FileObject file = scratchFolder.resolveFile("dir1/aa.txt");
122 
123         assertFalse(file.delete());
124     }
125 
126     /***
127      * deletes files
128      */
129     public void testDeleteAllFiles() throws Exception
130     {
131         final FileObject scratchFolder = createScratchFolder();
132 
133         assertEquals(scratchFolder.delete(new FileTypeSelector(FileType.FILE)), 2);
134     }
135 
136     /***
137      * deletes a.txt
138      */
139     public void testDeleteOneFiles() throws Exception
140     {
141         final FileObject scratchFolder = createScratchFolder();
142 
143         assertEquals(scratchFolder.delete(new FileNameSelector("a.txt")), 1);
144     }
145 }