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.RandomAccessContent;
22 import org.apache.commons.vfs.Selectors;
23 import org.apache.commons.vfs.util.RandomAccessMode;
24
25 /***
26 * RanomdRead/Write test cases for file providers.
27 *
28 * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
29 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
30 */
31 public class ProviderRandomReadWriteTests
32 extends AbstractProviderTestCase
33 {
34 private final String TEST_DATA = "This is a test file.";
35
36 /***
37 * Returns the capabilities required by the tests of this test case.
38 */
39 protected Capability[] getRequiredCaps()
40 {
41 return new Capability[]
42 {
43 Capability.GET_TYPE,
44 Capability.CREATE,
45 Capability.RANDOM_ACCESS_READ,
46 Capability.RANDOM_ACCESS_WRITE
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 * Read a file
66 */
67 public void testRandomWrite() throws Exception
68 {
69 FileObject file = null;
70 try
71 {
72 file = createScratchFolder().resolveFile("random_write.txt");
73 file.createFile();
74 RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
75
76
77 ra.writeByte(TEST_DATA.charAt(0));
78
79
80 ra.seek(3);
81 ra.writeByte(TEST_DATA.charAt(3));
82 ra.writeByte(TEST_DATA.charAt(4));
83
84
85 ra.seek(3);
86 ra.writeByte(TEST_DATA.charAt(7));
87 ra.writeByte(TEST_DATA.charAt(8));
88
89
90 ra.seek(10);
91 ra.writeByte(TEST_DATA.charAt(10));
92 ra.writeByte(TEST_DATA.charAt(11));
93
94
95 ra.seek(0);
96 assertEquals(ra.readByte(), TEST_DATA.charAt(0));
97
98 ra.seek(3);
99 assertEquals(ra.readByte(), TEST_DATA.charAt(7));
100 assertEquals(ra.readByte(), TEST_DATA.charAt(8));
101
102 ra.seek(10);
103 assertEquals(ra.readByte(), TEST_DATA.charAt(10));
104 assertEquals(ra.readByte(), TEST_DATA.charAt(11));
105 }
106 finally
107 {
108 if (file != null)
109 {
110 file.close();
111 }
112 }
113 }
114 }