@Test public void testNonPathologicalJustNext() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); // Yes, you *should* use first(). But sometimes, an iterator is passed 17 levels deep and then // used in this way (Hello Jackson!). final Map<String, Object> result = h.createQuery("select * from something order by id") .cleanupHandle() .iterator() .next(); assertEquals(1, result.get("id")); assertEquals("eric", result.get("name")); }
@Test public void testNonPathologicalJustNext() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); // Yes, you *should* use first(). But sometimes, an iterator is passed 17 levels deep and then // used in this way (Hello Jackson!). final Map<String, Object> result = h.createQuery("select * from something order by id") .cleanupHandle() .iterator() .next(); assertEquals(1, result.get("id")); assertEquals("eric", result.get("name")); }
@Test public void testClosing() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); List<Map<String, Object>> results = h.createQuery("select * from something order by id") .cleanupHandle() .list(); assertEquals(2, results.size()); Map<String, Object> first_row = results.get(0); assertEquals("eric", first_row.get("name")); assertTrue(h.isClosed()); }
@Test public void testEmptyWorksToo() throws Exception { ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); assertFalse(it.hasNext()); }
@Test public void testClosing() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); List<Map<String, Object>> results = h.createQuery("select * from something order by id") .cleanupHandle() .list(); assertEquals(2, results.size()); Map<String, Object> first_row = results.get(0); assertEquals("eric", first_row.get("name")); assertTrue(h.isClosed()); }
@Test public void testEmptyExplosion() throws Exception { ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); try { it.next(); fail("Expected IllegalStateException did not show up!"); } catch (IllegalStateException iae) { // TestCase does not deal with the annotations... } }
@Test public void testEmptyWorksToo() throws Exception { ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); assertFalse(it.hasNext()); }
@Test public void testEmptyExplosion() throws Exception { ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); try { it.next(); fail("Expected IllegalStateException did not show up!"); } catch (IllegalStateException iae) { // TestCase does not deal with the annotations... } }
@Test public void testBasicCleanupIterator() throws Exception { final Handle handle = dbi.open(); final Query<Integer> q = handle.createQuery("SELECT COUNT(1) FROM something") .cleanupHandle() .map(IntegerMapper.FIRST); final ResultIterator<Integer> it = q.iterator(); assertEquals(COUNT, Iterators.getOnlyElement(it).intValue()); assertFalse(it.hasNext()); assertTrue(handle.getConnection().isClosed()); }
@Test public void testBasicCleanupIterator() throws Exception { final Handle handle = dbi.open(); final Query<Integer> q = handle.createQuery("SELECT COUNT(1) FROM something") .cleanupHandle() .mapTo(Integer.class); final ResultIterator<Integer> it = q.iterator(); assertEquals(COUNT, Iterators.getOnlyElement(it).intValue()); assertFalse(it.hasNext()); assertTrue(handle.getConnection().isClosed()); }
@Test public void testJustNext() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); it.next(); it.next(); it.next(); }
@Test public void testJustNext() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); it.next(); it.next(); it.next(); }
@Test public void testIterateAllTheWay() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); int cnt = 0; while(it.hasNext()) { cnt++; it.next(); } assertEquals(2, cnt); assertTrue(h.isClosed()); }
@Test public void testIterateAllTheWay() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); int cnt = 0; while(it.hasNext()) { cnt++; it.next(); } assertEquals(2, cnt); assertTrue(h.isClosed()); }
@Test public void testNext() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); assertTrue(it.hasNext()); it.next(); it.next(); it.next(); assertFalse(it.hasNext()); }
@Test public void testNext() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); assertTrue(it.hasNext()); it.next(); it.next(); it.next(); assertFalse(it.hasNext()); }
@Test public void testTwoOne() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); assertTrue(it.hasNext()); it.next(); it.next(); assertTrue(it.hasNext()); it.next(); assertFalse(it.hasNext()); }
@Test public void testTwoOne() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); assertTrue(it.hasNext()); it.next(); it.next(); assertTrue(it.hasNext()); it.next(); assertFalse(it.hasNext()); }
@Test public void testTwoTwo() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); it.next(); it.next(); assertTrue(it.hasNext()); assertTrue(it.hasNext()); it.next(); assertFalse(it.hasNext()); assertFalse(it.hasNext()); }
@Test public void testTwoTwo() throws Exception { h.createStatement("insert into something (id, name) values (1, 'eric')").execute(); h.createStatement("insert into something (id, name) values (2, 'brian')").execute(); h.createStatement("insert into something (id, name) values (3, 'john')").execute(); ResultIterator<Map<String, Object>> it = h.createQuery("select * from something order by id") .cleanupHandle() .iterator(); it.next(); it.next(); assertTrue(it.hasNext()); assertTrue(it.hasNext()); it.next(); assertFalse(it.hasNext()); assertFalse(it.hasNext()); }