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