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
This commit is contained in:
Benedikt Ritter 2013-08-01 19:41:10 +00:00
parent 7e63096d47
commit 412d05d166
2 changed files with 8 additions and 6 deletions

View File

@ -381,8 +381,12 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
}
/**
* Returns an iterator on the records. IOExceptions occurring during the iteration are wrapped in a
* Returns an iterator on the records.
*
* <p>IOExceptions occurring during the iteration are wrapped in a
* RuntimeException.
* If the parser is closed a call to {@code next()} will throw a
* NoSuchElementException.</p>
*/
public Iterator<CSVRecord> iterator() {
return new Iterator<CSVRecord>() {
@ -410,7 +414,7 @@ public class CSVParser implements Iterable<CSVRecord>, Closeable {
public CSVRecord next() {
if (CSVParser.this.isClosed()) {
return null;
throw new NoSuchElementException("CSVParser has been closed");
}
CSVRecord next = this.current;
this.current = null;

View File

@ -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