public void testThatThrowExceptionCanBeLogged() throws Exception {
    CaptureOutputTerminal terminal = new CaptureOutputTerminal();
    NamedCommand cmd =
        new NamedCommand("cmd", terminal) {
          @Override
          public CliTool.ExitStatus execute(Settings settings, Environment env) throws Exception {
            throw new ElasticsearchException("error message");
          }
        };
    SingleCmdTool tool = new SingleCmdTool("tool", terminal, cmd);
    assertStatus(tool.execute(), CliTool.ExitStatus.CODE_ERROR);
    assertThat(terminal.getTerminalOutput(), hasSize(1));
    assertThat(terminal.getTerminalOutput(), hasItem(containsString("error message")));

    // set env... and log stack trace
    try {
      System.setProperty(Terminal.DEBUG_SYSTEM_PROPERTY, "true");
      terminal = new CaptureOutputTerminal();
      assertStatus(
          new SingleCmdTool("tool", terminal, cmd).execute(), CliTool.ExitStatus.CODE_ERROR);
      assertThat(terminal.getTerminalOutput(), hasSize(2));
      assertThat(terminal.getTerminalOutput(), hasItem(containsString("error message")));
      // This class must be part of the stack strace
      assertThat(terminal.getTerminalOutput(), hasItem(containsString(getClass().getName())));
    } finally {
      System.clearProperty(Terminal.DEBUG_SYSTEM_PROPERTY);
    }
  }
 public void testIOError() throws Exception {
   Terminal terminal = new MockTerminal();
   final AtomicReference<Boolean> executed = new AtomicReference<>(false);
   final NamedCommand cmd =
       new NamedCommand("cmd", terminal) {
         @Override
         public CliTool.ExitStatus execute(Settings settings, Environment env) throws Exception {
           executed.set(true);
           throw new IOException("io error");
         }
       };
   SingleCmdTool tool = new SingleCmdTool("tool", terminal, cmd);
   CliTool.ExitStatus status = tool.execute();
   assertStatus(status, CliTool.ExitStatus.IO_ERROR);
   assertCommandHasBeenExecuted(executed);
 }
 public void testSingleCommandToolHelp() throws Exception {
   CaptureOutputTerminal terminal = new CaptureOutputTerminal();
   final AtomicReference<Boolean> executed = new AtomicReference<>(false);
   final NamedCommand cmd =
       new NamedCommand("cmd1", terminal) {
         @Override
         public CliTool.ExitStatus execute(Settings settings, Environment env) throws Exception {
           executed.set(true);
           throw new IOException("io error");
         }
       };
   SingleCmdTool tool = new SingleCmdTool("tool", terminal, cmd);
   CliTool.ExitStatus status = tool.execute(args("-h"));
   assertStatus(status, CliTool.ExitStatus.OK_AND_EXIT);
   assertThat(terminal.getTerminalOutput(), hasSize(3));
   assertThat(terminal.getTerminalOutput(), hasItem(containsString("cmd1 help")));
 }