}
abstract Column[] getCols() throws SQLException;
- abstract Row getNext();
+ abstract Row getNext() throws SQLException;
// Returns the number of (data) rows that were output
public int printTable(PrintWriter pw) throws SQLException {
rowCount += pending;
}
+ if (rowCount > 0) {
+ writeDivider(pw, colWidths);
+ }
+
// TODO: Implement a String table for i18n
pw.println("" + rowCount + " row(s) returned.");
pw.flush();
// Examine and buffer up to rowBuf.length rows.
// Returns the number of actual rows that were buffered (zero if no more rows are available).
- RowBuffer bufferRows(int[] colWidths)
+ RowBuffer bufferRows(int[] colWidths) throws SQLException
{
RowBuffer rowBuf = new RowBuffer();
void writeHeader(PrintWriter pw, Column[] cols, int[] colWidths) {
writeDivider(pw, colWidths);
- for (int idx = 1; idx <= cols.length; ++idx) {
+ for (int idx = 0; idx < cols.length; ++idx) {
Column col = cols[idx];
pw.print("|" + centrePad(col.getLabel(), colWidths[idx]));
}
for (int rowIdx = 0; rowIdx < rowBuf.getPending(); ++rowIdx) {
Row row = rowBuf.getRow(rowIdx);
for (int colIdx = 0; colIdx < colWidths.length; ++colIdx) {
- String value = "" + row.getValue(colIdx);
+ String value = "" + row.getValue(colIdx + 1);
String padding = repChar(' ', colWidths[colIdx] - value.length());
pw.print("|" + value + padding);
}
package net.jaekl.squelch.stmt;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.sql.Types;
import org.junit.Test;
public class TabularTest {
- private static class TabularMock extends Tabular {
- @Override
- Column[] getCols() throws SQLException {
- return null;
- }
-
- @Override
- Row getNext() {
- return null;
- }
+ @Test
+ public void test_centrePad() {
+ Tabular tabular = new TabularMock();
+
+ assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
+ assertEquals(" Fred ", tabular.centrePad("Fred", 8));
+ assertEquals("NULL", tabular.centrePad(null, 0));
+ assertEquals(" NULL ", tabular.centrePad(null, 8));
+ assertEquals(" Wilma ", tabular.centrePad("Wilma", 12));
}
-
+
@Test
public void test_classForSqlType() {
Object[][] data = {
assertEquals(expected, actual);
}
}
+
+ @Test
+ public void test_printCsv_empTable() throws IOException, SQLException
+ {
+ TabularMock tabular = createEmpTable();
+
+ try (
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
+ )
+ {
+ tabular.printCsv(pw);
+ pw.close();
+ baos.close();
+ String actual = baos.toString();
+ assertEquals( "EmpId,FirstName,LastName\n"
+ + "12345,Fred,Flintstone\n"
+ + "7654321,Barney,Rubble\n",
+ actual);
+ }
+ }
+ @Test
+ public void test_printTable_empTable() throws IOException, SQLException
+ {
+ TabularMock tabular = createEmpTable();
+
+ try (
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
+ )
+ {
+ tabular.printTable(pw);
+ pw.close();
+ baos.close();
+ String actual = baos.toString();
+ assertEquals( "+-------+---------+----------+\n"
+ + "| EmpId |FirstName| LastName |\n"
+ + "+-------+---------+----------+\n"
+ + "|12345 |Fred |Flintstone|\n"
+ + "|7654321|Barney |Rubble |\n"
+ + "+-------+---------+----------+\n"
+ + "2 row(s) returned.\n",
+ actual);
+ }
+ }
+
@Test
public void test_repChar() {
Tabular tabular = new TabularMock();
assertEquals("###", tabular.repChar('#', 3));
assertEquals("------", tabular.repChar('-', 6));
}
-
- @Test
- public void test_centrePad() {
- Tabular tabular = new TabularMock();
+
+ private TabularMock createEmpTable()
+ {
+ TabularMock tabular = new TabularMock();
- assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
- assertEquals(" Fred ", tabular.centrePad("Fred", 8));
- assertEquals("NULL", tabular.centrePad(null, 0));
- assertEquals(" NULL ", tabular.centrePad(null, 8));
- assertEquals(" Wilma ", tabular.centrePad("Wilma", 12));
+ Column[] cols = {
+ new Column("EmpId", Long.class, 10),
+ new Column("FirstName", String.class, 14),
+ new Column("LastName", String.class, 14)
+ };
+ tabular.mock_setCols(cols);
+
+ Row row = new Row(cols.length);
+ row.setValue(1, Long.valueOf(12345));
+ row.setValue(2, "Fred");
+ row.setValue(3, "Flintstone");
+ tabular.mock_addRow(row);
+
+ row = new Row(cols.length);
+ row.setValue(1, Long.valueOf(7654321));
+ row.setValue(2, "Barney");
+ row.setValue(3, "Rubble");
+ tabular.mock_addRow(row);
+
+ return tabular;
}
}