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.OutputStream;
20 import java.nio.charset.StandardCharsets;
21
22 import org.junit.Test;
23
24
25
26
27 public class ProviderWriteAppendTests extends AbstractProviderTestCase {
28
29
30
31
32 protected FileObject createScratchFolder() throws Exception {
33 final FileObject scratchFolder = getWriteFolder();
34
35
36 scratchFolder.delete(Selectors.EXCLUDE_SELF);
37 scratchFolder.createFolder();
38
39 return scratchFolder;
40 }
41
42
43
44
45 @Override
46 protected Capability[] getRequiredCapabilities() {
47 return new Capability[] {Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
48 Capability.WRITE_CONTENT, Capability.APPEND_CONTENT};
49 }
50
51
52
53
54 @Test
55 public void testAppendContent() throws Exception {
56 try (FileObject scratchFolder = createScratchFolder();
57
58
59 final FileObject file = scratchFolder.resolveFile("file1.txt")) {
60 assertFalse(file.exists());
61
62
63 final String content = "Here is some sample content for the file. Blah Blah Blah.";
64 final String contentAppend = content + content;
65
66 try (FileContent fileContent = file.getContent(); OutputStream os = fileContent.getOutputStream()) {
67 os.write(content.getBytes(StandardCharsets.UTF_8));
68 }
69 assertSameContent(content, file);
70
71
72 try (FileContent fileContent = file.getContent(); OutputStream os2 = fileContent.getOutputStream(true)) {
73 os2.write(content.getBytes(StandardCharsets.UTF_8));
74 }
75 assertSameContent(contentAppend, file);
76
77
78 try (FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt")) {
79 assertFalse(fileCopy.exists());
80 fileCopy.copyFrom(file, Selectors.SELECT_SELF);
81
82 assertSameContent(contentAppend, fileCopy);
83
84
85 assertTrue(fileCopy.exists());
86 assertTrue(fileCopy.delete());
87 }
88 }
89 }
90
91
92
93
94
95
96 @Test
97 public void testAppendToNonExisting() throws Exception {
98 try (FileObject scratchFolder = createScratchFolder();
99
100
101 final FileObject file = scratchFolder.resolveFile("file2.txt")) {
102 assertFalse(file.exists());
103
104
105 final String content1 = "Here is some sample content for the file. Blah Blah Blah.";
106
107 try (FileContent fileContent = file.getContent(); OutputStream os = fileContent.getOutputStream(true)) {
108 os.write(content1.getBytes(StandardCharsets.UTF_8));
109 }
110 assertSameContent(content1, file);
111 }
112 }
113
114 }