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(DbDriver driver, String jdbcUrl)
36 throws ClassNotFoundException, SQLException, SquelchException
41 public ConnectionMock mock_getConnectionMock()
48 public void test_getDriverFor_success() throws SquelchException {
49 Squelch squelch = new Squelch();
52 { "jdbc:sqlserver://HOST:1433;DatabaseName=DATABASE", MsSqlDriver.class },
53 { "jdbc:mysql://HOST/DATABASE", MySqlDriver.class },
54 { "jdbc:mysql://HOST:5150/DATABASE", MySqlDriver.class },
55 { "jdbc:oracle:thin:@//localhost:1521/XE", OracleDriver.class },
56 { "jdbc:oracle:thin:@neptune.acme.com:1521:T10A", OracleDriver.class },
57 { "jdbc:oracle:thin:@127.0.0.1:1521:T10A", OracleDriver.class },
58 { "jdbc:oracle:oci:@TEST", OracleDriver.class },
59 { "jdbc:oracle:oci:@192.168.1.1:1521/XE", OracleDriver.class },
60 { "jdbc:postgresql://HOST/DATABASE", PostgresqlDriver.class }
63 for (Object[] datum : data) {
64 String jdbcUrl = (String)datum[0];
66 @SuppressWarnings("unchecked")
67 Class<? extends DbDriver> expected = (Class<? extends DbDriver>)datum[1];
69 DbDriver actual = squelch.getDriverFor(jdbcUrl);
71 assertEquals(expected, actual.getClass());
76 public void test_getDriverFor_notFound()
78 String[] data = { null, "bogus:host/user/pass", "The quick brown fox jumps over the lazy dog" };
79 Squelch squelch = new Squelch();
81 for (String datum : data) {
83 squelch.getDriverFor(datum);
84 fail("Should have thrown a SquelchException");
86 catch (SquelchException exc) {
87 ; // This is the success path.
93 public void test_isQuit() {
94 Squelch squelch = new Squelch();
96 final String[] AFFIRMATIVE = {
97 "QUIT", " QUIT", " QUIT ", "\tQUIT\r\n", "\\q", " \\q", "QUIT;", "Quit", "qUiT", " qUIt ", "quit ;",
98 "exit", "ExIT", "EXIT", " EXIT ; ", null
100 final String[] NEGATIVE = {
101 "Fred", "SELECT * FROM FOO", "describe tablename", "",
102 "CREATE TABLE Fred ( foo INTEGER );"
105 for (String s : AFFIRMATIVE) {
106 assertTrue("isQuit(\"" + s + "\") should be true.", squelch.isQuit(s));
108 for (String s : NEGATIVE) {
109 assertFalse("isQuit(\"" + s + "\") should be false.", squelch.isQuit(s));
114 public void test_pumpLines() throws IOException, ClassNotFoundException, SQLException, SquelchException
116 Squelch squelch = new NoDbMock();
117 String consoleOutput;
119 String[] noLines = {};
120 consoleOutput = runPump(squelch, noLines);
121 assertEquals("", consoleOutput);
123 String[] quitOnly = {"QUIT"};
124 consoleOutput = runPump(squelch, quitOnly);
125 assertEquals(Squelch.PROMPT + "QUIT\n", consoleOutput);
127 String[] unrecognizedThenQuit = {"This is not a valid command;", "\\q"};
128 consoleOutput = runPump(squelch, unrecognizedThenQuit);
129 assertEquals(Squelch.PROMPT + "This is not a valid command;\n"
130 + "??? \"This is not a valid command;\"\n"
131 + Squelch.PROMPT + "\\q\n",
136 public void test_pumpLines_withSelect() throws ClassNotFoundException, IOException, SQLException, SquelchException
138 NoDbMock squelch = new NoDbMock();
139 String consoleOutput;
141 String[] simpleSelect = { "SELECT * FROM Fred WHERE wife='Wilma';", "QUIT" };
142 consoleOutput = runPump(squelch, simpleSelect);
143 assertEquals( Squelch.PROMPT + simpleSelect[0] + "\n"
144 + "No rows returned.\n"
145 + Squelch.PROMPT + simpleSelect[1] + "\n", consoleOutput);
147 ConnectionMock cm = squelch.mock_getConnectionMock();
148 assertTrue(cm.mock_queryWasExecuted(simpleSelect[0].substring(0, simpleSelect[0].length() - 1)));
151 private String runPump(Squelch squelch, String[] lines) throws IOException, ClassNotFoundException, SQLException, SquelchException
154 ByteArrayOutputStream baos = new ByteArrayOutputStream();
155 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8))
158 ConsoleInputMock cim = new ConsoleInputMock(pw, lines);
159 squelch.pumpLines(pw, cim);
162 return baos.toString();