This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git


The following commit(s) were added to refs/heads/master by this push:
     new 8c4cad3a Add tests CSVParserTest.testTryWithResourcesParse*
8c4cad3a is described below

commit 8c4cad3a6e016350612d775e01e11976f9fd0b1d
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Aug 1 07:27:15 2026 -0400

    Add tests CSVParserTest.testTryWithResourcesParse*
    
    - Add testTryWithResourcesParseInputStreamWhenHeaderIsInvalid
    - Add testTryWithResourcesParseReaderWhenHeaderIsInvalid
---
 .../java/org/apache/commons/csv/CSVParserTest.java | 41 ++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java 
b/src/test/java/org/apache/commons/csv/CSVParserTest.java
index c9caa308..a0204142 100644
--- a/src/test/java/org/apache/commons/csv/CSVParserTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java
@@ -31,7 +31,10 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.FilterInputStream;
+import java.io.FilterReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
@@ -53,6 +56,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -2061,6 +2065,43 @@ class CSVParserTest {
         }
     }
 
+    @Test
+    void testTryWithResourcesParseInputStreamWhenHeaderIsInvalid() throws 
IOException {
+        final AtomicBoolean closed = new AtomicBoolean();
+        final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().get();
+        try (InputStream inputStream = new FilterInputStream(new 
ByteArrayInputStream("A,,C\n1,2,3\n".getBytes(UTF_8))) {
+
+            @Override
+            public void close() throws IOException {
+                closed.set(true);
+                super.close();
+            }
+        }) {
+            assertThrows(IllegalArgumentException.class, () -> 
CSVParser.parse(inputStream, UTF_8, format));
+            // parse(Path), parse(File) and parse(URL) open the stream 
themselves and reach this same code path.
+            assertFalse(closed.get(), "The input stream must be closed when 
the parser cannot be constructed");
+        }
+        assertTrue(closed.get(), "The input stream must be closed when the 
parser cannot be constructed");
+    }
+
+    @Test
+    void testTryWithResourcesParseReaderWhenHeaderIsInvalid() throws 
IOException {
+        final AtomicBoolean closed = new AtomicBoolean();
+        final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().get();
+        try (Reader reader = new FilterReader(new 
StringReader("A,,C\n1,2,3\n")) {
+
+            @Override
+            public void close() throws IOException {
+                closed.set(true);
+                super.close();
+            }
+        }) {
+            assertThrows(IllegalArgumentException.class, () -> 
CSVParser.builder().setReader(reader).setFormat(format).get());
+            assertFalse(closed.get(), "The input stream must be closed when 
the parser cannot be constructed");
+        }
+        assertTrue(closed.get(), "The reader must be closed when the parser 
cannot be constructed");
+    }
+
     private void validateLineNumbers(final String lineSeparator) throws 
IOException {
         try (CSVParser parser = CSVParser.parse("a" + lineSeparator + "b" + 
lineSeparator + "c", CSVFormat.DEFAULT.withRecordSeparator(lineSeparator))) {
             assertEquals(0, parser.getCurrentLineNumber());

Reply via email to