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.

ProjectReactorBuilderTest.java 30KB

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