import org.sonar.runner.impl.BatchLauncherMain;
import org.sonar.runner.impl.JarExtractor;
+import javax.annotation.Nullable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
public class ForkedRunner extends Runner<ForkedRunner> {
private static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000;
- private final Command.Builder commandBuilder;
+ private final Map<String, String> jvmEnvVariables = new HashMap<String, String>();
+ private final List<String> jvmArguments = new ArrayList<String>();
+ private String javaCommand;
+
private final JarExtractor jarExtractor;
- ForkedRunner(Command.Builder commandBuilder, JarExtractor jarExtractor) {
- this.commandBuilder = commandBuilder;
+ ForkedRunner(JarExtractor jarExtractor) {
this.jarExtractor = jarExtractor;
}
public static ForkedRunner create() {
- Os os = new Os();
- Command.Builder builder = Command.builder().setExecutable(os.usedJavaExe().getAbsolutePath());
- return new ForkedRunner(builder, new JarExtractor());
+ return new ForkedRunner(new JarExtractor());
}
- @Override
- public void doExecute() {
- File propertiesFile = writeProperties();
- File jarFile = extractJar();
- fork(jarFile, propertiesFile);
+ public ForkedRunner setJavaCommand(@Nullable String s) {
+ this.javaCommand = s;
+ return this;
}
- private File extractJar() {
- return jarExtractor.extract("sonar-runner-impl");
+ public ForkedRunner addJvmArguments(String... s) {
+ return addJvmArguments(Arrays.asList(s));
}
- private void fork(File jarFile, File propertiesFile) {
- // java -jar sonar-runner-impl.jar path/to/propertiesFile
- Command command = commandBuilder
- .addArguments("-cp", jarFile.getAbsolutePath())
- .addArguments(BatchLauncherMain.class.getName())
- .addArguments(propertiesFile.getAbsolutePath())
- .build();
- System.out.println("---------- execute: " + command);
- int status = CommandExecutor.create().execute(command, ONE_DAY_IN_MILLISECONDS);
- if (status != 0) {
- throw new IllegalStateException("TODO");
+ public ForkedRunner addJvmArguments(List<String> args) {
+ jvmArguments.addAll(args);
+ return this;
+ }
+
+ public ForkedRunner setJvmEnvVariable(String key, String value) {
+ jvmEnvVariables.put(key, value);
+ return this;
+ }
+
+ public ForkedRunner addJvmEnvVariables(Map<String, String> map) {
+ jvmEnvVariables.putAll(map);
+ return this;
+ }
+
+ @Override
+ protected void doExecute() {
+ fork(createCommand());
+ }
+
+ Command createCommand() {
+ File propertiesFile = writeProperties();
+ File jarFile = jarExtractor.extract("sonar-runner-impl");
+
+ Os os = new Os();
+ if (javaCommand == null) {
+ javaCommand = os.usedJavaExe().getAbsolutePath();
}
+ return Command.builder()
+ .setExecutable(javaCommand)
+ .addArguments(jvmArguments)
+ .addArguments("-cp", jarFile.getAbsolutePath(), BatchLauncherMain.class.getName(), propertiesFile.getAbsolutePath())
+ .build();
}
private File writeProperties() {
}
}
- public ForkedRunner setJavaCommand(String s) {
- commandBuilder.setExecutable(s);
- return this;
- }
-
- public ForkedRunner addJvmArgument(String... s) {
- commandBuilder.addArguments(Arrays.asList(s));
- return this;
- }
-
- public ForkedRunner addJvmArguments(List<String> args) {
- commandBuilder.addArguments(args);
- return this;
- }
-
- public ForkedRunner setJvmEnvVariable(String key, String value) {
- commandBuilder.setEnvVariable(key, value);
- return this;
- }
-
- public ForkedRunner addJvmEnvVariables(Map<String, String> map) {
- commandBuilder.addEnvVariables(map);
- return this;
+ private void fork(Command command) {
+ int status = CommandExecutor.create().execute(command, ONE_DAY_IN_MILLISECONDS);
+ if (status != 0) {
+ throw new IllegalStateException("TODO");
+ }
}
-
-
}
*/
package org.sonar.runner.api;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.runner.impl.JarExtractor;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Properties;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class ForkedRunnerTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
@Test
public void should_create() {
- assertThat(ForkedRunner.create()).isNotNull().isInstanceOf(ForkedRunner.class);
+ ForkedRunner runner = ForkedRunner.create();
+ assertThat(runner).isNotNull().isInstanceOf(ForkedRunner.class);
+ }
+
+ @Test
+ public void test_java_command() throws IOException {
+ JarExtractor jarExtractor = mock(JarExtractor.class);
+ File jar = temp.newFile();
+ when(jarExtractor.extract("sonar-runner-impl")).thenReturn(jar);
+
+ ForkedRunner runner = new ForkedRunner(jarExtractor);
+ runner.setJavaCommand("java");
+ runner.setProperty("sonar.dynamicAnalysis", "false");
+ runner.setProperty("sonar.login", "admin");
+ runner.addJvmArguments("-Xmx512m");
+ runner.setJvmEnvVariable("SONAR_HOME", "/path/to/sonar");
+
+ Command command = runner.createCommand();
+ assertThat(command).isNotNull();
+ assertThat(command.toStrings()).hasSize(6);
+ assertThat(command.toStrings()[0]).isEqualTo("java");
+ assertThat(command.toStrings()[1]).isEqualTo("-Xmx512m");
+ assertThat(command.toStrings()[2]).isEqualTo("-cp");
+ assertThat(command.toStrings()[3]).isEqualTo(jar.getAbsolutePath());
+ assertThat(command.toStrings()[4]).isEqualTo("org.sonar.runner.impl.BatchLauncherMain");
+
+ // the properties
+ String propsPath = command.toStrings()[5];
+ assertThat(propsPath).endsWith(".properties");
+ Properties properties = new Properties();
+ properties.load(new FileInputStream(propsPath));
+ assertThat(properties.size()).isGreaterThan(2);
+ assertThat(properties.getProperty("sonar.dynamicAnalysis")).isEqualTo("false");
+ assertThat(properties.getProperty("sonar.login")).isEqualTo("admin");
+ assertThat(properties.getProperty("-Xmx512m")).isNull();
+ assertThat(properties.getProperty("SONAR_HOME")).isNull();
+ // default values
+ assertThat(properties.getProperty("sonar.task")).isEqualTo("scan");
+ assertThat(properties.getProperty("sonar.host.url")).isEqualTo("http://localhost:9000");
}
}