select queries now work, and print tabular output.
[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 net.jaekl.squelch.sql.Column;
14 import net.jaekl.squelch.sql.Row;
15
16 import org.junit.Test;
17
18 public class TabularTest {
19         @Test
20         public void test_centrePad() {
21                 Tabular tabular = new TabularMock();
22                 
23                 assertEquals("Vestibule", tabular.centrePad("Vestibule", 2));
24                 assertEquals("  Fred  ", tabular.centrePad("Fred", 8));
25                 assertEquals("NULL", tabular.centrePad(null, 0));
26                 assertEquals("  NULL  ", tabular.centrePad(null, 8));
27                 assertEquals("   Wilma    ", tabular.centrePad("Wilma", 12));
28         }
29
30         @Test
31         public void test_classForSqlType() {
32                 Object[][] data = { 
33                                 { Types.ARRAY, Object.class },
34                                 { Types.BIGINT, Object.class },
35                                 { Types.BINARY, Object.class },
36                                 { Types.BIT, Object.class },
37                                 { Types.BLOB, Object.class },
38                                 { Types.BOOLEAN, Boolean.class },
39                                 { Types.CHAR, Character.class },
40                                 { Types.CLOB, Object.class },
41                                 { Types.DATALINK, Object.class },
42                                 { Types.DATE, java.util.Date.class },
43                                 { Types.DECIMAL, Double.class },
44                                 { Types.DISTINCT, Object.class },
45                                 { Types.DOUBLE, Double.class },
46                                 { Types.FLOAT, Double.class },
47                                 { Types.INTEGER, Long.class },
48                                 { Types.JAVA_OBJECT, Object.class },
49                                 { Types.LONGNVARCHAR, String.class },
50                                 { Types.LONGVARBINARY, Object.class },
51                                 { Types.NCHAR, String.class },
52                                 { Types.NCLOB, Object.class },
53                                 { Types.NULL, Object.class },
54                                 { Types.NUMERIC, Double.class },
55                                 { Types.NVARCHAR, String.class },
56                                 { Types.OTHER, Object.class },
57                                 { Types.REAL, Double.class },
58                                 { Types.REF, Object.class },
59                                 { Types.ROWID, Integer.class },
60                                 { Types.SMALLINT, Integer.class },
61                                 { Types.SQLXML, Object.class },
62                                 { Types.STRUCT, Object.class },
63                                 { Types.TIME, java.util.Date.class },
64                                 { Types.TIMESTAMP, java.util.Date.class },
65                                 { Types.TINYINT, Short.class },
66                                 { Types.VARBINARY, Object.class },
67                                 { Types.VARCHAR, String.class }
68                 };
69                 Tabular tabular = new TabularMock();
70                 
71                 for (Object[] datum : data) {
72                         Class<?> expected = (Class<?>)datum[1];
73                         Class<?> actual = tabular.classForSqlType((int) datum[0]);
74                         assertEquals(expected, actual);
75                 }
76         }
77         
78         @Test
79         public void test_printCsv_empTable() throws IOException, SQLException
80         {
81                 TabularMock tabular = createEmpTable();
82                 
83                 try (
84                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
86                         )
87                 {
88                         tabular.printCsv(pw);
89                         pw.close();
90                         baos.close();
91                         String actual = baos.toString();
92                         assertEquals(  "EmpId,FirstName,LastName\n"
93                                              + "12345,Fred,Flintstone\n"
94                                              + "7654321,Barney,Rubble\n",
95                                              actual);
96                 }               
97         }
98
99         @Test
100         public void test_printTable_empTable() throws IOException, SQLException
101         {
102                 TabularMock tabular = createEmpTable();
103                 
104                 try (
105                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
106                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
107                         )
108                 {
109                         tabular.printTable(pw);
110                         pw.close();
111                         baos.close();
112                         String actual = baos.toString();
113                         assertEquals(  "+-------+---------+----------+\n"
114                                              + "| EmpId |FirstName| LastName |\n"
115                                              + "+-------+---------+----------+\n"
116                                              + "|12345  |Fred     |Flintstone|\n"
117                                              + "|7654321|Barney   |Rubble    |\n"
118                                              + "+-------+---------+----------+\n"
119                                              + "2 row(s) returned.\n",
120                                              actual);
121                 }
122         }
123         
124         @Test
125         public void test_repChar() {
126                 Tabular tabular = new TabularMock();
127                 
128                 assertEquals("", tabular.repChar(' ', 0));
129                 assertEquals("    ", tabular.repChar(' ', 4));
130                 assertEquals("###", tabular.repChar('#', 3));
131                 assertEquals("------", tabular.repChar('-', 6));
132         }
133         
134         private TabularMock createEmpTable()
135         {
136                 TabularMock tabular = new TabularMock();
137                 
138                 Column[] cols = { 
139                                 new Column("EmpId", Long.class, 10),
140                                 new Column("FirstName", String.class, 14),
141                                 new Column("LastName", String.class, 14)
142                 };
143                 tabular.mock_setCols(cols);
144                 
145                 Row row = new Row(cols.length);
146                 row.setValue(1, Long.valueOf(12345));
147                 row.setValue(2, "Fred");
148                 row.setValue(3, "Flintstone");
149                 tabular.mock_addRow(row);
150                 
151                 row = new Row(cols.length);
152                 row.setValue(1, Long.valueOf(7654321));
153                 row.setValue(2, "Barney");
154                 row.setValue(3, "Rubble");
155                 tabular.mock_addRow(row);
156                 
157                 return tabular;
158         }
159 }