adds support for null suppression
[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.DbDriverMock;
19 import net.jaekl.squelch.sql.Column;
20 import net.jaekl.squelch.sql.Row;
21
22 import org.junit.Test;
23
24 public class TabularTest {
25         @Test
26         public void test_centrePad() {
27                 Tabular tabular = new TabularMock();
28                 
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));
34         }
35
36         @Test
37         public void test_classForSqlType() {
38                 Object[][] data = { 
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 }
74                 };
75                 Tabular tabular = new TabularMock();
76                 
77                 for (Object[] datum : data) {
78                         Class<?> expected = (Class<?>)datum[1];
79                         Class<?> actual = tabular.classForSqlType((int) datum[0]);
80                         assertEquals(expected, actual);
81                 }
82         }
83         
84         @Test
85         public void test_printCsv_empTable() throws IOException, SQLException
86         {
87                 TabularMock tabular = createEmpTable();
88                 
89                 try (
90                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
91                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
92                         )
93                 {
94                         tabular.printCsv(pw);
95                         pw.close();
96                         baos.close();
97                         String actual = baos.toString();
98                         assertEquals(  "EmpId,FirstName,LastName\n"
99                                              + "12345,Fred,Flintstone\n"
100                                              + "7654321,Barney,Rubble\n",
101                                              actual);
102                 }               
103         }
104
105         @Test
106         public void test_printTable_empTable() throws IOException, SQLException
107         {
108                 DbDriverMock driver = new DbDriverMock();
109                 TabularMock tabular = createEmpTable();
110                 
111                 try (
112                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
113                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
114                         )
115                 {
116                         tabular.printTable(driver, pw, "No rows returned.");
117                         pw.close();
118                         baos.close();
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",
127                                              actual);
128                 }
129         }
130         
131         @Test
132         public void test_repChar() {
133                 Tabular tabular = new TabularMock();
134                 
135                 assertEquals("", tabular.repChar(' ', 0));
136                 assertEquals("    ", tabular.repChar(' ', 4));
137                 assertEquals("###", tabular.repChar('#', 3));
138                 assertEquals("------", tabular.repChar('-', 6));
139         }
140         
141         @Test
142         public void test_stringify() throws SerialException, SQLException {
143                 Tabular tabular = new TabularMock();
144                 
145                 String[] data = {
146                         "This is the way the world ends, not with a bang but a whimper.",
147                         
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",
162                         
163                         "ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ\n" +
164                         "πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν:\n" +
165                         "πολλῶν δ᾽ ἀνθρώπων ἴδεν ἄστεα καὶ νόον ἔγνω,\n" +
166                         "πολλὰ δ᾽ ὅ γ᾽ ἐν πόντῳ πάθεν ἄλγεα ὃν κατὰ θυμόν,\n" +
167                         "5ἀρνύμενος ἥν τε ψυχὴν καὶ νόστον ἑταίρων.\n" +
168                         "ἀλλ᾽ οὐδ᾽ ὣς ἑτάρους ἐρρύσατο, ἱέμενός περ:\n" +
169                         "αὐτῶν γὰρ σφετέρῃσιν ἀτασθαλίῃσιν ὄλοντο,\n" +
170                         "νήπιοι, οἳ κατὰ βοῦς Ὑπερίονος Ἠελίοιο\n" +
171                         "ἤσθιον: αὐτὰρ ὁ τοῖσιν ἀφείλετο νόστιμον ἦμαρ.\n" +
172                         "10τῶν ἁμόθεν γε, θεά, θύγατερ Διός, εἰπὲ καὶ ἡμῖν.\n"
173                 };
174                 
175                 for (String datum : data) 
176                 {
177                         byte[] content = datum.getBytes(StandardCharsets.UTF_8);
178                         SerialBlob sblob = new SerialBlob(content);
179                         
180                         String expected = datum;
181                         String actual = tabular.stringify(sblob);
182                         
183                         Assert.assertEquals(expected, actual);
184                 }
185         }
186         
187         
188         private TabularMock createEmpTable()
189         {
190                 TabularMock tabular = new TabularMock();
191                 
192                 Column[] cols = { 
193                                 new Column("EmpId", Long.class, 10),
194                                 new Column("FirstName", String.class, 14),
195                                 new Column("LastName", String.class, 14)
196                 };
197                 tabular.mock_setCols(cols);
198                 
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);
204                 
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);
210                 
211                 return tabular;
212         }
213 }