You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Sonar Runner - API
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.runner;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import java.io.File;
  24. import java.util.Properties;
  25. import static org.fest.assertions.Assertions.assertThat;
  26. public class MainTest {
  27. private Main main;
  28. @Before
  29. public void prepare() {
  30. main = new Main();
  31. }
  32. @Test
  33. public void shouldParseEmptyArguments() {
  34. Properties props = main.parseArguments(new String[] {});
  35. assertThat(props).isEmpty();
  36. }
  37. @Test
  38. public void shouldParseArguments() {
  39. Properties props = main.parseArguments(new String[] {"-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
  40. assertThat(props).hasSize(3);
  41. assertThat(props.getProperty("foo")).isEqualTo("bar");
  42. assertThat(props.getProperty("hello")).isEqualTo("world");
  43. assertThat(props.getProperty("boolean")).isEqualTo("true");
  44. }
  45. @Test
  46. public void shouldParseCommand() {
  47. Properties props = main.parseArguments(new String[] {"cmd", "-D", "foo=bar", "--define", "hello=world", "-Dboolean"});
  48. assertThat(main.command).isEqualTo("cmd");
  49. assertThat(props).hasSize(3);
  50. }
  51. @Test
  52. public void shouldEnableDebugMode() {
  53. Properties props = main.parseArguments(new String[] {"-X"});
  54. assertThat(main.debugMode).isTrue();
  55. assertThat(main.displayStackTrace).isTrue();
  56. assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isEqualTo("true");
  57. }
  58. @Test
  59. public void shouldEnableError() {
  60. Properties props = main.parseArguments(new String[] {"-e"});
  61. assertThat(main.debugMode).isFalse();
  62. assertThat(main.displayStackTrace).isTrue();
  63. assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull();
  64. }
  65. @Test
  66. public void shouldDisableDebugModeAndStackByDefault() {
  67. Properties props = main.parseArguments(new String[] {});
  68. assertThat(main.debugMode).isFalse();
  69. assertThat(main.displayStackTrace).isFalse();
  70. assertThat(props.getProperty(Runner.PROPERTY_VERBOSE)).isNull();
  71. }
  72. @Test
  73. public void shouldLoadRunnerSettingsByHome() throws Exception {
  74. File home = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByHome/").toURI());
  75. Properties args = new Properties();
  76. args.setProperty("runner.home", home.getCanonicalPath());
  77. Properties props = new Main().loadRunnerConfiguration(args);
  78. assertThat(props.getProperty("sonar.host.url")).isEqualTo("http://moon/sonar");
  79. }
  80. @Test
  81. public void shouldNotFailIfNoHome() throws Exception {
  82. Properties args = new Properties();
  83. Properties props = new Main().loadRunnerConfiguration(args);
  84. assertThat(props).isEmpty();
  85. }
  86. @Test
  87. public void shouldLoadRunnerSettingsByDirectPath() throws Exception {
  88. File settings = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
  89. Properties args = new Properties();
  90. args.setProperty("runner.settings", settings.getCanonicalPath());
  91. Properties props = new Main().loadRunnerConfiguration(args);
  92. assertThat(props.getProperty("sonar.host.url")).isEqualTo("http://other/sonar");
  93. }
  94. @Test
  95. public void shouldLoadCompleteConfiguration() throws Exception {
  96. File runnerHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/runner").toURI());
  97. File projectHome = new File(getClass().getResource("/org/sonar/runner/MainTest/shouldLoadCompleteConfiguration/project").toURI());
  98. Main main = new Main();
  99. Properties args = main.parseArguments(new String[] {
  100. "-D", "runner.home=" + runnerHome.getCanonicalPath(),
  101. "-D", "project.home=" + projectHome.getCanonicalPath()
  102. });
  103. main.loadProperties(args);
  104. assertThat(main.projectProperties.getProperty("project.prop")).isEqualTo("foo");
  105. assertThat(main.projectProperties.getProperty("overridden.prop")).isEqualTo("project scope");
  106. assertThat(main.globalProperties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar");
  107. }
  108. }