1 package net.jaekl.squelch.stmt;
3 import static org.junit.Assert.*;
5 import java.io.ByteArrayOutputStream;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.sql.SQLException;
11 import net.jaekl.squelch.db.DbDriver;
12 import net.jaekl.squelch.db.DbDriverMock;
13 import net.jaekl.squelch.sql.ConnectionMock;
15 import org.junit.Test;
17 public class PSetTest {
20 public void testHandles()
33 "SELECT * FROM SPQR WHERE NAME LIKE '%CAESAR%'",
38 PSet pset = new PSet();
40 for (String input : valid) {
41 assertTrue(pset.handles(input));
44 for (String input : invalid) {
45 assertFalse(pset.handles(input));
50 public void testExec_setValue() throws IOException, SQLException
52 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54 ConnectionMock conn = new ConnectionMock();
55 DbDriverMock driver = new DbDriverMock();
56 PrintWriter pw = new PrintWriter(baos);
57 PSet pset = new PSet();
59 String[] on = { "true", "on", "yes", "TRUE", "On", "Yes", "YES", "yEs" };
60 String[] off = { "false", "off", "no", "FALSE", "Off", "No", "FaLsE", "nO" };
62 for (String value : on) {
63 pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
64 assertTrue(driver.isSet(DbDriver.SUPPRESS_NULLS));
66 pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
67 assertTrue(driver.isSet(DbDriver.SUPPRESS_NULLS));
70 for (String value : off) {
71 pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
72 assertFalse(driver.isSet(DbDriver.SUPPRESS_NULLS));
74 pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
75 assertFalse(driver.isSet(DbDriver.SUPPRESS_NULLS));
78 for (String value : on) {
79 pset.exec(driver, conn, pw, "\\PSET SuPPreSS_NuLLs=" + value);
80 assertTrue(driver.isSet(DbDriver.SUPPRESS_NULLS));
84 String output = baos.toString();
85 assertEquals("", output);
89 public void testExec_displayState() throws IOException, SQLException
91 String[] cmds = { "\\pset suppress_nulls", "\\pset suppress_nulls", "\\pset suppress_nulls ",
92 "\\pset SUPPRESS_NULLS", "\\PsET SUPPress_NuLLS", "\\PSET SUPPRESS_NULLS " };
94 for (String cmd : cmds) {
95 DbDriverMock driver = new DbDriverMock();
97 driver.set(DbDriver.SUPPRESS_NULLS, true);
98 String output = doExec(driver, cmd);
99 assertEquals("suppress_nulls: true\n", output);
101 driver.set(DbDriver.SUPPRESS_NULLS, false);
102 output = doExec(driver, cmd);
103 assertEquals("suppress_nulls: false\n", output);
108 public void testExec_displayAll() throws IOException, SQLException
110 String[] cmds = { "\\pset", "\\pset ", "\\pset ",
111 "\\PsET", "\\PsET ", "\\PSET", "\\PSET " };
113 for (String cmd : cmds) {
114 DbDriverMock driver = new DbDriverMock();
116 driver.set(DbDriver.SUPPRESS_NULLS, true);
117 String output = doExec(driver, cmd);
118 assertTrue(output.contains("suppress_nulls: true\n"));
120 driver.set(DbDriver.SUPPRESS_NULLS, false);
121 output = doExec(driver, cmd);
122 assertTrue(output.contains("suppress_nulls: false\n"));
126 private String doExec(DbDriverMock driver, String cmd) throws IOException, SQLException
128 ByteArrayOutputStream baos = new ByteArrayOutputStream();
130 ConnectionMock conn = new ConnectionMock();
131 PrintWriter pw = new PrintWriter(baos);
132 PSet pset = new PSet();
134 pset.exec(driver, conn, pw, cmd);
137 String output = baos.toString();