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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * SonarSource :: IT :: SonarQube Scanner
  3. * Copyright (C) 2009 SonarSource
  4. * sonarqube@googlegroups.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
  17. * License along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package com.sonar.runner.it;
  21. import com.sonar.orchestrator.build.BuildResult;
  22. import com.sonar.orchestrator.build.SonarRunner;
  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.junit.After;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.TemporaryFolder;
  31. import org.sonar.wsclient.issue.Issue;
  32. import org.sonar.wsclient.issue.IssueQuery;
  33. import org.sonar.wsclient.services.Resource;
  34. import org.sonar.wsclient.services.ResourceQuery;
  35. import static org.fest.assertions.Assertions.assertThat;
  36. import static org.junit.Assume.assumeTrue;
  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. SonarRunner 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. if (!orchestrator.getServer().version().isGreaterThanOrEquals("4.2")) {
  65. assertThat(project.getLanguage()).isEqualTo("java");
  66. }
  67. assertThat(project.getMeasureIntValue("files")).isEqualTo(2);
  68. assertThat(project.getMeasureIntValue("classes")).isEqualTo(2);
  69. assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(10);
  70. assertThat(project.getMeasureIntValue("lcom4")).isNull(); // no bytecode
  71. if (orchestrator.getServer().version().isGreaterThanOrEquals("3.7")) {
  72. // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0
  73. assertThat(project.getMeasureIntValue("violations")).isGreaterThan(0);
  74. }
  75. Resource file = orchestrator.getServer().getWsClient()
  76. .find(new ResourceQuery(helloFileKey()).setMetrics("files", "ncloc", "classes", "lcom4", "violations"));
  77. if (orchestrator.getServer().version().isGreaterThanOrEquals("4.2")) {
  78. assertThat(file.getName()).isEqualTo("Hello.java");
  79. } else {
  80. assertThat(file.getName()).isEqualTo("Hello");
  81. assertThat(file.getMeasureIntValue("lcom4")).isNull(); // no bytecode
  82. }
  83. assertThat(file.getMeasureIntValue("ncloc")).isEqualTo(7);
  84. if (orchestrator.getServer().version().isGreaterThanOrEquals("3.7")) {
  85. // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0
  86. assertThat(file.getMeasureIntValue("violations")).isGreaterThan(0);
  87. }
  88. }
  89. @Test
  90. public void scan_java_sources_and_bytecode() {
  91. orchestrator.getServer().restoreProfile(ResourceLocation.create("/requires-bytecode-profile.xml"));
  92. orchestrator.getServer().provisionProject("java:bytecode", "Java Bytecode Sample");
  93. orchestrator.getServer().associateProjectToQualityProfile("java:bytecode", "java", "requires-bytecode");
  94. SonarRunner build = newScanner(new File("projects/java-bytecode"));
  95. orchestrator.executeBuild(build);
  96. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:bytecode").setMetrics("lcom4", "violations"));
  97. assertThat(project.getName()).isEqualTo("Java Bytecode Sample");
  98. if (!orchestrator.getServer().version().isGreaterThanOrEquals("4.1")) {
  99. // SONAR-4853 LCOM4 is no more computed on SQ 4.1
  100. assertThat(project.getMeasureIntValue("lcom4")).isGreaterThanOrEqualTo(1);
  101. }
  102. // the squid rules enabled in sonar-way-profile do not exist in SQ 3.0
  103. assertThat(project.getMeasureIntValue("violations")).isGreaterThan(0);
  104. Resource file = orchestrator.getServer().getWsClient().find(new ResourceQuery(findbugsFileKey()).setMetrics("lcom4", "violations"));
  105. assertThat(file.getMeasureIntValue("violations")).isGreaterThan(0);
  106. // findbugs is executed on bytecode
  107. List<Issue> issues = orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("java:bytecode").rules("findbugs:DM_EXIT")).list();
  108. assertThat(issues).hasSize(1);
  109. assertThat(issues.get(0).ruleKey()).isEqualTo("findbugs:DM_EXIT");
  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. SonarRunner build = newScanner(new File("projects/basedir-with-source"));
  121. orchestrator.executeBuild(build);
  122. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:basedir-with-source").setMetrics("files", "ncloc"));
  123. assertThat(project.getMeasureIntValue("files")).isEqualTo(1);
  124. assertThat(project.getMeasureIntValue("ncloc")).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. SonarRunner build = newScanner(new File("projects/java-sample"))
  135. .setProjectKey("SAMPLE");
  136. orchestrator.executeBuild(build);
  137. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("SAMPLE").setMetrics("files", "ncloc"));
  138. assertThat(project.getMeasureIntValue("files")).isEqualTo(2);
  139. assertThat(project.getMeasureIntValue("ncloc")).isGreaterThan(1);
  140. }
  141. /**
  142. * SONARPLUGINS-1230
  143. */
  144. @Test
  145. public void should_override_working_dir_with_relative_path() {
  146. SonarRunner 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. SonarRunner 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. SonarRunner 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("The folder 'bad' does not exist for 'bad-source-dirs'");
  174. }
  175. /**
  176. * SONARPLUGINS-2203
  177. */
  178. @Test
  179. public void should_log_message_when_deprecated_properties_are_used() {
  180. assumeTrue(!orchestrator.getServer().version().isGreaterThanOrEquals("4.3"));
  181. SonarRunner build = newScanner(new File("projects/using-deprecated-props"));
  182. BuildResult result = orchestrator.executeBuild(build);
  183. String logs = result.getLogs();
  184. assertThat(logs).contains("/!\\ The 'sources' property is deprecated and is replaced by 'sonar.sources'. Don't forget to update your files.");
  185. assertThat(logs).contains("/!\\ The 'tests' property is deprecated and is replaced by 'sonar.tests'. Don't forget to update your files.");
  186. assertThat(logs).contains("/!\\ The 'binaries' property is deprecated and is replaced by 'sonar.binaries'. Don't forget to update your files.");
  187. assertThat(logs).contains("/!\\ The 'libraries' property is deprecated and is replaced by 'sonar.libraries'. Don't forget to update your files.");
  188. }
  189. /**
  190. * SONARPLUGINS-2256
  191. */
  192. @Test
  193. public void should_warn_when_analysis_is_platform_dependent() {
  194. SonarRunner build = newScanner(new File("projects/java-sample"))
  195. // ORCH-243
  196. .setSourceEncoding("");
  197. String log = orchestrator.executeBuild(build).getLogs();
  198. // 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
  199. // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set).
  200. assertThat(log).contains("Default locale:");
  201. assertThat(log).contains(", source code encoding:");
  202. assertThat(log).contains("(analysis is platform dependent)");
  203. }
  204. @Test
  205. public void should_fail_if_unable_to_connect() {
  206. SonarRunner build = newScanner(new File("projects/java-sample"))
  207. .setProperty("sonar.host.url", "http://foo");
  208. BuildResult result = orchestrator.executeBuildQuietly(build);
  209. // expect build failure
  210. assertThat(result.getStatus()).isNotEqualTo(0);
  211. // with the following message
  212. assertThat(result.getLogs()).contains("SonarQube server [http://foo] can not be reached");
  213. }
  214. // SONARPLUGINS-3574
  215. @Test
  216. public void run_from_external_location() throws IOException {
  217. File tempDir = temp.newFolder();
  218. SonarRunner build = newScanner(tempDir)
  219. .setProperty("sonar.projectBaseDir", new File("projects/java-sample").getAbsolutePath())
  220. .addArguments("-e");
  221. orchestrator.executeBuild(build);
  222. Resource project = orchestrator.getServer().getWsClient().find(new ResourceQuery("java:sample").setMetrics("files", "ncloc", "classes", "lcom4", "violations"));
  223. assertThat(project.getDescription()).isEqualTo("This is a Java sample");
  224. assertThat(project.getVersion()).isEqualTo("1.2.3");
  225. }
  226. private String findbugsFileKey() {
  227. if (orchestrator.getServer().version().isGreaterThanOrEquals("4.2")) {
  228. return "java:bytecode:src/HasFindbugsViolation.java";
  229. } else {
  230. return "java:bytecode:[default].HasFindbugsViolation";
  231. }
  232. }
  233. private String helloFileKey() {
  234. if (orchestrator.getServer().version().isGreaterThanOrEquals("4.2")) {
  235. return "java:sample:src/basic/Hello.java";
  236. } else {
  237. return "java:sample:basic.Hello";
  238. }
  239. }
  240. }