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.io.input;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.apache.commons.lang3.RandomUtils;
28  import org.junit.jupiter.api.AfterEach;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests functionality of {@link BufferedFileChannelInputStream}.
34   * <p>
35   * This class was ported and adapted from Apache Spark commit 933dc6cb7b3de1d8ccaf73d124d6eb95b947ed19 where it was
36   * called {@code GenericFileInputStreamSuite}.
37   * </p>
38   */
39  public abstract class AbstractInputStreamTest {
40  
41      private byte[] randomBytes;
42  
43      protected Path inputFile;
44  
45      protected InputStream[] inputStreams;
46  
47      @BeforeEach
48      public void setUp() throws IOException {
49          // Create a byte array of size 2 MB with random bytes
50          randomBytes = RandomUtils.nextBytes(2 * 1024 * 1024);
51          inputFile = Files.createTempFile("temp-file", ".tmp");
52          Files.write(inputFile, randomBytes);
53      }
54  
55      @AfterEach
56      public void tearDown() throws IOException {
57          Files.delete(inputFile);
58          IOUtils.close(inputStreams);
59      }
60  
61      @Test
62      public void testBytesSkipped() throws IOException {
63          for (final InputStream inputStream : inputStreams) {
64              assertEquals(1024, inputStream.skip(1024));
65              for (int i = 1024; i < randomBytes.length; i++) {
66                  assertEquals(randomBytes[i], (byte) inputStream.read());
67              }
68          }
69      }
70  
71      @Test
72      public void testBytesSkippedAfterEOF() throws IOException {
73          for (final InputStream inputStream : inputStreams) {
74              assertEquals(randomBytes.length, inputStream.skip(randomBytes.length + 1));
75              assertEquals(-1, inputStream.read());
76          }
77      }
78  
79      @Test
80      public void testBytesSkippedAfterRead() throws IOException {
81          for (final InputStream inputStream : inputStreams) {
82              for (int i = 0; i < 1024; i++) {
83                  assertEquals(randomBytes[i], (byte) inputStream.read());
84              }
85              assertEquals(1024, inputStream.skip(1024));
86              for (int i = 2048; i < randomBytes.length; i++) {
87                  assertEquals(randomBytes[i], (byte) inputStream.read());
88              }
89          }
90      }
91  
92      @Test
93      public void testNegativeBytesSkippedAfterRead() throws IOException {
94          for (final InputStream inputStream : inputStreams) {
95              for (int i = 0; i < 1024; i++) {
96                  assertEquals(randomBytes[i], (byte) inputStream.read());
97              }
98              // Skipping negative bytes should essential be a no-op
99              assertEquals(0, inputStream.skip(-1));
100             assertEquals(0, inputStream.skip(-1024));
101             assertEquals(0, inputStream.skip(Long.MIN_VALUE));
102             assertEquals(1024, inputStream.skip(1024));
103             for (int i = 2048; i < randomBytes.length; i++) {
104                 assertEquals(randomBytes[i], (byte) inputStream.read());
105             }
106         }
107     }
108 
109     @Test
110     public void testReadMultipleBytes() throws IOException {
111         for (final InputStream inputStream : inputStreams) {
112             final byte[] readBytes = new byte[8 * 1024];
113             int i = 0;
114             while (i < randomBytes.length) {
115                 final int read = inputStream.read(readBytes, 0, 8 * 1024);
116                 for (int j = 0; j < read; j++) {
117                     assertEquals(randomBytes[i], readBytes[j]);
118                     i++;
119                 }
120             }
121         }
122     }
123 
124     @Test
125     public void testReadOneByte() throws IOException {
126         for (final InputStream inputStream : inputStreams) {
127             for (final byte randomByte : randomBytes) {
128                 assertEquals(randomByte, (byte) inputStream.read());
129             }
130         }
131     }
132 
133     @Test
134     public void testReadPastEOF() throws IOException {
135         final InputStream is = inputStreams[0];
136         final byte[] buf = new byte[1024];
137         while (is.read(buf, 0, buf.length) != -1) {
138             // empty
139         }
140 
141         final int readAfterEOF = is.read(buf, 0, buf.length);
142         assertEquals(-1, readAfterEOF);
143     }
144 
145     @Test
146     public void testSkipFromFileChannel() throws IOException {
147         for (final InputStream inputStream : inputStreams) {
148             // Since the buffer is smaller than the skipped bytes, this will guarantee
149             // we skip from underlying file channel.
150             assertEquals(1024, inputStream.skip(1024));
151             for (int i = 1024; i < 2048; i++) {
152                 assertEquals(randomBytes[i], (byte) inputStream.read());
153             }
154             assertEquals(256, inputStream.skip(256));
155             assertEquals(256, inputStream.skip(256));
156             assertEquals(512, inputStream.skip(512));
157             for (int i = 3072; i < randomBytes.length; i++) {
158                 assertEquals(randomBytes[i], (byte) inputStream.read());
159             }
160         }
161     }
162 }