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.

SonarProjectBuilderTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Sonar Standalone Runner
  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' 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 MODULES
  82. List<ProjectDefinition> modules = rootProject.getSubProjects();
  83. assertThat(modules.size()).isEqualTo(2);
  84. // Module 1
  85. ProjectDefinition module1 = modules.get(0);
  86. assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module1"));
  87. assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  88. assertThat(module1.getName()).isEqualTo("Foo Module 1");
  89. assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
  90. // Description should not be inherited from parent if not set
  91. assertThat(module1.getDescription()).isNull();
  92. assertThat(module1.getSourceDirs()).contains("sources");
  93. assertThat(module1.getTestDirs()).contains("tests");
  94. assertThat(module1.getBinaries()).contains("target/classes");
  95. // and module properties must have been cleaned
  96. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  97. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  98. // Module 2
  99. ProjectDefinition module2 = modules.get(1);
  100. assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-all-in-root/module2"));
  101. assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
  102. assertThat(module2.getName()).isEqualTo("Foo Module 2");
  103. assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
  104. assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
  105. assertThat(module2.getSourceDirs()).contains("src");
  106. assertThat(module2.getTestDirs()).contains("tests");
  107. assertThat(module2.getBinaries()).contains("target/classes");
  108. // and module properties must have been cleaned
  109. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  110. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  111. }
  112. @Test
  113. public void shouldDefineMultiModuleProjectWithDefinitionsAllInEachModule() throws IOException {
  114. ProjectDefinition rootProject = loadProjectDefinition("multi-module-definitions-in-each-module");
  115. // CHECK ROOT
  116. assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
  117. assertThat(rootProject.getName()).isEqualTo("Foo Project");
  118. assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
  119. assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
  120. // root project must not contain some properties - even if they are defined in the root properties file
  121. assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
  122. assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
  123. assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
  124. // and module properties must have been cleaned
  125. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  126. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  127. // CHECK MODULES
  128. List<ProjectDefinition> modules = rootProject.getSubProjects();
  129. assertThat(modules.size()).isEqualTo(2);
  130. // Module 1
  131. ProjectDefinition module1 = modules.get(0);
  132. assertThat(module1.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module1"));
  133. assertThat(module1.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  134. assertThat(module1.getName()).isEqualTo("Foo Module 1");
  135. assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
  136. // Description should not be inherited from parent if not set
  137. assertThat(module1.getDescription()).isNull();
  138. assertThat(module1.getSourceDirs()).contains("sources");
  139. assertThat(module1.getTestDirs()).contains("tests");
  140. assertThat(module1.getBinaries()).contains("target/classes");
  141. // and module properties must have been cleaned
  142. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  143. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  144. // Module 2
  145. ProjectDefinition module2 = modules.get(1);
  146. assertThat(module2.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-definitions-in-each-module/module2/newBaseDir"));
  147. assertThat(module2.getKey()).isEqualTo("com.foo.project:com.foo.project.module2");
  148. assertThat(module2.getName()).isEqualTo("Foo Module 2");
  149. assertThat(module2.getVersion()).isEqualTo("1.0-SNAPSHOT");
  150. assertThat(module2.getDescription()).isEqualTo("Description of Module 2");
  151. assertThat(module2.getSourceDirs()).contains("src");
  152. assertThat(module2.getTestDirs()).contains("tests");
  153. assertThat(module2.getBinaries()).contains("target/classes");
  154. // and module properties must have been cleaned
  155. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  156. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  157. }
  158. @Test
  159. public void shouldDefineMultiModuleProjectWithBaseDir() throws IOException {
  160. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-basedir");
  161. List<ProjectDefinition> modules = rootProject.getSubProjects();
  162. assertThat(modules.size()).isEqualTo(1);
  163. assertThat(modules.get(0).getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  164. }
  165. @Test
  166. public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
  167. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
  168. List<ProjectDefinition> modules = rootProject.getSubProjects();
  169. assertThat(modules.size()).isEqualTo(1);
  170. ProjectDefinition module = modules.get(0);
  171. assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  172. // verify the base directory that has been changed in this config file
  173. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
  174. }
  175. @Test
  176. public void shouldDefineMultiModuleProjectWithConfigFileAndOverwrittenBasedir() throws IOException {
  177. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile-and-overwritten-basedir");
  178. List<ProjectDefinition> modules = rootProject.getSubProjects();
  179. assertThat(modules.size()).isEqualTo(1);
  180. ProjectDefinition module = modules.get(0);
  181. assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  182. // verify the base directory that has been changed in this config file
  183. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile-and-overwritten-basedir/any-folder"));
  184. }
  185. @Test
  186. public void shouldFailIfUnexistingModuleBaseDir() throws IOException {
  187. thrown.expect(RunnerException.class);
  188. thrown.expectMessage("The base directory of the module 'module1' does not exist: "
  189. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1");
  190. loadProjectDefinition("multi-module-with-unexisting-basedir");
  191. }
  192. @Test
  193. public void shouldFailIfUnexistingModuleFile() throws IOException {
  194. thrown.expect(RunnerException.class);
  195. thrown.expectMessage("The properties file of the module 'module1' does not exist: "
  196. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
  197. + File.separator + "any-file.properties");
  198. loadProjectDefinition("multi-module-with-unexisting-file");
  199. }
  200. @Test
  201. public void shouldExtractModuleProperties() {
  202. Properties props = new Properties();
  203. props.setProperty("sources", "src/main/java");
  204. props.setProperty("tests", "src/test/java");
  205. props.setProperty("foo.sources", "src/main/java");
  206. props.setProperty("foobar.tests", "src/test/java");
  207. props.setProperty("foobar.binaries", "target/classes");
  208. Properties moduleProps = SonarProjectBuilder.extractModuleProperties("bar", props);
  209. assertThat(moduleProps.size()).isEqualTo(0);
  210. moduleProps = SonarProjectBuilder.extractModuleProperties("foo", props);
  211. assertThat(moduleProps.size()).isEqualTo(1);
  212. assertThat(moduleProps.get("sources")).isEqualTo("src/main/java");
  213. moduleProps = SonarProjectBuilder.extractModuleProperties("foobar", props);
  214. assertThat(moduleProps.size()).isEqualTo(2);
  215. assertThat(moduleProps.get("tests")).isEqualTo("src/test/java");
  216. assertThat(moduleProps.get("binaries")).isEqualTo("target/classes");
  217. }
  218. @Test
  219. public void shouldFailIfMandatoryPropertiesAreNotPresent() {
  220. Properties props = new Properties();
  221. props.setProperty("foo1", "bla");
  222. props.setProperty("foo4", "bla");
  223. thrown.expect(RunnerException.class);
  224. thrown.expectMessage("You must define the following mandatory properties for 'foo': foo2, foo3");
  225. SonarProjectBuilder.checkMandatoryProperties("foo", props, new String[] {"foo1", "foo2", "foo3"});
  226. }
  227. @Test
  228. public void shouldNotFailIfMandatoryPropertiesArePresent() {
  229. Properties props = new Properties();
  230. props.setProperty("foo1", "bla");
  231. props.setProperty("foo4", "bla");
  232. SonarProjectBuilder.checkMandatoryProperties("foo", props, new String[] {"foo1"});
  233. // No exception should be thrown
  234. }
  235. @Test
  236. public void shouldFilterFiles() throws Exception {
  237. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  238. assertThat(SonarProjectBuilder.getLibraries(baseDir, "in*.txt").length).isEqualTo(1);
  239. assertThat(SonarProjectBuilder.getLibraries(baseDir, "*.txt").length).isEqualTo(2);
  240. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt").length).isEqualTo(1);
  241. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt").length).isEqualTo(2);
  242. }
  243. @Test
  244. public void shouldWorkWithAbsolutePath() throws Exception {
  245. File baseDir = new File("not-exists");
  246. String absolutePattern = TestUtils.getResource(this.getClass(), "shouldFilterFiles").getAbsolutePath() + "/in*.txt";
  247. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), absolutePattern).length).isEqualTo(1);
  248. }
  249. @Test
  250. public void shouldThrowExceptionWhenNoFilesMatchingPattern() throws Exception {
  251. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  252. thrown.expect(RunnerException.class);
  253. thrown.expectMessage("No files matching pattern \"*.jar\" in directory");
  254. SonarProjectBuilder.getLibraries(baseDir, "*.jar");
  255. }
  256. @Test
  257. public void shouldGetRelativeFile() {
  258. assertThat(SonarProjectBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/")))
  259. .isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"));
  260. }
  261. @Test
  262. public void shouldGetAbsoluteFile() {
  263. File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties");
  264. assertThat(SonarProjectBuilder.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/")))
  265. .isEqualTo(file);
  266. }
  267. @Test
  268. public void shouldMergeParentProperties() {
  269. Properties parentProps = new Properties();
  270. parentProps.setProperty("toBeMergeProps", "fooParent");
  271. parentProps.setProperty("existingChildProp", "barParent");
  272. parentProps.setProperty("sonar.modules", "mod1,mod2");
  273. parentProps.setProperty("sonar.projectDescription", "Desc from Parent");
  274. parentProps.setProperty("mod1.sonar.projectDescription", "Desc for Mod1");
  275. parentProps.setProperty("mod2.sonar.projectkey", "Key for Mod2");
  276. Properties childProps = new Properties();
  277. childProps.setProperty("existingChildProp", "barChild");
  278. childProps.setProperty("otherProp", "tutuChild");
  279. SonarProjectBuilder.mergeParentProperties(childProps, parentProps);
  280. assertThat(childProps.size()).isEqualTo(3);
  281. assertThat(childProps.getProperty("toBeMergeProps")).isEqualTo("fooParent");
  282. assertThat(childProps.getProperty("existingChildProp")).isEqualTo("barChild");
  283. assertThat(childProps.getProperty("otherProp")).isEqualTo("tutuChild");
  284. assertThat(childProps.getProperty("sonar.modules")).isNull();
  285. assertThat(childProps.getProperty("sonar.projectDescription")).isNull();
  286. assertThat(childProps.getProperty("mod1.sonar.projectDescription")).isNull();
  287. assertThat(childProps.getProperty("mod2.sonar.projectkey")).isNull();
  288. }
  289. @Test
  290. public void shouldInitWorkDir() {
  291. SonarProjectBuilder builder = SonarProjectBuilder.create(new Properties());
  292. File baseDir = new File("target/tmp/baseDir");
  293. File workDir = builder.initWorkDir(baseDir);
  294. assertThat(workDir).isEqualTo(new File(baseDir, ".sonar"));
  295. }
  296. @Test
  297. public void shouldInitWorkDirWithCustomRelativeFolder() {
  298. Properties properties = new Properties();
  299. properties.put("sonar.working.directory", ".foo");
  300. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  301. File baseDir = new File("target/tmp/baseDir");
  302. File workDir = builder.initWorkDir(baseDir);
  303. assertThat(workDir).isEqualTo(new File(baseDir, ".foo"));
  304. }
  305. @Test
  306. public void shouldInitWorkDirWithCustomAbsoluteFolder() {
  307. Properties properties = new Properties();
  308. properties.put("sonar.working.directory", new File("src").getAbsolutePath());
  309. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  310. File baseDir = new File("target/tmp/baseDir");
  311. File workDir = builder.initWorkDir(baseDir);
  312. assertThat(workDir).isEqualTo(new File("src").getAbsoluteFile());
  313. }
  314. @Test
  315. public void shouldReturnPrefixedKey() {
  316. Properties props = new Properties();
  317. props.put("sonar.projectKey", "my-module-key");
  318. SonarProjectBuilder.prefixProjectKeyWithParentKey(props, "my-parent-key");
  319. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("my-parent-key:my-module-key");
  320. }
  321. @Test
  322. public void shouldFailIf2ModulesWithSameKey() {
  323. Properties props = new Properties();
  324. props.put("sonar.projectKey", "root");
  325. ProjectDefinition root = ProjectDefinition.create(props);
  326. Properties props1 = new Properties();
  327. props1.put("sonar.projectKey", "mod1");
  328. root.addSubProject(ProjectDefinition.create(props1));
  329. // Check unicity of a new module: OK
  330. Properties props2 = new Properties();
  331. props2.put("sonar.projectKey", "mod2");
  332. ProjectDefinition mod2 = ProjectDefinition.create(props2);
  333. SonarProjectBuilder.checkUnicityOfChildKey(mod2, root);
  334. // Now, add it and check again
  335. root.addSubProject(mod2);
  336. thrown.expect(RunnerException.class);
  337. thrown.expectMessage("Project 'root' can't have 2 modules with the following key: mod2");
  338. SonarProjectBuilder.checkUnicityOfChildKey(mod2, root);
  339. }
  340. @Test
  341. public void shouldSetProjectKeyIfNotPresent() {
  342. Properties props = new Properties();
  343. props.put("sonar.projectVersion", "1.0");
  344. // should be set
  345. SonarProjectBuilder.setProjectKeyIfNotDefined(props, "foo");
  346. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  347. // but not this 2nd time
  348. SonarProjectBuilder.setProjectKeyIfNotDefined(props, "bar");
  349. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  350. }
  351. @Test
  352. public void shouldFailToLoadPropertiesFile() throws Exception {
  353. thrown.expect(RunnerException.class);
  354. thrown.expectMessage("Impossible to read the property file");
  355. SonarProjectBuilder.toProperties(new File("foo.properties"));
  356. }
  357. private ProjectDefinition loadProjectDefinition(String projectFolder) throws FileNotFoundException, IOException {
  358. Properties props = SonarProjectBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
  359. props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
  360. ProjectDefinition projectDefinition = SonarProjectBuilder.create(props)
  361. .generateProjectDefinition();
  362. return projectDefinition;
  363. }
  364. }