diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-24 16:56:40 +0000 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-24 16:56:40 +0000 |
commit | 9132e3292b142d7dbb5d89cb1973d9e09628bffe (patch) | |
tree | 9c7f82894cfbcae576706aa93ac4d51640982616 /src/test | |
parent | 1cff17644140587b582d079b1edfeeba59f2daef (diff) | |
download | sonar-scanner-cli-9132e3292b142d7dbb5d89cb1973d9e09628bffe.tar.gz sonar-scanner-cli-9132e3292b142d7dbb5d89cb1973d9e09628bffe.zip |
SONARPLUGINS-1220 Runner home must be optional
Diffstat (limited to 'src/test')
7 files changed, 153 insertions, 16 deletions
diff --git a/src/test/java/org/sonar/runner/MainTest.java b/src/test/java/org/sonar/runner/MainTest.java index f34b604..2e839f9 100644 --- a/src/test/java/org/sonar/runner/MainTest.java +++ b/src/test/java/org/sonar/runner/MainTest.java @@ -22,31 +22,81 @@ package org.sonar.runner; import org.junit.Test; -import static org.hamcrest.Matchers.containsString; +import java.io.File; +import java.util.Properties; + +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; public class MainTest { @Test - public void shouldCheckVersion() { - assertThat(Main.isVersionPriorTo2Dot6("1.0"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.0"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.1"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.2"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.3"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.4"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.4.1"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.5"), is(true)); - assertThat(Main.isVersionPriorTo2Dot6("2.6"), is(false)); + public void shouldParseEmptyArguments() { + Properties props = Main.parseArguments(new String[]{}); + assertThat(props.isEmpty(), is(true)); + } + + @Test + public void shouldParseArguments() { + Properties props = Main.parseArguments(new String[]{"-D", "foo=bar", "--define", "hello=world"}); + assertThat(props.size(), is(2)); + assertThat(props.getProperty("foo"), is("bar")); + assertThat(props.getProperty("hello"), is("world")); + } + + @Test + public void shouldEnableDebugMode() { + Properties props = Main.parseArguments(new String[]{"-X"}); + assertThat(props.getProperty(Runner.DEBUG_MODE), is("true")); + } + + @Test + public void shouldDisableDebugModeByDefault() { + Properties props = Main.parseArguments(new String[]{}); + assertThat(props.getProperty(Runner.DEBUG_MODE), nullValue()); } @Test - public void shouldGetVersion() { - String version = Main.getRunnerVersion(); - assertThat(version, containsString(".")); - assertThat(version, not(containsString("$"))); + public void shouldLoadRunnerSettingsByHome() throws Exception { + File home = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/").toURI()); + Properties args = new Properties(); + args.setProperty("runner.home", home.getCanonicalPath()); + + Properties props = Main.loadRunnerProperties(args); + + assertThat(props.getProperty("sonar.host.url"), is("http://moon/sonar")); } + @Test + public void shouldNotFailIfNoHome() throws Exception { + Properties args = new Properties(); + Properties props = Main.loadRunnerProperties(args); + + assertThat(props.isEmpty(), is(true)); + } + + @Test + public void shouldLoadRunnerSettingsByDirectPath() throws Exception { + File settings = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI()); + Properties args = new Properties(); + args.setProperty("runner.settings", settings.getCanonicalPath()); + Properties props = Main.loadRunnerProperties(args); + + assertThat(props.getProperty("sonar.host.url"), is("http://other/sonar")); + } + + @Test + public void shouldLoadCompleteConfiguration() throws Exception { + File runnerHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner").toURI()); + File projectHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project").toURI()); + Properties props = Main.loadProperties(new String[]{ + "-D", "runner.home=" + runnerHome.getCanonicalPath(), + "-D", "project.home=" + projectHome.getCanonicalPath() + }); + + assertThat(props.getProperty("project.key"), is("foo")); + assertThat(props.getProperty("sonar.host.url"), is("http://overridden/sonar")); + assertThat(props.getProperty("sonar.jdbc.url"), is("jdbc:mysql:localhost/sonar")); + } } diff --git a/src/test/java/org/sonar/runner/RunnerTest.java b/src/test/java/org/sonar/runner/RunnerTest.java new file mode 100644 index 0000000..9718830 --- /dev/null +++ b/src/test/java/org/sonar/runner/RunnerTest.java @@ -0,0 +1,77 @@ +/* + * Sonar Standalone Runner + * 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 java.io.File; +import java.net.URISyntaxException; +import java.util.Properties; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +public class RunnerTest { + @Test + public void shouldCheckVersion() { + assertThat(Runner.isVersionPriorTo2Dot6("1.0"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.0"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.1"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.2"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.3"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.4"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.4.1"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.5"), is(true)); + assertThat(Runner.isVersionPriorTo2Dot6("2.6"), is(false)); + } + + /** + * This test can only be executed by Maven, not by IDE + */ + @Test + public void shouldGetVersion() { + String version = Runner.create(new Properties()).getRunnerVersion(); + assertThat(version.length(), greaterThanOrEqualTo(3)); + assertThat(version, containsString(".")); + + // test that version is set by Maven build + assertThat(version, not(containsString("$"))); + } + + @Test + public void shouldInitDirs() throws Exception { + Properties props = new Properties(); + File home = new File(getClass().getResource("/org/sonar/runner/RunnerTest/shouldInitDirs/").toURI()); + props.setProperty("project.home", home.getCanonicalPath()); + Runner runner = Runner.create(props); + + assertThat(runner.getProjectDir(), is(home)); + assertThat(runner.getWorkDir(), is(new File(home, ".sonar"))); + } + + @Test + public void shouldInitProjectDirWithCurrentDir() throws Exception { + Runner runner = Runner.create(new Properties()); + + assertThat(runner.getProjectDir().isDirectory(), is(true)); + assertThat(runner.getProjectDir().exists(), is(true)); + } +} diff --git a/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties b/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties new file mode 100644 index 0000000..d9ed633 --- /dev/null +++ b/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project/sonar-project.properties @@ -0,0 +1,4 @@ +project.key=foo + +# overridden property +sonar.host.url=http://overridden/sonar
\ No newline at end of file diff --git a/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties b/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties new file mode 100644 index 0000000..0f6524d --- /dev/null +++ b/src/test/resources/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner/conf/sonar-runner.properties @@ -0,0 +1,2 @@ +sonar.host.url=http://localhost/sonar +sonar.jdbc.url=jdbc:mysql:localhost/sonar diff --git a/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties b/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties new file mode 100644 index 0000000..740a616 --- /dev/null +++ b/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties @@ -0,0 +1 @@ +sonar.host.url=http://other/sonar
\ No newline at end of file diff --git a/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties b/src/test/resources/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/conf/sonar-runner.properties new file mode 100644 index 0000000..e7d5c09 --- /dev/null +++ b/src/test/resources/org/sonar/runner/MainTest/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/src/test/resources/org/sonar/runner/RunnerTest/shouldInitDirs/fake.txt b/src/test/resources/org/sonar/runner/RunnerTest/shouldInitDirs/fake.txt new file mode 100644 index 0000000..f0f877c --- /dev/null +++ b/src/test/resources/org/sonar/runner/RunnerTest/shouldInitDirs/fake.txt @@ -0,0 +1 @@ +fake
\ No newline at end of file |