You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2022/10/07 18:55:23 UTC

[GitHub] [commons-io] digiovinazzo commented on a diff in pull request #391: IO-782 sequence reader should close readers when its close method is called

digiovinazzo commented on code in PR #391:
URL: https://github.com/apache/commons-io/pull/391#discussion_r990414931


##########
src/test/java/org/apache/commons/io/input/SequenceReaderTest.java:
##########
@@ -56,14 +60,86 @@ private void checkReadEof(final Reader reader) throws IOException {
     }
 
     @Test
-    public void testClose() throws IOException {
+    public void testAutoClose() throws IOException {
         try (Reader reader = new SequenceReader(new CharSequenceReader("FooBar"))) {
             checkRead(reader, "Foo");
             reader.close();
             checkReadEof(reader);
         }
     }
 
+    @Test
+    public void testClose() throws IOException {
+        final Reader reader = new SequenceReader(new CharSequenceReader("FooBar"));
+        checkRead(reader, "Foo");
+        reader.close();
+        checkReadEof(reader);
+    }
+
+    @Test
+    public void testCloseReaders() {
+        final AtomicBoolean closeEmptyReader = new AtomicBoolean(false);
+        final Reader emptyReader = new Reader() {
+            @Override
+            public int read(char[] cbuf, int off, int len) throws IOException {
+                if (closeEmptyReader.get()) {
+                    throw new IOException("emptyReader already closed");
+                }
+
+                return EOF;
+            }
+
+            @Override
+            public void close() throws IOException {
+                closeEmptyReader.set(true);
+            }
+        };
+
+        final AtomicBoolean closeReader1 = new AtomicBoolean(false);
+        final Reader reader1 = new Reader() {
+            private final char[] content = new char[]{'A'};
+            private int position = 0;
+
+            @Override
+            public int read(char[] cbuf, int off, int len) throws IOException {
+                if (closeReader1.get()) {
+                    throw new IOException("reader1 already closed");
+                }
+
+                if (off < 0) {
+                    throw new IndexOutOfBoundsException("off is negative");
+                } else if (len < 0) {
+                    throw new IndexOutOfBoundsException("len is negative");
+                } else if (len > cbuf.length - off) {
+                    throw new IndexOutOfBoundsException("len is greater than cbuf.length - off");
+                }
+
+                if (position > 0) {
+                    return EOF;
+                }
+
+                cbuf[off] = content[0];
+                position++;
+                return 1;
+            }
+
+            @Override
+            public void close() throws IOException {
+                closeReader1.set(true);
+            }
+        };
+
+        try (SequenceReader sequenceReader = new SequenceReader(reader1, emptyReader)) {
+            assertEquals('A', sequenceReader.read());
+            assertEquals(EOF, sequenceReader.read());
+        } catch (IOException e) {
+            fail("No IOException expected");

Review Comment:
   That's another option, it was just to have a meaningful message on fail



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org