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      /**
32       * Sets up a scratch folder for the test to use.
33       */
34      protected FileObject createScratchFolder() throws Exception {
35          final FileObject scratchFolder = getWriteFolder();
36  
37          // Make sure the test folder is empty
38          scratchFolder.delete(Selectors.EXCLUDE_SELF);
39          scratchFolder.createFolder();
40  
41          return scratchFolder;
42      }
43  
44      private String createTestFile(final FileObject file)
45              throws FileSystemException, IOException, UnsupportedEncodingException, Exception {
46          // Create the source file
47          final String content = "Here is some sample content for the file.  Blah Blah Blah.";
48  
49          try (OutputStream os = file.getContent().getOutputStream()) {
50              os.write(content.getBytes(StandardCharsets.UTF_8));
51          }
52          assertSameContent(content, file);
53          return content;
54      }
55  
56      /**
57       * Returns the capabilities required by the tests of this test case.
58       */
59      @Override
60      protected Capability[] getRequiredCapabilities() {
61          return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN,
62                  Capability.READ_CONTENT, Capability.WRITE_CONTENT, Capability.RENAME };
63      }
64  
65      private void moveFile(final FileObject scratchFolder, final FileObject file, final String content)
66              throws FileSystemException, Exception {
67          final FileObject fileMove = scratchFolder.resolveFile("file1move.txt");
68          assertFalse(fileMove.exists());
69  
70          file.moveTo(fileMove);
71  
72          assertFalse(file.exists());
73          assertTrue(fileMove.exists());
74  
75          assertSameContent(content, fileMove);
76  
77          // Delete the file.
78          assertTrue(fileMove.exists());
79          assertTrue(fileMove.delete());
80      }
81  
82      /**
83       * Tests create-delete-create-a-file sequence on the same file system.
84       */
85      @Test
86      public void testRenameFile() throws Exception {
87          final FileObject scratchFolder = createScratchFolder();
88  
89          // Create direct child of the test folder
90          final FileObject file = scratchFolder.resolveFile("file1.txt");
91          assertFalse(file.exists());
92  
93          final String content = createTestFile(file);
94  
95          // Make sure we can move the new file to another file on the same file system
96          moveFile(scratchFolder, file, content);
97      }
98  
99      /**
100      * Moves a file from a child folder to a parent folder to test what happens when the original folder is now empty.
101      *
102      * See [VFS-298] FTP: Exception is thrown when renaming a file.
103      */
104     @Test
105     public void testRenameFileAndLeaveFolderEmpty() throws Exception {
106         final FileObject scratchFolder = createScratchFolder();
107         final FileObject folder = scratchFolder.resolveFile("folder");
108         folder.createFolder();
109         assertTrue(folder.exists());
110         final FileObject file = folder.resolveFile("file1.txt");
111         assertFalse(file.exists());
112 
113         final String content = createTestFile(file);
114 
115         // Make sure we can move the new file to another file on the same file system
116         moveFile(scratchFolder, file, content);
117         assertEquals(0, folder.getChildren().length);
118     }
119 
120     /**
121      * Tests moving a file to empty folder.
122      * <p>
123      * This fails with VFS-558, but only with a CacheStrategy.ON_CALL.
124      */
125     @Test
126     public void testRenameFileIntoEmptyFolder() throws Exception {
127         try (FileObject scratchFolder = createScratchFolder();
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 empty", 0, destFolder.getChildren().length);
138 
139             moveFile(destFolder, file, content);
140         }
141     }
142 
143     /**
144      * Tests create-delete-create-a-file sequence on the same file system.
145      */
146     @Test
147     public void testRenameFileWithSpaces() throws Exception {
148         final FileObject scratchFolder = createScratchFolder();
149 
150         // Create direct child of the test folder
151         final FileObject file = scratchFolder.resolveFile("file space.txt");
152         assertFalse(file.exists());
153 
154         final String content = createTestFile(file);
155 
156         // Make sure we can move the new file to another file on the same file system
157         moveFile(scratchFolder, file, content);
158     }
159 
160 }