Repository: nifi Updated Branches: refs/heads/master 34b678d30 -> 336d3cf1f
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 <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/nifi/repo Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/336d3cf1 Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/336d3cf1 Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/336d3cf1 Branch: refs/heads/master Commit: 336d3cf1f2641bdf72e8f1847f16f01d4780b186 Parents: 34b678d Author: Andy LoPresto <[email protected]> Authored: Thu Feb 8 12:30:24 2018 -0800 Committer: Kevin Doran <[email protected]> Committed: Fri Feb 9 10:49:58 2018 -0500 ---------------------------------------------------------------------- .../nifi/stream/io/ByteCountingInputStream.java | 5 +++ .../stream/io/ByteCountingInputStreamTest.java | 32 ++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/nifi/blob/336d3cf1/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/ByteCountingInputStream.java ---------------------------------------------------------------------- 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 e9b8c9e..4c2372c 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(); + } } http://git-wip-us.apache.org/repos/asf/nifi/blob/336d3cf1/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/ByteCountingInputStreamTest.java ---------------------------------------------------------------------- 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 27b1493..1ab5f31 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
