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 java.io.IOException;
20  import java.io.OutputStream;
21  import java.io.UnsupportedEncodingException;
22  import java.nio.charset.StandardCharsets;
23  
24  import org.junit.Test;
25  
26  /**
27   * File system test that check that a file system can be renamed.
28   */
29  public class ProviderRenameTests extends AbstractProviderTestCase {
30      /**
31       * Sets up a scratch folder for the test to use.
32       */
33      protected FileObject createScratchFolder() throws Exception {
34          final FileObject scratchFolder = getWriteFolder();
35  
36          // Make sure the test folder is empty
37          scratchFolder.delete(Selectors.EXCLUDE_SELF);
38          scratchFolder.createFolder();
39  
40          return scratchFolder;
41      }
42  
43      private String createTestFile(final FileObject file)
44              throws FileSystemException, IOException, UnsupportedEncodingException, Exception {
45          // Create the source file
46          final String content = "Here is some sample content for the file.  Blah Blah Blah.";
47  
48          try (OutputStream os = file.getContent().getOutputStream()) {
49              os.write(content.getBytes(StandardCharsets.UTF_8));
50          }
51          assertSameContent(content, file);
52          return content;
53      }
54  
55      /**
56       * Returns the capabilities required by the tests of this test case.
57       */
58      @Override
59      protected Capability[] getRequiredCapabilities() {
60          return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN,
61                  Capability.READ_CONTENT, Capability.WRITE_CONTENT, Capability.RENAME };
62      }
63  
64      private void moveFile(final FileObject scratchFolder, final FileObject file, final String content)
65              throws FileSystemException, Exception {
66          final FileObject fileMove = scratchFolder.resolveFile("file1move.txt");
67          assertFalse(fileMove.exists());
68  
69          file.moveTo(fileMove);
70  
71          assertFalse(file.exists());
72          assertTrue(fileMove.exists());
73  
74          assertSameContent(content, fileMove);
75  
76          // Delete the file.
77          assertTrue(fileMove.exists());
78          assertTrue(fileMove.delete());
79      }
80  
81      /**
82       * Tests create-delete-create-a-file sequence on the same file system.
83       */
84      @Test
85      public void testRenameFile() throws Exception {
86          final FileObject scratchFolder = createScratchFolder();
87  
88          // Create direct child of the test folder
89          final FileObject file = scratchFolder.resolveFile("file1.txt");
90          assertFalse(file.exists());
91  
92          final String content = createTestFile(file);
93  
94          // Make sure we can move the new file to another file on the same file system
95          moveFile(scratchFolder, file, content);
96      }
97  
98      /**
99       * Moves a file from a child folder to a parent folder to test what happens when the original folder is now empty.
100      *
101      * See [VFS-298] FTP: Exception is thrown when renaming a file.
102      */
103     @Test
104     public void testRenameFileAndLeaveFolderEmpty() throws Exception {
105         final FileObject scratchFolder = createScratchFolder();
106         final FileObject folder = scratchFolder.resolveFile("folder");
107         folder.createFolder();
108         assertTrue(folder.exists());
109         final FileObject file = folder.resolveFile("file1.txt");
110         assertFalse(file.exists());
111 
112         final String content = createTestFile(file);
113 
114         // Make sure we can move the new file to another file on the same file system
115         moveFile(scratchFolder, file, content);
116         assertEquals(0, folder.getChildren().length);
117     }
118 
119     /**
120      * Tests moving a file to empty folder.
121      * <P>
122      * This fails with VFS-558, but only with a CacheStrategy.ON_CALL.
123      */
124     @Test
125     public void testRenameFileIntoEmptyFolder() throws Exception {
126         final FileObject scratchFolder = createScratchFolder();
127 
128         // Create direct child of the test folder
129         final FileObject file = scratchFolder.resolveFile("file1.txt");
130         assertFalse(file.exists());
131 
132         final String content = createTestFile(file);
133 
134         final FileObject destFolder = scratchFolder.resolveFile("empty-target-folder");
135         destFolder.createFolder();
136         assertTrue("new destination must be folder", destFolder.getType().hasChildren());
137         assertEquals("new destination must be emty", 0, destFolder.getChildren().length);
138 
139         moveFile(destFolder, file, content);
140     }
141 }