From: Simon Brandhof Date: Thu, 4 Apr 2013 22:06:46 +0000 (+0200) Subject: SONARPLUGINS-2574 improve unit tests of CLI X-Git-Tag: 2.5-rc1~179^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6d0b6dba7289d992f2f80899810f01ae007ca5ac;p=sonar-scanner-cli.git SONARPLUGINS-2574 improve unit tests of CLI --- diff --git a/sonar-runner-dist/src/main/java/org/sonar/runner/Cli.java b/sonar-runner-dist/src/main/java/org/sonar/runner/Cli.java new file mode 100644 index 0000000..7c7ce90 --- /dev/null +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Cli.java @@ -0,0 +1,129 @@ +/* + * 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.impl.Constants; +import org.sonar.runner.impl.Logs; + +import java.util.Properties; + +class Cli { + + private boolean debugMode = false; + private boolean displayVersionOnly = false; + private boolean displayStackTrace = false; + private Properties props = new Properties(); + + public boolean isDebugMode() { + return debugMode; + } + + public boolean isDisplayVersionOnly() { + return displayVersionOnly; + } + + public boolean isDisplayStackTrace() { + return displayStackTrace; + } + + public Properties properties() { + return props; + } + + Cli parse(String[] args) { + reset(); + props.putAll(System.getProperties()); + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + if (i == 0 && !arg.startsWith("-")) { + props.setProperty(Constants.TASK, arg); + + } else if ("-h".equals(arg) || "--help".equals(arg)) { + printUsage(); + + } else if ("-v".equals(arg) || "--version".equals(arg)) { + displayVersionOnly = true; + + } else if ("-e".equals(arg) || "--errors".equals(arg)) { + displayStackTrace = true; + + } else if ("-X".equals(arg) || "--debug".equals(arg)) { + props.setProperty("sonar.verbose", "true"); + displayStackTrace = true; + debugMode = true; + Logs.setDebugEnabled(true); + + } else if ("-D".equals(arg) || "--define".equals(arg)) { + i++; + if (i >= args.length) { + printError("Missing argument for option --define"); + } + arg = args[i]; + appendPropertyTo(arg, props); + + } else if (arg.startsWith("-D")) { + arg = arg.substring(2); + appendPropertyTo(arg, props); + + } else { + printError("Unrecognized option: " + arg); + } + } + return this; + } + + private void reset() { + props.clear(); + debugMode = false; + displayStackTrace = false; + displayVersionOnly = false; + } + + private void appendPropertyTo(String arg, Properties props) { + final String key, value; + int j = arg.indexOf('='); + if (j == -1) { + key = arg; + value = "true"; + } else { + key = arg.substring(0, j); + value = arg.substring(j + 1); + } + props.setProperty(key, value); + } + + private void printError(String message) { + Logs.error(message); + printUsage(); + } + + private void printUsage() { + Logs.info(""); + Logs.info("usage: sonar-runner [options]"); + Logs.info(""); + Logs.info("Options:"); + Logs.info(" -D,--define Define property"); + Logs.info(" -e,--errors Produce execution error messages"); + Logs.info(" -h,--help Display help information"); + Logs.info(" -v,--version Display version information"); + Logs.info(" -X,--debug Produce execution debug output"); + System.exit(0); + } +} 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 new file mode 100644 index 0000000..504fe3f --- /dev/null +++ b/sonar-runner-dist/src/main/java/org/sonar/runner/Conf.java @@ -0,0 +1,112 @@ +/* + * 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.impl.Logs; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +class Conf { + private static final String RUNNER_HOME = "runner.home"; + private static final String RUNNER_SETTINGS = "runner.settings"; + private static final String PROJECT_HOME = "project.home"; + private static final String PROJECT_SETTINGS = "project.settings"; + + private final Cli cli; + + Conf(Cli cli) { + this.cli = cli; + } + + Properties load() throws IOException { + Properties result = new Properties(); + result.putAll(loadGlobalProperties()); + result.putAll(loadProjectProperties()); + result.putAll(System.getProperties()); + result.putAll(cli.properties()); + + if (result.containsKey(PROJECT_HOME)) { + // the real property of the Sonar Runner is "sonar.projectDir" + String baseDir = result.getProperty(PROJECT_HOME); + result.remove(PROJECT_HOME); + result.put("sonar.projectBaseDir", baseDir); + } + + return result; + } + + private Properties loadGlobalProperties() throws IOException { + File settingsFile = locatePropertiesFile(cli.properties(), RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS); + if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { + Logs.info("Runner configuration file: " + settingsFile.getAbsolutePath()); + return toProperties(settingsFile); + } + Logs.info("Runner configuration file: NONE"); + return new Properties(); + } + + private Properties loadProjectProperties() throws IOException { + File settingsFile = locatePropertiesFile(cli.properties(), PROJECT_HOME, "sonar-project.properties", PROJECT_SETTINGS); + if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { + Logs.info("Project configuration file: " + settingsFile.getAbsolutePath()); + return toProperties(settingsFile); + } + Logs.info("Project configuration file: NONE"); + return new Properties(); + } + + private File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) { + File settingsFile = null; + String runnerHome = props.getProperty(homeKey); + if (runnerHome != null && !"".equals(runnerHome)) { + settingsFile = new File(runnerHome, relativePathFromHome); + } + + if (settingsFile == null || !settingsFile.exists()) { + String settingsPath = props.getProperty(settingsKey); + if (settingsPath != null && !"".equals(settingsPath)) { + settingsFile = new File(settingsPath); + } + } + return settingsFile; + } + + private static Properties toProperties(File file) throws IOException { + InputStream in = null; + try { + Properties properties = new Properties(); + in = new FileInputStream(file); + properties.load(in); + return properties; + + } catch (Exception e) { + throw new IllegalStateException("Fail to load file: " + file.getAbsolutePath(), e); + + } finally { + if (in != null) { + in.close(); + } + } + } +} 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 1e0b15f..fcd4a7a 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 @@ -20,15 +20,9 @@ package org.sonar.runner; import org.sonar.runner.api.EmbeddedRunner; -import org.sonar.runner.api.Runner; import org.sonar.runner.api.RunnerVersion; -import org.sonar.runner.impl.Constants; import org.sonar.runner.impl.Logs; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.Properties; /** @@ -45,46 +39,39 @@ import java.util.Properties; */ public final class Main { - private static final String RUNNER_HOME = "runner.home"; - private static final String RUNNER_SETTINGS = "runner.settings"; - private static final String PROJECT_HOME = "project.home"; - private static final String PROJECT_SETTINGS = "project.settings"; - - boolean debugMode = false; - boolean displayVersionOnly = false; - boolean displayStackTrace = false; - - /** - * Entry point of the program. - */ public static void main(String[] args) { - new Main().execute(args); + Cli cli = new Cli().parse(args); + new Main(cli).execute(); } - Main() { + private final Cli cli; + + Main(Cli cli) { + this.cli = cli; } - private void execute(String[] args) { - Properties argsProperties = parseArguments(args); - System.out.println("Runner version: " + RunnerVersion.version()); - System.out.println("Java version: " + System.getProperty("java.version", "") - + ", vendor: " + System.getProperty("java.vendor", "")); - System.out - .println("OS name: \"" + System.getProperty("os.name") + "\", version: \"" + System.getProperty("os.version") + "\", arch: \"" + System.getProperty("os.arch") + "\""); - if (!displayVersionOnly) { - int result = execute(argsProperties); - System.exit(result); + void execute() { + printSystem(); + if (!cli.isDisplayVersionOnly()) { + int status = doExecute(new Conf(cli)); + System.exit(status); } } - private int execute(Properties argsProperties) { - if (displayStackTrace) { + private void printSystem() { + System.out.println("Runner " + RunnerVersion.version()); + System.out.println("Java " + System.getProperty("java.version") + " (" + System.getProperty("java.vendor") + ")"); + System.out.println(System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch")); + } + + private int doExecute(Conf conf) { + if (cli.isDisplayStackTrace()) { Logs.info("Error stacktraces are turned on."); } Stats stats = new Stats().start(); try { - Properties properties = loadProperties(argsProperties); - Runner runner = EmbeddedRunner.create().addProperties(properties); + Properties properties = conf.load(); + EmbeddedRunner.create().addProperties(properties).execute(); // Logs.debug("Other system properties:"); // Logs.debug(" - sun.arch.data.model: \"" + System.getProperty("sun.arch.data.model") + "\""); @@ -95,10 +82,9 @@ public final class Main { // } catch (IOException e) { // throw new RunnerException("Unable to resolve directory", e); // } - runner.execute(); } catch (Exception e) { displayExecutionResult(stats, "FAILURE"); - showError("Error during Sonar runner execution", e, displayStackTrace); + showError("Error during Sonar runner execution", e, cli.isDisplayStackTrace()); return 1; } displayExecutionResult(stats, "SUCCESS"); @@ -116,7 +102,7 @@ public final class Main { public void showError(String message, Throwable e, boolean showStackTrace) { if (showStackTrace) { Logs.error(message, e); - if (!debugMode) { + if (!cli.isDebugMode()) { Logs.error(""); suggestDebugMode(); } @@ -126,15 +112,15 @@ public final class Main { Logs.error(e.getMessage()); String previousMsg = ""; for (Throwable cause = e.getCause(); cause != null - && cause.getMessage() != null - && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { + && cause.getMessage() != null + && !cause.getMessage().equals(previousMsg); cause = cause.getCause()) { Logs.error("Caused by: " + cause.getMessage()); previousMsg = cause.getMessage(); } } Logs.error(""); Logs.error("To see the full stack trace of the errors, re-run Sonar Runner with the -e switch."); - if (!debugMode) { + if (!cli.isDebugMode()) { suggestDebugMode(); } } @@ -144,172 +130,5 @@ public final class Main { Logs.error("Re-run Sonar Runner using the -X switch to enable full debug logging."); } - Properties loadProperties(Properties arguments) throws IOException { - Properties props = new Properties(); - props.putAll(System.getProperties()); - props.putAll(arguments); - - Properties result = new Properties(); - result.putAll(loadGlobalProperties(arguments)); - result.putAll(loadProjectProperties(arguments)); - result.putAll(props); - return result; - } - - Properties loadGlobalProperties(Properties argsProperties) throws IOException { - Properties commandLineProps = new Properties(); - commandLineProps.putAll(System.getProperties()); - commandLineProps.putAll(argsProperties); - - Properties result = new Properties(); - result.putAll(loadRunnerConfiguration(commandLineProps)); - result.putAll(commandLineProps); - - return result; - } - - Properties loadProjectProperties(Properties argsProperties) throws IOException { - Properties commandLineProps = new Properties(); - commandLineProps.putAll(System.getProperties()); - commandLineProps.putAll(argsProperties); - - Properties result = new Properties(); - result.putAll(loadProjectConfiguration(commandLineProps)); - result.putAll(commandLineProps); - - if (result.containsKey(PROJECT_HOME)) { - // the real property of the Sonar Runner is "sonar.projectDir" - String baseDir = result.getProperty(PROJECT_HOME); - result.remove(PROJECT_HOME); - result.put("sonar.projectBaseDir", baseDir); - } - - return result; - } - - Properties loadRunnerConfiguration(Properties props) throws IOException { - File settingsFile = locatePropertiesFile(props, RUNNER_HOME, "conf/sonar-runner.properties", RUNNER_SETTINGS); - if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { - Logs.info("Runner configuration file: " + settingsFile.getAbsolutePath()); - return toProperties(settingsFile); - } - Logs.info("Runner configuration file: NONE"); - return new Properties(); - } - - private Properties loadProjectConfiguration(Properties props) throws IOException { - File settingsFile = locatePropertiesFile(props, PROJECT_HOME, "sonar-project.properties", PROJECT_SETTINGS); - if (settingsFile != null && settingsFile.isFile() && settingsFile.exists()) { - Logs.info("Project configuration file: " + settingsFile.getAbsolutePath()); - return toProperties(settingsFile); - } - Logs.info("Project configuration file: NONE"); - return new Properties(); - } - - private File locatePropertiesFile(Properties props, String homeKey, String relativePathFromHome, String settingsKey) { - File settingsFile = null; - String runnerHome = props.getProperty(homeKey); - if (runnerHome != null && !"".equals(runnerHome)) { - settingsFile = new File(runnerHome, relativePathFromHome); - } - - if (settingsFile == null || !settingsFile.exists()) { - String settingsPath = props.getProperty(settingsKey); - if (settingsPath != null && !"".equals(settingsPath)) { - settingsFile = new File(settingsPath); - } - } - return settingsFile; - } - - private Properties toProperties(File file) throws IOException { - InputStream in = null; - Properties properties = new Properties(); - try { - in = new FileInputStream(file); - properties.load(in); - return properties; - - } catch (Exception e) { - throw new IllegalStateException("Fail to load file: " + file.getAbsolutePath(), e); - - } finally { - if (in != null) { - in.close(); - } - } - } - - Properties parseArguments(String[] args) { - Properties props = new Properties(); - int i = 0; - if (args.length > 0 && !args[0].startsWith("-")) { - String task = args[0]; - props.setProperty(Constants.TASK, task); - i++; - } - for (; i < args.length; i++) { - String arg = args[i]; - if ("-h".equals(arg) || "--help".equals(arg)) { - printUsage(); - } else if ("-v".equals(arg) || "--version".equals(arg)) { - displayVersionOnly = true; - } else if ("-e".equals(arg) || "--errors".equals(arg)) { - displayStackTrace = true; - } else if ("-X".equals(arg) || "--debug".equals(arg)) { - props.setProperty("sonar.verbose", "true"); - displayStackTrace = true; - debugMode = true; - Logs.setDebugEnabled(true); - } else if ("-D".equals(arg) || "--define".equals(arg)) { - i++; - if (i >= args.length) { - printError("Missing argument for option --define"); - } - arg = args[i]; - appendPropertyTo(arg, props); - - } else if (arg.startsWith("-D")) { - arg = arg.substring(2); - appendPropertyTo(arg, props); - - } else { - printError("Unrecognized option: " + arg); - } - } - return props; - } - - private void appendPropertyTo(String arg, Properties props) { - final String key, value; - int j = arg.indexOf('='); - if (j == -1) { - key = arg; - value = "true"; - } else { - key = arg.substring(0, j); - value = arg.substring(j + 1); - } - props.setProperty(key, value); - } - - private void printError(String message) { - Logs.error(message); - printUsage(); - } - - private void printUsage() { - Logs.info(""); - Logs.info("usage: sonar-runner [options]"); - Logs.info(""); - Logs.info("Options:"); - Logs.info(" -D,--define Define property"); - Logs.info(" -e,--errors Produce execution error messages"); - Logs.info(" -h,--help Display help information"); - Logs.info(" -v,--version Display version information"); - Logs.info(" -X,--debug Produce execution debug output"); - System.exit(0); - } } diff --git a/sonar-runner-dist/src/test/java/org/sonar/runner/CliTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/CliTest.java new file mode 100644 index 0000000..9389480 --- /dev/null +++ b/sonar-runner-dist/src/test/java/org/sonar/runner/CliTest.java @@ -0,0 +1,79 @@ +/* + * 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 static org.fest.assertions.Assertions.assertThat; + +public class CliTest { + + Cli cli = new Cli(); + + @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 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-dist/src/test/java/org/sonar/runner/ConfTest.java b/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java new file mode 100644 index 0000000..7cb2c38 --- /dev/null +++ b/sonar-runner-dist/src/test/java/org/sonar/runner/ConfTest.java @@ -0,0 +1,81 @@ +/* + * 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.Before; +import org.junit.Test; + +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 { + + Properties args = new Properties(); + Conf conf; + + @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()); + args.setProperty("runner.home", home.getCanonicalPath()); + + assertThat(conf.load().get("sonar.host.url")).isEqualTo("http://moon/sonar"); + } + + @Test + public void should_not_fail_if_no_home() throws Exception { + assertThat(conf.load()).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()); + args.setProperty("runner.settings", settings.getCanonicalPath()); + + assertThat(conf.load().get("sonar.host.url")).isEqualTo("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()); +// Main main = new Main(); +// Properties args = main.parseArguments(new String[] { +// "-D", "runner.home=" + runnerHome.getCanonicalPath(), +// "-D", "project.home=" + projectHome.getCanonicalPath() +// }); +// main.loadProperties(args); +// +// assertThat(main.projectProperties.getProperty("project.prop")).isEqualTo("foo"); +// assertThat(main.projectProperties.getProperty("overridden.prop")).isEqualTo("project scope"); +// assertThat(main.globalProperties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar"); +// } + +} 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 deleted file mode 100644 index d988bd2..0000000 --- a/sonar-runner-dist/src/test/java/org/sonar/runner/MainTest.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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.Before; -import org.junit.Test; - -import java.io.File; -import java.util.Properties; - -import static org.fest.assertions.Assertions.assertThat; - -public class MainTest { - - private Main main; - - @Before - public void prepare() { - main = new Main(); - } - - @Test - public void shouldParseEmptyArguments() { - Properties props = main.parseArguments(new String[]{}); - assertThat(props).isEmpty(); - } - - @Test - public void shouldParseArguments() { - Properties props = main.parseArguments(new String[]{"-D", "foo=bar", "--define", "hello=world", "-Dboolean"}); - assertThat(props).hasSize(3); - assertThat(props.getProperty("foo")).isEqualTo("bar"); - assertThat(props.getProperty("hello")).isEqualTo("world"); - assertThat(props.getProperty("boolean")).isEqualTo("true"); - } - -// @Test -// public void shouldParseCommand() { -// -// Properties props = main.parseArguments(new String[] {"cmd", "-D", "foo=bar", "--define", "hello=world", "-Dboolean"}); -// assertThat(main.command).isEqualTo("cmd"); -// assertThat(props).hasSize(3); -// } -// -// @Test -// public void shouldEnableDebugMode() { -// Properties props = main.parseArguments(new String[] {"-X"}); -// assertThat(main.debugMode).isTrue(); -// assertThat(main.displayStackTrace).isTrue(); -// assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isEqualTo("true"); -// } -// -// @Test -// public void shouldEnableError() { -// Properties props = main.parseArguments(new String[] {"-e"}); -// assertThat(main.debugMode).isFalse(); -// assertThat(main.displayStackTrace).isTrue(); -// assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull(); -// } -// -// @Test -// public void shouldDisableDebugModeAndStackByDefault() { -// Properties props = main.parseArguments(new String[] {}); -// assertThat(main.debugMode).isFalse(); -// assertThat(main.displayStackTrace).isFalse(); -// assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull(); -// } - - @Test - 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 = new Main().loadRunnerConfiguration(args); - - assertThat(props.getProperty("sonar.host.url")).isEqualTo("http://moon/sonar"); - } - - @Test - public void shouldNotFailIfNoHome() throws Exception { - Properties args = new Properties(); - Properties props = new Main().loadRunnerConfiguration(args); - - assertThat(props).isEmpty(); - } - - @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 = new Main().loadRunnerConfiguration(args); - - assertThat(props.getProperty("sonar.host.url")).isEqualTo("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()); -// Main main = new Main(); -// Properties args = main.parseArguments(new String[] { -// "-D", "runner.home=" + runnerHome.getCanonicalPath(), -// "-D", "project.home=" + projectHome.getCanonicalPath() -// }); -// main.loadProperties(args); -// -// assertThat(main.projectProperties.getProperty("project.prop")).isEqualTo("foo"); -// assertThat(main.projectProperties.getProperty("overridden.prop")).isEqualTo("project scope"); -// assertThat(main.globalProperties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar"); -// } - -}