DbDeployCommandLineParser { public void parse(String[] args, DbDeploy dbDeploy) throws UsageException { try { dbDeploy.setScriptdirectory(new File(".")); final CommandLine commandLine = new GnuParser().parse(getOptions(), args); copyValuesFromCommandLineToDbDeployBean(dbDeploy, commandLine); if (commandLine.hasOption("password") && commandLine.getOptionValue("password") == null) { dbDeploy.setPassword(userInputReader.read("Password")); } } catch (ParseException e) { throw new UsageException(e.getMessage(), e); } } DbDeployCommandLineParser();  DbDeployCommandLineParser(UserInputReader userInputReader); void parse(String[] args, DbDeploy dbDeploy); void printUsage();  }
@Test public void canParseUserIdFromCommandLine() throws Exception { parser.parse("-U myuserid".split(" "), dbDeploy); assertEquals("myuserid", dbDeploy.getUserid()); }
@Test public void checkAllOfTheOtherFieldsParseOkHere() throws Exception { parser.parse(("-U userid -Ppassword --driver a.b.c --url b:c:d " + "--scriptdirectory . -o output.sql " + "--changeLogTableName my-change-log " + "--dbms ora " + "--templatedir /tmp/mytemplates " + "--delimiter \\ --delimitertype row").split(" "), dbDeploy); assertThat(dbDeploy.getUserid(), is("userid")); assertThat(dbDeploy.getPassword(), is("password")); assertThat(dbDeploy.getDriver(), is("a.b.c")); assertThat(dbDeploy.getUrl(), is("b:c:d")); assertThat(dbDeploy.getScriptdirectory().getName(), is(".")); assertThat(dbDeploy.getOutputfile().getName(), is("output.sql")); assertThat(dbDeploy.getDbms(), is("ora")); assertThat(dbDeploy.getChangeLogTableName(), is("my-change-log")); assertThat(dbDeploy.getDelimiter(), is("\\")); assertThat(dbDeploy.getDelimiterType(), is(DelimiterType.row)); assertThat(dbDeploy.getTemplatedir().getPath(), is(File.separator + "tmp" + File.separator + "mytemplates")); }
@Test public void delimiterTypeWorksOk() throws Exception { parser.parse("--delimitertype normal".split(" "), dbDeploy); assertThat(dbDeploy.getDelimiterType(), is(DelimiterType.normal)); parser.parse("--delimitertype row".split(" "), dbDeploy); assertThat(dbDeploy.getDelimiterType(), is(DelimiterType.row)); }
@Test public void lineEndingWorksOk() throws Exception { assertThat(dbDeploy.getLineEnding(), is(LineEnding.platform)); parser.parse("--lineending cr".split(" "), dbDeploy); assertThat(dbDeploy.getLineEnding(), is(LineEnding.cr)); parser.parse("--lineending crlf".split(" "), dbDeploy); assertThat(dbDeploy.getLineEnding(), is(LineEnding.crlf)); parser.parse("--lineending lf".split(" "), dbDeploy); assertThat(dbDeploy.getLineEnding(), is(LineEnding.lf)); parser.parse("--lineending platform".split(" "), dbDeploy); assertThat(dbDeploy.getLineEnding(), is(LineEnding.platform)); }
@Test public void shouldPromptFromStdinForPasswordIfPasswordParamSuppliedWithNoArg() throws Exception { when(userInputReader.read("Password")).thenReturn("user entered password"); parser.parse(new String[] { "-P" }, dbDeploy); assertThat(dbDeploy.getPassword(), is("user entered password")); }
@Test public void shouldNotPromptForPasswordWhenSupplied() throws Exception { parser.parse(new String[]{"-P", "password"}, dbDeploy); verifyZeroInteractions(userInputReader); }
@Test public void shouldNotPromptForPasswordNotSpecifiedOnCommandLine() throws Exception { parser.parse(new String[] {}, dbDeploy); verifyZeroInteractions(userInputReader); }