import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Map;
/**
* @since 2.7
private String executable;
private List<String> arguments = Lists.newArrayList();
private File directory;
+ private Map<String, String> env = Maps.newHashMap();
private Command(String executable) {
this.executable = executable;
return directory;
}
+ /**
+ * Sets working directory.
+ */
public Command setDirectory(File d) {
this.directory = d;
return this;
}
+ /**
+ * @since 3.2
+ */
+ public Command setEnvironmentVariable(String name, String value) {
+ this.env.put(name, value);
+ return this;
+ }
+
+ /**
+ * @since 3.2
+ */
+ public Map<String, String> getEnvironmentVariables() {
+ return Collections.unmodifiableMap(env);
+ }
+
String[] toStrings() {
List<String> command = Lists.newArrayList();
command.add(executable);
import java.io.File;
import java.io.IOException;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.number.OrderingComparisons.greaterThanOrEqualTo;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
-import static org.junit.matchers.JUnitMatchers.containsString;
public class CommandExecutorTest {
}
@Test
- public void shouldConsumeStdOutAndStdErr() throws Exception {
+ public void should_consume_StdOut_and_StdErr() throws Exception {
final StringBuilder stdOutBuilder = new StringBuilder();
StreamConsumer stdOutConsumer = new StreamConsumer() {
public void consumeLine(String line) {
};
Command command = Command.create(getScript("output")).setDirectory(workDir);
int exitCode = CommandExecutor.create().execute(command, stdOutConsumer, stdErrConsumer, 1000L);
- assertThat(exitCode, is(0));
+ assertThat(exitCode).isEqualTo(0);
String stdOut = stdOutBuilder.toString();
String stdErr = stdErrBuilder.toString();
- assertThat(stdOut, containsString("stdOut: first line"));
- assertThat(stdOut, containsString("stdOut: second line"));
- assertThat(stdErr, containsString("stdErr: first line"));
- assertThat(stdErr, containsString("stdErr: second line"));
+ 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
- public void stdOutConsumerCanThrowException() throws Exception {
+ 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 parser");
}
@Test
- public void stdErrConsumerCanThrowException() throws Exception {
+ 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 parser");
};
@Test
- public void shouldEchoArguments() throws IOException {
- String executable = getScript("echo");
- int exitCode = CommandExecutor.create().execute(Command.create(executable), 1000L);
- assertThat(exitCode, is(0));
-
- // the script generates a log in current directory
- FileUtils.deleteQuietly(new File("echo.log"));
- }
-
- @Test
- public void shouldConfigureWorkingDirectory() throws IOException {
- String executable = getScript("echo");
-
- int exitCode = CommandExecutor.create().execute(Command.create(executable).setDirectory(workDir), 1000L);
- assertThat(exitCode, is(0));
-
- File log = new File(workDir, "echo.log");
- assertThat(FileUtils.readFileToString(log), containsString(workDir.getCanonicalPath()));
+ 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.getCanonicalPath());
+ assertThat(log).contains("Parameter: 1");
+ assertThat(log).contains("Environment variable: 2");
}
@Test
- public void shouldStopWithTimeout() throws IOException {
+ public void should_stop_after_timeout() throws IOException {
String executable = getScript("forever");
long start = System.currentTimeMillis();
try {
long duration = System.currentTimeMillis() - start;
// should test >= 300 but it strangly fails during build on windows.
// The timeout is raised after 297ms (??)
- assertThat(e.getMessage(), duration, greaterThanOrEqualTo(290L));
+ assertThat(duration).as(e.getMessage()).isGreaterThan(290L);
}
}
@Test
- public void shouldFailIfScriptNotFound() {
+ public void should_fail_if_script_not_found() {
thrown.expect(CommandException.class);
CommandExecutor.create().execute(Command.create("notfound").setDirectory(workDir), 1000L);
}