1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
72 final FileObject file = scratchFolder.resolveFile("file1.txt");
73 assertTrue(!file.exists());
74
75
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
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
103 assertTrue(fileMove.exists());
104 assertTrue(fileMove.delete());
105 }
106 }