From 412d05d166540dc3dc97ac5c08958934ea7112b3 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Thu, 1 Aug 2013 19:41:10 +0000 Subject: [PATCH] Better throw a NoSuchElementException if no more elements are available because parser has been closed git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1509395 13f79535-47bb-0310-9956-ffa450edef68 --- src/main/java/org/apache/commons/csv/CSVParser.java | 8 ++++++-- src/test/java/org/apache/commons/csv/CSVParserTest.java | 6 ++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index c2437a70..67338e67 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -381,8 +381,12 @@ public class CSVParser implements Iterable, Closeable { } /** - * Returns an iterator on the records. IOExceptions occurring during the iteration are wrapped in a + * Returns an iterator on the records. + * + *

IOExceptions occurring during the iteration are wrapped in a * RuntimeException. + * If the parser is closed a call to {@code next()} will throw a + * NoSuchElementException.

*/ public Iterator iterator() { return new Iterator() { @@ -410,7 +414,7 @@ public class CSVParser implements Iterable, Closeable { public CSVRecord next() { if (CSVParser.this.isClosed()) { - return null; + throw new NoSuchElementException("CSVParser has been closed"); } CSVRecord next = this.current; this.current = null; diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 6941bea5..6eab7354 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -394,7 +394,7 @@ public class CSVParserTest { assertEquals(4, records.size()); } - @Test + @Test(expected = NoSuchElementException.class) public void testClose() throws Exception { final Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z"); final CSVParser parser = CSVFormat.DEFAULT.withCommentStart('#').withHeader().parse(in); @@ -402,9 +402,7 @@ public class CSVParserTest { assertTrue(records.hasNext()); parser.close(); assertFalse(records.hasNext()); - assertNull(records.next()); - assertFalse(records.hasNext()); - assertNull(records.next()); + records.next(); } @Test