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

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