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 protected FileObject createScratchFolder() throws Exception {
32 final FileObject scratchFolder = getWriteFolder();
33
34
35 scratchFolder.delete(Selectors.EXCLUDE_SELF);
36 scratchFolder.createFolder();
37
38 return scratchFolder;
39 }
40
41
42
43
44 @Override
45 protected Capability[] getRequiredCapabilities() {
46 return new Capability[] { Capability.CREATE, Capability.DELETE, Capability.GET_TYPE, Capability.LIST_CHILDREN,
47 Capability.READ_CONTENT, Capability.WRITE_CONTENT, Capability.APPEND_CONTENT };
48 }
49
50
51
52
53 @Test
54 public void testAppendContent() throws Exception {
55 final FileObject scratchFolder = createScratchFolder();
56
57
58 final FileObject file = scratchFolder.resolveFile("file1.txt");
59 assertFalse(file.exists());
60
61
62 final String content = "Here is some sample content for the file. Blah Blah Blah.";
63 final String contentAppend = content + content;
64
65 try (OutputStream os = file.getContent().getOutputStream()) {
66 os.write(content.getBytes(StandardCharsets.UTF_8));
67 }
68 assertSameContent(content, file);
69
70
71 try (OutputStream os2 = file.getContent().getOutputStream(true)) {
72 os2.write(content.getBytes(StandardCharsets.UTF_8));
73 }
74 assertSameContent(contentAppend, file);
75
76
77 final FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
78 assertFalse(fileCopy.exists());
79 fileCopy.copyFrom(file, Selectors.SELECT_SELF);
80
81 assertSameContent(contentAppend, fileCopy);
82
83
84 assertTrue(fileCopy.exists());
85 assertTrue(fileCopy.delete());
86 }
87 }