@Test public void should_use_working_directory_to_store_argument_and_environment_variable() throws Exception { Command command = Command.create(getScript("echo")) .setDirectory(workDir) .addArgument("1") .setEnvironmentVariable("ENVVAR", "2"); int exitCode = CommandExecutor.create().execute(command, 1000L); assertThat(exitCode).isEqualTo(0); File logFile = new File(workDir, "echo.log"); assertThat(logFile).exists(); String log = FileUtils.readFileToString(logFile); assertThat(log).contains(workDir.getAbsolutePath()); assertThat(log).contains("Parameter: 1"); assertThat(log).contains("Environment variable: 2"); }
@Test(timeout = 3000L) public void should_consume_StdOut_and_StdErr() throws Exception { // too many false-positives on MS windows if (!SystemUtils.IS_OS_WINDOWS) { final StringBuilder stdOutBuilder = new StringBuilder(); StreamConsumer stdOutConsumer = new StreamConsumer() { public void consumeLine(String line) { stdOutBuilder.append(line).append(SystemUtils.LINE_SEPARATOR); } }; final StringBuilder stdErrBuilder = new StringBuilder(); StreamConsumer stdErrConsumer = new StreamConsumer() { public void consumeLine(String line) { stdErrBuilder.append(line).append(SystemUtils.LINE_SEPARATOR); } }; Command command = Command.create(getScript("output")).setDirectory(workDir); int exitCode = CommandExecutor.create().execute(command, stdOutConsumer, stdErrConsumer, 1000L); assertThat(exitCode).isEqualTo(0); String stdOut = stdOutBuilder.toString(); String stdErr = stdErrBuilder.toString(); assertThat(stdOut).contains("stdOut: first line"); assertThat(stdOut).contains("stdOut: second line"); assertThat(stdErr).contains("stdErr: first line"); assertThat(stdErr).contains("stdErr: second line"); } }
@Test(timeout = 6000L) public void should_stop_after_timeout() throws IOException { try { String executable = getScript("forever"); CommandExecutor.create().execute(Command.create(executable).setDirectory(workDir), 1000L); fail(); } catch (TimeoutException e) { // ok } }
@Test(timeout = 3000L) public void stdErr_consumer_can_throw_exception() throws Exception { Command command = Command.create(getScript("output")).setDirectory(workDir); thrown.expect(CommandException.class); thrown.expectMessage("Error inside stdErr stream"); CommandExecutor.create().execute(command, NOP_CONSUMER, BAD_CONSUMER, 1500L); }
@Test(timeout = 3000L) public void stdOut_consumer_can_throw_exception() throws Exception { Command command = Command.create(getScript("output")).setDirectory(workDir); thrown.expect(CommandException.class); thrown.expectMessage("Error inside stdOut stream"); CommandExecutor.create().execute(command, BAD_CONSUMER, NOP_CONSUMER, 1000L); }
@Test(timeout = 6000L) public void should_stop_after_timeout_and_new_shell() throws IOException { try { String executable = getScript("forever"); CommandExecutor.create().execute(Command.create(executable).setNewShell(true).setDirectory(workDir), 1000L); fail(); } catch (TimeoutException e) { // ok } }