1 package net.jaekl.squelch;
3 import static org.junit.Assert.*;
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;
11 import net.jaekl.squelch.util.FileMock;
12 import net.jaekl.squelch.util.FileUtilMock;
14 import org.junit.Test;
18 private static class Result {
19 private boolean m_retVal;
21 private String m_stdout;
23 public Result(boolean retVal, Args args, String stdout) {
29 public boolean getRetVal() { return m_retVal; }
30 public Args getArgs() { return m_args; }
31 public String getStdout() { return m_stdout; }
35 public void test_variousParams() throws IOException
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());
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());
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());
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());
77 public void test_configFile_success() throws IOException
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";
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));
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());
102 public void test_configFile_notReadable()
104 final String CONFIG_FILE_NAME = "configFileName";
106 FileUtilMock.mock_setInst();
107 FileMock configFileMock = FileUtilMock.mock_getInst().mock_newFile(CONFIG_FILE_NAME);
108 FileUtilMock.mock_getInst().mock_setReadable(configFileMock, false);
110 String[] params = { "-c", CONFIG_FILE_NAME };
112 runWithParams(params);
113 fail("Should have thrown an IOException");
115 catch (IOException exc) {
116 // This is the success path
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)
124 assertTrue(stdout.contains(this.getClass().getName()));
125 assertTrue(stdout.contains("--help"));
128 private Result runWithParams(String[] params)
132 ByteArrayOutputStream baos = new ByteArrayOutputStream();
133 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, StandardCharsets.UTF_8));
136 Args args = new Args(this.getClass().getName());
137 boolean retVal = args.parseArgs(pw, params);
141 String stdout = baos.toString(StandardCharsets.UTF_8.name());
143 return new Result(retVal, args, stdout);