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.Selectors;
22  
23  import java.io.OutputStream;
24  
25  /***
26   * File system test that check that a file system can be renamed.
27   *
28   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
29   */
30  public class ProviderRenameTests
31      extends AbstractProviderTestCase
32  {
33      /***
34       * Returns the capabilities required by the tests of this test case.
35       */
36      protected Capability[] getRequiredCaps()
37      {
38          return new Capability[]
39          {
40              Capability.CREATE,
41              Capability.DELETE,
42              Capability.GET_TYPE,
43              Capability.LIST_CHILDREN,
44              Capability.READ_CONTENT,
45              Capability.WRITE_CONTENT,
46              Capability.RENAME
47          };
48      }
49  
50      /***
51       * Sets up a scratch folder for the test to use.
52       */
53      protected FileObject createScratchFolder() throws Exception
54      {
55          FileObject scratchFolder = getWriteFolder();
56  
57          // Make sure the test folder is empty
58          scratchFolder.delete(Selectors.EXCLUDE_SELF);
59          scratchFolder.createFolder();
60  
61          return scratchFolder;
62      }
63  
64      /***
65       * Tests create-delete-create-a-file sequence on the same file system.
66       */
67      public void testRenameFile() throws Exception
68      {
69          final FileObject scratchFolder = createScratchFolder();
70  
71          // Create direct child of the test folder
72          final FileObject file = scratchFolder.resolveFile("file1.txt");
73          assertTrue(!file.exists());
74  
75          // Create the source file
76          final String content = "Here is some sample content for the file.  Blah Blah Blah.";
77  
78          final OutputStream os = file.getContent().getOutputStream();
79          try
80          {
81              os.write(content.getBytes("utf-8"));
82          }
83          finally
84          {
85              os.close();
86          }
87          assertSameContent(content, file);
88  
89  
90          // Make sure we can move the new file to another file on the same filesystem
91  
92          FileObject fileMove = scratchFolder.resolveFile("file1move.txt");
93          assertTrue(!fileMove.exists());
94  
95          file.moveTo(fileMove);
96  
97          assertTrue(!file.exists());
98          assertTrue(fileMove.exists());
99  
100         assertSameContent(content, fileMove);
101 
102         // Delete the file.
103         assertTrue(fileMove.exists());
104         assertTrue(fileMove.delete());
105     }
106 }