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