public void print(ResultSet resultSet, Consumer<String> lines) { try { ResultSetMetaData rsmd = resultSet.getMetaData(); int columnCount = rsmd.getColumnCount(); int[] columnSizes = findMaxLength(resultSet); lines.accept(delimiter(columnCount, columnSizes)); StringBuilder sb = new StringBuilder(); for (int i = 1; i <= columnCount; i++) { if (i > 1) sb.append(" | "); sb.append(Strings.setLength(rsmd.getColumnLabel(i), columnSizes[i], ' ')); } lines.accept(sb.toString()); sb.setLength(0); lines.accept(delimiter(columnCount, columnSizes)); while (resultSet.next()) { sb.setLength(0); for (int i = 1; i <= columnCount; i++) { if (i > 1) sb.append(" | "); sb.append(Strings.setLength(resultSet.getString(i), columnSizes[i], ' ')); } lines.accept(sb.toString()); sb.setLength(0); } lines.accept(delimiter(columnCount, columnSizes)); } catch (SQLException e) { throw new RuntimeException(e); } }
@Test public void setLengthShouldAppendCharacterForStringsThatAreTooShort() { assertEquals("This ", Strings.setLength("This", 10, ' ')); }
@Test public void setLengthShouldAppendCharacterForNullStrings() { assertEquals(" ", Strings.setLength(null, 10, ' ')); }
@Test public void setLengthShouldReturnStringsThatAreTheDesiredLength() { assertEquals("This is the string", Strings.setLength("This is the string", 18, ' ')); }
@Test public void setLengthShouldTruncateStringsThatAreTooLong() { assertEquals("This is the st", Strings.setLength("This is the string", 14, ' ')); }
@Test public void setLengthShouldAppendCharacterForEmptyStrings() { assertEquals(" ", Strings.setLength("", 10, ' ')); }
public void print(ResultSet resultSet, Consumer<String> lines) { try { ResultSetMetaData rsmd = resultSet.getMetaData(); int columnCount = rsmd.getColumnCount(); int[] columnSizes = findMaxLength(resultSet); lines.accept(delimiter(columnCount, columnSizes)); StringBuilder sb = new StringBuilder(); for (int i = 1; i <= columnCount; i++) { if (i > 1) sb.append(" | "); sb.append(Strings.setLength(rsmd.getColumnLabel(i), columnSizes[i], ' ')); } lines.accept(sb.toString()); sb.setLength(0); lines.accept(delimiter(columnCount, columnSizes)); while (resultSet.next()) { sb.setLength(0); for (int i = 1; i <= columnCount; i++) { if (i > 1) sb.append(" | "); sb.append(Strings.setLength(resultSet.getString(i), columnSizes[i], ' ')); } lines.accept(sb.toString()); sb.setLength(0); } lines.accept(delimiter(columnCount, columnSizes)); } catch (SQLException e) { throw new RuntimeException(e); } }