5a5c470deac1b05d297a5fad75918a6710df8790
[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
7 import java.io.ByteArrayOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStreamWriter;
10 import java.io.PrintWriter;
11 import java.nio.charset.StandardCharsets;
12
13 import net.jaekl.squelch.util.ConsoleInputMock;
14
15 import org.junit.Test;
16
17 public class SquelchTest {
18
19         @Test
20         public void test_isQuit() {
21                 Squelch squelch = new Squelch();
22                 
23                 final String[] AFFIRMATIVE = {
24                                 "QUIT", " QUIT", " QUIT ", "\tQUIT\r\n", "\\q", "  \\q", "QUIT;", "Quit", "qUiT", " qUIt ", "quit ;",
25                                 "exit", "ExIT", "EXIT", "  EXIT ; ", null
26                 };
27                 final String[] NEGATIVE = {
28                                 "Fred", "SELECT * FROM FOO", "describe tablename", "",
29                                 "CREATE TABLE Fred ( foo INTEGER );"
30                 };
31                 
32                 for (String s : AFFIRMATIVE) {
33                         assertTrue("isQuit(\"" + s + "\") should be true.", squelch.isQuit(s));
34                 }
35                 for (String s : NEGATIVE) {
36                         assertFalse("isQuit(\"" + s + "\") should be false.", squelch.isQuit(s));
37                 }
38         }
39         
40         @Test
41         public void test_pumpLines() throws IOException {
42                 Squelch squelch = new Squelch();
43                 String consoleOutput;
44                 
45                 String[] noLines = {};
46                 consoleOutput = runPump(squelch, noLines);
47                 assertEquals("", consoleOutput);
48                 
49                 String[] quitOnly = {"QUIT"};
50                 consoleOutput = runPump(squelch, quitOnly);
51                 assertEquals(Squelch.PROMPT + "QUIT\n", consoleOutput);
52                 
53                 String[] unrecognizedThenQuit = {"This is not a valid command;", "\\q"};
54                 consoleOutput = runPump(squelch, unrecognizedThenQuit);
55                 assertEquals(Squelch.PROMPT + "This is not a valid command;\n"
56                                      + "??? \"This is not a valid command;\"\n"
57                                      + Squelch.PROMPT + "\\q\n",
58                                      consoleOutput);
59         }
60         
61         private String runPump(Squelch squelch, String[] lines) throws IOException
62         {
63                 try (
64                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
65                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8))
66                         )
67                 {
68                         ConsoleInputMock cim = new ConsoleInputMock(pw, lines);
69                         squelch.pumpLines(pw, cim);
70                         pw.close();
71                         baos.close();
72                         return baos.toString();
73                 }               
74         }
75 }