import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
+import java.sql.Connection;
+import java.sql.SQLException;
+import net.jaekl.squelch.db.DbDriver;
+import net.jaekl.squelch.db.MsSqlDriver;
+import net.jaekl.squelch.db.MySqlDriver;
+import net.jaekl.squelch.db.OracleDriver;
+import net.jaekl.squelch.db.PostgresqlDriver;
+import net.jaekl.squelch.sql.ConnectionMock;
import net.jaekl.squelch.util.ConsoleInputMock;
import org.junit.Test;
public class SquelchTest {
+ private class NoDbMock extends Squelch {
+ private ConnectionMock m_conn;
+
+ public NoDbMock() {
+ m_conn = new ConnectionMock();
+ }
+
+ @Override
+ Connection getConnection(DbDriver driver, String jdbcUrl)
+ throws ClassNotFoundException, SQLException, SquelchException
+ {
+ return m_conn;
+ }
+
+ public ConnectionMock mock_getConnectionMock()
+ {
+ return m_conn;
+ }
+ }
+
+ @Test
+ public void test_getDriverFor_success() throws SquelchException {
+ Squelch squelch = new Squelch();
+
+ Object[][] data = {
+ { "jdbc:sqlserver://HOST:1433;DatabaseName=DATABASE", MsSqlDriver.class },
+ { "jdbc:mysql://HOST/DATABASE", MySqlDriver.class },
+ { "jdbc:mysql://HOST:5150/DATABASE", MySqlDriver.class },
+ { "jdbc:oracle:thin:@//localhost:1521/XE", OracleDriver.class },
+ { "jdbc:oracle:thin:@neptune.acme.com:1521:T10A", OracleDriver.class },
+ { "jdbc:oracle:thin:@127.0.0.1:1521:T10A", OracleDriver.class },
+ { "jdbc:oracle:oci:@TEST", OracleDriver.class },
+ { "jdbc:oracle:oci:@192.168.1.1:1521/XE", OracleDriver.class },
+ { "jdbc:postgresql://HOST/DATABASE", PostgresqlDriver.class }
+ };
+
+ for (Object[] datum : data) {
+ String jdbcUrl = (String)datum[0];
+
+ @SuppressWarnings("unchecked")
+ Class<? extends DbDriver> expected = (Class<? extends DbDriver>)datum[1];
+
+ DbDriver actual = squelch.getDriverFor(jdbcUrl);
+
+ assertEquals(expected, actual.getClass());
+ }
+ }
+
+ @Test
+ public void test_getDriverFor_notFound()
+ {
+ String[] data = { null, "bogus:host/user/pass", "The quick brown fox jumps over the lazy dog" };
+ Squelch squelch = new Squelch();
+
+ for (String datum : data) {
+ try {
+ squelch.getDriverFor(datum);
+ fail("Should have thrown a SquelchException");
+ }
+ catch (SquelchException exc) {
+ ; // This is the success path.
+ }
+ }
+ }
@Test
public void test_isQuit() {
}
@Test
- public void test_pumpLines() throws IOException {
- Squelch squelch = new Squelch();
+ public void test_pumpLines() throws IOException, ClassNotFoundException, SQLException, SquelchException
+ {
+ Squelch squelch = new NoDbMock();
String consoleOutput;
String[] noLines = {};
consoleOutput);
}
- private String runPump(Squelch squelch, String[] lines) throws IOException
+ @Test
+ public void test_pumpLines_withSelect() throws ClassNotFoundException, IOException, SQLException, SquelchException
+ {
+ NoDbMock squelch = new NoDbMock();
+ String consoleOutput;
+
+ String[] simpleSelect = { "SELECT * FROM Fred WHERE wife='Wilma';", "QUIT" };
+ consoleOutput = runPump(squelch, simpleSelect);
+ assertEquals( Squelch.PROMPT + simpleSelect[0] + "\n"
+ + "No rows returned.\n"
+ + Squelch.PROMPT + simpleSelect[1] + "\n", consoleOutput);
+
+ ConnectionMock cm = squelch.mock_getConnectionMock();
+ assertTrue(cm.mock_queryWasExecuted(simpleSelect[0].substring(0, simpleSelect[0].length() - 1)));
+ }
+
+ private String runPump(Squelch squelch, String[] lines) throws IOException, ClassNotFoundException, SQLException, SquelchException
{
try (
ByteArrayOutputStream baos = new ByteArrayOutputStream();