adds support for null suppression
[squelch.git] / src / test / java / net / jaekl / squelch / stmt / PSetTest.java
diff --git a/src/test/java/net/jaekl/squelch/stmt/PSetTest.java b/src/test/java/net/jaekl/squelch/stmt/PSetTest.java
new file mode 100644 (file)
index 0000000..c33e00a
--- /dev/null
@@ -0,0 +1,120 @@
+package net.jaekl.squelch.stmt;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.sql.SQLException;
+
+import net.jaekl.squelch.db.DbDriverMock;
+import net.jaekl.squelch.sql.ConnectionMock;
+
+import org.junit.Test;
+
+public class PSetTest {
+
+       @Test
+       public void testHandles() 
+       {
+               String[] valid = {
+                       "\\pset foo=bar",
+                       "\\PSet foo=bar",
+                       "\\Pset foo=bar",
+                       "\\PSET foo=bar",
+                       "\\pSET foo=bar"
+               };
+               
+               String[] invalid = {
+                       null,
+                       "",
+                       "SELECT * FROM SPQR WHERE NAME LIKE '%CAESAR%'",
+                       "\\psetfoo=bar",
+                       "\\pse foo=bar"
+               };
+               
+               PSet pset = new PSet();
+               
+               for (String input : valid) {
+                       assertTrue(pset.handles(input));
+               }
+               
+               for (String input : invalid) {
+                       assertFalse(pset.handles(input));
+               }
+       }
+
+       @Test
+       public void testExec_setValue() throws IOException, SQLException 
+       {
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               
+               ConnectionMock conn = new ConnectionMock();
+               DbDriverMock driver = new DbDriverMock();
+               PrintWriter pw = new PrintWriter(baos);
+               PSet pset = new PSet();
+               
+               String[] on = { "true", "on", "yes", "TRUE", "On", "Yes", "YES", "yEs" };
+               String[] off = { "false", "off", "no", "FALSE", "Off", "No", "FaLsE", "nO" };
+
+               for (String value : on) {
+                       pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
+                       assertTrue(driver.isSuppressNulls());
+                       
+                       pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
+                       assertTrue(driver.isSuppressNulls());
+               }
+               
+               for (String value : off) {
+                       pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
+                       assertFalse(driver.isSuppressNulls());
+                       
+                       pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
+                       assertFalse(driver.isSuppressNulls());
+               }
+               
+               for (String value : on) {
+                       pset.exec(driver, conn, pw, "\\PSET SuPPreSS_NuLLs=" + value);
+                       assertTrue(driver.isSuppressNulls());
+               }
+               
+               pw.close();
+               String output = baos.toString();
+               assertEquals("", output);
+       }
+       
+       @Test
+       public void testExec_displayState() throws IOException, SQLException
+       {
+               String[] cmds = { "\\pset suppress_nulls", "\\pset  suppress_nulls", "\\pset suppress_nulls ",
+                                         "\\pset SUPPRESS_NULLS", "\\PsET  SUPPress_NuLLS", "\\PSET SUPPRESS_NULLS " };
+               
+               for (String cmd : cmds) {
+                       DbDriverMock driver = new DbDriverMock();
+
+                       driver.setSuppressNulls(true);
+                       String output = doExec(driver, cmd);            
+                       assertEquals("suppress_nulls:  on\n", output);
+                       
+                       driver.setSuppressNulls(false);
+                       output = doExec(driver, cmd);
+                       assertEquals("suppress_nulls:  off\n", output);
+               }
+       }
+       
+       private String doExec(DbDriverMock driver, String cmd) throws IOException, SQLException 
+       {
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               
+               ConnectionMock conn = new ConnectionMock();
+               PrintWriter pw = new PrintWriter(baos);
+               PSet pset = new PSet();
+               
+               pset.exec(driver, conn, pw, cmd);
+               pw.close();
+               
+               String output = baos.toString();
+               return output;
+       }
+}