From 6a625e954eb5934913316b9b15e58e8a759a3153 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 7 Jul 2024 18:15:54 -0400 Subject: [PATCH] Add CharSequenceInputStreamTest.testReadAfterClose() --- .../io/input/CharSequenceInputStreamTest.java | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java index ef7cbc5adec..b2d23ce6ad7 100644 --- a/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/CharSequenceInputStreamTest.java @@ -81,19 +81,6 @@ private boolean isOddBallLegacyCharsetThatDoesNotSupportFrenchCharacters(final S "Shift_JIS".equalsIgnoreCase(csName); } - /** - * IO-781 available() returns 2 but only 1 byte is read afterwards. - */ - @Test - public void testAvailable() throws IOException { - final Charset charset = Charset.forName("Big5"); - final CharSequenceInputStream in = new CharSequenceInputStream("\uD800\uDC00", charset); - final int available = in.available(); - final byte[] data = new byte[available]; - final int bytesRead = in.read(data); - assertEquals(available, bytesRead); - } - @ParameterizedTest(name = "{0}") @MethodSource(CharsetsTest.AVAIL_CHARSETS) public void testAvailable(final String csName) throws Exception { @@ -128,6 +115,20 @@ public void testAvailableAfterClose() throws Exception { assertEquals(0, shadow.available()); } + /** + * IO-781 available() returns 2 but only 1 byte is read afterwards. + */ + @Test + public void testAvailableAfterOpen() throws IOException { + final Charset charset = Charset.forName("Big5"); + try (CharSequenceInputStream in = new CharSequenceInputStream("\uD800\uDC00", charset)) { + final int available = in.available(); + final byte[] data = new byte[available]; + final int bytesRead = in.read(data); + assertEquals(available, bytesRead); + } + } + private void testAvailableRead(final String csName) throws Exception { final String input = "test"; try (InputStream r = new CharSequenceInputStream(input, csName)) { @@ -447,6 +448,16 @@ public void testNullCharsetName() throws IOException { } } + @Test + public void testReadAfterClose() throws Exception { + final InputStream shadow; + try (InputStream in = CharSequenceInputStream.builder().setCharSequence("Hi").get()) { + assertTrue(in.available() > 0); + shadow = in; + } + assertEquals(IOUtils.EOF, shadow.read()); + } + private void testReadZero(final String csName) throws Exception { try (InputStream r = new CharSequenceInputStream("test", csName)) { final byte[] bytes = new byte[30];