From 336d3cf1f2641bdf72e8f1847f16f01d4780b186 Mon Sep 17 00:00:00 2001 From: Andy LoPresto Date: Thu, 8 Feb 2018 12:30:24 -0800 Subject: [PATCH] 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 --- .../stream/io/ByteCountingInputStream.java | 5 +++ .../io/ByteCountingInputStreamTest.java | 32 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/ByteCountingInputStream.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/ByteCountingInputStream.java index e9b8c9ea72..4c2372caf9 100644 --- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/ByteCountingInputStream.java +++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/ByteCountingInputStream.java @@ -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(); + } } diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java index 27b149341b..1ab5f31cbb 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java @@ -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); + } } \ No newline at end of file