@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); }
@Override public int runUntilEnd() { List<String> statements; try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { bufferedReader .lines() .forEach(line -> statementParser.parseMoreText(line + "\n")); statements = statementParser.consumeStatements(); } catch (Throwable e) { logger.printError(e); return 1; } int exitCode = EXIT_SUCCESS; for (String statement : statements) { try { executer.execute(statement); } catch (ExitException e) { return e.getCode(); } catch (Throwable e) { exitCode = EXIT_FAILURE; logger.printError(e); if (FailBehavior.FAIL_AT_END != failBehavior) { return exitCode; } } } return exitCode; }
NonInteractiveShellRunner implements ShellRunner { @Override public int runUntilEnd() { List<String> statements; try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { bufferedReader .lines() .forEach(line -> statementParser.parseMoreText(line + "\n")); statements = statementParser.consumeStatements(); } catch (Throwable e) { logger.printError(e); return 1; } int exitCode = EXIT_SUCCESS; for (String statement : statements) { try { executer.execute(statement); } catch (ExitException e) { return e.getCode(); } catch (Throwable e) { exitCode = EXIT_FAILURE; logger.printError(e); if (FailBehavior.FAIL_AT_END != failBehavior) { return exitCode; } } } return exitCode; } }
NonInteractiveShellRunner implements ShellRunner { @Override public int runUntilEnd() { List<String> statements; try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { bufferedReader .lines() .forEach(line -> statementParser.parseMoreText(line + "\n")); statements = statementParser.consumeStatements(); } catch (Throwable e) { logger.printError(e); return 1; } int exitCode = EXIT_SUCCESS; for (String statement : statements) { try { executer.execute(statement); } catch (ExitException e) { return e.getCode(); } catch (Throwable e) { exitCode = EXIT_FAILURE; logger.printError(e); if (FailBehavior.FAIL_AT_END != failBehavior) { return exitCode; } } } return exitCode; } NonInteractiveShellRunner(@Nonnull FailBehavior failBehavior,
                                     @Nonnull StatementExecuter executer,
                                     @Nonnull Logger logger,
                                     @Nonnull StatementParser statementParser,
                                     @Nonnull InputStream inputStream); }
NonInteractiveShellRunner implements ShellRunner { @Override public int runUntilEnd() { List<String> statements; try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { bufferedReader .lines() .forEach(line -> statementParser.parseMoreText(line + "\n")); statements = statementParser.consumeStatements(); } catch (Throwable e) { logger.printError(e); return 1; } int exitCode = EXIT_SUCCESS; for (String statement : statements) { try { executer.execute(statement); } catch (ExitException e) { return e.getCode(); } catch (Throwable e) { exitCode = EXIT_FAILURE; logger.printError(e); if (FailBehavior.FAIL_AT_END != failBehavior) { return exitCode; } } } return exitCode; } NonInteractiveShellRunner(@Nonnull FailBehavior failBehavior,
                                     @Nonnull StatementExecuter executer,
                                     @Nonnull Logger logger,
                                     @Nonnull StatementParser statementParser,
                                     @Nonnull InputStream inputStream); @Override int runUntilEnd(); @Nonnull @Override Historian getHistorian(); }
NonInteractiveShellRunner implements ShellRunner { @Override public int runUntilEnd() { List<String> statements; try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { bufferedReader .lines() .forEach(line -> statementParser.parseMoreText(line + "\n")); statements = statementParser.consumeStatements(); } catch (Throwable e) { logger.printError(e); return 1; } int exitCode = EXIT_SUCCESS; for (String statement : statements) { try { executer.execute(statement); } catch (ExitException e) { return e.getCode(); } catch (Throwable e) { exitCode = EXIT_FAILURE; logger.printError(e); if (FailBehavior.FAIL_AT_END != failBehavior) { return exitCode; } } } return exitCode; } NonInteractiveShellRunner(@Nonnull FailBehavior failBehavior,
                                     @Nonnull StatementExecuter executer,
                                     @Nonnull Logger logger,
                                     @Nonnull StatementParser statementParser,
                                     @Nonnull InputStream inputStream); @Override int runUntilEnd(); @Nonnull @Override Historian getHistorian();  }