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.

ConfTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonarsource.scanner.cli;
  21. import java.io.File;
  22. import java.util.Properties;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.TemporaryFolder;
  27. import static org.fest.assertions.Assertions.assertThat;
  28. import static org.mockito.Mockito.mock;
  29. import static org.mockito.Mockito.when;
  30. public class ConfTest {
  31. @Rule
  32. public TemporaryFolder temp = new TemporaryFolder();
  33. Properties args = new Properties();
  34. Logs logs = new Logs(System.out, System.err);
  35. Cli cli = mock(Cli.class);
  36. Conf conf = new Conf(cli, logs);
  37. @Before
  38. public void initConf() {
  39. when(cli.properties()).thenReturn(args);
  40. }
  41. @Test
  42. public void should_load_global_settings_by_home() throws Exception {
  43. File home = new File(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByHome/").toURI());
  44. args.setProperty("runner.home", home.getCanonicalPath());
  45. assertThat(conf.properties().get("sonar.prop")).isEqualTo("value");
  46. }
  47. @Test
  48. public void should_not_fail_if_no_home() throws Exception {
  49. assertThat(conf.properties()).isNotEmpty();
  50. }
  51. @Test
  52. public void should_load_conf_by_direct_path() throws Exception {
  53. File settings = new File(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
  54. args.setProperty("runner.settings", settings.getCanonicalPath());
  55. assertThat(conf.properties().get("sonar.prop")).isEqualTo("otherValue");
  56. }
  57. @Test
  58. public void shouldLoadCompleteConfiguration() throws Exception {
  59. File runnerHome = new File(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/runner").toURI());
  60. File projectHome = new File(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/project").toURI());
  61. args.setProperty("runner.home", runnerHome.getCanonicalPath());
  62. args.setProperty("project.home", projectHome.getCanonicalPath());
  63. Properties properties = conf.properties();
  64. assertThat(properties.getProperty("project.prop")).isEqualTo("foo");
  65. assertThat(properties.getProperty("overridden.prop")).isEqualTo("project scope");
  66. assertThat(properties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar");
  67. }
  68. @Test
  69. public void shouldLoadModuleConfiguration() throws Exception {
  70. File projectHome = new File(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  71. args.setProperty("project.home", projectHome.getCanonicalPath());
  72. Properties properties = conf.properties();
  73. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  74. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  75. }
  76. @Test
  77. public void shouldSupportDeepModuleConfigurationInRoot() throws Exception {
  78. File projectHome = new File(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI());
  79. args.setProperty("project.home", projectHome.getCanonicalPath());
  80. Properties properties = conf.properties();
  81. assertThat(properties.getProperty("1.sonar.projectName")).isEqualTo("Module 1");
  82. assertThat(properties.getProperty("1.11.sonar.projectName")).isEqualTo("Module 11");
  83. assertThat(properties.getProperty("1.11.111.sonar.projectName")).isEqualTo("Module 111");
  84. assertThat(properties.getProperty("1.12.sonar.projectName")).isEqualTo("Module 12");
  85. assertThat(properties.getProperty("2.sonar.projectName")).isEqualTo("Module 2");
  86. // SONARUNNER-125
  87. assertThat(properties.getProperty("11.111.sonar.projectName")).isNull();
  88. }
  89. @Test
  90. public void shouldLoadModuleConfigurationOverrideBasedir() throws Exception {
  91. File projectHome = new File(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  92. args.setProperty("project.home", projectHome.getCanonicalPath());
  93. Properties properties = conf.properties();
  94. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  95. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  96. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  97. }
  98. @Test
  99. public void shouldSupportSettingBaseDirFromCli() throws Exception {
  100. File projectHome = new File(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  101. args.setProperty("project.home", temp.newFolder().getCanonicalPath());
  102. args.setProperty("sonar.projectBaseDir", projectHome.getCanonicalPath());
  103. Properties properties = conf.properties();
  104. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  105. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  106. }
  107. @Test
  108. public void ignoreEmptyModule() throws Exception {
  109. File projectHome = new File(getClass().getResource("ConfTest/emptyModules/project").toURI());
  110. args.setProperty("project.home", temp.newFolder().getCanonicalPath());
  111. args.setProperty("sonar.projectBaseDir", projectHome.getCanonicalPath());
  112. conf.properties();
  113. }
  114. @Test
  115. public void shouldGetList() {
  116. Properties props = new Properties();
  117. props.put("prop", " foo ,, bar , \n\ntoto,tutu");
  118. assertThat(Conf.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
  119. }
  120. }