@Test public void nonInteractiveHasNoHistory() throws Exception { // given NonInteractiveShellRunner runner = new NonInteractiveShellRunner( FailBehavior.FAIL_AT_END, cmdExecuter, logger, statementParser, new ByteArrayInputStream("".getBytes())); // when then assertEquals(Historian.empty, runner.getHistorian()); } }
@Test public void testSimple() throws Exception { String input = "good1;\n" + "good2;\n"; NonInteractiveShellRunner runner = new NonInteractiveShellRunner( FailBehavior.FAIL_FAST, cmdExecuter, logger, statementParser, new ByteArrayInputStream(input.getBytes())); int code = runner.runUntilEnd(); assertEquals("Exit code incorrect", 0, code); verify(logger, times(0)).printError(anyString()); }
@Test public void testFailAtEnd() throws Exception { String input = "good1;\n" + "bad;\n" + "good2;\n" + "bad;\n"; NonInteractiveShellRunner runner = new NonInteractiveShellRunner( FailBehavior.FAIL_AT_END, cmdExecuter, logger, statementParser, new ByteArrayInputStream(input.getBytes())); int code = runner.runUntilEnd(); assertEquals("Exit code incorrect", 1, code); verify(logger, times(2)).printError(badLineError); }
/** * Get an appropriate shellrunner depending on the given arguments and if we are running in a TTY. * * @param cliArgs * @param cypherShell * @param logger * @param connectionConfig * @return a ShellRunner * @throws IOException */ @Nonnull static ShellRunner getShellRunner(@Nonnull CliArgs cliArgs, @Nonnull CypherShell cypherShell, @Nonnull Logger logger, @Nonnull ConnectionConfig connectionConfig) throws IOException { if (cliArgs.getCypher().isPresent()) { return new StringShellRunner(cliArgs, cypherShell, logger); } else if (shouldBeInteractive(cliArgs)) { UserMessagesHandler userMessagesHandler = new UserMessagesHandler(connectionConfig, cypherShell.getServerVersion()); return new InteractiveShellRunner(cypherShell, cypherShell, logger, new ShellStatementParser(), System.in, FileHistorian.getDefaultHistoryFile(), userMessagesHandler); } else { return new NonInteractiveShellRunner(cliArgs.getFailBehavior(), cypherShell, logger, new ShellStatementParser(), System.in); } }
@Test public void testFailFast() throws Exception { String input = "good1;\n" + "bad;\n" + "good2;\n" + "bad;\n"; NonInteractiveShellRunner runner = new NonInteractiveShellRunner( FailBehavior.FAIL_FAST, cmdExecuter, logger, statementParser, new ByteArrayInputStream(input.getBytes())); int code = runner.runUntilEnd(); assertEquals("Exit code incorrect", 1, code); verify(logger).printError(badLineError); }
@Test public void runUntilEndExitsImmediatelyOnParseError() throws Exception { // given StatementParser statementParser = mock(StatementParser.class); RuntimeException boom = new RuntimeException("BOOM"); doThrow(boom).when(statementParser).parseMoreText(anyString()); String input = "good1;\n" + "bad;\n" + "good2;\n" + "bad;\n"; NonInteractiveShellRunner runner = new NonInteractiveShellRunner( FailBehavior.FAIL_AT_END, cmdExecuter, logger, statementParser, new ByteArrayInputStream(input.getBytes())); // when int code = runner.runUntilEnd(); // then assertEquals(1, code); verify(logger).printError(boom); }
@Test public void runUntilEndExitsImmediatelyOnExitCommand() throws Exception { // given String input = "good1;\n" + "bad;\n" + "good2;\n" + "bad;\n"; NonInteractiveShellRunner runner = new NonInteractiveShellRunner( FailBehavior.FAIL_AT_END, cmdExecuter, logger, statementParser, new ByteArrayInputStream(input.getBytes())); // when doThrow(new ExitException(99)).when(cmdExecuter).execute(anyString()); int code = runner.runUntilEnd(); // then assertEquals(99, code); verify(cmdExecuter).execute("good1;"); verifyNoMoreInteractions(cmdExecuter); }