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.DbDriverMock;
12 import net.jaekl.squelch.sql.ConnectionMock;
14 import org.junit.Test;
16 public class PSetTest {
19 public void testHandles()
32 "SELECT * FROM SPQR WHERE NAME LIKE '%CAESAR%'",
37 PSet pset = new PSet();
39 for (String input : valid) {
40 assertTrue(pset.handles(input));
43 for (String input : invalid) {
44 assertFalse(pset.handles(input));
49 public void testExec_setValue() throws IOException, SQLException
51 ByteArrayOutputStream baos = new ByteArrayOutputStream();
53 ConnectionMock conn = new ConnectionMock();
54 DbDriverMock driver = new DbDriverMock();
55 PrintWriter pw = new PrintWriter(baos);
56 PSet pset = new PSet();
58 String[] on = { "true", "on", "yes", "TRUE", "On", "Yes", "YES", "yEs" };
59 String[] off = { "false", "off", "no", "FALSE", "Off", "No", "FaLsE", "nO" };
61 for (String value : on) {
62 pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
63 assertTrue(driver.isSuppressNulls());
65 pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
66 assertTrue(driver.isSuppressNulls());
69 for (String value : off) {
70 pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
71 assertFalse(driver.isSuppressNulls());
73 pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
74 assertFalse(driver.isSuppressNulls());
77 for (String value : on) {
78 pset.exec(driver, conn, pw, "\\PSET SuPPreSS_NuLLs=" + value);
79 assertTrue(driver.isSuppressNulls());
83 String output = baos.toString();
84 assertEquals("", output);
88 public void testExec_displayState() throws IOException, SQLException
90 String[] cmds = { "\\pset suppress_nulls", "\\pset suppress_nulls", "\\pset suppress_nulls ",
91 "\\pset SUPPRESS_NULLS", "\\PsET SUPPress_NuLLS", "\\PSET SUPPRESS_NULLS " };
93 for (String cmd : cmds) {
94 DbDriverMock driver = new DbDriverMock();
96 driver.setSuppressNulls(true);
97 String output = doExec(driver, cmd);
98 assertEquals("suppress_nulls: on\n", output);
100 driver.setSuppressNulls(false);
101 output = doExec(driver, cmd);
102 assertEquals("suppress_nulls: off\n", output);
106 private String doExec(DbDriverMock driver, String cmd) throws IOException, SQLException
108 ByteArrayOutputStream baos = new ByteArrayOutputStream();
110 ConnectionMock conn = new ConnectionMock();
111 PrintWriter pw = new PrintWriter(baos);
112 PSet pset = new PSet();
114 pset.exec(driver, conn, pw, cmd);
117 String output = baos.toString();