### Question:
JDBCDataSource extends ColumnarDataSource { public RecordIterator getRecords() { verifyProperty(jdbcuri, "connection-string"); verifyProperty(driverclass, "driver-class"); verifyProperty(query, "query"); try { Properties props = new Properties(); if (username != null) props.put("user", username); if (password != null) props.put("password", password); Statement stmt = JDBCUtils.open(driverclass, jdbcuri, props); ResultSet rs = stmt.executeQuery(query); return new JDBCIterator(rs); } catch (SQLException e) { throw new RuntimeException(e); } } JDBCDataSource(); void setConnectionString(String str); void setDriverClass(String klass); void setUserName(String username); void setPassword(String password); void setQuery(String query); String getQuery(); String getDriverClass(); String getConnectionString(); String getUserName(); String getPassword(); RecordIterator getRecords(); @Override void writeConfig(ConfigWriter cw);  }### Answer:
@Test public void testEmpty() { RecordIterator it = source.getRecords(); assertTrue(!it.hasNext()); }
@Test public void testOneRow() { perform("insert into testdata values (1, 'foo')"); source.addColumn(new Column("ID", null, null, null)); source.addColumn(new Column("NAME", null, null, null)); RecordIterator it = source.getRecords(); assertTrue(it.hasNext()); Record r = it.next(); assertEquals("1", r.getValue("ID")); assertEquals("foo", r.getValue("NAME")); assertFalse(it.hasNext()); }
@Test public void testOneRowSkipColumn() { perform("insert into testdata values (1, 'foo')"); source.addColumn(new Column("ID", null, null, null)); RecordIterator it = source.getRecords(); assertTrue(it.hasNext()); Record r = it.next(); assertEquals("1", r.getValue("ID")); assertEquals(null, r.getValue("NAME")); assertFalse(it.hasNext()); }
@Test public void testOneRowMapColumnTwice() { perform("insert into testdata values (1, 'smith, john')"); RegexpCleaner givencleaner = new RegexpCleaner(); givencleaner.setRegexp(", (.+)"); RegexpCleaner familycleaner = new RegexpCleaner(); familycleaner.setRegexp("^([^,]+), "); source.addColumn(new Column("ID", null, null, null)); source.addColumn(new Column("NAME", "GIVENNAME", null, givencleaner)); source.addColumn(new Column("NAME", "FAMILYNAME", null, familycleaner)); RecordIterator it = source.getRecords(); assertTrue(it.hasNext()); Record r = it.next(); assertEquals("1", r.getValue("ID")); assertEquals("john", r.getValue("GIVENNAME")); assertEquals("smith", r.getValue("FAMILYNAME")); assertFalse(it.hasNext()); }
@Test public void testNull() { perform("insert into testdata values (1, NULL)"); LowerCaseNormalizeCleaner cleaner = new LowerCaseNormalizeCleaner(); source.addColumn(new Column("ID", null, null, null)); source.addColumn(new Column("NAME", "GIVENNAME", null, cleaner)); RecordIterator it = source.getRecords(); assertTrue(it.hasNext()); Record r = it.next(); assertEquals("1", r.getValue("ID")); assertEquals(null, r.getValue("GIVENNAME")); assertFalse(it.hasNext()); }
@Test public void testSplitting() { perform("insert into testdata values (1, 'foo bar baz')"); source.addColumn(new Column("ID", null, null, null)); Column col = new Column("NAME", null, null, null); col.setSplitOn(" "); source.addColumn(col); RecordIterator it = source.getRecords(); assertTrue(it.hasNext()); Record r = it.next(); assertEquals("1", r.getValue("ID")); Collection<String> values = r.getValues("NAME"); assertEquals(3, values.size()); assertTrue(values.contains("foo")); assertTrue(values.contains("bar")); assertTrue(values.contains("baz")); assertFalse(it.hasNext()); }