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.

JavaTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * SonarSource :: IT :: SonarQube Scanner
  3. * Copyright (C) 2009-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 com.sonar.runner.it;
  21. import com.sonar.orchestrator.build.BuildResult;
  22. import com.sonar.orchestrator.build.SonarScanner;
  23. import com.sonar.orchestrator.locator.ResourceLocation;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import org.apache.commons.lang.SystemUtils;
  28. import org.junit.After;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonar.wsclient.issue.Issue;
  33. import org.sonar.wsclient.issue.IssueQuery;
  34. import org.sonar.wsclient.services.Resource;
  35. import org.sonar.wsclient.services.ResourceQuery;
  36. import static org.fest.assertions.Assertions.assertThat;
  37. public class JavaTest extends ScannerTestCase {
  38. @Rule
  39. public TemporaryFolder temp = new TemporaryFolder();
  40. @After
  41. public void cleanup() {
  42. orchestrator.resetData();
  43. }
  44. /**
  45. * No bytecode, only sources
  46. */
  47. @Test
  48. public void scan_java_sources() {
  49. orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
  50. orchestrator.getServer().provisionProject("java:sample", "Java Sample, with comma");
  51. orchestrator.getServer().associateProjectToQualityProfile("java:sample", "java", "sonar-way");
  52. SonarScanner build = newScanner(new File("projects/java-sample"))
  53. .setProperty("sonar.verbose", "true")
  54. .addArguments("-e");
  55. // SONARPLUGINS-3061
  56. // Add a trailing slash
  57. build.setProperty("sonar.host.url", orchestrator.getServer().getUrl() + "/");
  58. orchestrator.executeBuild(build);
  59. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:sample").setMetrics("files", "ncloc", "classes", "lcom4", "violations"));
  60. // SONARPLUGINS-2399
  61. assertThat(project.getName()).isEqualTo("Java Sample, with comma");
  62. assertThat(project.getDescription()).isEqualTo("This is a Java sample");
  63. assertThat(project.getVersion()).isEqualTo("1.2.3");
  64. assertThat(project.getMeasureIntValue("files")).isEqualTo(2);
  65. assertThat(project.getMeasureIntValue("classes")).isEqualTo(2);
  66. assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(10);
  67. assertThat(project.getMeasureIntValue("violations")).isGreaterThan(0);
  68. Resource file = orchestrator.getServer().getWsClient()
  69. .find(new ResourceQuery("java:sample:src/basic/Hello.java").setMetrics("files", "ncloc", "classes", "violations"));
  70. assertThat(file.getName()).isEqualTo("Hello.java");
  71. assertThat(file.getMeasureIntValue("ncloc")).isEqualTo(7);
  72. assertThat(file.getMeasureIntValue("violations")).isGreaterThan(0);
  73. }
  74. @Test
  75. public void scan_java_sources_and_bytecode() {
  76. orchestrator.getServer().restoreProfile(ResourceLocation.create("/requires-bytecode-profile.xml"));
  77. orchestrator.getServer().provisionProject("java:bytecode", "Java Bytecode Sample");
  78. orchestrator.getServer().associateProjectToQualityProfile("java:bytecode", "java", "requires-bytecode");
  79. SonarScanner build = newScanner(new File("projects/java-bytecode"));
  80. orchestrator.executeBuild(build);
  81. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:bytecode").setMetrics("lcom4", "violations"));
  82. assertThat(project.getName()).isEqualTo("Java Bytecode Sample");
  83. // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0
  84. assertThat(project.getMeasureIntValue("violations")).isGreaterThan(0);
  85. Resource file = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:bytecode:src/HasFindbugsViolation.java").setMetrics("lcom4", "violations"));
  86. assertThat(file.getMeasureIntValue("violations")).isGreaterThan(0);
  87. // findbugs is executed on bytecode
  88. List<Issue> issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:S1147")).list();
  89. assertThat(issues).hasSize(1);
  90. assertThat(issues.get(0).ruleKey()).isEqualTo("squid:S1147");
  91. // Squid performs analysis of dependencies
  92. issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("squid:CallToDeprecatedMethod")).list();
  93. assertThat(issues).hasSize(1);
  94. assertThat(issues.get(0).ruleKey()).isEqualTo("squid:CallToDeprecatedMethod");
  95. }
  96. @Test
  97. public void basedir_contains_java_sources() {
  98. orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
  99. orchestrator.getServer().provisionProject("java:basedir-with-source", "Basedir with source");
  100. orchestrator.getServer().associateProjectToQualityProfile("java:basedir-with-source", "java", "sonar-way");
  101. SonarScanner build = newScanner(new File("projects/basedir-with-source"));
  102. orchestrator.executeBuild(build);
  103. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:basedir-with-source").setMetrics("files", "ncloc"));
  104. assertThat(project.getMeasureIntValue("files")).isEqualTo(1);
  105. assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(1);
  106. }
  107. /**
  108. * Replace the maven format groupId:artifactId by a single key
  109. */
  110. @Test
  111. public void should_support_simple_project_keys() {
  112. orchestrator.getServer().restoreProfile(ResourceLocation.create("/sonar-way-profile.xml"));
  113. orchestrator.getServer().provisionProject("SAMPLE", "Java Sample, with comma");
  114. orchestrator.getServer().associateProjectToQualityProfile("SAMPLE", "java", "sonar-way");
  115. SonarScanner build = newScanner(new File("projects/java-sample"))
  116. .setProjectKey("SAMPLE");
  117. orchestrator.executeBuild(build);
  118. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("SAMPLE").setMetrics("files", "ncloc"));
  119. assertThat(project.getMeasureIntValue("files")).isEqualTo(2);
  120. assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(1);
  121. }
  122. /**
  123. * SONARPLUGINS-1230
  124. */
  125. @Test
  126. public void should_override_working_dir_with_relative_path() {
  127. SonarScanner build = newScanner(new File("projects/override-working-dir"))
  128. .setProperty("sonar.working.directory", ".overridden-relative-sonar");
  129. orchestrator.executeBuild(build);
  130. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  131. assertThat(new File("projects/override-working-dir/.overridden-relative-sonar")).exists().isDirectory();
  132. }
  133. /**
  134. * SONARPLUGINS-1230
  135. */
  136. @Test
  137. public void should_override_working_dir_with_absolute_path() {
  138. File projectHome = new File("projects/override-working-dir");
  139. SonarScanner build = newScanner(projectHome)
  140. .setProperty("sonar.working.directory", new File(projectHome, ".overridden-absolute-sonar").getAbsolutePath());
  141. orchestrator.executeBuild(build);
  142. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  143. assertThat(new File("projects/override-working-dir/.overridden-absolute-sonar")).exists().isDirectory();
  144. }
  145. /**
  146. * SONARPLUGINS-1856
  147. */
  148. @Test
  149. public void should_fail_if_source_dir_does_not_exist() {
  150. SonarScanner build = newScanner(new File("projects/bad-source-dirs"));
  151. BuildResult result = orchestrator.executeBuildQuietly(build);
  152. assertThat(result.getStatus()).isNotEqualTo(0);
  153. // with the following message
  154. assertThat(result.getLogs()).contains("The folder 'bad' does not exist for 'bad-source-dirs'");
  155. }
  156. /**
  157. * SONARPLUGINS-2256
  158. */
  159. @Test
  160. public void should_warn_when_analysis_is_platform_dependent() {
  161. SonarScanner build = newScanner(new File("projects/java-sample"))
  162. // ORCH-243
  163. .setSourceEncoding("");
  164. String log = orchestrator.executeBuild(build).getLogs();
  165. // 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
  166. // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set).
  167. assertThat(log).contains("Default locale:");
  168. assertThat(log).contains(", source code encoding:");
  169. assertThat(log).contains("(analysis is platform dependent)");
  170. }
  171. /**
  172. * SONARUNNER-153
  173. */
  174. @Test
  175. public void should_enable_verbose() {
  176. // this line should appear in all versions (LTS-DEV) in debug only
  177. String expectedLog = "Available languages:";
  178. SonarScanner build = newScanner(new File("projects/java-sample"))
  179. .setProperty("sonar.verbose", "true");
  180. String logs = orchestrator.executeBuild(build).getLogs();
  181. assertThat(logs).contains(expectedLog);
  182. }
  183. @Test
  184. public void should_fail_if_unable_to_connect() {
  185. SonarScanner build = newScanner(new File("projects/java-sample"))
  186. .setProperty("sonar.host.url", "http://foo");
  187. BuildResult result = orchestrator.executeBuildQuietly(build);
  188. // expect build failure
  189. assertThat(result.getStatus()).isNotEqualTo(0);
  190. // with the following message
  191. assertThat(result.getLogs()).contains("SonarQube server [http://foo] can not be reached");
  192. }
  193. // SONARPLUGINS-3574
  194. @Test
  195. public void run_from_external_location() throws IOException {
  196. File tempDir = temp.newFolder();
  197. SonarScanner build = newScanner(tempDir)
  198. .setProperty("sonar.projectBaseDir", new File("projects/java-sample").getAbsolutePath())
  199. .addArguments("-e");
  200. orchestrator.executeBuild(build);
  201. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:sample").setMetrics("files", "ncloc", "classes", "lcom4", "violations"));
  202. assertThat(project.getDescription()).isEqualTo("This is a Java sample");
  203. assertThat(project.getVersion()).isEqualTo("1.2.3");
  204. }
  205. @Test
  206. public void use_old_script_and_old_env_variable() {
  207. SonarScanner build = newScanner(new File("projects/java-sample"))
  208. .setUseOldSonarRunnerScript(true)
  209. .setEnvironmentVariable("SONAR_RUNNER_OPTS", "-Xmx2m");
  210. BuildResult executeBuild = orchestrator.executeBuildQuietly(build);
  211. assertThat(executeBuild.getStatus()).isNotEqualTo(0);
  212. String logs = executeBuild.getLogs();
  213. if (SystemUtils.IS_OS_WINDOWS) {
  214. assertThat(logs).contains("WARN: sonar-runner.bat script is deprecated. Please use sonar-scanner.bat instead.");
  215. assertThat(logs).contains("WARN: SONAR_RUNNER_OPTS is deprecated. Please use SONAR_SCANNER_OPTS instead.");
  216. } else {
  217. assertThat(logs).contains("WARN: sonar-runner script is deprecated. Please use sonar-scanner instead.");
  218. assertThat(logs).contains("WARN: $SONAR_RUNNER_OPTS is deprecated. Please use $SONAR_SCANNER_OPTS instead.");
  219. }
  220. assertThat(logs).contains("java.lang.OutOfMemoryError");
  221. }
  222. @Test
  223. public void use_new_script_and_new_env_variable() {
  224. SonarScanner build = newScanner(new File("projects/java-sample"))
  225. .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx2m");
  226. BuildResult executeBuild = orchestrator.executeBuildQuietly(build);
  227. assertThat(executeBuild.getStatus()).isNotEqualTo(0);
  228. String logs = executeBuild.getLogs();
  229. assertThat(logs).doesNotContain("sonar-runner");
  230. assertThat(logs).doesNotContain("SONAR_RUNNER_OPTS");
  231. assertThat(logs).contains("java.lang.OutOfMemoryError");
  232. }
  233. }