]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-2574 refactoring of ForkedRunner
authorSimonBrandhof <simon.brandhof@gmail.com>
Thu, 4 Apr 2013 14:12:41 +0000 (16:12 +0200)
committerSimonBrandhof <simon.brandhof@gmail.com>
Thu, 4 Apr 2013 14:12:41 +0000 (16:12 +0200)
sonar-runner-api/src/main/java/org/sonar/runner/api/EmbeddedRunner.java
sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java
sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java

index 3357e9a9cfa07cb93d13bb9e3cd5b51115c3e608..0218a32b838dda888a4317eca5631352203086ef 100644 (file)
@@ -50,7 +50,7 @@ public class EmbeddedRunner extends Runner<EmbeddedRunner> {
   }
 
   @Override
-  public void doExecute() {
+  protected void doExecute() {
     new BatchLauncher().execute(properties(), extensions);
   }
 }
index 7a6c3cc3a713e6569f8daff8caecd6f8688842a6..3e772670628826ccb1100a3e88ebe7ee988bd20f 100644 (file)
@@ -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<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() {
@@ -89,30 +107,10 @@ public class ForkedRunner extends Runner<ForkedRunner> {
     }
   }
 
-  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");
+    }
   }
-
-
 }
index 5c8767b6efb636297d02e9d45f201161accbfa07..7f4d32592a6d44b334e3a306a0d794fb08a71f01 100644 (file)
  */
 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");
   }
 }