View Javadoc
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.vfs2;
18  
19  import org.apache.commons.vfs2.util.RandomAccessMode;
20  import org.junit.Test;
21  
22  /**
23   * Random read-only test case for file providers.
24   */
25  public class ProviderRandomReadTests extends AbstractProviderTestCase {
26  
27      private static final String TEST_DATA = "This is a test file.";
28  
29      /**
30       * Returns the capabilities required by the tests of this test case.
31       */
32      @Override
33      protected Capability[] getRequiredCapabilities() {
34          return new Capability[] { Capability.GET_TYPE, Capability.RANDOM_ACCESS_READ };
35      }
36  
37      /**
38       * Read a file.
39       */
40      @Test
41      public void testRandomRead() throws Exception {
42          try (FileObject file = getReadFolder().resolveFile("file1.txt")) {
43              final RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READ);
44  
45              // read first byte
46              byte c = ra.readByte();
47              assertEquals(TEST_DATA.charAt(0), c);
48              assertEquals("fp", 1, ra.getFilePointer());
49  
50              // start at pos 4
51              ra.seek(3);
52              c = ra.readByte();
53              assertEquals(TEST_DATA.charAt(3), c);
54              assertEquals("fp", 4, ra.getFilePointer());
55  
56              c = ra.readByte();
57              assertEquals(TEST_DATA.charAt(4), c);
58              assertEquals("fp", 5, ra.getFilePointer());
59  
60              // restart at pos 4
61              ra.seek(3);
62              c = ra.readByte();
63              assertEquals(TEST_DATA.charAt(3), c);
64              assertEquals("fp", 4, ra.getFilePointer());
65  
66              c = ra.readByte();
67              assertEquals(TEST_DATA.charAt(4), c);
68              assertEquals("fp", 5, ra.getFilePointer());
69  
70              // advance to pos 11
71              ra.seek(10);
72              c = ra.readByte();
73              assertEquals(TEST_DATA.charAt(10), c);
74              assertEquals("fp", 11, ra.getFilePointer());
75  
76              c = ra.readByte();
77              assertEquals(TEST_DATA.charAt(11), c);
78              assertEquals("fp", 12, ra.getFilePointer());
79          }
80      }
81  
82  }