1 package net.jaekl.squelch.stmt;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
7 import java.io.ByteArrayOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10 import java.io.PrintWriter;
11 import java.nio.charset.StandardCharsets;
12 import java.sql.DatabaseMetaData;
13 import java.sql.SQLException;
15 import net.jaekl.squelch.db.DbDriverMock;
16 import net.jaekl.squelch.sql.ConnectionMock;
17 import net.jaekl.squelch.sql.DatabaseMetaDataMock;
18 import net.jaekl.squelch.sql.ResultSetMock;
19 import net.jaekl.squelch.sql.Row;
21 import org.junit.Test;
23 public class DescribeTest {
26 public void testHandles() {
27 final String[] AFFIRMATIVE = {
28 "\\d", "\\d ", " \\d", " \\d ",
29 "describe", "describe ", " describe", " describe ",
30 "DESCRIBE", "DESCRIBE ", " DESCRIBE", " DESCRIBE ",
31 "descRIbe", "DEscribE ", " DEScribe", " DEscRIbE ",
32 "\\d tablename", "\\d tablename ", " \\d tablename", " \\d tableName ",
33 "describe tablename", "DESCRIBE tablename ", " DesCrIbE tablename", " DESCribE fred "
35 final String[] NEGATIVE = {
36 "\\d\\q", "", null, " ", "select * from foo", " select * from foo ",
37 "DESCRIBEQ", "describeFOO", " describeJackAndDianne "
40 Describe describe = new Describe();
41 for (String s : AFFIRMATIVE) {
42 assertTrue("handles " + s, describe.handles(s));
44 for (String s : NEGATIVE) {
45 assertFalse("does not handle " + s, describe.handles(s));
50 public void testDescribeAll_noTables() throws IOException, SQLException
52 DatabaseMetaDataMock dbmdm = new DatabaseMetaDataMock();
53 Describe describe = new Describe();
56 ByteArrayOutputStream baos = new ByteArrayOutputStream();
57 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
60 describe.describeAll(pw, dbmdm);
63 String actual = baos.toString();
64 final String EXPECTED = "???\n";
65 assertEquals(EXPECTED, actual);
70 public void testDescribeTable_noColumns()
71 throws IOException, SQLException
73 DatabaseMetaDataMock dbmdm = new DatabaseMetaDataMock();
74 Describe describe = new Describe();
77 ByteArrayOutputStream baos = new ByteArrayOutputStream();
78 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
81 describe.describeTable(pw, dbmdm, "%");
84 String actual = baos.toString();
85 final String EXPECTED = "??? %\n";
86 assertEquals(EXPECTED, actual);
90 private DatabaseMetaDataMock construct_runs_dbmdm()
92 ResultSetMock rsmCols = new ResultSetMock();
93 ResultSetMock rsmTables = new ResultSetMock();
95 DatabaseMetaDataMock dbmdm = new DatabaseMetaDataMock();
99 "public", // 2) table schema
100 "runs", // 3) table name
101 "runid", // 4) column name
102 Integer.valueOf(4), // 5) data type
103 "int4", // 6) type name
104 Integer.valueOf(10), // 7) column size
105 null, // 8) buffer length
106 null, // 9) decimal digits
107 Integer.valueOf(10), // 10) num_prec_radix
108 Integer.valueOf(DatabaseMetaData.columnNoNulls), // 11) nullable
110 null, // 13) default value for the column
111 Integer.valueOf(0), // 14) sql_data_type
112 Integer.valueOf(0), // 15) sql_datetime_sub
113 null, // 16) char_octet_length
114 Integer.valueOf(1), // 17) ordinal_position
115 "NO", // 18) is_nullable
116 null, // 19) scope_catalog
117 null, // 20) scope_schema
118 null, // 21) scope_table
119 null, // 22) source_data_type
120 "NO", // 23) is_autoincrement
121 "NO" // 24) is_generated_column
123 new Row(new Object[]{
124 null, // 1) table_cat
125 "public", // 2) table schema
126 "runs", // 3) table name
127 "projname", // 4) column name
128 Integer.valueOf(12), // 5) data type
129 "varchar", // 6) type name
130 Integer.valueOf(80), // 7) column size
131 null, // 8) buffer length
132 null, // 9) decimal digits
133 Integer.valueOf(10), // 10) num_prec_radix
134 Integer.valueOf(DatabaseMetaData.columnNoNulls), // 11) nullable
136 null, // 13) default value for the column
137 Integer.valueOf(0), // 14) sql_data_type
138 Integer.valueOf(0), // 15) sql_datetime_sub
139 null, // 16) char_octet_length
140 Integer.valueOf(2), // 17) ordinal_position
141 "NO", // 18) is_nullable
142 null, // 19) scope_catalog
143 null, // 20) scope_schema
144 null, // 21) scope_table
145 null, // 22) source_data_type
146 "NO", // 23) is_autoincrement
147 "NO" // 24) is_generated_column
149 new Row(new Object[]{
150 null, // 1) table_cat
151 "public", // 2) table schema
152 "runs", // 3) table name
153 "version", // 4) column name
154 Integer.valueOf(12), // 5) data type
155 "varchar", // 6) type name
156 Integer.valueOf(10), // 7) column size
157 null, // 8) buffer length
158 null, // 9) decimal digits
159 Integer.valueOf(10), // 10) num_prec_radix
160 Integer.valueOf(DatabaseMetaData.columnNullable), // 11) nullable
162 null, // 13) default value for the column
163 Integer.valueOf(0), // 14) sql_data_type
164 Integer.valueOf(0), // 15) sql_datetime_sub
165 null, // 16) char_octet_length
166 Integer.valueOf(1), // 17) ordinal_position
167 "YES", // 18) is_nullable
168 null, // 19) scope_catalog
169 null, // 20) scope_schema
170 null, // 21) scope_table
171 null, // 22) source_data_type
172 "NO", // 23) is_autoincrement
173 "NO" // 24) is_generated_column
176 rsmCols.mock_addRows(rows);
179 new Row(new Object[]{
180 null, // TABLE_CAT String => table catalog (may be null)
181 null, // TABLE_SCHEM String => table schema (may be null)
182 "runs", // TABLE_NAME String => table name
183 "TABLE", // TABLE_TYPE String => table type. Typical types are "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
184 "", // REMARKS String => explanatory comment on the table
185 null, // TYPE_CAT String => the types catalog (may be null)
186 null, // TYPE_SCHEM String => the types schema (may be null)
187 null, // TYPE_NAME String => type name (may be null)
188 null, // SELF_REFERENCING_COL_NAME String => name of the designated "identifier" column of a typed table (may be null)
189 null // REF_GENERATION String => specifies how values in SELF_REFERENCING_COL_NAME are created. Values are "SYSTEM", "USER", "DERIVED". (may be null)
192 rsmTables.mock_addRows(rows);
194 dbmdm.mock_setColRS(null, null, "runs", null, rsmCols);
195 dbmdm.mock_setTableRS(null, null, "runs", null, rsmTables);
200 private String construct_runs_expected() {
201 return "TABLE runs\n"
202 + "+--------+-----------+---------+\n"
203 + "| Column | Type |Modifiers|\n"
204 + "+--------+-----------+---------+\n"
205 + "|runid |int4(10) |NOT NULL |\n"
206 + "|projname|varchar(80)|NOT NULL |\n"
207 + "|version |varchar(10)|NULL |\n"
208 + "+--------+-----------+---------+\n"
209 + "3 row(s) returned.\n";
213 public void testDescribeTable_threeColumns()
214 throws IOException, SQLException
216 DatabaseMetaDataMock dbmdm = construct_runs_dbmdm();
217 Describe describe = new Describe();
220 ByteArrayOutputStream baos = new ByteArrayOutputStream();
221 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
224 describe.describeTable(pw, dbmdm, "runs");
227 String actual = baos.toString();
228 String expected = construct_runs_expected();
230 assertEquals(expected, actual);
235 public void testExec() throws IOException, SQLException
237 final String[] SUCCESSFUL = {
238 "\\d runs", " \\d runs", "\\d runs ", " \\d runs ",
239 "DESCRIBE runs", " DescRIBe runs", "describe runs ", " describe runs "
241 String expected = construct_runs_expected();
242 for (String stmt : SUCCESSFUL) {
243 String actual = doExec(stmt);
244 assertEquals("doExec(): " + stmt, expected, actual);
248 public String doExec(String stmt) throws IOException, SQLException
250 DatabaseMetaDataMock dbmdm = construct_runs_dbmdm();
251 DbDriverMock driver = new DbDriverMock();
252 ConnectionMock cm = new ConnectionMock();
253 cm.mock_setDatabaseMetaData(dbmdm);
255 Describe describe = new Describe();
258 ByteArrayOutputStream baos = new ByteArrayOutputStream();
259 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
262 describe.exec(driver, cm, pw, stmt);
265 String actual = baos.toString();