fc12402396d67159dd18155430b85c198fda7083
[squelch.git] / src / test / java / net / jaekl / squelch / SquelchTest.java
1 package net.jaekl.squelch;
2
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;
7
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;
15
16 import net.jaekl.squelch.db.ConnectionMock;
17 import net.jaekl.squelch.db.DbDriver;
18 import net.jaekl.squelch.db.MsSqlDriver;
19 import net.jaekl.squelch.db.MySqlDriver;
20 import net.jaekl.squelch.db.OracleDriver;
21 import net.jaekl.squelch.db.PostgresqlDriver;
22 import net.jaekl.squelch.util.ConsoleInputMock;
23
24 import org.junit.Test;
25
26 public class SquelchTest {
27         private class NoDbMock extends Squelch {
28                 private ConnectionMock m_conn;
29                 
30                 public NoDbMock() {
31                         m_conn = new ConnectionMock();
32                 }
33                 
34                 @Override
35                 Connection getConnection() throws ClassNotFoundException, SQLException, SquelchException 
36                 {
37                         return m_conn;
38                 }
39                 
40                 public ConnectionMock mock_getConnectionMock() 
41                 {
42                         return m_conn;
43                 }
44         }
45         
46         @Test
47         public void test_getDriverFor_success() throws SquelchException {
48                 Squelch squelch = new Squelch();
49                 
50                 Object[][] data = {
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 }
60                 };
61                 
62                 for (Object[] datum : data) {
63                         String jdbcUrl = (String)datum[0];
64                         
65                         @SuppressWarnings("unchecked")
66                         Class<? extends DbDriver> expected = (Class<? extends DbDriver>)datum[1];
67                         
68                         DbDriver actual = squelch.getDriverFor(jdbcUrl);
69                         
70                         assertEquals(expected, actual.getClass());
71                 }
72         }
73         
74         @Test
75         public void test_getDriverFor_notFound() 
76         {
77                 String[] data = { null, "bogus:host/user/pass", "The quick brown fox jumps over the lazy dog" };
78                 Squelch squelch = new Squelch();
79                 
80                 for (String datum : data) {
81                         try {
82                                 squelch.getDriverFor(datum);
83                                 fail("Should have thrown a SquelchException");
84                         }
85                         catch (SquelchException exc) {
86                                 ;       // This is the success path.
87                         }
88                 }
89         }
90
91         @Test
92         public void test_isQuit() {
93                 Squelch squelch = new Squelch();
94                 
95                 final String[] AFFIRMATIVE = {
96                                 "QUIT", " QUIT", " QUIT ", "\tQUIT\r\n", "\\q", "  \\q", "QUIT;", "Quit", "qUiT", " qUIt ", "quit ;",
97                                 "exit", "ExIT", "EXIT", "  EXIT ; ", null
98                 };
99                 final String[] NEGATIVE = {
100                                 "Fred", "SELECT * FROM FOO", "describe tablename", "",
101                                 "CREATE TABLE Fred ( foo INTEGER );"
102                 };
103                 
104                 for (String s : AFFIRMATIVE) {
105                         assertTrue("isQuit(\"" + s + "\") should be true.", squelch.isQuit(s));
106                 }
107                 for (String s : NEGATIVE) {
108                         assertFalse("isQuit(\"" + s + "\") should be false.", squelch.isQuit(s));
109                 }
110         }
111         
112         @Test
113         public void test_pumpLines() throws IOException, ClassNotFoundException, SQLException, SquelchException 
114         {
115                 Squelch squelch = new NoDbMock();
116                 String consoleOutput;
117                 
118                 String[] noLines = {};
119                 consoleOutput = runPump(squelch, noLines);
120                 assertEquals("", consoleOutput);
121                 
122                 String[] quitOnly = {"QUIT"};
123                 consoleOutput = runPump(squelch, quitOnly);
124                 assertEquals(Squelch.PROMPT + "QUIT\n", consoleOutput);
125                 
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",
131                                      consoleOutput);
132         }
133         
134         @Test
135         public void test_pumpLines_withSelect() throws ClassNotFoundException, IOException, SQLException, SquelchException
136         {
137                 NoDbMock squelch = new NoDbMock();
138                 String consoleOutput;
139                 
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);
145                 
146                 ConnectionMock cm = squelch.mock_getConnectionMock();
147                 assertTrue(cm.mock_queryWasExecuted(simpleSelect[0]));
148         }
149         
150         private String runPump(Squelch squelch, String[] lines) throws IOException, ClassNotFoundException, SQLException, SquelchException
151         {
152                 try (
153                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
154                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8))
155                         )
156                 {
157                         ConsoleInputMock cim = new ConsoleInputMock(pw, lines);
158                         squelch.pumpLines(pw, cim);
159                         pw.close();
160                         baos.close();
161                         return baos.toString();
162                 }               
163         }
164 }