Fix and fine-tune suppress_nulls.
[squelch.git] / src / test / java / net / jaekl / squelch / stmt / TabularTest.java
index b72add390ab0aefcc64d8c59e20a96c0d1f71ff2..1dfe4dbcf3d8e1aec893b840a4f79f529fde88db 100644 (file)
@@ -15,6 +15,8 @@ import javax.sql.rowset.serial.SerialException;
 
 import junit.framework.Assert;
 
+import net.jaekl.squelch.db.DbDriver;
+import net.jaekl.squelch.db.DbDriverMock;
 import net.jaekl.squelch.sql.Column;
 import net.jaekl.squelch.sql.Row;
 
@@ -104,6 +106,7 @@ public class TabularTest {
        @Test
        public void test_printTable_empTable() throws IOException, SQLException
        {
+               DbDriverMock driver = new DbDriverMock();
                TabularMock tabular = createEmpTable();
                
                try (
@@ -111,7 +114,7 @@ public class TabularTest {
                                PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
                        )
                {
-                       tabular.printTable(pw, "No rows returned.");
+                       tabular.printTable(driver, pw, "No rows returned.");
                        pw.close();
                        baos.close();
                        String actual = baos.toString();
@@ -126,6 +129,57 @@ public class TabularTest {
                }
        }
        
+       @Test
+       public void test_printTable_withNulls() throws IOException, SQLException
+       {
+               DbDriverMock driver = new DbDriverMock();
+
+               TabularMock tabular = createTableWithNulls();
+               driver.set(DbDriver.SUPPRESS_NULLS, true);
+               
+               try (
+                               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                               PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
+                       )
+               {
+                       tabular.printTable(driver, pw, "No rows returned.");
+                       pw.close();
+                       baos.close();
+                       String actual = baos.toString();
+                       assertEquals(  "+---------+--------+------------+\n"
+                                            + "|  EmpId  | Value1 |   Value3   |\n"
+                                            + "+---------+--------+------------+\n"
+                                            + "| 12345   | Fred   | Flintstone |\n"
+                                            + "| 7654321 | Barney | Rubble     |\n"
+                                            + "+---------+--------+------------+\n"
+                                            + "2 row(s) returned.\n",
+                                            actual);
+               }               
+               
+               tabular = createTableWithNulls();
+               driver.set(DbDriver.SUPPRESS_NULLS, false);
+               
+               try (
+                               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                               PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
+                       )
+               {
+                       tabular.printTable(driver, pw, "No rows returned.");
+                       pw.close();
+                       baos.close();
+                       String actual = baos.toString();
+                       assertEquals(  "+---------+--------+--------+------------+\n"
+                                            + "|  EmpId  | Value1 | Value2 |   Value3   |\n"
+                                            + "+---------+--------+--------+------------+\n"
+                                            + "| 12345   | Fred   | null   | Flintstone |\n"
+                                            + "| 7654321 | Barney | null   | Rubble     |\n"
+                                            + "+---------+--------+--------+------------+\n"
+                                            + "2 row(s) returned.\n",
+                                            actual);
+               }               
+               
+       }
+       
        @Test
        public void test_repChar() {
                Tabular tabular = new TabularMock();
@@ -208,4 +262,33 @@ public class TabularTest {
                
                return tabular;
        }
+       
+       private TabularMock createTableWithNulls()
+       {
+               TabularMock tabular = new TabularMock();
+               
+               Column[] cols = { 
+                               new Column("EmpId", Long.class, 10),
+                               new Column("Value1", String.class, 14),
+                               new Column("Value2", String.class, 14),
+                               new Column("Value3", 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, null);
+               row.setValue(4, "Flintstone");
+               tabular.mock_addRow(row);
+               
+               row = new Row(cols.length);
+               row.setValue(1, Long.valueOf(7654321));
+               row.setValue(2, "Barney");
+               row.setValue(3, null);
+               row.setValue(4, "Rubble");
+               tabular.mock_addRow(row);
+               
+               return tabular;         
+       }
 }