*/
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;
/**
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();
*/
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;
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);
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|");
}
}));
}
+ @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 {
}
}