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