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 net.jaekl.squelch.sql.Column;
14 import net.jaekl.squelch.sql.Row;
16 import org.junit.Test;
18 public class TabularTest {
20 public void test_centrePad() {
21 Tabular tabular = new TabularMock();
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));
31 public void test_classForSqlType() {
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 }
69 Tabular tabular = new TabularMock();
71 for (Object[] datum : data) {
72 Class<?> expected = (Class<?>)datum[1];
73 Class<?> actual = tabular.classForSqlType((int) datum[0]);
74 assertEquals(expected, actual);
79 public void test_printCsv_empTable() throws IOException, SQLException
81 TabularMock tabular = createEmpTable();
84 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
91 String actual = baos.toString();
92 assertEquals( "EmpId,FirstName,LastName\n"
93 + "12345,Fred,Flintstone\n"
94 + "7654321,Barney,Rubble\n",
100 public void test_printTable_empTable() throws IOException, SQLException
102 TabularMock tabular = createEmpTable();
105 ByteArrayOutputStream baos = new ByteArrayOutputStream();
106 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
109 tabular.printTable(pw);
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",
125 public void test_repChar() {
126 Tabular tabular = new TabularMock();
128 assertEquals("", tabular.repChar(' ', 0));
129 assertEquals(" ", tabular.repChar(' ', 4));
130 assertEquals("###", tabular.repChar('#', 3));
131 assertEquals("------", tabular.repChar('-', 6));
134 private TabularMock createEmpTable()
136 TabularMock tabular = new TabularMock();
139 new Column("EmpId", Long.class, 10),
140 new Column("FirstName", String.class, 14),
141 new Column("LastName", String.class, 14)
143 tabular.mock_setCols(cols);
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);
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);