Fix and fine-tune suppress_nulls.
[squelch.git] / src / test / java / net / jaekl / squelch / stmt / TabularTest.java
1 package net.jaekl.squelch.stmt;
2
3 import static org.junit.Assert.assertEquals;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.nio.charset.StandardCharsets;
10 import java.sql.SQLException;
11 import java.sql.Types;
12
13 import javax.sql.rowset.serial.SerialBlob;
14 import javax.sql.rowset.serial.SerialException;
15
16 import junit.framework.Assert;
17
18 import net.jaekl.squelch.db.DbDriver;
19 import net.jaekl.squelch.db.DbDriverMock;
20 import net.jaekl.squelch.sql.Column;
21 import net.jaekl.squelch.sql.Row;
22
23 import org.junit.Test;
24
25 public class TabularTest {
26         @Test
27         public void test_centrePad() {
28                 Tabular tabular = new TabularMock();
29                 
30                 assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
31                 assertEquals("  Fred  ", tabular.centrePad("Fred", 8));
32                 assertEquals("NULL", tabular.centrePad(null, 0));
33                 assertEquals("  NULL  ", tabular.centrePad(null, 8));
34                 assertEquals("   Wilma    ", tabular.centrePad("Wilma", 12));
35         }
36
37         @Test
38         public void test_classForSqlType() {
39                 Object[][] data = { 
40                                 { Types.ARRAY, Object.class },
41                                 { Types.BIGINT, Object.class },
42                                 { Types.BINARY, Object.class },
43                                 { Types.BIT, Object.class },
44                                 { Types.BLOB, Object.class },
45                                 { Types.BOOLEAN, Boolean.class },
46                                 { Types.CHAR, Character.class },
47                                 { Types.CLOB, Object.class },
48                                 { Types.DATALINK, Object.class },
49                                 { Types.DATE, java.util.Date.class },
50                                 { Types.DECIMAL, Double.class },
51                                 { Types.DISTINCT, Object.class },
52                                 { Types.DOUBLE, Double.class },
53                                 { Types.FLOAT, Double.class },
54                                 { Types.INTEGER, Long.class },
55                                 { Types.JAVA_OBJECT, Object.class },
56                                 { Types.LONGNVARCHAR, String.class },
57                                 { Types.LONGVARBINARY, Object.class },
58                                 { Types.NCHAR, String.class },
59                                 { Types.NCLOB, Object.class },
60                                 { Types.NULL, Object.class },
61                                 { Types.NUMERIC, Double.class },
62                                 { Types.NVARCHAR, String.class },
63                                 { Types.OTHER, Object.class },
64                                 { Types.REAL, Double.class },
65                                 { Types.REF, Object.class },
66                                 { Types.ROWID, Integer.class },
67                                 { Types.SMALLINT, Integer.class },
68                                 { Types.SQLXML, Object.class },
69                                 { Types.STRUCT, Object.class },
70                                 { Types.TIME, java.util.Date.class },
71                                 { Types.TIMESTAMP, java.util.Date.class },
72                                 { Types.TINYINT, Short.class },
73                                 { Types.VARBINARY, Object.class },
74                                 { Types.VARCHAR, String.class }
75                 };
76                 Tabular tabular = new TabularMock();
77                 
78                 for (Object[] datum : data) {
79                         Class<?> expected = (Class<?>)datum[1];
80                         Class<?> actual = tabular.classForSqlType((int) datum[0]);
81                         assertEquals(expected, actual);
82                 }
83         }
84         
85         @Test
86         public void test_printCsv_empTable() throws IOException, SQLException
87         {
88                 TabularMock tabular = createEmpTable();
89                 
90                 try (
91                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
92                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
93                         )
94                 {
95                         tabular.printCsv(pw);
96                         pw.close();
97                         baos.close();
98                         String actual = baos.toString();
99                         assertEquals(  "EmpId,FirstName,LastName\n"
100                                              + "12345,Fred,Flintstone\n"
101                                              + "7654321,Barney,Rubble\n",
102                                              actual);
103                 }               
104         }
105
106         @Test
107         public void test_printTable_empTable() throws IOException, SQLException
108         {
109                 DbDriverMock driver = new DbDriverMock();
110                 TabularMock tabular = createEmpTable();
111                 
112                 try (
113                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
114                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
115                         )
116                 {
117                         tabular.printTable(driver, pw, "No rows returned.");
118                         pw.close();
119                         baos.close();
120                         String actual = baos.toString();
121                         assertEquals(  "+---------+-----------+------------+\n"
122                                              + "|  EmpId  | FirstName |  LastName  |\n"
123                                              + "+---------+-----------+------------+\n"
124                                              + "| 12345   | Fred      | Flintstone |\n"
125                                              + "| 7654321 | Barney    | Rubble     |\n"
126                                              + "+---------+-----------+------------+\n"
127                                              + "2 row(s) returned.\n",
128                                              actual);
129                 }
130         }
131         
132         @Test
133         public void test_printTable_withNulls() throws IOException, SQLException
134         {
135                 DbDriverMock driver = new DbDriverMock();
136
137                 TabularMock tabular = createTableWithNulls();
138                 driver.set(DbDriver.SUPPRESS_NULLS, true);
139                 
140                 try (
141                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
142                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
143                         )
144                 {
145                         tabular.printTable(driver, pw, "No rows returned.");
146                         pw.close();
147                         baos.close();
148                         String actual = baos.toString();
149                         assertEquals(  "+---------+--------+------------+\n"
150                                              + "|  EmpId  | Value1 |   Value3   |\n"
151                                              + "+---------+--------+------------+\n"
152                                              + "| 12345   | Fred   | Flintstone |\n"
153                                              + "| 7654321 | Barney | Rubble     |\n"
154                                              + "+---------+--------+------------+\n"
155                                              + "2 row(s) returned.\n",
156                                              actual);
157                 }               
158                 
159                 tabular = createTableWithNulls();
160                 driver.set(DbDriver.SUPPRESS_NULLS, false);
161                 
162                 try (
163                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
164                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
165                         )
166                 {
167                         tabular.printTable(driver, pw, "No rows returned.");
168                         pw.close();
169                         baos.close();
170                         String actual = baos.toString();
171                         assertEquals(  "+---------+--------+--------+------------+\n"
172                                              + "|  EmpId  | Value1 | Value2 |   Value3   |\n"
173                                              + "+---------+--------+--------+------------+\n"
174                                              + "| 12345   | Fred   | null   | Flintstone |\n"
175                                              + "| 7654321 | Barney | null   | Rubble     |\n"
176                                              + "+---------+--------+--------+------------+\n"
177                                              + "2 row(s) returned.\n",
178                                              actual);
179                 }               
180                 
181         }
182         
183         @Test
184         public void test_repChar() {
185                 Tabular tabular = new TabularMock();
186                 
187                 assertEquals("", tabular.repChar(' ', 0));
188                 assertEquals("    ", tabular.repChar(' ', 4));
189                 assertEquals("###", tabular.repChar('#', 3));
190                 assertEquals("------", tabular.repChar('-', 6));
191         }
192         
193         @Test
194         public void test_stringify() throws SerialException, SQLException {
195                 Tabular tabular = new TabularMock();
196                 
197                 String[] data = {
198                         "This is the way the world ends, not with a bang but a whimper.",
199                         
200                         "To be, or not to be, that is the question:\n" +
201                         "Whether 'tis Nobler in the mind to suffer\n" +
202                         "The Slings and Arrows of outrageous Fortune,\n" +
203                         "Or to take Arms against a Sea of troubles,\n" +
204                         "And by opposing end them: to die, to sleep\n" +
205                         "No more; and by a sleep, to say we end\n" +
206                         "The Heart-ache, and the thousand Natural shocks\n" +
207                         "That Flesh is heir to? 'Tis a consummation\n" +
208                         "Devoutly to be wished. To die, to sleep,\n" +
209                         "To sleep, perchance to Dream; aye, there's the rub,\n" +
210                         "For in that sleep of death, what dreams may come,\n" +
211                         "When we have shuffled off this mortal coil,\n" +
212                         "Must give us pause. There's the respect\n" +
213                         "That makes Calamity of so long life:\n",
214                         
215                         "ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ\n" +
216                         "πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν:\n" +
217                         "πολλῶν δ᾽ ἀνθρώπων ἴδεν ἄστεα καὶ νόον ἔγνω,\n" +
218                         "πολλὰ δ᾽ ὅ γ᾽ ἐν πόντῳ πάθεν ἄλγεα ὃν κατὰ θυμόν,\n" +
219                         "5ἀρνύμενος ἥν τε ψυχὴν καὶ νόστον ἑταίρων.\n" +
220                         "ἀλλ᾽ οὐδ᾽ ὣς ἑτάρους ἐρρύσατο, ἱέμενός περ:\n" +
221                         "αὐτῶν γὰρ σφετέρῃσιν ἀτασθαλίῃσιν ὄλοντο,\n" +
222                         "νήπιοι, οἳ κατὰ βοῦς Ὑπερίονος Ἠελίοιο\n" +
223                         "ἤσθιον: αὐτὰρ ὁ τοῖσιν ἀφείλετο νόστιμον ἦμαρ.\n" +
224                         "10τῶν ἁμόθεν γε, θεά, θύγατερ Διός, εἰπὲ καὶ ἡμῖν.\n"
225                 };
226                 
227                 for (String datum : data) 
228                 {
229                         byte[] content = datum.getBytes(StandardCharsets.UTF_8);
230                         SerialBlob sblob = new SerialBlob(content);
231                         
232                         String expected = datum;
233                         String actual = tabular.stringify(sblob);
234                         
235                         Assert.assertEquals(expected, actual);
236                 }
237         }
238         
239         
240         private TabularMock createEmpTable()
241         {
242                 TabularMock tabular = new TabularMock();
243                 
244                 Column[] cols = { 
245                                 new Column("EmpId", Long.class, 10),
246                                 new Column("FirstName", String.class, 14),
247                                 new Column("LastName", String.class, 14)
248                 };
249                 tabular.mock_setCols(cols);
250                 
251                 Row row = new Row(cols.length);
252                 row.setValue(1, Long.valueOf(12345));
253                 row.setValue(2, "Fred");
254                 row.setValue(3, "Flintstone");
255                 tabular.mock_addRow(row);
256                 
257                 row = new Row(cols.length);
258                 row.setValue(1, Long.valueOf(7654321));
259                 row.setValue(2, "Barney");
260                 row.setValue(3, "Rubble");
261                 tabular.mock_addRow(row);
262                 
263                 return tabular;
264         }
265         
266         private TabularMock createTableWithNulls()
267         {
268                 TabularMock tabular = new TabularMock();
269                 
270                 Column[] cols = { 
271                                 new Column("EmpId", Long.class, 10),
272                                 new Column("Value1", String.class, 14),
273                                 new Column("Value2", String.class, 14),
274                                 new Column("Value3", String.class, 14)
275                 };
276                 tabular.mock_setCols(cols);
277                 
278                 Row row = new Row(cols.length);
279                 row.setValue(1, Long.valueOf(12345));
280                 row.setValue(2, "Fred");
281                 row.setValue(3, null);
282                 row.setValue(4, "Flintstone");
283                 tabular.mock_addRow(row);
284                 
285                 row = new Row(cols.length);
286                 row.setValue(1, Long.valueOf(7654321));
287                 row.setValue(2, "Barney");
288                 row.setValue(3, null);
289                 row.setValue(4, "Rubble");
290                 tabular.mock_addRow(row);
291                 
292                 return tabular;         
293         }
294 }