]> jaekl.net Git - squelch.git/blob - src/test/java/net/jaekl/squelch/stmt/PSetTest.java
adds support for null suppression
[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.DbDriverMock;
12 import net.jaekl.squelch.sql.ConnectionMock;
13
14 import org.junit.Test;
15
16 public class PSetTest {
17
18         @Test
19         public void testHandles() 
20         {
21                 String[] valid = {
22                         "\\pset foo=bar",
23                         "\\PSet foo=bar",
24                         "\\Pset foo=bar",
25                         "\\PSET foo=bar",
26                         "\\pSET foo=bar"
27                 };
28                 
29                 String[] invalid = {
30                         null,
31                         "",
32                         "SELECT * FROM SPQR WHERE NAME LIKE '%CAESAR%'",
33                         "\\psetfoo=bar",
34                         "\\pse foo=bar"
35                 };
36                 
37                 PSet pset = new PSet();
38                 
39                 for (String input : valid) {
40                         assertTrue(pset.handles(input));
41                 }
42                 
43                 for (String input : invalid) {
44                         assertFalse(pset.handles(input));
45                 }
46         }
47
48         @Test
49         public void testExec_setValue() throws IOException, SQLException 
50         {
51                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
52                 
53                 ConnectionMock conn = new ConnectionMock();
54                 DbDriverMock driver = new DbDriverMock();
55                 PrintWriter pw = new PrintWriter(baos);
56                 PSet pset = new PSet();
57                 
58                 String[] on = { "true", "on", "yes", "TRUE", "On", "Yes", "YES", "yEs" };
59                 String[] off = { "false", "off", "no", "FALSE", "Off", "No", "FaLsE", "nO" };
60
61                 for (String value : on) {
62                         pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
63                         assertTrue(driver.isSuppressNulls());
64                         
65                         pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
66                         assertTrue(driver.isSuppressNulls());
67                 }
68                 
69                 for (String value : off) {
70                         pset.exec(driver, conn, pw, "\\pset suppress_nulls=" + value);
71                         assertFalse(driver.isSuppressNulls());
72                         
73                         pset.exec(driver, conn, pw, "\\pset Suppress_NULLS=" + value);
74                         assertFalse(driver.isSuppressNulls());
75                 }
76                 
77                 for (String value : on) {
78                         pset.exec(driver, conn, pw, "\\PSET SuPPreSS_NuLLs=" + value);
79                         assertTrue(driver.isSuppressNulls());
80                 }
81                 
82                 pw.close();
83                 String output = baos.toString();
84                 assertEquals("", output);
85         }
86         
87         @Test
88         public void testExec_displayState() throws IOException, SQLException
89         {
90                 String[] cmds = { "\\pset suppress_nulls", "\\pset  suppress_nulls", "\\pset suppress_nulls ",
91                                           "\\pset SUPPRESS_NULLS", "\\PsET  SUPPress_NuLLS", "\\PSET SUPPRESS_NULLS " };
92                 
93                 for (String cmd : cmds) {
94                         DbDriverMock driver = new DbDriverMock();
95
96                         driver.setSuppressNulls(true);
97                         String output = doExec(driver, cmd);            
98                         assertEquals("suppress_nulls:  on\n", output);
99                         
100                         driver.setSuppressNulls(false);
101                         output = doExec(driver, cmd);
102                         assertEquals("suppress_nulls:  off\n", output);
103                 }
104         }
105         
106         private String doExec(DbDriverMock driver, String cmd) throws IOException, SQLException 
107         {
108                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
109                 
110                 ConnectionMock conn = new ConnectionMock();
111                 PrintWriter pw = new PrintWriter(baos);
112                 PSet pset = new PSet();
113                 
114                 pset.exec(driver, conn, pw, cmd);
115                 pw.close();
116                 
117                 String output = baos.toString();
118                 return output;
119         }
120 }