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  
18  package org.apache.commons.io.input;
19  
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertNotEquals;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.io.File;
28  import java.io.FileNotFoundException;
29  import java.io.IOException;
30  import java.io.RandomAccessFile;
31  import java.nio.charset.StandardCharsets;
32  import java.nio.file.Path;
33  import java.nio.file.Paths;
34  import java.nio.file.StandardOpenOption;
35  
36  import org.apache.commons.io.RandomAccessFileMode;
37  import org.junit.jupiter.api.Test;
38  
39  public class RandomAccessFileInputStreamTest {
40  
41      private static final String DATA_FILE_NAME = "src/test/resources/org/apache/commons/io/test-file-iso8859-1.bin";
42      private static final Path DATA_PATH = Paths.get(DATA_FILE_NAME);
43      private static final int DATA_FILE_LEN = 1430;
44  
45      private RandomAccessFile createRandomAccessFile() throws FileNotFoundException {
46          return RandomAccessFileMode.READ_ONLY.create(DATA_FILE_NAME);
47      }
48  
49      @Test
50      public void testAvailable() throws IOException {
51          try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
52              true)) {
53              assertEquals(DATA_FILE_LEN, inputStream.available());
54          }
55      }
56  
57      @Test
58      public void testAvailableLong() throws IOException {
59          try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
60              true)) {
61              assertEquals(DATA_FILE_LEN, inputStream.availableLong());
62          }
63      }
64  
65      @SuppressWarnings("resource") // instance variable access
66      @Test
67      public void testBuilderFile() throws IOException {
68          try (RandomAccessFile file = createRandomAccessFile()) {
69              try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder().setFile(new File(DATA_FILE_NAME)).get()) {
70                  assertFalse(inputStream.isCloseOnClose());
71                  assertNotEquals(-1, inputStream.getRandomAccessFile().read());
72              }
73              file.read();
74          }
75      }
76  
77      @Test
78      public void testBuilderGet() {
79          // java.lang.IllegalStateException: origin == null
80          assertThrows(IllegalStateException.class, () -> RandomAccessFileInputStream.builder().get());
81      }
82  
83      @SuppressWarnings("resource") // instance variable access
84      @Test
85      public void testBuilderPath() throws IOException {
86          try (RandomAccessFile file = createRandomAccessFile()) {
87              try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder().setPath(DATA_PATH).get()) {
88                  assertFalse(inputStream.isCloseOnClose());
89                  assertNotEquals(-1, inputStream.getRandomAccessFile().read());
90              }
91              file.read();
92          }
93      }
94  
95      @SuppressWarnings("resource") // instance variable access
96      @Test
97      public void testBuilderPathOpenOptions() throws IOException {
98          try (RandomAccessFile file = createRandomAccessFile()) {
99              try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder().setPath(DATA_PATH).setOpenOptions(StandardOpenOption.READ)
100                     .get()) {
101                 assertFalse(inputStream.isCloseOnClose());
102                 assertNotEquals(-1, inputStream.getRandomAccessFile().read());
103             }
104             file.read();
105         }
106     }
107 
108     @SuppressWarnings("resource") // instance variable access
109     @Test
110     public void testBuilderRandomAccessFile() throws IOException {
111         try (RandomAccessFile file = createRandomAccessFile()) {
112             try (RandomAccessFileInputStream inputStream = RandomAccessFileInputStream.builder().setRandomAccessFile(file).get()) {
113                 assertFalse(inputStream.isCloseOnClose());
114                 assertNotEquals(-1, inputStream.getRandomAccessFile().read());
115             }
116             file.read();
117         }
118     }
119 
120     @SuppressWarnings("resource") // instance variable access
121     @Test
122     public void testConstructorCloseOnCloseFalse() throws IOException {
123         try (RandomAccessFile file = createRandomAccessFile()) {
124             try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, false)) {
125                 assertFalse(inputStream.isCloseOnClose());
126                 assertNotEquals(-1, inputStream.getRandomAccessFile().read());
127             }
128             file.read();
129         }
130     }
131 
132     @SuppressWarnings("resource") // instance variable access
133     @Test
134     public void testConstructorCloseOnCloseTrue() throws IOException {
135         try (RandomAccessFile file = createRandomAccessFile()) {
136             try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, true)) {
137                 assertTrue(inputStream.isCloseOnClose());
138                 assertNotEquals(-1, inputStream.getRandomAccessFile().read());
139             }
140             assertThrows(IOException.class, file::read);
141         }
142     }
143 
144     @SuppressWarnings("resource") // instance variable access
145     @Test
146     public void testConstructorRandomAccessFile() throws IOException {
147         try (RandomAccessFile file = createRandomAccessFile()) {
148             try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file)) {
149                 assertFalse(inputStream.isCloseOnClose());
150                 assertNotEquals(-1, inputStream.getRandomAccessFile().read());
151             }
152             file.read();
153         }
154     }
155 
156     @Test
157     public void testConstructorRandomAccessFileNull() {
158         assertThrows(NullPointerException.class, () -> new RandomAccessFileInputStream(null));
159     }
160 
161     @Test
162     public void testGetters() throws IOException {
163         try (RandomAccessFile file = createRandomAccessFile()) {
164             try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, true)) {
165                 assertEquals(file, inputStream.getRandomAccessFile());
166                 assertTrue(inputStream.isCloseOnClose());
167             }
168         }
169     }
170 
171     @Test
172     public void testRead() throws IOException {
173         try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
174             true)) {
175             // A Test Line.
176             assertEquals('A', inputStream.read());
177             assertEquals(' ', inputStream.read());
178             assertEquals('T', inputStream.read());
179             assertEquals('e', inputStream.read());
180             assertEquals('s', inputStream.read());
181             assertEquals('t', inputStream.read());
182             assertEquals(' ', inputStream.read());
183             assertEquals('L', inputStream.read());
184             assertEquals('i', inputStream.read());
185             assertEquals('n', inputStream.read());
186             assertEquals('e', inputStream.read());
187             assertEquals('.', inputStream.read());
188             assertEquals(DATA_FILE_LEN - 12, inputStream.available());
189             assertEquals(DATA_FILE_LEN - 12, inputStream.availableLong());
190         }
191     }
192 
193     @Test
194     public void testReadByteArray() throws IOException {
195         try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
196             true)) {
197             // A Test Line.
198             final int dataLen = 12;
199             final byte[] buffer = new byte[dataLen];
200             assertEquals(dataLen, inputStream.read(buffer));
201             assertArrayEquals("A Test Line.".getBytes(StandardCharsets.ISO_8859_1), buffer);
202             //
203             assertEquals(DATA_FILE_LEN - dataLen, inputStream.available());
204             assertEquals(DATA_FILE_LEN - dataLen, inputStream.availableLong());
205         }
206     }
207 
208     @Test
209     public void testReadByteArrayBounds() throws IOException {
210         try (RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(createRandomAccessFile(),
211             true)) {
212             // A Test Line.
213             final int dataLen = 12;
214             final byte[] buffer = new byte[dataLen];
215             assertEquals(dataLen, inputStream.read(buffer, 0, dataLen));
216             assertArrayEquals("A Test Line.".getBytes(StandardCharsets.ISO_8859_1), buffer);
217             //
218             assertEquals(DATA_FILE_LEN - dataLen, inputStream.available());
219             assertEquals(DATA_FILE_LEN - dataLen, inputStream.availableLong());
220         }
221     }
222 
223     @Test
224     public void testSkip() throws IOException {
225 
226         try (RandomAccessFile file = createRandomAccessFile();
227             final RandomAccessFileInputStream inputStream = new RandomAccessFileInputStream(file, false)) {
228             assertEquals(0, inputStream.skip(-1));
229             assertEquals(0, inputStream.skip(Integer.MIN_VALUE));
230             assertEquals(0, inputStream.skip(0));
231             // A Test Line.
232             assertEquals('A', inputStream.read());
233             assertEquals(1, inputStream.skip(1));
234             assertEquals('T', inputStream.read());
235             assertEquals(1, inputStream.skip(1));
236             assertEquals('s', inputStream.read());
237             assertEquals(1, inputStream.skip(1));
238             assertEquals(' ', inputStream.read());
239             assertEquals(1, inputStream.skip(1));
240             assertEquals('i', inputStream.read());
241             assertEquals(1, inputStream.skip(1));
242             assertEquals('e', inputStream.read());
243             assertEquals(1, inputStream.skip(1));
244             //
245             assertEquals(DATA_FILE_LEN - 12, inputStream.available());
246             assertEquals(DATA_FILE_LEN - 12, inputStream.availableLong());
247             assertEquals(10, inputStream.skip(10));
248             assertEquals(DATA_FILE_LEN - 22, inputStream.availableLong());
249             //
250             final long avail = inputStream.availableLong();
251             assertEquals(avail, inputStream.skip(inputStream.availableLong()));
252             // At EOF
253             assertEquals(DATA_FILE_LEN, file.length());
254             assertEquals(DATA_FILE_LEN, file.getFilePointer());
255             //
256             assertEquals(0, inputStream.skip(1));
257             assertEquals(0, inputStream.skip(1000000000000L));
258         }
259     }
260 }