Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JavaTest.java 13KB

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