diff options
Diffstat (limited to 'sonar-runner-cli/src/test/java')
7 files changed, 577 insertions, 0 deletions
diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java new file mode 100644 index 0000000..9eda2b9 --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/CliTest.java @@ -0,0 +1,92 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import static org.mockito.Mockito.mock; + +import static org.mockito.Mockito.verify; + +import org.junit.Test; +import org.sonar.runner.cli.Cli; +import org.sonar.runner.cli.Exit; + +import static org.fest.assertions.Assertions.assertThat; + +public class CliTest { + Exit exit = mock(Exit.class); + Cli cli = new Cli(exit); + + @Test + public void should_parse_empty_arguments() { + cli.parse(new String[0]); + assertThat(cli.properties()).isNotEmpty(); + assertThat(cli.isDebugMode()).isFalse(); + assertThat(cli.isDisplayStackTrace()).isFalse(); + assertThat(cli.isDisplayVersionOnly()).isFalse(); + } + + @Test + public void should_extract_properties() { + cli.parse(new String[]{"-D", "foo=bar", "--define", "hello=world", "-Dboolean"}); + assertThat(cli.properties().get("foo")).isEqualTo("bar"); + assertThat(cli.properties().get("hello")).isEqualTo("world"); + assertThat(cli.properties().get("boolean")).isEqualTo("true"); + } + + @Test + public void dont_allow_interactive_fork() { + cli.parse(new String[]{"-i", "-DsonarRunner.mode=fork"}); + cli.verify(); + verify(exit).exit(Exit.SUCCESS); + } + + @Test + public void should_parse_optional_task() { + cli.parse(new String[]{"-D", "foo=bar"}); + assertThat(cli.properties().get("sonar.task")).isNull(); + + cli.parse(new String[]{"views", "-D", "foo=bar"}); + assertThat(cli.properties().get("sonar.task")).isEqualTo("views"); + } + + @Test + public void should_enable_debug_mode() { + cli.parse(new String[]{"-X"}); + assertThat(cli.isDebugMode()).isTrue(); + assertThat(cli.isDisplayStackTrace()).isTrue(); + assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true"); + } + + @Test + public void should_enable_stacktrace_log() { + cli.parse(new String[]{"-e"}); + assertThat(cli.isDebugMode()).isFalse(); + assertThat(cli.isDisplayStackTrace()).isTrue(); + assertThat(cli.properties().get("sonar.verbose")).isNull(); + } + + @Test + public void should_disable_debug_mode_and_stacktrace_log_by_default() { + cli.parse(new String[0]); + assertThat(cli.isDebugMode()).isFalse(); + assertThat(cli.isDisplayStackTrace()).isFalse(); + assertThat(cli.properties().get("sonar.verbose")).isNull(); + } +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java new file mode 100644 index 0000000..bfeb23d --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ConfTest.java @@ -0,0 +1,136 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.runner.cli.Cli; +import org.sonar.runner.cli.Conf; +import java.io.File; +import java.util.Properties; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ConfTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + Properties args = new Properties(); + Cli cli = mock(Cli.class); + Conf conf = new Conf(cli); + + @Before + public void initConf() { + when(cli.properties()).thenReturn(args); + } + + @Test + public void should_load_global_settings_by_home() throws Exception { + File home = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByHome/").toURI()); + args.setProperty("runner.home", home.getCanonicalPath()); + + assertThat(conf.properties().get("sonar.prop")).isEqualTo("value"); + } + + @Test + public void should_not_fail_if_no_home() throws Exception { + assertThat(conf.properties()).isNotEmpty(); + } + + @Test + public void should_load_conf_by_direct_path() throws Exception { + File settings = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI()); + args.setProperty("runner.settings", settings.getCanonicalPath()); + + assertThat(conf.properties().get("sonar.prop")).isEqualTo("otherValue"); + } + + @Test + public void shouldLoadCompleteConfiguration() throws Exception { + File runnerHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/runner").toURI()); + File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadCompleteConfiguration/project").toURI()); + args.setProperty("runner.home", runnerHome.getCanonicalPath()); + args.setProperty("project.home", projectHome.getCanonicalPath()); + + Properties properties = conf.properties(); + + assertThat(properties.getProperty("project.prop")).isEqualTo("foo"); + assertThat(properties.getProperty("overridden.prop")).isEqualTo("project scope"); + assertThat(properties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar"); + } + + @Test + public void shouldLoadModuleConfiguration() throws Exception { + File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadModuleConfiguration/project").toURI()); + args.setProperty("project.home", projectHome.getCanonicalPath()); + + Properties properties = conf.properties(); + + assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1"); + assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2"); + } + + @Test + public void shouldSupportDeepModuleConfigurationInRoot() throws Exception { + File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI()); + args.setProperty("project.home", projectHome.getCanonicalPath()); + + Properties properties = conf.properties(); + + assertThat(properties.getProperty("1.sonar.projectName")).isEqualTo("Module 1"); + assertThat(properties.getProperty("1.11.sonar.projectName")).isEqualTo("Module 11"); + assertThat(properties.getProperty("1.11.111.sonar.projectName")).isEqualTo("Module 111"); + assertThat(properties.getProperty("1.12.sonar.projectName")).isEqualTo("Module 12"); + assertThat(properties.getProperty("2.sonar.projectName")).isEqualTo("Module 2"); + + // SONARUNNER-125 + assertThat(properties.getProperty("11.111.sonar.projectName")).isNull(); + } + + @Test + public void shouldLoadModuleConfigurationOverrideBasedir() throws Exception { + File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI()); + args.setProperty("project.home", projectHome.getCanonicalPath()); + + Properties properties = conf.properties(); + + assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1"); + assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2"); + assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3"); + } + + @Test + public void shouldSupportSettingBaseDirFromCli() throws Exception { + File projectHome = new File(getClass().getResource("/org/sonar/runner/ConfTest/shouldLoadModuleConfiguration/project").toURI()); + args.setProperty("project.home", temp.newFolder().getCanonicalPath()); + args.setProperty("sonar.projectBaseDir", projectHome.getCanonicalPath()); + + Properties properties = conf.properties(); + + assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1"); + assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2"); + } + +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java new file mode 100644 index 0000000..4ae594d --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/MainTest.java @@ -0,0 +1,125 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Properties; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.sonar.runner.api.EmbeddedRunner; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class MainTest { + + @Mock + private Shutdown exit; + @Mock + private Cli cli; + @Mock + private Conf conf; + @Mock + private Properties properties; + @Mock + private RunnerFactory runnerFactory; + @Mock + private EmbeddedRunner runner; + + @Before + public void setUp() throws IOException { + MockitoAnnotations.initMocks(this); + when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + when(conf.properties()).thenReturn(properties); + + } + + @Test + public void should_execute_runner() { + Main main = new Main(exit, cli, conf, runnerFactory); + main.execute(); + + verify(exit).exit(Exit.SUCCESS); + verify(runnerFactory).create(properties); + + verify(runner, times(1)).start(); + verify(runner, times(1)).runAnalysis(properties); + verify(runner, times(1)).stop(); + } + + @Test + public void should_fail_on_error() { + EmbeddedRunner runner = mock(EmbeddedRunner.class); + doThrow(new IllegalStateException("Error")).when(runner).runAnalysis(any(Properties.class)); + when(runnerFactory.create(any(Properties.class))).thenReturn(runner); + + Main main = new Main(exit, cli, conf, runnerFactory); + main.execute(); + + verify(exit).exit(Exit.ERROR); + } + + @Test + public void should_only_display_version() throws IOException { + + Properties p = new Properties(); + when(cli.isDisplayVersionOnly()).thenReturn(true); + when(conf.properties()).thenReturn(p); + + Main main = new Main(exit, cli, conf, runnerFactory); + main.execute(); + + InOrder inOrder = Mockito.inOrder(exit, runnerFactory); + + inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); + inOrder.verify(runnerFactory, times(1)).create(p); + inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); + } + + @Test(timeout = 30000) + public void test_interactive_mode() throws IOException { + String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator(); + InputStream input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8)); + System.setIn(input); + input.close(); + + when(cli.isInteractive()).thenReturn(true); + when(cli.isDebugMode()).thenReturn(true); + when(cli.isDisplayStackTrace()).thenReturn(true); + + Main main = new Main(exit, cli, conf, runnerFactory); + main.execute(); + + verify(runner, times(1)).start(); + verify(runner, times(3)).runAnalysis(any(Properties.class)); + verify(runner, times(1)).stop(); + } +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/RunnerFactoryTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/RunnerFactoryTest.java new file mode 100644 index 0000000..29955d3 --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/RunnerFactoryTest.java @@ -0,0 +1,41 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import java.util.Properties; +import org.junit.Test; +import org.sonar.runner.api.EmbeddedRunner; + +import static org.fest.assertions.Assertions.assertThat; + +public class RunnerFactoryTest { + + Properties props = new Properties(); + + @Test + public void should_create_embedded_runner() { + props.setProperty("foo", "bar"); + EmbeddedRunner runner = new RunnerFactory().create(props); + + assertThat(runner).isInstanceOf(EmbeddedRunner.class); + assertThat(runner.globalProperties().get("foo")).isEqualTo("bar"); + } + +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java new file mode 100644 index 0000000..7e570b1 --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/ShutdownTest.java @@ -0,0 +1,80 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import static org.mockito.Mockito.verify; +import static org.fest.assertions.Assertions.assertThat; +import org.mockito.MockitoAnnotations; +import org.sonar.runner.cli.Exit; +import org.sonar.runner.cli.Shutdown; +import org.mockito.Mock; +import org.junit.Test; +import org.junit.Before; + +public class ShutdownTest { + @Mock + private Exit exit; + private Shutdown shutdown; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + shutdown = new Shutdown(exit); + } + + @Test + public void testShutdown() { + shutdown.exit(3); + verify(exit).exit(3); + } + + @Test(timeout = 60_000) + public void testWaitReady() throws InterruptedException { + shutdown = new Shutdown(exit, 100_000); + shutdown.signalReady(false); + assertThat(shutdown.shouldExit()).isFalse(); + + Thread t = new HookCaller(); + t.start(); + Thread.sleep(1000); + + assertThat(t.isAlive()).isTrue(); + assertThat(shutdown.shouldExit()).isTrue(); + + shutdown.signalReady(true); + t.join(); + } + + @Test(timeout = 60_000) + public void testTimeout() throws InterruptedException { + shutdown = new Shutdown(exit, 0); + + Thread t = new HookCaller(); + t.start(); + t.join(); + } + + private class HookCaller extends Thread { + @Override + public void run() { + shutdown.hook.run(); + } + } +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java new file mode 100644 index 0000000..6093b41 --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/StatsTest.java @@ -0,0 +1,60 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.UnsupportedEncodingException; +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class StatsTest { + + @Test + public void shouldPrintStats() throws UnsupportedEncodingException { + + PrintStream backupOut = System.out; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + System.setOut(new PrintStream(baos)); + + new Stats().start().stop(); + + String out = baos.toString(); + String[] lines = out.split(System.lineSeparator()); + + assertThat(lines).hasSize(2); + + assertThat(lines[0]).contains("Total time: "); + assertThat(lines[1]).contains("Final Memory: "); + } finally { + System.setOut(backupOut); + } + } + + @Test + public void shouldFormatTime() { + assertThat(Stats.formatTime(1 * 60 * 60 * 1000 + 2 * 60 * 1000 + 3 * 1000 + 400)).isEqualTo("1:02:03.400s"); + assertThat(Stats.formatTime(2 * 60 * 1000 + 3 * 1000 + 400)).isEqualTo("2:03.400s"); + assertThat(Stats.formatTime(3 * 1000 + 400)).isEqualTo("3.400s"); + assertThat(Stats.formatTime(400)).isEqualTo("0.400s"); + } +} diff --git a/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java new file mode 100644 index 0000000..edeffff --- /dev/null +++ b/sonar-runner-cli/src/test/java/org/sonar/runner/cli/SystemInfoTest.java @@ -0,0 +1,43 @@ +/* + * SonarQube Runner - CLI - 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.cli; + +import org.junit.Test; +import org.sonar.runner.cli.SystemInfo; + +import static org.fest.assertions.Assertions.assertThat; + +public class SystemInfoTest { + @Test + public void test_java() { + assertThat(SystemInfo.java()).matches("Java .* \\((32|64)-bit\\)"); + } + + @Test + public void test_os() { + assertThat(SystemInfo.os()).isNotEmpty(); + } + + @Test + public void should_print() { + SystemInfo.print(); + // should mock output + } +} |