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