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 java.io.IOException;
20  
21  import org.apache.commons.vfs2.util.RandomAccessMode;
22  import org.junit.Assert;
23  import org.junit.Test;
24  
25  /**
26   * Random set length test cases for file providers.
27   *
28   */
29  public class ProviderRandomSetLengthTests extends AbstractProviderTestCase {
30      private static final String TEST_DATA = "This is a test file.";
31  
32      /**
33       * Sets up a scratch folder for the test to use.
34       */
35      protected FileObject createScratchFolder() throws Exception {
36          final FileObject scratchFolder = this.getWriteFolder();
37  
38          // Make sure the test folder is empty
39          scratchFolder.delete(Selectors.EXCLUDE_SELF);
40          scratchFolder.createFolder();
41  
42          return scratchFolder;
43      }
44  
45      /**
46       * Returns the capabilities required by the tests of this test case.
47       */
48      @Override
49      protected Capability[] getRequiredCapabilities() {
50          return new Capability[] { Capability.GET_TYPE, Capability.RANDOM_ACCESS_READ, Capability.RANDOM_ACCESS_WRITE,
51                  Capability.RANDOM_ACCESS_SET_LENGTH };
52      }
53  
54      /**
55       * Writes a file
56       */
57      @Test
58      public void testRandomSetLength() throws Exception {
59          try (FileObject file = this.createScratchFolder().resolveFile("random_write.txt")) {
60              file.createFile();
61              final String fileString = file.toString();
62              final RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
63  
64              // Write long string
65              ra.writeBytes(TEST_DATA);
66              Assert.assertEquals(fileString, TEST_DATA.length(), ra.length());
67  
68              // Shrink to length 1
69              ra.setLength(1);
70              Assert.assertEquals(fileString, 1, ra.length());
71              // now read 1
72              ra.seek(0);
73              Assert.assertEquals(fileString, TEST_DATA.charAt(0), ra.readByte());
74  
75              try {
76                  ra.readByte();
77                  Assert.fail("Expected " + Exception.class.getName());
78              } catch (final IOException e) {
79                  // Expected
80              }
81  
82              // Grow to length 2
83              ra.setLength(2);
84              Assert.assertEquals(fileString, 2, ra.length());
85              // We have an undefined extra byte
86              ra.seek(1);
87              ra.readByte();
88  
89          }
90      }
91  }