NIFI-4856 Removed deprecated ByteArrayInputStream references in ByteCountingInputStreamTest.

Added failing unit test for #available() at various states (initial, during read, after read).
Implemented #available() delegation.
All tests pass.

This closes #2461.

Signed-off-by: Kevin Doran <kdoran@apache.org>
This commit is contained in:
Andy LoPresto 2018-02-08 12:30:24 -08:00 committed by Kevin Doran
parent 34b678d30d
commit 336d3cf1f2
No known key found for this signature in database
GPG Key ID: 5621A6244B77AC02
2 changed files with 34 additions and 3 deletions

View File

@ -107,4 +107,9 @@ public class ByteCountingInputStream extends InputStream {
public void close() throws IOException {
in.close();
}
@Override
public int available() throws IOException {
return in.available();
}
}

View File

@ -16,15 +16,15 @@
*/
package org.apache.nifi.stream.io;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import junit.framework.TestCase;
public class ByteCountingInputStreamTest extends TestCase {
final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes());
public void testReset() throws Exception {
final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes());
final ByteArrayInputStream reader = new ByteArrayInputStream("abcdefghijklmnopqrstuvwxyz".getBytes(StandardCharsets.UTF_8));
final ByteCountingInputStream bcis = new ByteCountingInputStream(reader);
int tmp;
@ -52,4 +52,30 @@ public class ByteCountingInputStreamTest extends TestCase {
bcis.reset();
assertEquals(bytesAtMark, bcis.getBytesRead());
}
public void testAvailableShouldReturnCorrectCount() throws Exception {
// Arrange
final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
final ByteArrayInputStream inputStream = new ByteArrayInputStream(ALPHABET.getBytes(StandardCharsets.UTF_8));
final ByteCountingInputStream bcis = new ByteCountingInputStream(inputStream);
int tmp;
int initialAvailableBytes = bcis.available();
assertEquals(ALPHABET.length(), initialAvailableBytes);
// Act
/* verify first 2 bytes */
tmp = bcis.read();
assertEquals(tmp, 97);
tmp = bcis.read();
assertEquals(tmp, 98);
int availableBytes = bcis.available();
assertEquals(ALPHABET.length() - 2, availableBytes);
bcis.skip(24);
// Assert
int finalAvailableBytes = bcis.available();
assertEquals(0, finalAvailableBytes);
}
}