1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import java.util.function.Supplier;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.lang3.RandomUtils;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.io.TempDir;
36 import org.junit.jupiter.params.ParameterizedTest;
37 import org.junit.jupiter.params.provider.MethodSource;
38
39
40
41
42
43
44
45
46 public abstract class AbstractInputStreamTest {
47
48 static final String ARRAY_LENGTHS_NAME = "org.apache.commons.io.input.AbstractInputStreamTest#getArrayLengths";
49
50 static final int[] ARRAY_LENGTHS = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
51
52 static int[] getArrayLengths() {
53 return ARRAY_LENGTHS;
54 }
55
56 protected static byte[] ActualBytes;
57 protected static byte[] ExpectedBytes;
58 protected static Path InputPath;
59
60
61
62
63 protected InputStream[] inputStreams;
64
65 @TempDir
66 static Path tempDir;
67
68 @BeforeAll
69 public static void setUp() throws IOException {
70
71 ActualBytes = RandomUtils.insecure().randomBytes(2 * 1024 * 1024);
72 ExpectedBytes = ActualBytes.clone();
73 InputPath = tempDir.resolve(AbstractInputStreamTest.class.getSimpleName() + ".tmp");
74 Files.write(InputPath, ActualBytes);
75 }
76
77 @AfterEach
78 public void tearDown() throws IOException {
79 IOUtils.close(inputStreams);
80 }
81
82 @Test
83 public void testAvailableAfterClose() throws Exception {
84 for (final InputStream inputStream : inputStreams) {
85 inputStream.close();
86 assertEquals(0, inputStream.available());
87 }
88 }
89
90 @Test
91 public void testAvailableAfterOpen() throws Exception {
92 for (final InputStream inputStream : inputStreams) {
93 assertTrue(inputStream.available() >= 0);
94 }
95 }
96
97 @Test
98 public void testAvailableAfterRead() throws Exception {
99 for (final InputStream inputStream : inputStreams) {
100 assertNotEquals(IOUtils.EOF, inputStream.read());
101 assertTrue(inputStream.available() > 0);
102 }
103 }
104
105 @Test
106 public void testAvailableAtEnd() throws Exception {
107 for (final InputStream inputStream : inputStreams) {
108 IOUtils.consume(inputStream);
109 assertEquals(0, inputStream.available());
110 }
111 }
112
113 @Test
114 public void testBytesSkipped() throws IOException {
115 for (final InputStream inputStream : inputStreams) {
116 assertEquals(1024, inputStream.skip(1024));
117 for (int i = 1024; i < ExpectedBytes.length; i++) {
118 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
119 }
120 }
121 }
122
123 @Test
124 public void testBytesSkippedAfterEOF() throws IOException {
125 for (final InputStream inputStream : inputStreams) {
126 assertEquals(ExpectedBytes.length, inputStream.skip(ActualBytes.length + 1));
127 assertEquals(-1, inputStream.read());
128 }
129 }
130
131 @Test
132 public void testBytesSkippedAfterRead() throws IOException {
133 for (final InputStream inputStream : inputStreams) {
134 for (int i = 0; i < 1024; i++) {
135 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
136 }
137 assertEquals(1024, inputStream.skip(1024));
138 for (int i = 2048; i < ExpectedBytes.length; i++) {
139 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
140 }
141 }
142 }
143
144 @Test
145 public void testNegativeBytesSkippedAfterRead() throws IOException {
146 for (final InputStream inputStream : inputStreams) {
147 for (int i = 0; i < 1024; i++) {
148 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
149 }
150
151 assertEquals(0, inputStream.skip(-1));
152 assertEquals(0, inputStream.skip(-1024));
153 assertEquals(0, inputStream.skip(Long.MIN_VALUE));
154 assertEquals(1024, inputStream.skip(1024));
155 for (int i = 2048; i < ExpectedBytes.length; i++) {
156 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
157 }
158 }
159 }
160
161 @ParameterizedTest
162 @MethodSource("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes")
163 public void testReadMultipleBytes(final int bufferSize) throws IOException {
164 for (final InputStream inputStream : inputStreams) {
165 final byte[] readBytes = new byte[bufferSize];
166 int i = 0;
167 while (i < ExpectedBytes.length) {
168 final int read = inputStream.read(readBytes, 0, readBytes.length);
169 for (int j = 0; j < read; j++) {
170 assertEquals(ExpectedBytes[i], readBytes[j]);
171 i++;
172 }
173 }
174 }
175 }
176
177 @Test
178 public void testReadOneByOne() throws IOException {
179 for (final InputStream inputStream : inputStreams) {
180 for (final byte randomByte : ExpectedBytes) {
181 assertEquals(randomByte, (byte) inputStream.read());
182 }
183 }
184 }
185
186 @Test
187 public void testReadOneByOneCheckAvailable() throws IOException {
188 final AtomicInteger refII = new AtomicInteger();
189 for (int idxInputs = 0; idxInputs < inputStreams.length; idxInputs++) {
190 refII.set(idxInputs);
191 final AtomicInteger refIB = new AtomicInteger();
192 @SuppressWarnings("resource")
193 final InputStream inputStream = inputStreams[idxInputs];
194 for (int idxBytes = 0; idxBytes < ExpectedBytes.length; idxBytes++) {
195 refIB.set(idxBytes);
196 final byte randomByte = ExpectedBytes[idxBytes];
197
198 final int available = inputStream.available();
199 final Supplier<String> messageSupplier = () -> String.format("idxInputs = %,d, idxBytes = %,d, available = %,d", refII.get(), refIB.get(),
200 available);
201 assertTrue(available >= 0, messageSupplier);
202 assertEquals(randomByte, (byte) inputStream.read());
203 }
204 }
205 }
206
207 @Test
208 public void testReadPastEof() throws IOException {
209 final InputStream is = inputStreams[0];
210 final byte[] buf = new byte[1024];
211 while (is.read(buf, 0, buf.length) != -1) {
212
213 }
214 final int readAfterEOF = is.read(buf, 0, buf.length);
215 assertEquals(-1, readAfterEOF);
216 }
217
218 @Test
219 public void testSkipFromFileChannel() throws IOException {
220 for (final InputStream inputStream : inputStreams) {
221
222
223 assertEquals(1024, inputStream.skip(1024));
224 for (int i = 1024; i < 2048; i++) {
225 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
226 }
227 assertEquals(256, inputStream.skip(256));
228 assertEquals(256, inputStream.skip(256));
229 assertEquals(512, inputStream.skip(512));
230 for (int i = 3072; i < ExpectedBytes.length; i++) {
231 assertEquals(ExpectedBytes[i], (byte) inputStream.read());
232 }
233 }
234 }
235 }