]> jaekl.net Git - squelch.git/blob - src/test/java/net/jaekl/squelch/ArgsTest.java
dumps the content of blobs, and reworks table formatting to add spaces around lines
[squelch.git] / src / test / java / net / jaekl / squelch / ArgsTest.java
1 package net.jaekl.squelch;
2
3 import static org.junit.Assert.*;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.io.PrintWriter;
9 import java.nio.charset.StandardCharsets;
10
11 import net.jaekl.squelch.util.FileMock;
12 import net.jaekl.squelch.util.FileUtilMock;
13
14 import org.junit.Test;
15
16 public class ArgsTest 
17 {
18         private static class Result {
19                 private boolean m_retVal;
20                 private Args m_args;
21                 private String m_stdout;
22                 
23                 public Result(boolean retVal, Args args, String stdout) {
24                         m_retVal = retVal;
25                         m_args = args;
26                         m_stdout = stdout;
27                 }
28                 
29                 public boolean getRetVal() { return m_retVal; }
30                 public Args getArgs() { return m_args; }
31                 public String getStdout() { return m_stdout; }
32         }
33
34         @Test
35         public void test_variousParams() throws IOException 
36         {
37                 Result result = null;
38                 
39                 String[] noParams = {};
40                 result = runWithParams(noParams);
41                 assertTrue(result.getRetVal());
42                 assertEquals(false, result.getArgs().isHelp());
43                 assertEquals(Args.DEFAULT_JDBC_URL, result.getArgs().getUrl());
44                 assertEquals("", result.getArgs().getUser());
45                 assertEquals("", result.getArgs().getPass());
46                 assertEquals("", result.getStdout());
47                 
48                 String[] helpOnly = { "--help" };
49                 result = runWithParams(helpOnly);
50                 assertTrue(result.getRetVal());
51                 assertEquals(true, result.getArgs().isHelp());
52                 assertEquals(Args.DEFAULT_JDBC_URL, result.getArgs().getUrl());
53                 assertEquals("", result.getArgs().getUser());
54                 assertEquals("", result.getArgs().getPass());
55                 validateUsageMessage(result.getStdout());
56                 
57                 String[] oneTwoThree = { "-r", "one", "-u", "two", "-p", "three" };
58                 result = runWithParams(oneTwoThree);
59                 assertTrue(result.getRetVal());
60                 assertEquals(false, result.getArgs().isHelp());
61                 assertEquals("one", result.getArgs().getUrl());
62                 assertEquals("two", result.getArgs().getUser());
63                 assertEquals("three", result.getArgs().getPass());
64                 assertEquals("", result.getStdout());
65                 
66                 String[] invalid = { "--bogus" };
67                 result = runWithParams(invalid);
68                 assertFalse(result.getRetVal());
69                 assertEquals(false, result.getArgs().isHelp());
70                 assertEquals(Args.DEFAULT_JDBC_URL, result.getArgs().getUrl());
71                 assertEquals("", result.getArgs().getUser());
72                 assertEquals("", result.getArgs().getPass());
73                 validateUsageMessage(result.getStdout());
74         }
75         
76         @Test
77         public void test_configFile_success() throws IOException
78         {
79                 final String CONFIG_FILE_NAME = "configFileName";
80                 final String SIMPLE_CONTENT = "db.user=fred\n"
81                                                     + "db.password=p455w0rd\n"
82                                                     + "db.url=jdbc:postgresql://localhost/cfb\n";
83                 
84                 Result result = null;
85                 
86                 FileUtilMock.mock_setInst();
87                 FileMock configFileMock = FileUtilMock.mock_getInst().mock_newFile(CONFIG_FILE_NAME);
88                 FileUtilMock.mock_getInst().mock_setReadable(configFileMock, true);
89                 configFileMock.mock_setContent(SIMPLE_CONTENT.getBytes(StandardCharsets.UTF_8));
90                 
91                 String[] simple = { "-c", CONFIG_FILE_NAME };
92                 result = runWithParams(simple);
93                 assertTrue(result.getRetVal());
94                 assertEquals(false, result.getArgs().isHelp());
95                 assertEquals("jdbc:postgresql://localhost/cfb", result.getArgs().getUrl());
96                 assertEquals("fred", result.getArgs().getUser());
97                 assertEquals("p455w0rd", result.getArgs().getPass());
98                 assertEquals("", result.getStdout());
99         }
100         
101         @Test
102         public void test_configFile_notReadable()
103         {
104                 final String CONFIG_FILE_NAME = "configFileName";
105                 
106                 FileUtilMock.mock_setInst();
107                 FileMock configFileMock = FileUtilMock.mock_getInst().mock_newFile(CONFIG_FILE_NAME);
108                 FileUtilMock.mock_getInst().mock_setReadable(configFileMock, false);
109                 
110                 String[] params = { "-c", CONFIG_FILE_NAME };
111                 try {
112                         runWithParams(params);
113                         fail("Should have thrown an IOException");
114                 }
115                 catch (IOException exc) {
116                         // This is the success path
117                 }
118         }
119         
120         // We expect the stdout string to contain a usage message.
121         // Do some sanity-checking to ensure that it looks vaguely correct.
122         private void validateUsageMessage(String stdout) 
123         {
124                 assertTrue(stdout.contains(this.getClass().getName()));
125                 assertTrue(stdout.contains("--help"));
126         }
127
128         private Result runWithParams(String[] params)
129                 throws IOException
130         {
131                 try (
132                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
133                                 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
134                         )
135                 {
136                         Args args = new Args(this.getClass().getName());
137                         boolean retVal = args.parseArgs(pw, params);
138
139                         pw.flush();
140                         baos.flush();
141                         String stdout = baos.toString(StandardCharsets.UTF_8.name());
142                         
143                         return new Result(retVal, args, stdout);
144                 }               
145         }
146 }