Skip to content

Commit e48e3e3

Browse files
committed
Migrate from deprecated code
Add a test to check that available() doesn't have a side effect on read()
1 parent 819afc8 commit e48e3e3

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/test/java/org/apache/commons/io/input/AbstractInputStreamTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.InputStream;
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
27+
import java.util.concurrent.atomic.AtomicInteger;
28+
import java.util.function.Supplier;
2729

2830
import org.apache.commons.io.IOUtils;
2931
import org.apache.commons.lang3.RandomUtils;
@@ -56,7 +58,7 @@ static int[] getArrayLengths() {
5658
@BeforeEach
5759
public void setUp() throws IOException {
5860
// Create a byte array of size 2 MB with random bytes
59-
actualRandomBytes = RandomUtils.insecure().nextBytes(2 * 1024 * 1024);
61+
actualRandomBytes = RandomUtils.insecure().randomBytes(2 * 1024 * 1024);
6062
expectedRandomBytes = actualRandomBytes;
6163
inputFile = Files.createTempFile("temp-file", ".tmp");
6264
Files.write(inputFile, actualRandomBytes);
@@ -171,6 +173,27 @@ public void testReadOneByOne() throws IOException {
171173
}
172174
}
173175

176+
@Test
177+
public void testReadOneByOneCheckAvailable() throws IOException {
178+
final AtomicInteger refII = new AtomicInteger();
179+
for (int idxInputs = 0; idxInputs < inputStreams.length; idxInputs++) {
180+
refII.set(idxInputs);
181+
final AtomicInteger refIB = new AtomicInteger();
182+
@SuppressWarnings("resource")
183+
final InputStream inputStream = inputStreams[idxInputs];
184+
for (int idxBytes = 0; idxBytes < expectedRandomBytes.length; idxBytes++) {
185+
refIB.set(idxBytes);
186+
final byte randomByte = expectedRandomBytes[idxBytes];
187+
// Check that available() doesn't have a side effect on read()
188+
final int available = inputStream.available();
189+
final Supplier<String> messageSupplier = () -> String.format("idxInputs = %,d, idxBytes = %,d, available = %,d", refII.get(), refIB.get(),
190+
available);
191+
assertTrue(available >= 0, messageSupplier);
192+
assertEquals(randomByte, (byte) inputStream.read());
193+
}
194+
}
195+
}
196+
174197
@Test
175198
public void testReadPastEOF() throws IOException {
176199
final InputStream is = inputStreams[0];

0 commit comments

Comments
 (0)