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
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
34
35
36
37
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
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
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
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
149
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 }