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.Selectors;
23  import org.apache.commons.vfs.util.RandomAccessMode;
24  
25  /***
26   * RanomdRead/Write test cases for file providers.
27   *
28   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
29   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
30   */
31  public class ProviderRandomReadWriteTests
32      extends AbstractProviderTestCase
33  {
34      private final String TEST_DATA = "This is a test file.";
35  
36      /***
37       * Returns the capabilities required by the tests of this test case.
38       */
39      protected Capability[] getRequiredCaps()
40      {
41          return new Capability[]
42          {
43              Capability.GET_TYPE,
44              Capability.CREATE,
45              Capability.RANDOM_ACCESS_READ,
46              Capability.RANDOM_ACCESS_WRITE
47          };
48      }
49  
50      /***
51       * Sets up a scratch folder for the test to use.
52       */
53      protected FileObject createScratchFolder() throws Exception
54      {
55          FileObject scratchFolder = getWriteFolder();
56  
57          // Make sure the test folder is empty
58          scratchFolder.delete(Selectors.EXCLUDE_SELF);
59          scratchFolder.createFolder();
60  
61          return scratchFolder;
62      }
63  
64      /***
65       * Read a file
66       */
67      public void testRandomWrite() throws Exception
68      {
69          FileObject file = null;
70          try
71          {
72              file = createScratchFolder().resolveFile("random_write.txt");
73              file.createFile();
74              RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
75  
76              // write first byte
77              ra.writeByte(TEST_DATA.charAt(0));
78  
79              // start at pos 4
80              ra.seek(3);
81              ra.writeByte(TEST_DATA.charAt(3));
82              ra.writeByte(TEST_DATA.charAt(4));
83  
84              // restart at pos 4 (but overwrite with different content)
85              ra.seek(3);
86              ra.writeByte(TEST_DATA.charAt(7));
87              ra.writeByte(TEST_DATA.charAt(8));
88  
89              // advance to pos 11
90              ra.seek(10);
91              ra.writeByte(TEST_DATA.charAt(10));
92              ra.writeByte(TEST_DATA.charAt(11));
93  
94              // now read
95              ra.seek(0);
96              assertEquals(ra.readByte(), TEST_DATA.charAt(0));
97  
98              ra.seek(3);
99              assertEquals(ra.readByte(), TEST_DATA.charAt(7));
100             assertEquals(ra.readByte(), TEST_DATA.charAt(8));
101 
102             ra.seek(10);
103             assertEquals(ra.readByte(), TEST_DATA.charAt(10));
104             assertEquals(ra.readByte(), TEST_DATA.charAt(11));
105         }
106         finally
107         {
108             if (file != null)
109             {
110                 file.close();
111             }
112         }
113     }
114 }