From: Simon Brandhof Date: Fri, 5 Apr 2013 22:59:45 +0000 (+0200) Subject: Add unit tests X-Git-Tag: 2.5-rc1~172 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=df9143f33c3acd3a50dd385affbbb656a96ee32f;p=sonar-scanner-cli.git Add unit tests --- diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java index ec148d6..c7769b7 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ForkedRunner.java @@ -71,6 +71,10 @@ public class ForkedRunner extends Runner { return this; } + public List jvmArguments() { + return new ArrayList(jvmArguments); + } + /** * See {@link #addJvmArguments(java.util.List)} */ diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java index ee87a88..7cfa627 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/ForkedRunnerTest.java @@ -93,6 +93,7 @@ public class ForkedRunnerTest { runner.setStdOut(mock(StreamConsumer.class)); runner.setStdErr(mock(StreamConsumer.class)); + assertThat(runner.jvmArguments()).contains("-Xmx512m"); runner.execute(); verify(commandExecutor).execute(argThat(new ArgumentMatcher() { 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 @@ - 200000 - 220000 + 220000 + 240000 ${pom.build.directory}/sonar-runner-${pom.version}.zip 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 : *
    @@ -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/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties new file mode 100644 index 0000000..0d1e025 --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project/sonar-project.properties @@ -0,0 +1,4 @@ +project.prop=foo + +# overridden property +overridden.prop=project scope \ No newline at end of file diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties new file mode 100644 index 0000000..7edfb99 --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties @@ -0,0 +1,2 @@ +overridden.prop=runner scope +global.prop=jdbc:mysql:localhost/sonar diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties new file mode 100644 index 0000000..740a616 --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties @@ -0,0 +1 @@ +sonar.host.url=http://other/sonar \ No newline at end of file diff --git a/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties new file mode 100644 index 0000000..e7d5c09 --- /dev/null +++ b/sonar-runner-dist/src/test/resources/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties @@ -0,0 +1,2 @@ +sonar.host.url=http://moon/sonar +sonar.jdbc.url=jdbc:mysql:localhost/sonar \ No newline at end of file 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/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties deleted file mode 100644 index 0d1e025..0000000 --- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties +++ /dev/null @@ -1,4 +0,0 @@ -project.prop=foo - -# overridden property -overridden.prop=project scope \ No newline at end of file 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/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties deleted file mode 100644 index 7edfb99..0000000 --- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties +++ /dev/null @@ -1,2 +0,0 @@ -overridden.prop=runner scope -global.prop=jdbc:mysql:localhost/sonar 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/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties deleted file mode 100644 index 740a616..0000000 --- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties +++ /dev/null @@ -1 +0,0 @@ -sonar.host.url=http://other/sonar \ No newline at end of file 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/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties deleted file mode 100644 index e7d5c09..0000000 --- a/sonar-runner-dist/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties +++ /dev/null @@ -1,2 +0,0 @@ -sonar.host.url=http://moon/sonar -sonar.jdbc.url=jdbc:mysql:localhost/sonar \ No newline at end of file