KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > test > ProviderRandomReadWriteTests


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.test;
17
18 import org.apache.commons.vfs.Capability;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.RandomAccessContent;
21 import org.apache.commons.vfs.Selectors;
22 import org.apache.commons.vfs.util.RandomAccessMode;
23
24 /**
25  * RanomdRead/Write test cases for file providers.
26  *
27  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
28  */

29 public class ProviderRandomReadWriteTests
30     extends AbstractProviderTestCase
31 {
32     private final String JavaDoc TEST_DATA = "This is a test file.";
33
34     /**
35      * Returns the capabilities required by the tests of this test case.
36      */

37     protected Capability[] getRequiredCaps()
38     {
39         return new Capability[]
40         {
41             Capability.GET_TYPE,
42             Capability.CREATE,
43             Capability.RANDOM_ACCESS_READ,
44             Capability.RANDOM_ACCESS_WRITE
45         };
46     }
47
48     /**
49      * Sets up a scratch folder for the test to use.
50      */

51     protected FileObject createScratchFolder() throws Exception JavaDoc
52     {
53         FileObject scratchFolder = getWriteFolder();
54
55         // Make sure the test folder is empty
56
scratchFolder.delete(Selectors.EXCLUDE_SELF);
57         scratchFolder.createFolder();
58
59         return scratchFolder;
60     }
61
62     /**
63      * Read a file
64      */

65     public void testRandomWrite() throws Exception JavaDoc
66     {
67         FileObject file = null;
68         try
69         {
70             file = createScratchFolder().resolveFile("random_write.txt");
71             file.createFile();
72             RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
73
74             // write first byte
75
ra.writeByte(TEST_DATA.charAt(0));
76
77             // start at pos 4
78
ra.seek(3);
79             ra.writeByte(TEST_DATA.charAt(3));
80             ra.writeByte(TEST_DATA.charAt(4));
81
82             // restart at pos 4 (but overwrite with different content)
83
ra.seek(3);
84             ra.writeByte(TEST_DATA.charAt(7));
85             ra.writeByte(TEST_DATA.charAt(8));
86
87             // advance to pos 11
88
ra.seek(10);
89             ra.writeByte(TEST_DATA.charAt(10));
90             ra.writeByte(TEST_DATA.charAt(11));
91
92             // now read
93
ra.seek(0);
94             assertEquals(ra.readByte(), TEST_DATA.charAt(0));
95
96             ra.seek(3);
97             assertEquals(ra.readByte(), TEST_DATA.charAt(7));
98             assertEquals(ra.readByte(), TEST_DATA.charAt(8));
99
100             ra.seek(10);
101             assertEquals(ra.readByte(), TEST_DATA.charAt(10));
102             assertEquals(ra.readByte(), TEST_DATA.charAt(11));
103         }
104         finally
105         {
106             if (file != null)
107             {
108                 file.close();
109             }
110         }
111     }
112 }
113
Popular Tags