]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-3634 Provide a simulation mode allowing to dump input configuration
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 4 Apr 2014 08:03:54 +0000 (10:03 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 4 Apr 2014 08:03:54 +0000 (10:03 +0200)
sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java
sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java
sonar-runner-impl/src/main/java/org/sonar/runner/impl/InternalProperties.java

index b7583de0d290ac780c84be72fa6e99f6bd822330..c4013f6181df42200777f59690d0ad5cbb28bbd7 100644 (file)
  */
 package org.sonar.runner.api;
 
+import org.apache.commons.io.IOUtils;
 import org.sonar.runner.impl.InternalProperties;
 
 import javax.annotation.Nullable;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
 import java.util.Properties;
 
 /**
@@ -87,7 +91,26 @@ public abstract class Runner<T extends Runner> {
     initDefaultValues();
     new SourceEncoding().init(this);
     new Dirs().init(this);
-    doExecute();
+    String dumpToFile = properties.getProperty(InternalProperties.RUNNER_DUMP_TO_FILE);
+    if (dumpToFile != null) {
+      File dumpFile = new File(dumpToFile);
+      writeProperties(dumpFile);
+      System.out.println("Simulation mode. Configuration written to " + dumpFile.getAbsolutePath());
+    } else {
+      doExecute();
+    }
+  }
+
+  private void writeProperties(File outputFile) {
+    OutputStream output = null;
+    try {
+      output = new FileOutputStream(outputFile);
+      properties().store(output, "Generated by sonar-runner");
+    } catch (Exception e) {
+      throw new IllegalStateException("Fail to export sonar-runner properties", e);
+    } finally {
+      IOUtils.closeQuietly(output);
+    }
   }
 
   protected abstract void doExecute();
index d5734174d9fa79ff005d5a30f9687a5461c7972d..2a4302b6d4171e956e42fb5a027894b3e136b8ed 100644 (file)
  */
 package org.sonar.runner.api;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
 import org.mockito.ArgumentMatcher;
 import org.sonar.runner.impl.BatchLauncher;
 import org.sonar.runner.impl.InternalProperties;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.util.List;
 import java.util.Properties;
 
@@ -33,6 +38,10 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 public class EmbeddedRunnerTest {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
   @Test
   public void should_create() {
     assertThat(EmbeddedRunner.create()).isNotNull().isInstanceOf(EmbeddedRunner.class);
@@ -60,10 +69,10 @@ public class EmbeddedRunnerTest {
     assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isNull();
 
     runner = EmbeddedRunner.create()
-        .unmask("org.slf4j.Logger")
-        .mask("org.slf4j.")
-        .mask("ch.qos.logback.")
-        .unmask("");
+      .unmask("org.slf4j.Logger")
+      .mask("org.slf4j.")
+      .mask("ch.qos.logback.")
+      .unmask("");
     assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isEqualTo("UNMASK|org.slf4j.Logger,MASK|org.slf4j.,MASK|ch.qos.logback.,UNMASK|");
   }
 
@@ -116,6 +125,22 @@ public class EmbeddedRunnerTest {
     }));
   }
 
+  @Test
+  public void should_launch_in_simulation_mode() throws IOException {
+    File dump = temp.newFile();
+
+    BatchLauncher batchLauncher = mock(BatchLauncher.class);
+    EmbeddedRunner runner = new EmbeddedRunner(batchLauncher);
+    runner.setProperty("sonar.projectKey", "foo");
+    runner.setProperty("sonarRunner.dumpToFile", dump.getAbsolutePath());
+    runner.execute();
+
+    Properties props = new Properties();
+    props.load(new FileInputStream(dump));
+
+    assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
+  }
+
   static class FakeExtension {
   }
 }
index d4fb4f5df208baad054978da320244cbfe1d8284..1a856e92e5f3723e6d33522f7a0c12f06e20701d 100644 (file)
@@ -23,4 +23,5 @@ public interface InternalProperties {
   String RUNNER_APP = "sonarRunner.app";
   String RUNNER_APP_VERSION = "sonarRunner.appVersion";
   String RUNNER_MASK_RULES = "sonarRunner.maskRules";
+  String RUNNER_DUMP_TO_FILE = "sonarRunner.dumpToFile";
 }