Add DbDriver, with support for a few popular JDBC drivers.
[squelch.git] / src / test / java / net / jaekl / squelch / SquelchTest.java
index 5a5c470deac1b05d297a5fad75918a6710df8790..fc12402396d67159dd18155430b85c198fda7083 100644 (file)
@@ -3,18 +3,90 @@ package net.jaekl.squelch;
 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.ConnectionMock;
+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.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() 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() {
@@ -38,8 +110,9 @@ public class SquelchTest {
        }
        
        @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 = {};
@@ -58,7 +131,23 @@ public class SquelchTest {
                                     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"
+                                        + "0 row(s) returned.\n"
+                                    + Squelch.PROMPT + simpleSelect[1] + "\n", consoleOutput);
+               
+               ConnectionMock cm = squelch.mock_getConnectionMock();
+               assertTrue(cm.mock_queryWasExecuted(simpleSelect[0]));
+       }
+       
+       private String runPump(Squelch squelch, String[] lines) throws IOException, ClassNotFoundException, SQLException, SquelchException
        {
                try (
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();