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.DbDriverMock;
19 import net.jaekl.squelch.sql.Column;
20 import net.jaekl.squelch.sql.Row;
22 import org.junit.Test;
24 public class TabularTest {
26 public void test_centrePad() {
27 Tabular tabular = new TabularMock();
29 assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
30 assertEquals(" Fred ", tabular.centrePad("Fred", 8));
31 assertEquals("NULL", tabular.centrePad(null, 0));
32 assertEquals(" NULL ", tabular.centrePad(null, 8));
33 assertEquals(" Wilma ", tabular.centrePad("Wilma", 12));
37 public void test_classForSqlType() {
39 { Types.ARRAY, Object.class },
40 { Types.BIGINT, Object.class },
41 { Types.BINARY, Object.class },
42 { Types.BIT, Object.class },
43 { Types.BLOB, Object.class },
44 { Types.BOOLEAN, Boolean.class },
45 { Types.CHAR, Character.class },
46 { Types.CLOB, Object.class },
47 { Types.DATALINK, Object.class },
48 { Types.DATE, java.util.Date.class },
49 { Types.DECIMAL, Double.class },
50 { Types.DISTINCT, Object.class },
51 { Types.DOUBLE, Double.class },
52 { Types.FLOAT, Double.class },
53 { Types.INTEGER, Long.class },
54 { Types.JAVA_OBJECT, Object.class },
55 { Types.LONGNVARCHAR, String.class },
56 { Types.LONGVARBINARY, Object.class },
57 { Types.NCHAR, String.class },
58 { Types.NCLOB, Object.class },
59 { Types.NULL, Object.class },
60 { Types.NUMERIC, Double.class },
61 { Types.NVARCHAR, String.class },
62 { Types.OTHER, Object.class },
63 { Types.REAL, Double.class },
64 { Types.REF, Object.class },
65 { Types.ROWID, Integer.class },
66 { Types.SMALLINT, Integer.class },
67 { Types.SQLXML, Object.class },
68 { Types.STRUCT, Object.class },
69 { Types.TIME, java.util.Date.class },
70 { Types.TIMESTAMP, java.util.Date.class },
71 { Types.TINYINT, Short.class },
72 { Types.VARBINARY, Object.class },
73 { Types.VARCHAR, String.class }
75 Tabular tabular = new TabularMock();
77 for (Object[] datum : data) {
78 Class<?> expected = (Class<?>)datum[1];
79 Class<?> actual = tabular.classForSqlType((int) datum[0]);
80 assertEquals(expected, actual);
85 public void test_printCsv_empTable() throws IOException, SQLException
87 TabularMock tabular = createEmpTable();
90 ByteArrayOutputStream baos = new ByteArrayOutputStream();
91 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
97 String actual = baos.toString();
98 assertEquals( "EmpId,FirstName,LastName\n"
99 + "12345,Fred,Flintstone\n"
100 + "7654321,Barney,Rubble\n",
106 public void test_printTable_empTable() throws IOException, SQLException
108 DbDriverMock driver = new DbDriverMock();
109 TabularMock tabular = createEmpTable();
112 ByteArrayOutputStream baos = new ByteArrayOutputStream();
113 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
116 tabular.printTable(driver, pw, "No rows returned.");
119 String actual = baos.toString();
120 assertEquals( "+---------+-----------+------------+\n"
121 + "| EmpId | FirstName | LastName |\n"
122 + "+---------+-----------+------------+\n"
123 + "| 12345 | Fred | Flintstone |\n"
124 + "| 7654321 | Barney | Rubble |\n"
125 + "+---------+-----------+------------+\n"
126 + "2 row(s) returned.\n",
132 public void test_repChar() {
133 Tabular tabular = new TabularMock();
135 assertEquals("", tabular.repChar(' ', 0));
136 assertEquals(" ", tabular.repChar(' ', 4));
137 assertEquals("###", tabular.repChar('#', 3));
138 assertEquals("------", tabular.repChar('-', 6));
142 public void test_stringify() throws SerialException, SQLException {
143 Tabular tabular = new TabularMock();
146 "This is the way the world ends, not with a bang but a whimper.",
148 "To be, or not to be, that is the question:\n" +
149 "Whether 'tis Nobler in the mind to suffer\n" +
150 "The Slings and Arrows of outrageous Fortune,\n" +
151 "Or to take Arms against a Sea of troubles,\n" +
152 "And by opposing end them: to die, to sleep\n" +
153 "No more; and by a sleep, to say we end\n" +
154 "The Heart-ache, and the thousand Natural shocks\n" +
155 "That Flesh is heir to? 'Tis a consummation\n" +
156 "Devoutly to be wished. To die, to sleep,\n" +
157 "To sleep, perchance to Dream; aye, there's the rub,\n" +
158 "For in that sleep of death, what dreams may come,\n" +
159 "When we have shuffled off this mortal coil,\n" +
160 "Must give us pause. There's the respect\n" +
161 "That makes Calamity of so long life:\n",
163 "ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ\n" +
164 "πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν:\n" +
165 "πολλῶν δ᾽ ἀνθρώπων ἴδεν ἄστεα καὶ νόον ἔγνω,\n" +
166 "πολλὰ δ᾽ ὅ γ᾽ ἐν πόντῳ πάθεν ἄλγεα ὃν κατὰ θυμόν,\n" +
167 "5ἀρνύμενος ἥν τε ψυχὴν καὶ νόστον ἑταίρων.\n" +
168 "ἀλλ᾽ οὐδ᾽ ὣς ἑτάρους ἐρρύσατο, ἱέμενός περ:\n" +
169 "αὐτῶν γὰρ σφετέρῃσιν ἀτασθαλίῃσιν ὄλοντο,\n" +
170 "νήπιοι, οἳ κατὰ βοῦς Ὑπερίονος Ἠελίοιο\n" +
171 "ἤσθιον: αὐτὰρ ὁ τοῖσιν ἀφείλετο νόστιμον ἦμαρ.\n" +
172 "10τῶν ἁμόθεν γε, θεά, θύγατερ Διός, εἰπὲ καὶ ἡμῖν.\n"
175 for (String datum : data)
177 byte[] content = datum.getBytes(StandardCharsets.UTF_8);
178 SerialBlob sblob = new SerialBlob(content);
180 String expected = datum;
181 String actual = tabular.stringify(sblob);
183 Assert.assertEquals(expected, actual);
188 private TabularMock createEmpTable()
190 TabularMock tabular = new TabularMock();
193 new Column("EmpId", Long.class, 10),
194 new Column("FirstName", String.class, 14),
195 new Column("LastName", String.class, 14)
197 tabular.mock_setCols(cols);
199 Row row = new Row(cols.length);
200 row.setValue(1, Long.valueOf(12345));
201 row.setValue(2, "Fred");
202 row.setValue(3, "Flintstone");
203 tabular.mock_addRow(row);
205 row = new Row(cols.length);
206 row.setValue(1, Long.valueOf(7654321));
207 row.setValue(2, "Barney");
208 row.setValue(3, "Rubble");
209 tabular.mock_addRow(row);