aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-07-10 18:50:56 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-07-11 17:03:16 +0200
commit4a6b9541a01065121b6472360c178708c5697f38 (patch)
treefcd4c6342e1f5af44c4024da399b6eea8c53396d /sonar-plugin-api
parentdc893d90da17245af18884502531554a727ea034 (diff)
downloadsonarqube-4a6b9541a01065121b6472360c178708c5697f38.tar.gz
sonarqube-4a6b9541a01065121b6472360c178708c5697f38.zip
Command env variables are initialized with System.getenv()
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java4
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java5
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java50
3 files changed, 42 insertions, 17 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
index fd44f5b70e5..257a8209387 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
@@ -33,12 +33,12 @@ import java.util.Map;
/**
* @since 2.7
*/
-public final class Command {
+public class Command {
private String executable;
private List<String> arguments = Lists.newArrayList();
private File directory;
- private Map<String, String> env = Maps.newHashMap();
+ private Map<String, String> env = Maps.newHashMap(System.getenv());
private Command(String executable) {
this.executable = executable;
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
index 887a62495a4..efb8c5bb789 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
@@ -64,10 +64,7 @@ public class CommandExecutor {
if (command.getDirectory() != null) {
builder.directory(command.getDirectory());
}
- Map<String, String> envVars = command.getEnvironmentVariables();
- if (!envVars.isEmpty()) {
- builder.environment().putAll(envVars);
- }
+ builder.environment().putAll(command.getEnvironmentVariables());
process = builder.start();
outputGobbler = new StreamGobbler(process.getInputStream(), stdOut);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
index b069ac4f931..cd47f03703a 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
@@ -19,23 +19,30 @@
*/
package org.sonar.api.utils.command;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import java.io.File;
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.nullValue;
-import static org.junit.Assert.assertThat;
public class CommandTest {
- @Test(expected = IllegalArgumentException.class)
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
public void shouldFailWhenBlankExecutable() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
Command.create(" ");
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void shouldFailWhenNullExecutable() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
Command.create(null);
}
@@ -43,19 +50,40 @@ public class CommandTest {
public void shouldCreateCommand() throws Exception {
Command command = Command.create("java");
command.addArgument("-Xmx512m");
- command.addArgument("-Dfoo=bar");
- assertThat(command.getExecutable(), is("java"));
- assertThat(command.getArguments().size(), is(2));
- assertThat(command.toCommandLine(), is("java -Xmx512m -Dfoo=bar"));
+ command.addArguments(Arrays.asList("-a", "-b"));
+ command.addArguments(new String[]{"-x", "-y"});
+ assertThat(command.getExecutable()).isEqualTo("java");
+ assertThat(command.getArguments()).hasSize(5);
+ assertThat(command.toCommandLine()).isEqualTo("java -Xmx512m -a -b -x -y");
+ }
+
+ @Test
+ public void toString_is_the_command_line() {
+ Command command = Command.create("java");
+ command.addArgument("-Xmx512m");
+ assertThat(command.toString()).isEqualTo(command.toCommandLine());
}
@Test
public void shouldSetWorkingDirectory() throws Exception {
Command command = Command.create("java");
- assertThat(command.getDirectory(), nullValue());
+ assertThat(command.getDirectory()).isNull();
File working = new File("working");
command = Command.create("java").setDirectory(working);
- assertThat(command.getDirectory(), is(working));
+ assertThat(command.getDirectory()).isEqualTo(working);
+ }
+
+ @Test
+ public void initialize_with_current_env() throws Exception {
+ Command command = Command.create("java");
+ assertThat(command.getEnvironmentVariables()).isNotEmpty();
+ }
+
+ @Test
+ public void override_env_variables() throws Exception {
+ Command command = Command.create("java");
+ command.setEnvironmentVariable("JAVA_HOME", "/path/to/java");
+ assertThat(command.getEnvironmentVariables().get("JAVA_HOME")).isEqualTo("/path/to/java");
}
}