KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.RandomAccessMode;
22
23 /**
24  * RanomdRead-only test cases for file providers.
25  *
26  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
27  */

28 public class ProviderRandomReadTests
29     extends AbstractProviderTestCase
30 {
31     private final String JavaDoc TEST_DATA = "This is a test file.";
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.GET_TYPE,
41             Capability.RANDOM_ACCESS_READ
42         };
43     }
44
45     /**
46      * Read a file
47      */

48     public void testRandomRead() throws Exception JavaDoc
49     {
50         FileObject file = null;
51         try
52         {
53             file = getReadFolder().resolveFile("file1.txt");
54             RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READ);
55
56             // read first byte
57
byte c = ra.readByte();
58             assertEquals(c, TEST_DATA.charAt(0));
59             assertEquals("fp", ra.getFilePointer(), 1);
60
61             // start at pos 4
62
ra.seek(3);
63             c = ra.readByte();
64             assertEquals(c, TEST_DATA.charAt(3));
65             assertEquals("fp", ra.getFilePointer(), 4);
66
67             c = ra.readByte();
68             assertEquals(c, TEST_DATA.charAt(4));
69             assertEquals("fp", ra.getFilePointer(), 5);
70
71             // restart at pos 4
72
ra.seek(3);
73             c = ra.readByte();
74             assertEquals(c, TEST_DATA.charAt(3));
75             assertEquals("fp", ra.getFilePointer(), 4);
76
77             c = ra.readByte();
78             assertEquals(c, TEST_DATA.charAt(4));
79             assertEquals("fp", ra.getFilePointer(), 5);
80
81             // advance to pos 11
82
ra.seek(10);
83             c = ra.readByte();
84             assertEquals(c, TEST_DATA.charAt(10));
85             assertEquals("fp", ra.getFilePointer(), 11);
86
87             c = ra.readByte();
88             assertEquals(c, TEST_DATA.charAt(11));
89             assertEquals("fp", ra.getFilePointer(), 12);
90         }
91         finally
92         {
93             if (file != null)
94             {
95                 file.close();
96             }
97         }
98     }
99 }
100
Popular Tags