DroidCommandLine implements AutoCloseable { public int processExecution() throws CommandLineException { PrintWriter out = new PrintWriter(System.out); final CommandFactoryImpl localCommandFactory = new CommandFactoryImpl(context, out); this.setCommandFactory(localCommandFactory); int returnCode = 0; try { run(); out.flush(); context.close(); } catch (CommandLineSyntaxException clsx) { returnCode = 1; PrintWriter err = errorPrintWriterForTests == null ? new PrintWriter(System.err) : errorPrintWriterForTests; HelpFormatter formatter = new HelpFormatter(); formatter.printWrapped(err, WRAP_WIDTH, String.format(CLI_SYNTAX_INCORRECT, clsx.getMessage())); err.flush(); return returnCode; } catch (CommandExecutionException ceex) { returnCode = 1; PrintWriter err = new PrintWriter(System.err); HelpFormatter formatter = new HelpFormatter(); formatter.printWrapped(err, WRAP_WIDTH, ceex.getMessage()); err.flush(); throw ceex; } catch (CommandLineException clex) { returnCode = 1; PrintWriter err = new PrintWriter(System.err); HelpFormatter formatter = new HelpFormatter(); formatter.printWrapped(err, WRAP_WIDTH, clex.getMessage()); err.flush(); throw clex; } return returnCode; } DroidCommandLine(final String[] args); protected  DroidCommandLine(final String[] args, PrintWriter errorPrintWriter); GlobalContext getContext(); void setContext(GlobalContext context); void run(); static void main(final String[] args); int processExecution(); CommandFactory getCommandFactory(); void setCommandFactory(CommandFactory commandFactory); CommandLine getCommandLine(); @Override void close(); static final String USAGE; static final String CLI_SYNTAX_INCORRECT; static final int WRAP_WIDTH; static boolean systemExit; }
@Test public void testParseNonsense() throws CommandLineException{ String[] args = new String[] { "--zzzzzz" }; DroidCommandLine droidCommandLine = new DroidCommandLine(args, printWriter); Assert.assertEquals(1, droidCommandLine.processExecution()); verify(printWriter).println("Incorrect command line syntax: org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: --zzzzzz"); }
@Ignore @Test public void testVersionShort() throws CommandLineException { String[] args = new String[] { "-v" }; DroidCommandLine droidCommandLine = new DroidCommandLine(args); droidCommandLine.processExecution(); verify(printWriter).println("5.0.3-beta"); }
@Ignore @Test public void testVersionLong() throws CommandLineException { String[] args = new String[] { "-version", }; DroidCommandLine droidCommandLine = new DroidCommandLine(args); droidCommandLine.processExecution(); verify(printWriter).println("5.0.3-beta"); }
@Test @Ignore public void testHelpShort() throws CommandLineException { String[] args = new String[] { "-h" }; DroidCommandLine droidCommandLine = new DroidCommandLine(args); droidCommandLine.processExecution(); verify(printWriter).println("usage: droid [options]"); verify(printWriter).println("OPTIONS:"); TestUtil.verifyHelpOptions(printWriter); }
@Test @Ignore public void testHelpLong() throws CommandLineException { String[] args = new String[] { "-help" }; DroidCommandLine droidCommandLine = new DroidCommandLine(args); droidCommandLine.processExecution(); verify(printWriter).println("usage: droid [options]"); verify(printWriter).println("OPTIONS:"); TestUtil.verifyHelpOptions(printWriter); }
@Test public void testRunAndSaveProfileToMultipleFiles() throws CommandLineException { String[] args = new String[] { "-a", "file1.txt", "file/number/2.txt", "-p", "test1", "test2" }; ProfileRunCommand command = mock(ProfileRunCommand.class); when(context.getProfileRunCommand()).thenReturn(command); DroidCommandLine commandLine = new DroidCommandLine(args, printWriter); commandLine.processExecution(); verify(printWriter).println("Incorrect command line syntax: Must specify exactly one profile."); }
@Test public void testRunAndSaveProfileWithNoProfileName() throws CommandLineException { String[] args = new String[] { "-a", "file1.txt", "file/number/2.txt" }; DroidCommandLine commandLine = new DroidCommandLine(args, printWriter); commandLine.processExecution(); verify(printWriter).println("Incorrect command line syntax: Must specify exactly one profile."); }
@Test public void testRunAndSaveProfileWithNoResources() throws CommandLineException { String[] args = new String[] { "-a", "-p", "test.droid" }; DroidCommandLine commandLine = new DroidCommandLine(args, printWriter); commandLine.processExecution(); verify(printWriter).println("Incorrect command line syntax: org.apache.commons.cli.MissingArgumentException: Missing argument for option: a"); }
@Test public void testFilterHelp() throws CommandLineException { String[] args = new String[] { "-k" }; DroidCommandLine commandLine = new DroidCommandLine(args); commandLine.processExecution(); }
@Test public void testDisplayAllSignatureFiles() throws CommandLineException { String[] args = new String[] { "-X" }; ListAllSignatureFilesCommand command = mock(ListAllSignatureFilesCommand.class); when(context.getListAllSignatureFilesCommand()).thenReturn(command); DroidCommandLine commandLine = new DroidCommandLine(args); commandLine.processExecution(); }
@Test public void testConfigureDefaultSignatureFileVersionWithMissingValue() throws CommandLineException { String[] args = new String[] { "-s" }; ConfigureDefaultSignatureFileVersionCommand command = mock(ConfigureDefaultSignatureFileVersionCommand.class); when(context.getConfigureDefaultSignatureFileVersionCommand()).thenReturn(command); DroidCommandLine commandLine = new DroidCommandLine(args, printWriter); commandLine.processExecution(); verify(printWriter).println("Incorrect command line syntax: org.apache.commons.cli.MissingArgumentException: Missing argument for option: s"); }