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;
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;
13 import net.jaekl.squelch.util.ConsoleInputMock;
15 import org.junit.Test;
17 public class SquelchTest {
20 public void test_isQuit() {
21 Squelch squelch = new Squelch();
23 final String[] AFFIRMATIVE = {
24 "QUIT", " QUIT", " QUIT ", "\tQUIT\r\n", "\\q", " \\q", "QUIT;", "Quit", "qUiT", " qUIt ", "quit ;",
25 "exit", "ExIT", "EXIT", " EXIT ; ", null
27 final String[] NEGATIVE = {
28 "Fred", "SELECT * FROM FOO", "describe tablename", "",
29 "CREATE TABLE Fred ( foo INTEGER );"
32 for (String s : AFFIRMATIVE) {
33 assertTrue("isQuit(\"" + s + "\") should be true.", squelch.isQuit(s));
35 for (String s : NEGATIVE) {
36 assertFalse("isQuit(\"" + s + "\") should be false.", squelch.isQuit(s));
41 public void test_pumpLines() throws IOException {
42 Squelch squelch = new Squelch();
45 String[] noLines = {};
46 consoleOutput = runPump(squelch, noLines);
47 assertEquals("", consoleOutput);
49 String[] quitOnly = {"QUIT"};
50 consoleOutput = runPump(squelch, quitOnly);
51 assertEquals(Squelch.PROMPT + "QUIT\n", consoleOutput);
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",
61 private String runPump(Squelch squelch, String[] lines) throws IOException
64 ByteArrayOutputStream baos = new ByteArrayOutputStream();
65 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8))
68 ConsoleInputMock cim = new ConsoleInputMock(pw, lines);
69 squelch.pumpLines(pw, cim);
72 return baos.toString();