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 modified.
27 *
28 * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
29 */
30 public class ProviderWriteAppendTests
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.APPEND_CONTENT
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 testAppendContent() 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 final String contentAppend = content + content;
78
79 final OutputStream os = file.getContent().getOutputStream();
80 try
81 {
82 os.write(content.getBytes("utf-8"));
83 }
84 finally
85 {
86 os.close();
87 }
88 assertSameContent(content, file);
89
90
91 final OutputStream os2 = file.getContent().getOutputStream(true);
92 try
93 {
94 os2.write(content.getBytes("utf-8"));
95 }
96 finally
97 {
98 os2.close();
99 }
100 assertSameContent(contentAppend, file);
101
102
103 FileObject fileCopy = scratchFolder.resolveFile("file1copy.txt");
104 assertTrue(!fileCopy.exists());
105 fileCopy.copyFrom(file, Selectors.SELECT_SELF);
106
107 assertSameContent(contentAppend, fileCopy);
108
109
110 assertTrue(fileCopy.exists());
111 assertTrue(fileCopy.delete());
112 }
113 }