aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-dist
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2013-04-06 00:59:45 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2013-04-06 00:59:45 +0200
commitdf9143f33c3acd3a50dd385affbbb656a96ee32f (patch)
tree02bb39b13635740450a519ecf6acc50064a7dbb1 /sonar-runner-dist
parent0d9baa7b02f94e1706c6922cc56b1fae2a2bf0f5 (diff)
downloadsonar-scanner-cli-df9143f33c3acd3a50dd385affbbb656a96ee32f.tar.gz
sonar-scanner-cli-df9143f33c3acd3a50dd385affbbb656a96ee32f.zip
Add unit tests
Diffstat (limited to 'sonar-runner-dist')
-rw-r--r--sonar-runner-dist/pom.xml4
-rw-r--r--sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java5
-rw-r--r--sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java29
-rw-r--r--sonar-runner-dist/src/main/java/org/sonar/runner/Main.java35
-rw-r--r--sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java42
-rw-r--r--sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java15
-rw-r--r--sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java69
-rw-r--r--sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java55
-rw-r--r--sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java6
-rw-r--r--sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties (renamed from sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties)0
-rw-r--r--sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties (renamed from sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties)0
-rw-r--r--sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties (renamed from sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties)0
-rw-r--r--sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties (renamed from sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties)0
13 files changed, 231 insertions, 29 deletions
diff --git a/sonar-runner-dist/pom.xml b/sonar-runner-dist/pom.xml
index b5e023a..54d2e6d 100644
--- a/sonar-runner-dist/pom.xml
+++ b/sonar-runner-dist/pom.xml
@@ -113,8 +113,8 @@
<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>
diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java
index 504fe3f..13b5239 100644
--- a/sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java
+++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java
@@ -39,7 +39,7 @@ class Conf {
this.cli = cli;
}
- Properties load() throws IOException {
+ Properties properties() throws IOException {
Properties result = new Properties();
result.putAll(loadGlobalProperties());
result.putAll(loadProjectProperties());
@@ -47,12 +47,11 @@ class Conf {
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;
}
diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java
new file mode 100644
index 0000000..c1b8e74
--- /dev/null
+++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Exit.java
@@ -0,0 +1,29 @@
+/*
+ * 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);
+ }
+}
diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java
index 8e3a833..57a1623 100644
--- a/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java
+++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Main.java
@@ -19,11 +19,8 @@
*/
package org.sonar.runner;
-import org.sonar.runner.api.EmbeddedRunner;
import org.sonar.runner.impl.Logs;
-import java.util.Properties;
-
/**
* Arguments :
* <ul>
@@ -40,40 +37,46 @@ public class Main {
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) {
diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java b/sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java
new file mode 100644
index 0000000..736db56
--- /dev/null
+++ b/sonar-runner-dist/src/main/java/org/sonar/runner/RunnerFactory.java
@@ -0,0 +1,42 @@
+/*
+ * 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;
+ }
+}
diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java
index 7cb2c38..f363c56 100644
--- a/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java
+++ b/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java
@@ -32,34 +32,33 @@ import static org.mockito.Mockito.when;
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
diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java
new file mode 100644
index 0000000..bb462ec
--- /dev/null
+++ b/sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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);
+ }
+}
diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java
new file mode 100644
index 0000000..6f21c18
--- /dev/null
+++ b/sonar-runner-dist/src/test/java/org/sonar/runner/RunnerFactoryTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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");
+ }
+}
diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java
index 0e57284..ece65c5 100644
--- a/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java
+++ b/sonar-runner-dist/src/test/java/org/sonar/runner/SystemInfoTest.java
@@ -33,4 +33,10 @@ public class SystemInfoTest {
public void test_os() {
assertThat(SystemInfo.os()).isNotEmpty();
}
+
+ @Test
+ public void should_print() {
+ SystemInfo.print();
+ // should mock output
+ }
}
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties
index 0d1e025..0d1e025 100644
--- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties
+++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties
index 7edfb99..7edfb99 100644
--- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties
+++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties
index 740a616..740a616 100644
--- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties
+++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties
diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties
index e7d5c09..e7d5c09 100644
--- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties
+++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties