3030import org .apache .commons .io .IOUtils ;
3131import org .apache .commons .lang3 .RandomUtils ;
3232import org .junit .jupiter .api .AfterEach ;
33- import org .junit .jupiter .api .BeforeEach ;
33+ import org .junit .jupiter .api .BeforeAll ;
3434import 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 ;
3538
3639/**
3740 * Tests {@link InputStream} subclasses.
@@ -50,23 +53,29 @@ static int[] getArrayLengths() {
5053 return ARRAY_LENGTHS ;
5154 }
5255
53- protected byte [] actualRandomBytes ;
54- protected byte [] expectedRandomBytes ;
55- protected Path inputFile ;
56+ protected static byte [] ActualBytes ;
57+ protected static byte [] ExpectedBytes ;
58+ protected static Path InputPath ;
59+
60+ /**
61+ * Set in subclasses.
62+ */
5663 protected InputStream [] inputStreams ;
5764
58- @ BeforeEach
59- public void setUp () throws IOException {
65+ @ TempDir
66+ static Path tempDir ;
67+
68+ @ BeforeAll
69+ public static void setUp () throws IOException {
6070 // Create a byte array of size 2 MB with random bytes
61- actualRandomBytes = RandomUtils .insecure ().randomBytes (2 * 1024 * 1024 );
62- expectedRandomBytes = actualRandomBytes ;
63- inputFile = Files . createTempFile ( "temp-file" , ".tmp" );
64- Files .write (inputFile , actualRandomBytes );
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 );
6575 }
6676
6777 @ AfterEach
6878 public void tearDown () throws IOException {
69- Files .delete (inputFile );
7079 IOUtils .close (inputStreams );
7180 }
7281
@@ -81,7 +90,7 @@ public void testAvailableAfterClose() throws Exception {
8190 @ Test
8291 public void testAvailableAfterOpen () throws Exception {
8392 for (final InputStream inputStream : inputStreams ) {
84- assertEquals ( 0 , inputStream .available ());
93+ assertTrue ( inputStream .available () >= 0 );
8594 }
8695 }
8796
@@ -105,16 +114,16 @@ public void testAvailableAtEnd() throws Exception {
105114 public void testBytesSkipped () throws IOException {
106115 for (final InputStream inputStream : inputStreams ) {
107116 assertEquals (1024 , inputStream .skip (1024 ));
108- for (int i = 1024 ; i < expectedRandomBytes .length ; i ++) {
109- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
117+ for (int i = 1024 ; i < ExpectedBytes .length ; i ++) {
118+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
110119 }
111120 }
112121 }
113122
114123 @ Test
115124 public void testBytesSkippedAfterEOF () throws IOException {
116125 for (final InputStream inputStream : inputStreams ) {
117- assertEquals (expectedRandomBytes .length , inputStream .skip (actualRandomBytes .length + 1 ));
126+ assertEquals (ExpectedBytes .length , inputStream .skip (ActualBytes .length + 1 ));
118127 assertEquals (-1 , inputStream .read ());
119128 }
120129 }
@@ -123,11 +132,11 @@ public void testBytesSkippedAfterEOF() throws IOException {
123132 public void testBytesSkippedAfterRead () throws IOException {
124133 for (final InputStream inputStream : inputStreams ) {
125134 for (int i = 0 ; i < 1024 ; i ++) {
126- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
135+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
127136 }
128137 assertEquals (1024 , inputStream .skip (1024 ));
129- for (int i = 2048 ; i < expectedRandomBytes .length ; i ++) {
130- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
138+ for (int i = 2048 ; i < ExpectedBytes .length ; i ++) {
139+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
131140 }
132141 }
133142 }
@@ -136,28 +145,29 @@ public void testBytesSkippedAfterRead() throws IOException {
136145 public void testNegativeBytesSkippedAfterRead () throws IOException {
137146 for (final InputStream inputStream : inputStreams ) {
138147 for (int i = 0 ; i < 1024 ; i ++) {
139- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
148+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
140149 }
141150 // Skipping negative bytes should essential be a no-op
142151 assertEquals (0 , inputStream .skip (-1 ));
143152 assertEquals (0 , inputStream .skip (-1024 ));
144153 assertEquals (0 , inputStream .skip (Long .MIN_VALUE ));
145154 assertEquals (1024 , inputStream .skip (1024 ));
146- for (int i = 2048 ; i < expectedRandomBytes .length ; i ++) {
147- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
155+ for (int i = 2048 ; i < ExpectedBytes .length ; i ++) {
156+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
148157 }
149158 }
150159 }
151160
152- @ Test
153- public void testReadMultipleBytes () throws IOException {
161+ @ ParameterizedTest
162+ @ MethodSource ("org.apache.commons.io.input.ReversedLinesFileReaderParamBlockSizeTest#blockSizes" )
163+ public void testReadMultipleBytes (final int bufferSize ) throws IOException {
154164 for (final InputStream inputStream : inputStreams ) {
155- final byte [] readBytes = new byte [8 * 1024 ];
165+ final byte [] readBytes = new byte [bufferSize ];
156166 int i = 0 ;
157- while (i < expectedRandomBytes .length ) {
158- final int read = inputStream .read (readBytes , 0 , 8 * 1024 );
167+ while (i < ExpectedBytes .length ) {
168+ final int read = inputStream .read (readBytes , 0 , readBytes . length );
159169 for (int j = 0 ; j < read ; j ++) {
160- assertEquals (expectedRandomBytes [i ], readBytes [j ]);
170+ assertEquals (ExpectedBytes [i ], readBytes [j ]);
161171 i ++;
162172 }
163173 }
@@ -167,7 +177,7 @@ public void testReadMultipleBytes() throws IOException {
167177 @ Test
168178 public void testReadOneByOne () throws IOException {
169179 for (final InputStream inputStream : inputStreams ) {
170- for (final byte randomByte : expectedRandomBytes ) {
180+ for (final byte randomByte : ExpectedBytes ) {
171181 assertEquals (randomByte , (byte ) inputStream .read ());
172182 }
173183 }
@@ -181,9 +191,9 @@ public void testReadOneByOneCheckAvailable() throws IOException {
181191 final AtomicInteger refIB = new AtomicInteger ();
182192 @ SuppressWarnings ("resource" )
183193 final InputStream inputStream = inputStreams [idxInputs ];
184- for (int idxBytes = 0 ; idxBytes < expectedRandomBytes .length ; idxBytes ++) {
194+ for (int idxBytes = 0 ; idxBytes < ExpectedBytes .length ; idxBytes ++) {
185195 refIB .set (idxBytes );
186- final byte randomByte = expectedRandomBytes [idxBytes ];
196+ final byte randomByte = ExpectedBytes [idxBytes ];
187197 // Check that available() doesn't have a side effect on read()
188198 final int available = inputStream .available ();
189199 final Supplier <String > messageSupplier = () -> String .format ("idxInputs = %,d, idxBytes = %,d, available = %,d" , refII .get (), refIB .get (),
@@ -195,13 +205,12 @@ public void testReadOneByOneCheckAvailable() throws IOException {
195205 }
196206
197207 @ Test
198- public void testReadPastEOF () throws IOException {
208+ public void testReadPastEof () throws IOException {
199209 final InputStream is = inputStreams [0 ];
200210 final byte [] buf = new byte [1024 ];
201211 while (is .read (buf , 0 , buf .length ) != -1 ) {
202212 // empty
203213 }
204-
205214 final int readAfterEOF = is .read (buf , 0 , buf .length );
206215 assertEquals (-1 , readAfterEOF );
207216 }
@@ -213,13 +222,13 @@ public void testSkipFromFileChannel() throws IOException {
213222 // we skip from underlying file channel.
214223 assertEquals (1024 , inputStream .skip (1024 ));
215224 for (int i = 1024 ; i < 2048 ; i ++) {
216- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
225+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
217226 }
218227 assertEquals (256 , inputStream .skip (256 ));
219228 assertEquals (256 , inputStream .skip (256 ));
220229 assertEquals (512 , inputStream .skip (512 ));
221- for (int i = 3072 ; i < expectedRandomBytes .length ; i ++) {
222- assertEquals (expectedRandomBytes [i ], (byte ) inputStream .read ());
230+ for (int i = 3072 ; i < ExpectedBytes .length ; i ++) {
231+ assertEquals (ExpectedBytes [i ], (byte ) inputStream .read ());
223232 }
224233 }
225234 }
0 commit comments