Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SonarProjectBuilderTest.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Sonar Runner - Implementation
  3. * Copyright (C) 2011 SonarSource
  4. * dev@sonar.codehaus.org
  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 org.sonar.runner.internal.batch;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  25. import org.sonar.runner.RunnerException;
  26. import org.sonar.test.TestUtils;
  27. import java.io.File;
  28. import java.io.FileNotFoundException;
  29. import java.io.IOException;
  30. import java.util.List;
  31. import java.util.Properties;
  32. import static org.fest.assertions.Assertions.assertThat;
  33. public class SonarProjectBuilderTest {
  34. @Rule
  35. public ExpectedException thrown = ExpectedException.none();
  36. @Test
  37. public void shouldDefineSimpleProject() throws IOException {
  38. ProjectDefinition projectDefinition = loadProjectDefinition("simple-project");
  39. assertThat(projectDefinition.getKey()).isEqualTo("com.foo.project");
  40. assertThat(projectDefinition.getName()).isEqualTo("Foo Project");
  41. assertThat(projectDefinition.getVersion()).isEqualTo("1.0-SNAPSHOT");
  42. assertThat(projectDefinition.getDescription()).isEqualTo("Description of Foo Project");
  43. assertThat(projectDefinition.getSourceDirs()).contains("sources");
  44. assertThat(projectDefinition.getTestDirs()).contains("tests");
  45. assertThat(projectDefinition.getBinaries()).contains("target/classes");
  46. assertThat(projectDefinition.getLibraries()).contains(TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath(),
  47. TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath());
  48. }
  49. @Test
  50. public void shouldDefineSimpleProjectWithDeprecatedProperties() throws IOException {
  51. ProjectDefinition projectDefinition = loadProjectDefinition("simple-project-with-deprecated-props");
  52. assertThat(projectDefinition.getSourceDirs()).contains("sources");
  53. assertThat(projectDefinition.getTestDirs()).contains("tests");
  54. assertThat(projectDefinition.getBinaries()).contains("target/classes");
  55. assertThat(projectDefinition.getLibraries()).contains(
  56. TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath(),
  57. TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath());
  58. }
  59. @Test
  60. public void shouldFailIfUnexistingSourceDirectory() throws IOException {
  61. thrown.expect(RunnerException.class);
  62. thrown.expectMessage("The folder 'unexisting-source-dir' does not exist for 'com.foo.project' (base directory = "
  63. + TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-source-dir") + ")");
  64. loadProjectDefinition("simple-project-with-unexisting-source-dir");
  65. }
  66. @Test
  67. public void shouldDefineMultiModuleProjectWithDefinitionsAllInRootProject() throws IOException {
  68. ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-all-in-root");
  69. // CHECK ROOT
  70. assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
  71. assertThat(rootProject.getName()).isEqualTo("Foo Project");
  72. assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
  73. assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
  74. // root project must not contain some properties - even if they are defined in the root properties file
  75. assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
  76. assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
  77. assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
  78. // and module properties must have been cleaned
  79. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  80. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  81. // Check baseDir and workDir
  82. assertThat(rootProject.getBaseDir().getCanonicalFile())
  83. .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"));
  84. assertThat(rootProject.getWorkDir().getCanonicalFile())
  85. .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar"));
  86. // CHECK MODULES
  87. List<ProjectDefinition> modules = rootProject.getSubProjects();
  88. assertThat(modules.size()).isEqualTo(2);
  89. // Module 1
  90. ProjectDefinition module1 = modules.get(0);
  91. assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1"));
  92. assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
  93. assertThat(module1.getName()).isEqualTo("module1");
  94. assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
  95. // Description should not be inherited from parent if not set
  96. assertThat(module1.getDescription()).isNull();
  97. assertThat(module1.getSourceDirs()).contains("sources");
  98. assertThat(module1.getTestDirs()).contains("tests");
  99. assertThat(module1.getBinaries()).contains("target/classes");
  100. // and module properties must have been cleaned
  101. assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  102. assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  103. // Check baseDir and workDir
  104. assertThat(module1.getBaseDir().getCanonicalFile())
  105. .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1"));
  106. assertThat(module1.getWorkDir().getCanonicalFile())
  107. .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_module1"));
  108. // Module 2
  109. ProjectDefinition module2 = modules.get(1);
  110. assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2"));
  111. assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
  112. assertThat(module2.getName()).isEqualTo("Foo Module 2");
  113. assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
  114. assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
  115. assertThat(module2.getSourceDirs()).contains("src");
  116. assertThat(module2.getTestDirs()).contains("tests");
  117. assertThat(module2.getBinaries()).contains("target/classes");
  118. // and module properties must have been cleaned
  119. assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  120. assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  121. // Check baseDir and workDir
  122. assertThat(module2.getBaseDir().getCanonicalFile())
  123. .isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2"));
  124. assertThat(module2.getWorkDir().getCanonicalFile())
  125. .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root"), ".sonar/com.foo.project_com.foo.project.module2"));
  126. }
  127. @Test
  128. public void shouldDefineMultiModuleProjectWithDefinitionsAllInEachModule() throws IOException {
  129. ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module");
  130. // CHECK ROOT
  131. assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
  132. assertThat(rootProject.getName()).isEqualTo("Foo Project");
  133. assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
  134. assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
  135. // root project must not contain some properties - even if they are defined in the root properties file
  136. assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
  137. assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
  138. assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
  139. // and module properties must have been cleaned
  140. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  141. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  142. // CHECK MODULES
  143. List<ProjectDefinition> modules = rootProject.getSubProjects();
  144. assertThat(modules.size()).isEqualTo(2);
  145. // Module 1
  146. ProjectDefinition module1 = modules.get(0);
  147. assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module1"));
  148. assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  149. assertThat(module1.getName()).isEqualTo("Foo Module 1");
  150. assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
  151. // Description should not be inherited from parent if not set
  152. assertThat(module1.getDescription()).isEqualTo("Description of Module 1");
  153. assertThat(module1.getSourceDirs()).contains("sources");
  154. assertThat(module1.getTestDirs()).contains("tests");
  155. assertThat(module1.getBinaries()).contains("target/classes");
  156. // and module properties must have been cleaned
  157. assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  158. assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  159. // Module 2
  160. ProjectDefinition module2 = modules.get(1);
  161. assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module2/newBaseDir"));
  162. assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
  163. assertThat(module2.getName()).isEqualTo("Foo Module 2");
  164. assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
  165. assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
  166. assertThat(module2.getSourceDirs()).contains("src");
  167. assertThat(module2.getTestDirs()).contains("tests");
  168. assertThat(module2.getBinaries()).contains("target/classes");
  169. // and module properties must have been cleaned
  170. assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  171. assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  172. }
  173. @Test
  174. public void shouldDefineMultiModuleProjectWithDefinitionsModule1Inherited() throws IOException {
  175. ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module-inherited");
  176. // CHECK ROOT
  177. assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
  178. assertThat(rootProject.getName()).isEqualTo("Foo Project");
  179. assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
  180. assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
  181. // root project must not contain some properties - even if they are defined in the root properties file
  182. assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
  183. assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
  184. assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
  185. // and module properties must have been cleaned
  186. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  187. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  188. // CHECK MODULES
  189. List<ProjectDefinition> modules = rootProject.getSubProjects();
  190. assertThat(modules.size()).isEqualTo(2);
  191. // Module 1
  192. ProjectDefinition module1 = modules.get(0);
  193. assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module1"));
  194. assertThat(module1.getKey()).isEqualTo("com.foo.project:module1");
  195. assertThat(module1.getName()).isEqualTo("module1");
  196. assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
  197. // Description should not be inherited from parent if not set
  198. assertThat(module1.getDescription()).isNull();
  199. assertThat(module1.getSourceDirs()).contains("sources");
  200. assertThat(module1.getTestDirs()).contains("tests");
  201. assertThat(module1.getBinaries()).contains("target/classes");
  202. // and module properties must have been cleaned
  203. assertThat(module1.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  204. assertThat(module1.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  205. // Module 2
  206. ProjectDefinition module2 = modules.get(1);
  207. assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module-inherited/module2/newBaseDir"));
  208. assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
  209. assertThat(module2.getName()).isEqualTo("Foo Module 2");
  210. assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
  211. assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
  212. assertThat(module2.getSourceDirs()).contains("src");
  213. assertThat(module2.getTestDirs()).contains("tests");
  214. assertThat(module2.getBinaries()).contains("target/classes");
  215. // and module properties must have been cleaned
  216. assertThat(module2.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  217. assertThat(module2.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  218. }
  219. // SONARPLUGINS-2421
  220. @Test
  221. public void shouldDefineMultiLanguageProjectWithDefinitionsAllInRootProject() throws IOException {
  222. ProjectDefinition rootProject = loadProjectDefinition("multi-language-definitions-all-in-root");
  223. // CHECK ROOT
  224. assertThat(rootProject.getKey()).isEqualTo("example");
  225. assertThat(rootProject.getName()).isEqualTo("Example");
  226. assertThat(rootProject.getVersion()).isEqualTo("1.0");
  227. // CHECK MODULES
  228. List<ProjectDefinition> modules = rootProject.getSubProjects();
  229. assertThat(modules.size()).isEqualTo(2);
  230. // Module 1
  231. ProjectDefinition module1 = modules.get(0);
  232. assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"));
  233. assertThat(module1.getSourceDirs()).contains("src/main/java");
  234. // and module properties must have been cleaned
  235. assertThat(module1.getWorkDir().getCanonicalFile())
  236. .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_java-module"));
  237. // Module 2
  238. ProjectDefinition module2 = modules.get(1);
  239. assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"));
  240. assertThat(module2.getSourceDirs()).contains("src/main/groovy");
  241. // and module properties must have been cleaned
  242. assertThat(module2.getWorkDir().getCanonicalFile())
  243. .isEqualTo(new File(TestUtils.getResource(this.getClass(), "multi-language-definitions-all-in-root"), ".sonar/example_groovy-module"));
  244. }
  245. @Test
  246. public void shouldDefineMultiModuleProjectWithBaseDir() throws IOException {
  247. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-basedir");
  248. List<ProjectDefinition> modules = rootProject.getSubProjects();
  249. assertThat(modules.size()).isEqualTo(1);
  250. assertThat(modules.get(0).getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  251. }
  252. @Test
  253. public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
  254. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
  255. List<ProjectDefinition> modules = rootProject.getSubProjects();
  256. assertThat(modules.size()).isEqualTo(1);
  257. ProjectDefinition module = modules.get(0);
  258. assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  259. // verify the base directory that has been changed in this config file
  260. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
  261. }
  262. @Test
  263. public void shouldDefineMultiModuleProjectWithConfigFileAndOverwrittenBasedir() throws IOException {
  264. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile-and-overwritten-basedir");
  265. List<ProjectDefinition> modules = rootProject.getSubProjects();
  266. assertThat(modules.size()).isEqualTo(1);
  267. ProjectDefinition module = modules.get(0);
  268. assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  269. // verify the base directory that has been changed in this config file
  270. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile-and-overwritten-basedir/any-folder"));
  271. }
  272. @Test
  273. public void shouldFailIfUnexistingModuleBaseDir() throws IOException {
  274. thrown.expect(RunnerException.class);
  275. thrown.expectMessage("The base directory of the module 'module1' does not exist: "
  276. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1");
  277. loadProjectDefinition("multi-module-with-unexisting-basedir");
  278. }
  279. @Test
  280. public void shouldFailIfUnexistingModuleFile() throws IOException {
  281. thrown.expect(RunnerException.class);
  282. thrown.expectMessage("The properties file of the module 'module1' does not exist: "
  283. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
  284. + File.separator + "any-file.properties");
  285. loadProjectDefinition("multi-module-with-unexisting-file");
  286. }
  287. @Test
  288. public void shouldFailIfUnexistingSourceFolderInheritedInMultimodule() throws IOException {
  289. thrown.expect(RunnerException.class);
  290. thrown.expectMessage("The folder 'unexisting-source-dir' does not exist for 'com.foo.project:module1' (base directory = "
  291. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-source-dir").getAbsolutePath() + File.separator + "module1)");
  292. loadProjectDefinition("multi-module-with-unexisting-source-dir");
  293. }
  294. @Test
  295. public void shouldNotFailIfUnexistingTestBinLibFolderInheritedInMultimodule() throws IOException {
  296. loadProjectDefinition("multi-module-with-unexisting-test-bin-lib-dir");
  297. }
  298. @Test
  299. public void shouldFailIfExplicitUnexistingTestFolder() throws IOException {
  300. thrown.expect(RunnerException.class);
  301. thrown.expectMessage("The folder 'tests' does not exist for 'module1' (base directory = "
  302. + TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-test-dir").getAbsolutePath() + File.separator + "module1)");
  303. loadProjectDefinition("multi-module-with-explicit-unexisting-test-dir");
  304. }
  305. @Test
  306. public void shouldFailIfExplicitUnexistingBinaryFolder() throws IOException {
  307. thrown.expect(RunnerException.class);
  308. thrown.expectMessage("The folder 'bin' does not exist for 'module1' (base directory = "
  309. + TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-binary-dir").getAbsolutePath() + File.separator + "module1)");
  310. loadProjectDefinition("multi-module-with-explicit-unexisting-binary-dir");
  311. }
  312. @Test
  313. public void shouldFailIfExplicitUnmatchingLibFolder() throws IOException {
  314. thrown.expect(RunnerException.class);
  315. thrown.expectMessage("No files matching pattern \"lib/*.jar\" in directory \""
  316. + TestUtils.getResource(this.getClass(), "multi-module-with-explicit-unexisting-lib").getAbsolutePath() + File.separator + "module1\"");
  317. loadProjectDefinition("multi-module-with-explicit-unexisting-lib");
  318. }
  319. @Test
  320. public void shouldExtractModuleProperties() {
  321. Properties props = new Properties();
  322. props.setProperty("sources", "src/main/java");
  323. props.setProperty("tests", "src/test/java");
  324. props.setProperty("foo.sources", "src/main/java");
  325. props.setProperty("foobar.tests", "src/test/java");
  326. props.setProperty("foobar.binaries", "target/classes");
  327. Properties moduleProps = SonarProjectBuilder.extractModuleProperties("bar", props);
  328. assertThat(moduleProps.size()).isEqualTo(0);
  329. moduleProps = SonarProjectBuilder.extractModuleProperties("foo", props);
  330. assertThat(moduleProps.size()).isEqualTo(1);
  331. assertThat(moduleProps.get("sources")).isEqualTo("src/main/java");
  332. moduleProps = SonarProjectBuilder.extractModuleProperties("foobar", props);
  333. assertThat(moduleProps.size()).isEqualTo(2);
  334. assertThat(moduleProps.get("tests")).isEqualTo("src/test/java");
  335. assertThat(moduleProps.get("binaries")).isEqualTo("target/classes");
  336. }
  337. @Test
  338. public void shouldFailIfMandatoryPropertiesAreNotPresent() {
  339. Properties props = new Properties();
  340. props.setProperty("foo1", "bla");
  341. props.setProperty("foo4", "bla");
  342. thrown.expect(RunnerException.class);
  343. thrown.expectMessage("You must define the following mandatory properties for 'Unknown': foo2, foo3");
  344. SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
  345. }
  346. @Test
  347. public void shouldFailIfMandatoryPropertiesAreNotPresentButWithProjectKey() {
  348. Properties props = new Properties();
  349. props.setProperty("foo1", "bla");
  350. props.setProperty("sonar.projectKey", "my-project");
  351. thrown.expect(RunnerException.class);
  352. thrown.expectMessage("You must define the following mandatory properties for 'my-project': foo2, foo3");
  353. SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
  354. }
  355. @Test
  356. public void shouldNotFailIfMandatoryPropertiesArePresent() {
  357. Properties props = new Properties();
  358. props.setProperty("foo1", "bla");
  359. props.setProperty("foo4", "bla");
  360. SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1"});
  361. // No exception should be thrown
  362. }
  363. @Test
  364. public void shouldFilterFiles() throws Exception {
  365. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  366. assertThat(SonarProjectBuilder.getLibraries(baseDir, "in*.txt").length).isEqualTo(1);
  367. assertThat(SonarProjectBuilder.getLibraries(baseDir, "*.txt").length).isEqualTo(2);
  368. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt").length).isEqualTo(1);
  369. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt").length).isEqualTo(2);
  370. }
  371. @Test
  372. public void shouldWorkWithAbsolutePath() throws Exception {
  373. File baseDir = new File("not-exists");
  374. String absolutePattern = TestUtils.getResource(this.getClass(), "shouldFilterFiles").getAbsolutePath() + "/in*.txt";
  375. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), absolutePattern).length).isEqualTo(1);
  376. }
  377. @Test
  378. public void shouldGetRelativeFile() {
  379. assertThat(SonarProjectBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/")))
  380. .isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"));
  381. }
  382. @Test
  383. public void shouldGetAbsoluteFile() {
  384. File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties");
  385. assertThat(SonarProjectBuilder.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/")))
  386. .isEqualTo(file);
  387. }
  388. @Test
  389. public void shouldMergeParentProperties() {
  390. Properties parentProps = new Properties();
  391. parentProps.setProperty("toBeMergeProps", "fooParent");
  392. parentProps.setProperty("existingChildProp", "barParent");
  393. parentProps.setProperty("sonar.modules", "mod1,mod2");
  394. parentProps.setProperty("sonar.projectDescription", "Desc from Parent");
  395. parentProps.setProperty("mod1.sonar.projectDescription", "Desc for Mod1");
  396. parentProps.setProperty("mod2.sonar.projectkey", "Key for Mod2");
  397. Properties childProps = new Properties();
  398. childProps.setProperty("existingChildProp", "barChild");
  399. childProps.setProperty("otherProp", "tutuChild");
  400. SonarProjectBuilder.mergeParentProperties(childProps, parentProps);
  401. assertThat(childProps.size()).isEqualTo(3);
  402. assertThat(childProps.getProperty("toBeMergeProps")).isEqualTo("fooParent");
  403. assertThat(childProps.getProperty("existingChildProp")).isEqualTo("barChild");
  404. assertThat(childProps.getProperty("otherProp")).isEqualTo("tutuChild");
  405. assertThat(childProps.getProperty("sonar.modules")).isNull();
  406. assertThat(childProps.getProperty("sonar.projectDescription")).isNull();
  407. assertThat(childProps.getProperty("mod1.sonar.projectDescription")).isNull();
  408. assertThat(childProps.getProperty("mod2.sonar.projectkey")).isNull();
  409. }
  410. @Test
  411. public void shouldInitRootWorkDir() {
  412. SonarProjectBuilder builder = SonarProjectBuilder.create(new Properties());
  413. File baseDir = new File("target/tmp/baseDir");
  414. File workDir = builder.initRootProjectWorkDir(baseDir);
  415. assertThat(workDir).isEqualTo(new File(baseDir, ".sonar"));
  416. }
  417. @Test
  418. public void shouldInitWorkDirWithCustomRelativeFolder() {
  419. Properties properties = new Properties();
  420. properties.put("sonar.working.directory", ".foo");
  421. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  422. File baseDir = new File("target/tmp/baseDir");
  423. File workDir = builder.initRootProjectWorkDir(baseDir);
  424. assertThat(workDir).isEqualTo(new File(baseDir, ".foo"));
  425. }
  426. @Test
  427. public void shouldInitRootWorkDirWithCustomAbsoluteFolder() {
  428. Properties properties = new Properties();
  429. properties.put("sonar.working.directory", new File("src").getAbsolutePath());
  430. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  431. File baseDir = new File("target/tmp/baseDir");
  432. File workDir = builder.initRootProjectWorkDir(baseDir);
  433. assertThat(workDir).isEqualTo(new File("src").getAbsoluteFile());
  434. }
  435. @Test
  436. public void shouldReturnPrefixedKey() {
  437. Properties props = new Properties();
  438. props.put("sonar.projectKey", "my-module-key");
  439. SonarProjectBuilder.prefixProjectKeyWithParentKey(props, "my-parent-key");
  440. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("my-parent-key:my-module-key");
  441. }
  442. @Test
  443. public void shouldFailIf2ModulesWithSameKey() {
  444. Properties props = new Properties();
  445. props.put("sonar.projectKey", "root");
  446. ProjectDefinition root = ProjectDefinition.create().setProperties(props);
  447. Properties props1 = new Properties();
  448. props1.put("sonar.projectKey", "mod1");
  449. root.addSubProject(ProjectDefinition.create().setProperties(props1));
  450. // Check uniqueness of a new module: OK
  451. Properties props2 = new Properties();
  452. props2.put("sonar.projectKey", "mod2");
  453. ProjectDefinition mod2 = ProjectDefinition.create().setProperties(props2);
  454. SonarProjectBuilder.checkUniquenessOfChildKey(mod2, root);
  455. // Now, add it and check again
  456. root.addSubProject(mod2);
  457. thrown.expect(RunnerException.class);
  458. thrown.expectMessage("Project 'root' can't have 2 modules with the following key: mod2");
  459. SonarProjectBuilder.checkUniquenessOfChildKey(mod2, root);
  460. }
  461. @Test
  462. public void shouldSetProjectKeyIfNotPresent() {
  463. Properties props = new Properties();
  464. props.put("sonar.projectVersion", "1.0");
  465. // should be set
  466. SonarProjectBuilder.setProjectKeyAndNameIfNotDefined(props, "foo");
  467. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  468. assertThat(props.getProperty("sonar.projectName")).isEqualTo("foo");
  469. // but not this 2nd time
  470. SonarProjectBuilder.setProjectKeyAndNameIfNotDefined(props, "bar");
  471. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  472. assertThat(props.getProperty("sonar.projectName")).isEqualTo("foo");
  473. }
  474. @Test
  475. public void shouldFailToLoadPropertiesFile() throws Exception {
  476. thrown.expect(RunnerException.class);
  477. thrown.expectMessage("Impossible to read the property file");
  478. SonarProjectBuilder.toProperties(new File("foo.properties"));
  479. }
  480. private ProjectDefinition loadProjectDefinition(String projectFolder) throws FileNotFoundException, IOException {
  481. Properties props = SonarProjectBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
  482. props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
  483. ProjectDefinition projectDefinition = SonarProjectBuilder.create(props)
  484. .generateProjectDefinition();
  485. return projectDefinition;
  486. }
  487. }