Fix and fine-tune suppress_nulls.
[squelch.git] / src / test / java / net / jaekl / squelch / stmt / PSetTest.java
1 package net.jaekl.squelch.stmt;
2
3 import static org.junit.Assert.*;
4
5 import java.io.ByteArrayOutputStream;
6
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.sql.SQLException;
10
11 import net.jaekl.squelch.db.DbDriver;
12 import net.jaekl.squelch.db.DbDriverMock;
13 import net.jaekl.squelch.sql.ConnectionMock;
14
15 import org.junit.Test;
16
17 public class PSetTest {
18
19         @Test
20         public void testHandles() 
21         {
22                 String[] valid = {
23                         "\\pset foo=bar",
24                         "\\PSet foo=bar",
25                         "\\Pset foo=bar",
26                         "\\PSET foo=bar",
27                         "\\pSET foo=bar"
28                 };
29                 
30                 String[] invalid = {
31                         null,
32                         "",
33                         "SELECT * FROM SPQR WHERE NAME LIKE '%CAESAR%'",
34                         "\\psetfoo=bar",
35                         "\\pse foo=bar"
36                 };
37                 
38                 PSet pset = new PSet();
39                 
40                 for (String input : valid) {
41                         assertTrue(pset.handles(input));
42                 }
43                 
44                 for (String input : invalid) {
45                         assertFalse(pset.handles(input));
46                 }
47         }
48
49         @Test
50         public void testExec_setValue() throws IOException, SQLException 
51         {
52                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
53                 
54                 ConnectionMock conn = new ConnectionMock();
55                 DbDriverMock driver = new DbDriverMock();
56                 PrintWriter pw = new PrintWriter(baos);
57                 PSet pset = new PSet();
58                 
59                 String[] on = { "true", "on", "yes", "TRUE", "On", "Yes", "YES", "yEs" };
60                 String[] off = { "false", "off", "no", "FALSE", "Off", "No", "FaLsE", "nO" };
61
62                 for (String value : on) {
63                         pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
64                         assertTrue(driver.isSet(DbDriver.SUPPRESS_NULLS));
65                         
66                         pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
67                         assertTrue(driver.isSet(DbDriver.SUPPRESS_NULLS));
68                 }
69                 
70                 for (String value : off) {
71                         pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
72                         assertFalse(driver.isSet(DbDriver.SUPPRESS_NULLS));
73                         
74                         pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
75                         assertFalse(driver.isSet(DbDriver.SUPPRESS_NULLS));
76                 }
77                 
78                 for (String value : on) {
79                         pset.exec(driver, conn, pw, "\\PSET SuPPreSS_NuLLs=" + value);
80                         assertTrue(driver.isSet(DbDriver.SUPPRESS_NULLS));
81                 }
82                 
83                 pw.close();
84                 String output = baos.toString();
85                 assertEquals("", output);
86         }
87         
88         @Test
89         public void testExec_displayState() throws IOException, SQLException
90         {
91                 String[] cmds = { "\\pset suppress_nulls", "\\pset  suppress_nulls", "\\pset suppress_nulls ",
92                                           "\\pset SUPPRESS_NULLS", "\\PsET  SUPPress_NuLLS", "\\PSET SUPPRESS_NULLS " };
93                 
94                 for (String cmd : cmds) {
95                         DbDriverMock driver = new DbDriverMock();
96
97                         driver.set(DbDriver.SUPPRESS_NULLS, true);
98                         String output = doExec(driver, cmd);            
99                         assertEquals("suppress_nulls:  true\n", output);
100                         
101                         driver.set(DbDriver.SUPPRESS_NULLS, false);
102                         output = doExec(driver, cmd);
103                         assertEquals("suppress_nulls:  false\n", output);
104                 }
105         }
106         
107         @Test
108         public void testExec_displayAll() throws IOException, SQLException
109         {
110                 String[] cmds = { "\\pset", "\\pset  ", "\\pset ",
111                           "\\PsET", "\\PsET ", "\\PSET", "\\PSET  " };
112
113                 for (String cmd : cmds) {
114                         DbDriverMock driver = new DbDriverMock();
115                 
116                         driver.set(DbDriver.SUPPRESS_NULLS, true);
117                         String output = doExec(driver, cmd);            
118                         assertTrue(output.contains("suppress_nulls:  true\n"));
119                         
120                         driver.set(DbDriver.SUPPRESS_NULLS, false);
121                         output = doExec(driver, cmd);
122                         assertTrue(output.contains("suppress_nulls:  false\n"));
123                 }               
124         }
125         
126         private String doExec(DbDriverMock driver, String cmd) throws IOException, SQLException 
127         {
128                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
129                 
130                 ConnectionMock conn = new ConnectionMock();
131                 PrintWriter pw = new PrintWriter(baos);
132                 PSet pset = new PSet();
133                 
134                 pset.exec(driver, conn, pw, cmd);
135                 pw.close();
136                 
137                 String output = baos.toString();
138                 return output;
139         }
140 }