ResultSetIterator implements Iterator<Record> { @Override public Record next() { List<Attribute> attributes = new ArrayList<Attribute>(); try { ResultSetMetaData metaData = resultSet.getMetaData(); if (metaData.getColumnCount() == 0) { return null; } for (int i = 0; i < metaData.getColumnCount(); i++) { Attribute attribute = new Attribute(columnNames.get(i), i); String value = null; try { Object object = resultSet.getObject(i + 1); value = object == null ? null : String.valueOf(object); } catch (SQLException ex) { LOGGER.debug(ex.getMessage(), ex); } attribute.setValue(value); attributes.add(attribute); } return new RichRecord(attributes, String.valueOf(index++), 0, StringUtils.EMPTY); } catch (Exception e) { LOGGER.error(e.getMessage(), e); throw new DQRecordLinkageRuntimeException("Could not build next result", e); } } ResultSetIterator(Connection sqlConnection, String sqlQuery, List<String> elementNames); @Override boolean hasNext(); @Override Record next(); @Override void remove();  }
@Test public void testNext() throws SQLException { ResultSetMetaData metaData = Mockito.mock(ResultSetMetaData.class); when(metaData.getColumnCount()).thenReturn(3); ResultSet results = Mockito.mock(ResultSet.class); when(results.getMetaData()).thenReturn(metaData); when(results.getObject(any())).thenReturn(1, "Lily", "2015-6-25"); Statement statement = Mockito.mock(Statement.class); when(statement.getResultSet()).thenReturn(results); Connection conn = Mockito.mock(Connection.class); when(conn.createStatement()).thenReturn(statement); ResultSetIterator resIterator = new ResultSetIterator(conn, null, elementNames); Record next = resIterator.next(); Assert.assertNotNull(next); List<Attribute> attributes = next.getAttributes(); Assert.assertTrue(attributes.size() == 3); for (Attribute attribute : attributes) { Assert.assertNotNull(attribute); } }
@Test public void testNext_zerodDate() throws SQLException { ResultSetMetaData metaData = Mockito.mock(ResultSetMetaData.class); when(metaData.getColumnCount()).thenReturn(3); ResultSet results = Mockito.mock(ResultSet.class); when(results.getMetaData()).thenReturn(metaData); when(results.getObject(any())).thenReturn(2, "Lily", "0000-00-00 00:00:00"); Statement statement = Mockito.mock(Statement.class); when(statement.getResultSet()).thenReturn(results); Connection conn = Mockito.mock(Connection.class); when(conn.createStatement()).thenReturn(statement); ResultSetIterator resIterator = new ResultSetIterator(conn, null, elementNames); Record next = resIterator.next(); Assert.assertNotNull(next); Assert.assertTrue(next.getAttributes().size() == 3); Assert.assertNull(next.getAttributes().get(2).getValue()); }