1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.util.RandomAccessMode;
23  
24  /***
25   * RanomdRead-only test cases for file providers.
26   *
27   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
28   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
29   */
30  public class ProviderRandomReadTests
31      extends AbstractProviderTestCase
32  {
33      private final String TEST_DATA = "This is a test file.";
34  
35      /***
36       * Returns the capabilities required by the tests of this test case.
37       */
38      protected Capability[] getRequiredCaps()
39      {
40          return new Capability[]
41          {
42              Capability.GET_TYPE,
43              Capability.RANDOM_ACCESS_READ
44          };
45      }
46  
47      /***
48       * Read a file
49       */
50      public void testRandomRead() throws Exception
51      {
52          FileObject file = null;
53          try
54          {
55              file = getReadFolder().resolveFile("file1.txt");
56              RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READ);
57  
58              // read first byte
59              byte c = ra.readByte();
60              assertEquals(c, TEST_DATA.charAt(0));
61              assertEquals("fp", ra.getFilePointer(), 1);
62  
63              // start at pos 4
64              ra.seek(3);
65              c = ra.readByte();
66              assertEquals(c, TEST_DATA.charAt(3));
67              assertEquals("fp", ra.getFilePointer(), 4);
68  
69              c = ra.readByte();
70              assertEquals(c, TEST_DATA.charAt(4));
71              assertEquals("fp", ra.getFilePointer(), 5);
72  
73              // restart at pos 4
74              ra.seek(3);
75              c = ra.readByte();
76              assertEquals(c, TEST_DATA.charAt(3));
77              assertEquals("fp", ra.getFilePointer(), 4);
78  
79              c = ra.readByte();
80              assertEquals(c, TEST_DATA.charAt(4));
81              assertEquals("fp", ra.getFilePointer(), 5);
82  
83              // advance to pos 11
84              ra.seek(10);
85              c = ra.readByte();
86              assertEquals(c, TEST_DATA.charAt(10));
87              assertEquals("fp", ra.getFilePointer(), 11);
88  
89              c = ra.readByte();
90              assertEquals(c, TEST_DATA.charAt(11));
91              assertEquals("fp", ra.getFilePointer(), 12);
92          }
93          finally
94          {
95              if (file != null)
96              {
97                  file.close();
98              }
99          }
100     }
101 }