Skip to content

Commit 27ca6db

Browse files
committed
org.apache.commons.dbutils.ResultSetIterator.next() now throws
NoSuchElementException as defined in java.util.Iterator.next() See also PR apache#283
1 parent 0742165 commit 27ca6db

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The <action> type attribute can be add,update,fix,remove.
5757
<action type="fix" dev="ggregory" due-to="Gary Gregory">ResultSetIterator.hasNext() now throws IllegalStateException instead of RuntimeException to wrap cases of SQLException.</action>
5858
<action type="fix" dev="ggregory" due-to="Gary Gregory">ResultSetIterator.next() now throws IllegalStateException instead of RuntimeException to wrap cases of SQLException.</action>
5959
<action type="fix" dev="ggregory" due-to="Gary Gregory">ResultSetIterator.remove() now throws IllegalStateException instead of RuntimeException to wrap cases of SQLException.</action>
60+
<action type="fix" dev="ggregory" due-to="Gary Gregory, Matheus_Maas">org.apache.commons.dbutils.ResultSetIterator.next() now throws NoSuchElementException as defined in java.util.Iterator.next() #283.</action>
6061
<!-- ADD -->
6162
<action type="add" dev="ggregory" due-to="strangelookingnerd, Gary Gregory">BaseResultSetHandler implements ResultSet.</action>
6263
<!-- UPDATE -->

src/main/java/org/apache/commons/dbutils/ResultSetIterator.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.sql.ResultSet;
2020
import java.sql.SQLException;
2121
import java.util.Iterator;
22+
import java.util.NoSuchElementException;
2223

2324
/**
2425
* <p>
@@ -97,17 +98,22 @@ public boolean hasNext() {
9798
*
9899
* @return An {@code Object[]} with the same number of elements as
99100
* columns in the {@code ResultSet}.
100-
* @see java.util.Iterator#next()
101101
* @throws RuntimeException if an SQLException occurs.
102+
* @throws NoSuchElementException if the iteration has no more elements.
103+
* @see java.util.Iterator#next()
102104
*/
103105
@Override
104106
public Object[] next() {
105-
try {
106-
return resultSet.next() ? this.convert.toArray(resultSet) : new Object[0];
107-
} catch (final SQLException e) {
108-
rethrow(e);
109-
return null;
107+
if (hasNext()) {
108+
try {
109+
resultSet.next();
110+
return this.convert.toArray(resultSet);
111+
} catch (final SQLException e) {
112+
rethrow(e);
113+
return null;
114+
}
110115
}
116+
throw new NoSuchElementException("No more rows in the ResultSet");
111117
}
112118

113119
/**

src/test/java/org/apache/commons/dbutils/ResultSetIteratorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
2223
import static org.junit.jupiter.api.Assertions.fail;
2324
import static org.mockito.Mockito.mock;
2425

2526
import java.sql.ResultSet;
2627
import java.sql.SQLException;
2728
import java.util.Iterator;
29+
import java.util.NoSuchElementException;
2830

2931
import org.junit.jupiter.api.Test;
3032

@@ -63,7 +65,7 @@ void testNext() {
6365
assertEquals("SIX", row[2]);
6466

6567
assertFalse(iter.hasNext());
66-
assertEquals(0, iter.next().length);
68+
assertThrows(NoSuchElementException.class, iter::next);
6769
}
6870

6971
@Test

0 commit comments

Comments
 (0)