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.

ScannerTest.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * SonarSource :: IT :: SonarQube Scanner
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info 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 com.sonarsource.scanner.it;
  21. import com.sonar.orchestrator.build.BuildResult;
  22. import com.sonar.orchestrator.build.SonarScanner;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.util.Map;
  26. import java.util.stream.Collectors;
  27. import org.fest.assertions.Condition;
  28. import org.junit.After;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonarqube.ws.WsMeasures.Measure;
  33. import static java.lang.Integer.parseInt;
  34. import static org.fest.assertions.Assertions.assertThat;
  35. public class ScannerTest extends ScannerTestCase {
  36. @Rule
  37. public TemporaryFolder temp = new TemporaryFolder();
  38. @After
  39. public void cleanup() {
  40. orchestrator.resetData();
  41. }
  42. @Test
  43. public void basedir_contains_sources() {
  44. SonarScanner build = newScanner(new File("projects/basedir-with-source"));
  45. orchestrator.executeBuild(build);
  46. Map<String, Measure> projectMeasures = getMeasures("java:basedir-with-source", "files", "ncloc");
  47. assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1);
  48. assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1);
  49. }
  50. /**
  51. * Replace the maven format groupId:artifactId by a single key
  52. */
  53. @Test
  54. public void should_support_simple_project_keys() {
  55. SonarScanner build = newScanner(new File("projects/simple-sample"))
  56. .setProjectKey("SAMPLE");
  57. orchestrator.executeBuild(build);
  58. Map<String, Measure> projectMeasures = getMeasures("SAMPLE", "files", "ncloc");
  59. assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2);
  60. assertThat(parseInt(projectMeasures.get("ncloc").getValue())).isGreaterThan(1);
  61. }
  62. /**
  63. * SONARPLUGINS-1230
  64. */
  65. @Test
  66. public void should_override_working_dir_with_relative_path() {
  67. SonarScanner build = newScanner(new File("projects/override-working-dir"))
  68. .setProperty("sonar.working.directory", ".overridden-relative-sonar");
  69. orchestrator.executeBuild(build);
  70. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  71. assertThat(new File("projects/override-working-dir/.overridden-relative-sonar")).exists().isDirectory();
  72. }
  73. /**
  74. * SONARPLUGINS-1230
  75. */
  76. @Test
  77. public void should_override_working_dir_with_absolute_path() {
  78. File projectHome = new File("projects/override-working-dir");
  79. SonarScanner build = newScanner(projectHome)
  80. .setProperty("sonar.working.directory", new File(projectHome, ".overridden-absolute-sonar").getAbsolutePath());
  81. orchestrator.executeBuild(build);
  82. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  83. assertThat(new File("projects/override-working-dir/.overridden-absolute-sonar")).exists().isDirectory();
  84. }
  85. /**
  86. * SONARPLUGINS-1856
  87. */
  88. @Test
  89. public void should_fail_if_source_dir_does_not_exist() {
  90. SonarScanner build = newScanner(new File("projects/bad-source-dirs"));
  91. BuildResult result = orchestrator.executeBuildQuietly(build);
  92. assertThat(result.getStatus()).isNotEqualTo(0);
  93. // with the following message
  94. assertThat(result.getLogs()).contains("Invalid value of sonar.sources for bad-source-dirs");
  95. }
  96. /**
  97. * SONARPLUGINS-2256
  98. */
  99. @Test
  100. public void should_warn_when_analysis_is_platform_dependent() {
  101. SonarScanner build = newScanner(new File("projects/simple-sample"))
  102. // ORCH-243
  103. .setSourceEncoding("");
  104. String log = orchestrator.executeBuild(build).getLogs();
  105. // Note: we can't really check the locale value and the charset because the ones used during the Sonar analysis may not be the ones
  106. // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set).
  107. assertThat(log).contains("Default locale:");
  108. assertThat(log).contains(", source code encoding:");
  109. assertThat(log).contains("(analysis is platform dependent)");
  110. }
  111. /**
  112. * SONARUNNER-153
  113. */
  114. @Test
  115. public void should_enable_verbose() {
  116. // this line should appear in all versions (LTS-DEV) in debug only
  117. String expectedLog = "Available languages:";
  118. SonarScanner build = newScanner(new File("projects/simple-sample"))
  119. .setProperty("sonar.verbose", "true");
  120. String logs = orchestrator.executeBuild(build).getLogs();
  121. assertThat(logs).contains(expectedLog);
  122. }
  123. @Test
  124. public void should_use_environment_props() {
  125. SonarScanner build = newScanner(new File("projects/simple-sample-no-properties"))
  126. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  127. + "\"sonar.projectKey\" : \"sample\"," +
  128. "\"sonar.projectName\" : \"Sample, with comma\"," +
  129. "\"sonar.projectDescription\" : \"This is a sample\"," +
  130. "\"sonar.projectVersion\" : \"1.2.3\"," +
  131. "\"sonar.sources\" : \"src\" }");
  132. orchestrator.executeBuild(build);
  133. }
  134. @Test
  135. public void should_skip_analysis() {
  136. SonarScanner build = newScanner(new File("projects/simple-sample"))
  137. .setProperty("sonar.host.url", "http://foo")
  138. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{ \"sonar.scanner.skip\":\"true\" }");
  139. BuildResult result = orchestrator.executeBuild(build);
  140. assertThat(result.getLogs()).contains("SonarQube Scanner analysis skipped");
  141. }
  142. @Test
  143. public void should_fail_if_unable_to_connect() {
  144. SonarScanner build = newScanner(new File("projects/simple-sample"))
  145. .setProperty("sonar.host.url", "http://foo");
  146. BuildResult result = orchestrator.executeBuildQuietly(build);
  147. // expect build failure
  148. assertThat(result.getStatus()).isNotEqualTo(0);
  149. // with the following message
  150. assertThat(result.getLogs()).contains("SonarQube server [http://foo] can not be reached");
  151. }
  152. // SONARPLUGINS-3574
  153. @Test
  154. public void run_from_external_location() throws IOException {
  155. File tempDir = temp.newFolder();
  156. SonarScanner build = newScanner(tempDir)
  157. .setProperty("sonar.projectBaseDir", new File("projects/simple-sample").getAbsolutePath())
  158. .addArguments("-e");
  159. orchestrator.executeBuild(build);
  160. assertThat(getComponent("sample").getDescription()).isEqualTo("This is a sample");
  161. Map<String, Measure> projectMeasures = getMeasures("sample", "files", "ncloc", "classes", "violations");
  162. assertThat(projectMeasures.values().stream().filter(measure -> measure.getValue() != null).collect(Collectors.toList())).hasSize(4);
  163. }
  164. @Test
  165. public void verify_env_variable() {
  166. SonarScanner build = newScanner(new File("projects/simple-sample"))
  167. .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx2m");
  168. BuildResult executeBuild = orchestrator.executeBuildQuietly(build);
  169. assertThat(executeBuild.getStatus()).isNotEqualTo(0);
  170. String logs = executeBuild.getLogs();
  171. assertThat(logs).satisfies(new Condition<String>("Contain error message about OOM") {
  172. @Override
  173. public boolean matches(String value) {
  174. return value.contains("java.lang.OutOfMemoryError")
  175. || value.contains("GC overhead limit exceeded") || value.contains("Java heap space");
  176. }
  177. });
  178. }
  179. }