1 package net.jaekl.squelch;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6 import static org.junit.Assert.fail;
8 import java.io.ByteArrayOutputStream;
9 import java.io.IOException;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.nio.charset.StandardCharsets;
13 import java.sql.Connection;
14 import java.sql.SQLException;
16 import net.jaekl.squelch.db.DbDriver;
17 import net.jaekl.squelch.db.MsSqlDriver;
18 import net.jaekl.squelch.db.MySqlDriver;
19 import net.jaekl.squelch.db.OracleDriver;
20 import net.jaekl.squelch.db.PostgresqlDriver;
21 import net.jaekl.squelch.sql.ConnectionMock;
22 import net.jaekl.squelch.util.ConsoleInputMock;
24 import org.junit.Test;
26 public class SquelchTest {
27 private class NoDbMock extends Squelch {
28 private ConnectionMock m_conn;
31 m_conn = new ConnectionMock();
35 Connection getConnection() throws ClassNotFoundException, SQLException, SquelchException
40 public ConnectionMock mock_getConnectionMock()
47 public void test_getDriverFor_success() throws SquelchException {
48 Squelch squelch = new Squelch();
51 { "jdbc:sqlserver://HOST:1433;DatabaseName=DATABASE", MsSqlDriver.class },
52 { "jdbc:mysql://HOST/DATABASE", MySqlDriver.class },
53 { "jdbc:mysql://HOST:5150/DATABASE", MySqlDriver.class },
54 { "jdbc:oracle:thin:@//localhost:1521/XE", OracleDriver.class },
55 { "jdbc:oracle:thin:@neptune.acme.com:1521:T10A", OracleDriver.class },
56 { "jdbc:oracle:thin:@127.0.0.1:1521:T10A", OracleDriver.class },
57 { "jdbc:oracle:oci:@TEST", OracleDriver.class },
58 { "jdbc:oracle:oci:@192.168.1.1:1521/XE", OracleDriver.class },
59 { "jdbc:postgresql://HOST/DATABASE", PostgresqlDriver.class }
62 for (Object[] datum : data) {
63 String jdbcUrl = (String)datum[0];
65 @SuppressWarnings("unchecked")
66 Class<? extends DbDriver> expected = (Class<? extends DbDriver>)datum[1];
68 DbDriver actual = squelch.getDriverFor(jdbcUrl);
70 assertEquals(expected, actual.getClass());
75 public void test_getDriverFor_notFound()
77 String[] data = { null, "bogus:host/user/pass", "The quick brown fox jumps over the lazy dog" };
78 Squelch squelch = new Squelch();
80 for (String datum : data) {
82 squelch.getDriverFor(datum);
83 fail("Should have thrown a SquelchException");
85 catch (SquelchException exc) {
86 ; // This is the success path.
92 public void test_isQuit() {
93 Squelch squelch = new Squelch();
95 final String[] AFFIRMATIVE = {
96 "QUIT", " QUIT", " QUIT ", "\tQUIT\r\n", "\\q", " \\q", "QUIT;", "Quit", "qUiT", " qUIt ", "quit ;",
97 "exit", "ExIT", "EXIT", " EXIT ; ", null
99 final String[] NEGATIVE = {
100 "Fred", "SELECT * FROM FOO", "describe tablename", "",
101 "CREATE TABLE Fred ( foo INTEGER );"
104 for (String s : AFFIRMATIVE) {
105 assertTrue("isQuit(\"" + s + "\") should be true.", squelch.isQuit(s));
107 for (String s : NEGATIVE) {
108 assertFalse("isQuit(\"" + s + "\") should be false.", squelch.isQuit(s));
113 public void test_pumpLines() throws IOException, ClassNotFoundException, SQLException, SquelchException
115 Squelch squelch = new NoDbMock();
116 String consoleOutput;
118 String[] noLines = {};
119 consoleOutput = runPump(squelch, noLines);
120 assertEquals("", consoleOutput);
122 String[] quitOnly = {"QUIT"};
123 consoleOutput = runPump(squelch, quitOnly);
124 assertEquals(Squelch.PROMPT + "QUIT\n", consoleOutput);
126 String[] unrecognizedThenQuit = {"This is not a valid command;", "\\q"};
127 consoleOutput = runPump(squelch, unrecognizedThenQuit);
128 assertEquals(Squelch.PROMPT + "This is not a valid command;\n"
129 + "??? \"This is not a valid command;\"\n"
130 + Squelch.PROMPT + "\\q\n",
135 public void test_pumpLines_withSelect() throws ClassNotFoundException, IOException, SQLException, SquelchException
137 NoDbMock squelch = new NoDbMock();
138 String consoleOutput;
140 String[] simpleSelect = { "SELECT * FROM Fred WHERE wife='Wilma';", "QUIT" };
141 consoleOutput = runPump(squelch, simpleSelect);
142 assertEquals( Squelch.PROMPT + simpleSelect[0] + "\n"
143 + "0 row(s) returned.\n"
144 + Squelch.PROMPT + simpleSelect[1] + "\n", consoleOutput);
146 ConnectionMock cm = squelch.mock_getConnectionMock();
147 assertTrue(cm.mock_queryWasExecuted(simpleSelect[0]));
150 private String runPump(Squelch squelch, String[] lines) throws IOException, ClassNotFoundException, SQLException, SquelchException
153 ByteArrayOutputStream baos = new ByteArrayOutputStream();
154 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8))
157 ConsoleInputMock cim = new ConsoleInputMock(pw, lines);
158 squelch.pumpLines(pw, cim);
161 return baos.toString();