From 4a6b9541a01065121b6472360c178708c5697f38 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 10 Jul 2012 18:50:56 +0200 Subject: [PATCH] Command env variables are initialized with System.getenv() --- .../org/sonar/api/utils/command/Command.java | 4 +- .../api/utils/command/CommandExecutor.java | 5 +- .../sonar/api/utils/command/CommandTest.java | 50 +++++++++++++++---- 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 arguments = Lists.newArrayList(); private File directory; - private Map env = Maps.newHashMap(); + private Map 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 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"); } } -- 2.39.5