1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29 public class ProviderRenameTests extends AbstractProviderTestCase {
30
31
32
33
34 protected FileObject createScratchFolder() throws Exception {
35 final FileObject scratchFolder = getWriteFolder();
36
37
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
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
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
78 assertTrue(fileMove.exists());
79 assertTrue(fileMove.delete());
80 }
81
82
83
84
85 @Test
86 public void testRenameFile() throws Exception {
87 final FileObject scratchFolder = createScratchFolder();
88
89
90 final FileObject file = scratchFolder.resolveFile("file1.txt");
91 assertFalse(file.exists());
92
93 final String content = createTestFile(file);
94
95
96 moveFile(scratchFolder, file, content);
97 }
98
99
100
101
102
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
116 moveFile(scratchFolder, file, content);
117 assertEquals(0, folder.getChildren().length);
118 }
119
120
121
122
123
124
125 @Test
126 public void testRenameFileIntoEmptyFolder() throws Exception {
127 try (FileObject scratchFolder = createScratchFolder();
128
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
145
146 @Test
147 public void testRenameFileWithSpaces() throws Exception {
148 final FileObject scratchFolder = createScratchFolder();
149
150
151 final FileObject file = scratchFolder.resolveFile("file space.txt");
152 assertFalse(file.exists());
153
154 final String content = createTestFile(file);
155
156
157 moveFile(scratchFolder, file, content);
158 }
159
160 }