1 package net.jaekl.squelch.stmt;
3 import static org.junit.Assert.assertEquals;
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;
13 import javax.sql.rowset.serial.SerialBlob;
14 import javax.sql.rowset.serial.SerialException;
16 import junit.framework.Assert;
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;
23 import org.junit.Test;
25 public class TabularTest {
27 public void test_centrePad() {
28 Tabular tabular = new TabularMock();
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));
38 public void test_classForSqlType() {
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 }
76 Tabular tabular = new TabularMock();
78 for (Object[] datum : data) {
79 Class<?> expected = (Class<?>)datum[1];
80 Class<?> actual = tabular.classForSqlType((int) datum[0]);
81 assertEquals(expected, actual);
86 public void test_printCsv_empTable() throws IOException, SQLException
88 TabularMock tabular = createEmpTable();
91 ByteArrayOutputStream baos = new ByteArrayOutputStream();
92 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
98 String actual = baos.toString();
99 assertEquals( "EmpId,FirstName,LastName\n"
100 + "12345,Fred,Flintstone\n"
101 + "7654321,Barney,Rubble\n",
107 public void test_printTable_empTable() throws IOException, SQLException
109 DbDriverMock driver = new DbDriverMock();
110 TabularMock tabular = createEmpTable();
113 ByteArrayOutputStream baos = new ByteArrayOutputStream();
114 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
117 tabular.printTable(driver, pw, "No rows returned.");
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",
133 public void test_printTable_withNulls() throws IOException, SQLException
135 DbDriverMock driver = new DbDriverMock();
137 TabularMock tabular = createTableWithNulls();
138 driver.set(DbDriver.SUPPRESS_NULLS, true);
141 ByteArrayOutputStream baos = new ByteArrayOutputStream();
142 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
145 tabular.printTable(driver, pw, "No rows returned.");
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",
159 tabular = createTableWithNulls();
160 driver.set(DbDriver.SUPPRESS_NULLS, false);
163 ByteArrayOutputStream baos = new ByteArrayOutputStream();
164 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
167 tabular.printTable(driver, pw, "No rows returned.");
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",
184 public void test_repChar() {
185 Tabular tabular = new TabularMock();
187 assertEquals("", tabular.repChar(' ', 0));
188 assertEquals(" ", tabular.repChar(' ', 4));
189 assertEquals("###", tabular.repChar('#', 3));
190 assertEquals("------", tabular.repChar('-', 6));
194 public void test_stringify() throws SerialException, SQLException {
195 Tabular tabular = new TabularMock();
198 "This is the way the world ends, not with a bang but a whimper.",
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",
215 "ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ\n" +
216 "πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν:\n" +
217 "πολλῶν δ᾽ ἀνθρώπων ἴδεν ἄστεα καὶ νόον ἔγνω,\n" +
218 "πολλὰ δ᾽ ὅ γ᾽ ἐν πόντῳ πάθεν ἄλγεα ὃν κατὰ θυμόν,\n" +
219 "5ἀρνύμενος ἥν τε ψυχὴν καὶ νόστον ἑταίρων.\n" +
220 "ἀλλ᾽ οὐδ᾽ ὣς ἑτάρους ἐρρύσατο, ἱέμενός περ:\n" +
221 "αὐτῶν γὰρ σφετέρῃσιν ἀτασθαλίῃσιν ὄλοντο,\n" +
222 "νήπιοι, οἳ κατὰ βοῦς Ὑπερίονος Ἠελίοιο\n" +
223 "ἤσθιον: αὐτὰρ ὁ τοῖσιν ἀφείλετο νόστιμον ἦμαρ.\n" +
224 "10τῶν ἁμόθεν γε, θεά, θύγατερ Διός, εἰπὲ καὶ ἡμῖν.\n"
227 for (String datum : data)
229 byte[] content = datum.getBytes(StandardCharsets.UTF_8);
230 SerialBlob sblob = new SerialBlob(content);
232 String expected = datum;
233 String actual = tabular.stringify(sblob);
235 Assert.assertEquals(expected, actual);
240 private TabularMock createEmpTable()
242 TabularMock tabular = new TabularMock();
245 new Column("EmpId", Long.class, 10),
246 new Column("FirstName", String.class, 14),
247 new Column("LastName", String.class, 14)
249 tabular.mock_setCols(cols);
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);
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);
266 private TabularMock createTableWithNulls()
268 TabularMock tabular = new TabularMock();
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)
276 tabular.mock_setCols(cols);
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);
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);