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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 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:module1");
  149. assertThat(module1.getName()).isEqualTo("module1");
  150. assertThat(module1.getVersion()).isEqualTo("1.0-SNAPSHOT");
  151. // Description should not be inherited from parent if not set
  152. assertThat(module1.getDescription()).isNull();
  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 shouldDefineMultiModuleProjectWithBaseDir() throws IOException {
  175. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-basedir");
  176. List<ProjectDefinition> modules = rootProject.getSubProjects();
  177. assertThat(modules.size()).isEqualTo(1);
  178. assertThat(modules.get(0).getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  179. }
  180. @Test
  181. public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
  182. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
  183. List<ProjectDefinition> modules = rootProject.getSubProjects();
  184. assertThat(modules.size()).isEqualTo(1);
  185. ProjectDefinition module = modules.get(0);
  186. assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  187. // verify the base directory that has been changed in this config file
  188. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
  189. }
  190. @Test
  191. public void shouldDefineMultiModuleProjectWithConfigFileAndOverwrittenBasedir() throws IOException {
  192. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile-and-overwritten-basedir");
  193. List<ProjectDefinition> modules = rootProject.getSubProjects();
  194. assertThat(modules.size()).isEqualTo(1);
  195. ProjectDefinition module = modules.get(0);
  196. assertThat(module.getKey()).isEqualTo("com.foo.project:com.foo.project.module1");
  197. // verify the base directory that has been changed in this config file
  198. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile-and-overwritten-basedir/any-folder"));
  199. }
  200. @Test
  201. public void shouldFailIfUnexistingModuleBaseDir() throws IOException {
  202. thrown.expect(RunnerException.class);
  203. thrown.expectMessage("The base directory of the module 'module1' does not exist: "
  204. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1");
  205. loadProjectDefinition("multi-module-with-unexisting-basedir");
  206. }
  207. @Test
  208. public void shouldFailIfUnexistingModuleFile() throws IOException {
  209. thrown.expect(RunnerException.class);
  210. thrown.expectMessage("The properties file of the module 'module1' does not exist: "
  211. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
  212. + File.separator + "any-file.properties");
  213. loadProjectDefinition("multi-module-with-unexisting-file");
  214. }
  215. @Test
  216. public void shouldExtractModuleProperties() {
  217. Properties props = new Properties();
  218. props.setProperty("sources", "src/main/java");
  219. props.setProperty("tests", "src/test/java");
  220. props.setProperty("foo.sources", "src/main/java");
  221. props.setProperty("foobar.tests", "src/test/java");
  222. props.setProperty("foobar.binaries", "target/classes");
  223. Properties moduleProps = SonarProjectBuilder.extractModuleProperties("bar", props);
  224. assertThat(moduleProps.size()).isEqualTo(0);
  225. moduleProps = SonarProjectBuilder.extractModuleProperties("foo", props);
  226. assertThat(moduleProps.size()).isEqualTo(1);
  227. assertThat(moduleProps.get("sources")).isEqualTo("src/main/java");
  228. moduleProps = SonarProjectBuilder.extractModuleProperties("foobar", props);
  229. assertThat(moduleProps.size()).isEqualTo(2);
  230. assertThat(moduleProps.get("tests")).isEqualTo("src/test/java");
  231. assertThat(moduleProps.get("binaries")).isEqualTo("target/classes");
  232. }
  233. @Test
  234. public void shouldFailIfMandatoryPropertiesAreNotPresent() {
  235. Properties props = new Properties();
  236. props.setProperty("foo1", "bla");
  237. props.setProperty("foo4", "bla");
  238. thrown.expect(RunnerException.class);
  239. thrown.expectMessage("You must define the following mandatory properties for 'Unknown': foo2, foo3");
  240. SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
  241. }
  242. @Test
  243. public void shouldFailIfMandatoryPropertiesAreNotPresentButWithProjectKey() {
  244. Properties props = new Properties();
  245. props.setProperty("foo1", "bla");
  246. props.setProperty("sonar.projectKey", "my-project");
  247. thrown.expect(RunnerException.class);
  248. thrown.expectMessage("You must define the following mandatory properties for 'my-project': foo2, foo3");
  249. SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1", "foo2", "foo3"});
  250. }
  251. @Test
  252. public void shouldNotFailIfMandatoryPropertiesArePresent() {
  253. Properties props = new Properties();
  254. props.setProperty("foo1", "bla");
  255. props.setProperty("foo4", "bla");
  256. SonarProjectBuilder.checkMandatoryProperties(props, new String[] {"foo1"});
  257. // No exception should be thrown
  258. }
  259. @Test
  260. public void shouldFilterFiles() throws Exception {
  261. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  262. assertThat(SonarProjectBuilder.getLibraries(baseDir, "in*.txt").length).isEqualTo(1);
  263. assertThat(SonarProjectBuilder.getLibraries(baseDir, "*.txt").length).isEqualTo(2);
  264. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt").length).isEqualTo(1);
  265. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt").length).isEqualTo(2);
  266. }
  267. @Test
  268. public void shouldWorkWithAbsolutePath() throws Exception {
  269. File baseDir = new File("not-exists");
  270. String absolutePattern = TestUtils.getResource(this.getClass(), "shouldFilterFiles").getAbsolutePath() + "/in*.txt";
  271. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), absolutePattern).length).isEqualTo(1);
  272. }
  273. @Test
  274. public void shouldThrowExceptionWhenNoFilesMatchingPattern() throws Exception {
  275. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  276. thrown.expect(RunnerException.class);
  277. thrown.expectMessage("No files matching pattern \"*.jar\" in directory");
  278. SonarProjectBuilder.getLibraries(baseDir, "*.jar");
  279. }
  280. @Test
  281. public void shouldGetRelativeFile() {
  282. assertThat(SonarProjectBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/")))
  283. .isEqualTo(TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties"));
  284. }
  285. @Test
  286. public void shouldGetAbsoluteFile() {
  287. File file = TestUtils.getResource(this.getClass(), "shouldGetFile/foo.properties");
  288. assertThat(SonarProjectBuilder.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/")))
  289. .isEqualTo(file);
  290. }
  291. @Test
  292. public void shouldMergeParentProperties() {
  293. Properties parentProps = new Properties();
  294. parentProps.setProperty("toBeMergeProps", "fooParent");
  295. parentProps.setProperty("existingChildProp", "barParent");
  296. parentProps.setProperty("sonar.modules", "mod1,mod2");
  297. parentProps.setProperty("sonar.projectDescription", "Desc from Parent");
  298. parentProps.setProperty("mod1.sonar.projectDescription", "Desc for Mod1");
  299. parentProps.setProperty("mod2.sonar.projectkey", "Key for Mod2");
  300. Properties childProps = new Properties();
  301. childProps.setProperty("existingChildProp", "barChild");
  302. childProps.setProperty("otherProp", "tutuChild");
  303. SonarProjectBuilder.mergeParentProperties(childProps, parentProps);
  304. assertThat(childProps.size()).isEqualTo(3);
  305. assertThat(childProps.getProperty("toBeMergeProps")).isEqualTo("fooParent");
  306. assertThat(childProps.getProperty("existingChildProp")).isEqualTo("barChild");
  307. assertThat(childProps.getProperty("otherProp")).isEqualTo("tutuChild");
  308. assertThat(childProps.getProperty("sonar.modules")).isNull();
  309. assertThat(childProps.getProperty("sonar.projectDescription")).isNull();
  310. assertThat(childProps.getProperty("mod1.sonar.projectDescription")).isNull();
  311. assertThat(childProps.getProperty("mod2.sonar.projectkey")).isNull();
  312. }
  313. @Test
  314. public void shouldInitRootWorkDir() {
  315. SonarProjectBuilder builder = SonarProjectBuilder.create(new Properties());
  316. File baseDir = new File("target/tmp/baseDir");
  317. File workDir = builder.initRootProjectWorkDir(baseDir);
  318. assertThat(workDir).isEqualTo(new File(baseDir, ".sonar"));
  319. }
  320. @Test
  321. public void shouldInitWorkDirWithCustomRelativeFolder() {
  322. Properties properties = new Properties();
  323. properties.put("sonar.working.directory", ".foo");
  324. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  325. File baseDir = new File("target/tmp/baseDir");
  326. File workDir = builder.initRootProjectWorkDir(baseDir);
  327. assertThat(workDir).isEqualTo(new File(baseDir, ".foo"));
  328. }
  329. @Test
  330. public void shouldInitRootWorkDirWithCustomAbsoluteFolder() {
  331. Properties properties = new Properties();
  332. properties.put("sonar.working.directory", new File("src").getAbsolutePath());
  333. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  334. File baseDir = new File("target/tmp/baseDir");
  335. File workDir = builder.initRootProjectWorkDir(baseDir);
  336. assertThat(workDir).isEqualTo(new File("src").getAbsoluteFile());
  337. }
  338. @Test
  339. public void shouldReturnPrefixedKey() {
  340. Properties props = new Properties();
  341. props.put("sonar.projectKey", "my-module-key");
  342. SonarProjectBuilder.prefixProjectKeyWithParentKey(props, "my-parent-key");
  343. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("my-parent-key:my-module-key");
  344. }
  345. @Test
  346. public void shouldFailIf2ModulesWithSameKey() {
  347. Properties props = new Properties();
  348. props.put("sonar.projectKey", "root");
  349. ProjectDefinition root = ProjectDefinition.create(props);
  350. Properties props1 = new Properties();
  351. props1.put("sonar.projectKey", "mod1");
  352. root.addSubProject(ProjectDefinition.create(props1));
  353. // Check unicity of a new module: OK
  354. Properties props2 = new Properties();
  355. props2.put("sonar.projectKey", "mod2");
  356. ProjectDefinition mod2 = ProjectDefinition.create(props2);
  357. SonarProjectBuilder.checkUnicityOfChildKey(mod2, root);
  358. // Now, add it and check again
  359. root.addSubProject(mod2);
  360. thrown.expect(RunnerException.class);
  361. thrown.expectMessage("Project 'root' can't have 2 modules with the following key: mod2");
  362. SonarProjectBuilder.checkUnicityOfChildKey(mod2, root);
  363. }
  364. @Test
  365. public void shouldSetProjectKeyIfNotPresent() {
  366. Properties props = new Properties();
  367. props.put("sonar.projectVersion", "1.0");
  368. // should be set
  369. SonarProjectBuilder.setProjectKeyAndNameIfNotDefined(props, "foo");
  370. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  371. assertThat(props.getProperty("sonar.projectName")).isEqualTo("foo");
  372. // but not this 2nd time
  373. SonarProjectBuilder.setProjectKeyAndNameIfNotDefined(props, "bar");
  374. assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo");
  375. assertThat(props.getProperty("sonar.projectName")).isEqualTo("foo");
  376. }
  377. @Test
  378. public void shouldFailToLoadPropertiesFile() throws Exception {
  379. thrown.expect(RunnerException.class);
  380. thrown.expectMessage("Impossible to read the property file");
  381. SonarProjectBuilder.toProperties(new File("foo.properties"));
  382. }
  383. private ProjectDefinition loadProjectDefinition(String projectFolder) throws FileNotFoundException, IOException {
  384. Properties props = SonarProjectBuilder.toProperties(TestUtils.getResource(this.getClass(), projectFolder + "/sonar-project.properties"));
  385. props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
  386. ProjectDefinition projectDefinition = SonarProjectBuilder.create(props)
  387. .generateProjectDefinition();
  388. return projectDefinition;
  389. }
  390. }