From 0aba058c75832c2db746f09604a4ea467074a088 Mon Sep 17 00:00:00 2001 From: SimonBrandhof Date: Thu, 4 Apr 2013 16:12:41 +0200 Subject: [PATCH] SONARPLUGINS-2574 refactoring of ForkedRunner --- .../org/sonar/runner/api/EmbeddedRunner.java | 2 +- .../org/sonar/runner/api/ForkedRunner.java | 102 +++++++++--------- .../sonar/runner/api/ForkedRunnerTest.java | 55 +++++++++- 3 files changed, 105 insertions(+), 54 deletions(-) diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java index 3357e9a..0218a32 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java @@ -50,7 +50,7 @@ public class EmbeddedRunner extends Runner { } @Override - public void doExecute() { + protected void doExecute() { new BatchLauncher().execute(properties(), extensions); } } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index 7a6c3cc..3e77267 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -23,54 +23,72 @@ import org.apache.commons.io.IOUtils; 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 { private static final int ONE_DAY_IN_MILLISECONDS = 24 * 60 * 60 * 1000; - private final Command.Builder commandBuilder; + private final Map jvmEnvVariables = new HashMap(); + private final List jvmArguments = new ArrayList(); + 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 args) { + jvmArguments.addAll(args); + return this; + } + + public ForkedRunner setJvmEnvVariable(String key, String value) { + jvmEnvVariables.put(key, value); + return this; + } + + public ForkedRunner addJvmEnvVariables(Map 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() { @@ -89,30 +107,10 @@ public class ForkedRunner extends Runner { } } - 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 args) { - commandBuilder.addArguments(args); - return this; - } - - public ForkedRunner setJvmEnvVariable(String key, String value) { - commandBuilder.setEnvVariable(key, value); - return this; - } - - public ForkedRunner addJvmEnvVariables(Map 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"); + } } - - } diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java index 5c8767b..7f4d325 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java @@ -19,13 +19,66 @@ */ 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"); } } -- 2.39.5