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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.IOException;
  22. import java.net.URISyntaxException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.util.Properties;
  27. import org.apache.commons.lang.SystemUtils;
  28. import org.junit.Before;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.TemporaryFolder;
  32. import static org.fest.assertions.Assertions.assertThat;
  33. import static org.junit.Assume.assumeTrue;
  34. import static org.mockito.Mockito.mock;
  35. import static org.mockito.Mockito.when;
  36. public class ConfTest {
  37. @Rule
  38. public TemporaryFolder temp = new TemporaryFolder();
  39. Properties args = new Properties();
  40. Logs logs = new Logs(System.out, System.err);
  41. Cli cli = mock(Cli.class);
  42. Conf conf = new Conf(cli, logs);
  43. @Before
  44. public void initConf() {
  45. when(cli.properties()).thenReturn(args);
  46. }
  47. @Test
  48. public void should_load_global_settings_by_home() throws Exception {
  49. Path home = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByHome/").toURI());
  50. args.setProperty("scanner.home", home.toAbsolutePath().toString());
  51. Properties properties = conf.properties();
  52. assertThat(properties.get("sonar.prop")).isEqualTo("value");
  53. }
  54. @Test
  55. public void should_not_fail_if_no_home() throws Exception {
  56. assertThat(conf.properties()).isNotEmpty();
  57. // worst case, use current path
  58. assertThat(conf.properties().getProperty("sonar.projectBaseDir")).isEqualTo(Paths.get("").toAbsolutePath().toString());
  59. }
  60. @Test
  61. public void base_dir_can_be_relative() throws URISyntaxException, IOException {
  62. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  63. args.setProperty("project.home", projectHome.getParent().toAbsolutePath().toString());
  64. args.setProperty("sonar.projectBaseDir", "project");
  65. Properties properties = conf.properties();
  66. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  67. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  68. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  69. }
  70. @Test
  71. public void should_load_conf_by_direct_path() throws Exception {
  72. Path settings = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
  73. args.setProperty("scanner.settings", settings.toAbsolutePath().toString());
  74. assertThat(conf.properties().get("sonar.prop")).isEqualTo("otherValue");
  75. }
  76. @Test
  77. public void shouldLoadCompleteConfiguration() throws Exception {
  78. Path runnerHome = Paths.get(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/runner").toURI());
  79. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/project").toURI());
  80. args.setProperty("scanner.home", runnerHome.toAbsolutePath().toString());
  81. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  82. Properties properties = conf.properties();
  83. assertThat(properties.getProperty("project.prop")).isEqualTo("foo");
  84. assertThat(properties.getProperty("overridden.prop")).isEqualTo("project scope");
  85. assertThat(properties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar");
  86. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  87. }
  88. @Test
  89. public void shouldLoadModuleConfiguration() throws Exception {
  90. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  91. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  92. Properties properties = conf.properties();
  93. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  94. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  95. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  96. }
  97. @Test
  98. public void shouldSupportDeepModuleConfigurationInRoot() throws Exception {
  99. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI());
  100. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  101. Properties properties = conf.properties();
  102. assertThat(properties.getProperty("1.sonar.projectName")).isEqualTo("Module 1");
  103. assertThat(properties.getProperty("1.11.sonar.projectName")).isEqualTo("Module 11");
  104. assertThat(properties.getProperty("1.11.111.sonar.projectName")).isEqualTo("Module 111");
  105. assertThat(properties.getProperty("1.12.sonar.projectName")).isEqualTo("Module 12");
  106. assertThat(properties.getProperty("2.sonar.projectName")).isEqualTo("Module 2");
  107. // SONARUNNER-125
  108. assertThat(properties.getProperty("11.111.sonar.projectName")).isNull();
  109. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  110. }
  111. @Test
  112. public void shouldLoadModuleConfigurationOverrideBasedir() throws Exception {
  113. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  114. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  115. Properties properties = conf.properties();
  116. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  117. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  118. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  119. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  120. }
  121. @Test
  122. public void shouldCliOverrideSettingFiles() throws Exception {
  123. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  124. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  125. args.setProperty("module1.sonar.projectName", "mod1");
  126. args.setProperty("module2.sonar.projectName", "mod2");
  127. args.setProperty("module3.sonar.projectName", "mod3");
  128. Properties properties = conf.properties();
  129. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("mod1");
  130. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("mod2");
  131. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("mod3");
  132. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  133. }
  134. @Test
  135. public void shouldUseCliToDiscoverModules() throws Exception {
  136. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  137. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  138. args.setProperty("sonar.modules", "module1");
  139. args.setProperty("module1.sonar.projectBaseDir", "module_3");
  140. Properties properties = conf.properties();
  141. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 3");
  142. assertThat(properties.getProperty("module2.sonar.projectName")).isNull();
  143. assertThat(properties.getProperty("module3.sonar.projectName")).isNull();
  144. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  145. }
  146. @Test
  147. public void shouldNotUseCurrentDir() throws Exception {
  148. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  149. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  150. Properties properties = conf.properties();
  151. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  152. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  153. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  154. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  155. }
  156. @Test
  157. public void shouldLoadModuleConfigurationWithoutRootConf() throws Exception {
  158. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationWithoutRootConf/project").toURI());
  159. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  160. args.put("sonar.modules", "module1,module2");
  161. Properties properties = conf.properties();
  162. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  163. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  164. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  165. }
  166. @Test
  167. public void shouldSupportSettingBaseDirFromCli() throws Exception {
  168. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  169. args.setProperty("project.home", temp.newFolder().getCanonicalPath());
  170. args.setProperty("sonar.projectBaseDir", projectHome.toAbsolutePath().toString());
  171. Properties properties = conf.properties();
  172. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  173. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  174. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  175. }
  176. @Test
  177. public void ignoreEmptyModule() throws Exception {
  178. Path projectHome = Paths.get(getClass().getResource("ConfTest/emptyModules/project").toURI());
  179. args.setProperty("project.home", temp.newFolder().getCanonicalPath());
  180. args.setProperty("sonar.projectBaseDir", projectHome.toAbsolutePath().toString());
  181. conf.properties();
  182. }
  183. @Test
  184. public void shouldGetList() {
  185. Properties props = new Properties();
  186. props.put("prop", " foo ,, bar , \n\ntoto,tutu");
  187. assertThat(Conf.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
  188. }
  189. @Test
  190. public void shouldNotResolveSymlinks() throws IOException, URISyntaxException {
  191. assumeTrue(SystemUtils.IS_OS_UNIX);
  192. Path root = temp.getRoot().toPath();
  193. Path realProjectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  194. Path linkProjectHome = root.resolve("link");
  195. try {
  196. Files.createSymbolicLink(linkProjectHome, realProjectHome);
  197. args.setProperty("project.home", linkProjectHome.toAbsolutePath().toString());
  198. Properties properties = conf.properties();
  199. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  200. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  201. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(linkProjectHome.toString());
  202. assertThat(properties.getProperty("module1.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module1").toString());
  203. assertThat(properties.getProperty("module2.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module2").toString());
  204. } finally {
  205. if (linkProjectHome != null) {
  206. Files.delete(linkProjectHome);
  207. }
  208. }
  209. }
  210. }