[ 
https://issues.apache.org/jira/browse/IO-782?focusedWorklogId=814779&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-814779
 ]

ASF GitHub Bot logged work on IO-782:
-------------------------------------

                Author: ASF GitHub Bot
            Created on: 07/Oct/22 18:50
            Start Date: 07/Oct/22 18:50
    Worklog Time Spent: 10m 
      Work Description: garydgregory commented on code in PR #391:
URL: https://github.com/apache/commons-io/pull/391#discussion_r990411492


##########
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:
   Why not just let the exception percolate to fail the test?





Issue Time Tracking
-------------------

    Worklog Id:     (was: 814779)
    Time Spent: 40m  (was: 0.5h)

> SequenceReader should close readers when its close method is called
> -------------------------------------------------------------------
>
>                 Key: IO-782
>                 URL: https://issues.apache.org/jira/browse/IO-782
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Streams/Writers
>    Affects Versions: 2.11.0
>            Reporter: matteodg
>            Priority: Major
>          Time Spent: 40m
>  Remaining Estimate: 0h
>
> {{SequenceReader.close()}} method should close readers.
> This is to be compliant with common practice of any {{Reader}} decorator, 
> like for example {{BufferedReader}} is doing.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to