return this;
}
+ public List<String> jvmArguments() {
+ return new ArrayList<String>(jvmArguments);
+ }
+
/**
* See {@link #addJvmArguments(java.util.List)}
*/
runner.setStdOut(mock(StreamConsumer.class));
runner.setStdErr(mock(StreamConsumer.class));
+ assertThat(runner.jvmArguments()).contains("-Xmx512m");
runner.execute();
verify(commandExecutor).execute(argThat(new ArgumentMatcher<Command>() {
<configuration>
<rules>
<requireFilesSize>
- <minsize>200000</minsize>
- <maxsize>220000</maxsize>
+ <minsize>220000</minsize>
+ <maxsize>240000</maxsize>
<files>
<file>${pom.build.directory}/sonar-runner-${pom.version}.zip</file>
</files>
this.cli = cli;
}
- Properties load() throws IOException {
+ Properties properties() throws IOException {
Properties result = new Properties();
result.putAll(loadGlobalProperties());
result.putAll(loadProjectProperties());
result.putAll(cli.properties());
if (result.containsKey(PROJECT_HOME)) {
- // the real property of the Sonar Runner is "sonar.projectDir"
+ // the real property of the Sonar Runner is "sonar.projectBaseDir"
String baseDir = result.getProperty(PROJECT_HOME);
result.remove(PROJECT_HOME);
result.put("sonar.projectBaseDir", baseDir);
}
-
return result;
}
--- /dev/null
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.runner;
+
+class Exit {
+ static final int SUCCESS = 0;
+ static final int ERROR = 1;
+
+ void exit(int status) {
+ System.exit(status);
+ }
+}
*/
package org.sonar.runner;
-import org.sonar.runner.api.EmbeddedRunner;
import org.sonar.runner.impl.Logs;
-import java.util.Properties;
-
/**
* Arguments :
* <ul>
public static void main(String[] args) {
Cli cli = new Cli().parse(args);
- new Main(cli).execute();
+ Main main = new Main(new Exit(), cli, new Conf(cli), new RunnerFactory());
+ main.execute();
}
+ private final Exit exit;
private final Cli cli;
+ private final Conf conf;
+ private final RunnerFactory runnerFactory;
- Main(Cli cli) {
+ Main(Exit exit, Cli cli, Conf conf, RunnerFactory runnerFactory) {
+ this.exit = exit;
this.cli = cli;
+ this.conf = conf;
+ this.runnerFactory = runnerFactory;
}
void execute() {
SystemInfo.print();
if (!cli.isDisplayVersionOnly()) {
- int status = doExecute(new Conf(cli));
- System.exit(status);
+ int status = executeTask();
+ exit.exit(status);
}
}
- private int doExecute(Conf conf) {
- if (cli.isDisplayStackTrace()) {
- Logs.info("Error stacktraces are turned on.");
- }
+ private int executeTask() {
Stats stats = new Stats().start();
try {
- Properties properties = conf.load();
- EmbeddedRunner.create().addProperties(properties).execute();
- // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
+ if (cli.isDisplayStackTrace()) {
+ Logs.info("Error stacktraces are turned on.");
+ }
+ runnerFactory.create(conf.properties()).execute();
+ // Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
} catch (Exception e) {
displayExecutionResult(stats, "FAILURE");
showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace());
- return 1;
+ return Exit.ERROR;
}
displayExecutionResult(stats, "SUCCESS");
- return 0;
+ return Exit.SUCCESS;
}
private void displayExecutionResult(Stats stats, String resultMsg) {
--- /dev/null
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.runner;
+
+import org.sonar.runner.api.EmbeddedRunner;
+import org.sonar.runner.api.ForkedRunner;
+import org.sonar.runner.api.Runner;
+
+import java.util.Properties;
+
+class RunnerFactory {
+
+ Runner create(Properties props) {
+ Runner runner;
+ if ("fork".equals(props.getProperty("sonarRunner.mode"))) {
+ String jvmArgs = props.getProperty("sonarRunner.fork.jvmArgs", "");
+ runner = ForkedRunner.create().addJvmArguments(jvmArgs.split(" "));
+
+ } else {
+ runner = EmbeddedRunner.create();
+ }
+ runner.addProperties(props);
+ return runner;
+ }
+}
public class ConfTest {
Properties args = new Properties();
- Conf conf;
+ Cli cli = mock(Cli.class);
+ Conf conf = new Conf(cli);
@Before
public void initConf() {
- Cli cli = mock(Cli.class);
when(cli.properties()).thenReturn(args);
- conf = new Conf(cli);
}
@Test
public void should_load_global_settings_by_home() throws Exception {
- File home = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/").toURI());
+ File home = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/").toURI());
args.setProperty("runner.home", home.getCanonicalPath());
- assertThat(conf.load().get("sonar.host.url")).isEqualTo("http://moon/sonar");
+ assertThat(conf.properties().get("sonar.host.url")).isEqualTo("http://moon/sonar");
}
@Test
public void should_not_fail_if_no_home() throws Exception {
- assertThat(conf.load()).isNotEmpty();
+ assertThat(conf.properties()).isNotEmpty();
}
@Test
public void should_load_conf_by_direct_path() throws Exception {
- File settings = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
+ File settings = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
args.setProperty("runner.settings", settings.getCanonicalPath());
- assertThat(conf.load().get("sonar.host.url")).isEqualTo("http://other/sonar");
+ assertThat(conf.properties().get("sonar.host.url")).isEqualTo("http://other/sonar");
}
// @Test
--- /dev/null
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.runner;
+
+import org.junit.Test;
+import org.sonar.runner.api.Runner;
+
+import java.util.Properties;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.RETURNS_MOCKS;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+public class MainTest {
+
+ Exit exit = mock(Exit.class);
+ Cli cli = mock(Cli.class);
+ Conf conf = mock(Conf.class);
+ RunnerFactory runnerFactory = mock(RunnerFactory.class, RETURNS_MOCKS);
+
+ @Test
+ public void should_execute_runner() {
+ Main main = new Main(exit, cli, conf, runnerFactory);
+ main.execute();
+
+ verify(exit).exit(0);
+ }
+
+ @Test
+ public void should_fail_on_error() {
+ Runner runner = mock(Runner.class);
+ doThrow(new IllegalStateException("Error")).when(runner).execute();
+ when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
+
+ Main main = new Main(exit, cli, conf, runnerFactory);
+ main.execute();
+
+ verify(exit).exit(1);
+ }
+
+ @Test
+ public void should_only_display_version() {
+ when(cli.isDisplayVersionOnly()).thenReturn(true);
+ Main main = new Main(exit, cli, conf, runnerFactory);
+ main.execute();
+ verifyZeroInteractions(runnerFactory);
+ }
+}
--- /dev/null
+/*
+ * Sonar Runner - Distribution
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.runner;
+
+import org.junit.Test;
+import org.sonar.runner.api.EmbeddedRunner;
+import org.sonar.runner.api.ForkedRunner;
+import org.sonar.runner.api.Runner;
+
+import java.util.Properties;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RunnerFactoryTest {
+
+ Properties props = new Properties();
+
+ @Test
+ public void should_create_embedded_runner_by_default() {
+ props.setProperty("foo", "bar");
+ Runner runner = new RunnerFactory().create(props);
+
+ assertThat(runner).isInstanceOf(EmbeddedRunner.class);
+ assertThat(runner.properties().get("foo")).isEqualTo("bar");
+ }
+
+ @Test
+ public void should_create_forked_runner() {
+ props.setProperty("foo", "bar");
+ props.setProperty("sonarRunner.mode", "fork");
+ props.setProperty("sonarRunner.fork.jvmArgs", "-Xms128m -Xmx512m");
+ Runner runner = new RunnerFactory().create(props);
+
+ assertThat(runner).isInstanceOf(ForkedRunner.class);
+ assertThat(runner.properties().get("foo")).isEqualTo("bar");
+ assertThat(((ForkedRunner)runner).jvmArguments()).contains("-Xms128m", "-Xmx512m");
+ }
+}
public void test_os() {
assertThat(SystemInfo.os()).isNotEmpty();
}
+
+ @Test
+ public void should_print() {
+ SystemInfo.print();
+ // should mock output
+ }
}
--- /dev/null
+project.prop=foo
+
+# overridden property
+overridden.prop=project scope
\ No newline at end of file
--- /dev/null
+overridden.prop=runner scope
+global.prop=jdbc:mysql:localhost/sonar
--- /dev/null
+sonar.host.url=http://other/sonar
\ No newline at end of file
--- /dev/null
+sonar.host.url=http://moon/sonar
+sonar.jdbc.url=jdbc:mysql:localhost/sonar
\ No newline at end of file
+++ /dev/null
-project.prop=foo
-
-# overridden property
-overridden.prop=project scope
\ No newline at end of file
+++ /dev/null
-overridden.prop=runner scope
-global.prop=jdbc:mysql:localhost/sonar
+++ /dev/null
-sonar.host.url=http://other/sonar
\ No newline at end of file
+++ /dev/null
-sonar.host.url=http://moon/sonar
-sonar.jdbc.url=jdbc:mysql:localhost/sonar
\ No newline at end of file