]> source.dussan.org Git - sonarqube.git/commitdiff
Command env variables are initialized with System.getenv()
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 10 Jul 2012 16:50:56 +0000 (18:50 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 11 Jul 2012 15:03:16 +0000 (17:03 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java

index fd44f5b70e5b40fa1e175c50a115882aac4cf639..257a8209387a9d1668135a45cb9dd46eb900a667 100644 (file)
@@ -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;
index 887a62495a4cb9566dfe2cfb9a9c362d0a8e3bf2..efb8c5bb78906f7308f4a6dbd4a7f8e80e419c47 100644 (file)
@@ -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);
index b069ac4f9312dde7e6631f930346cd1182fac28c..cd47f03703aca6b8be13740699052c0747c35cd6 100644 (file)
  */
 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");
   }
 }