diff --git a/src/main/java/org/sqlite/driver/ResultSetIterable.java b/src/main/java/org/sqlite/driver/ResultSetIterable.java index 93a6b6b..7007713 100644 --- a/src/main/java/org/sqlite/driver/ResultSetIterable.java +++ b/src/main/java/org/sqlite/driver/ResultSetIterable.java @@ -75,12 +75,14 @@ public boolean hasNext() { @Override public T next() { if (!hasNext()) { + state = State.FAILED; throw new NoSuchElementException(); } state = State.NOT_READY; try { return mapper.map(rs); } catch (SQLException e) { + state = State.FAILED; return sneakyThrow(e); } } diff --git a/src/test/java/org/sqlite/driver/ResultSetIterableTest.java b/src/test/java/org/sqlite/driver/ResultSetIterableTest.java new file mode 100644 index 0000000..e3a142a --- /dev/null +++ b/src/test/java/org/sqlite/driver/ResultSetIterableTest.java @@ -0,0 +1,104 @@ +package org.sqlite.driver; + +import junit.framework.TestCase; +import org.junit.Test; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Iterator; +import java.util.NoSuchElementException; + +import static org.junit.Assert.*; + +public class ResultSetIterableTest { + @Test + public void close() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT ?"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + rsi.close(); + } + } + @Test + public void iterator() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT 2"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + long sum = 0; + for (Long l : rsi) { + sum += l; + } + assertEquals(3L, sum); + } + } + @Test + public void stream() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT 2"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + assertEquals(3L, rsi.stream().mapToLong(l -> l.longValue()).sum()); + } + } + + @Test + public void empty() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT 1 LIMIT 0"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + assertEquals(0, rsi.stream().count()); + + { + Iterator iterator = rsi.iterator(); + try { + iterator.next(); + fail("No such element"); + } catch (NoSuchElementException e) { + } + try { + iterator.next(); + fail("Illegal state"); + } catch (IllegalStateException e) { + } + } + { + Iterator iterator = rsi.iterator(); + assertFalse(iterator.hasNext()); + assertFalse(iterator.hasNext()); + } + } + } + + @Test(expected = SQLException.class) + public void map_error() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT 'a'"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + for (Long l : rsi) { + assertEquals(1L, l.longValue()); + } + } + } + + @Test(expected = SQLException.class) + public void runtime_error() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT '{[]}' -> 1"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + for (Long l : rsi) { + assertEquals(1L, l.longValue()); + } + } + } + + @Test(expected = SQLException.class) + public void invalid_query() throws SQLException { + try (Connection c = DriverManager.getConnection(JDBC.MEMORY); + PreparedStatement s = c.prepareStatement("SELECT ?"); + ResultSetIterable rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { + rsi.iterator(); + } + } +}