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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.model;
  21. import org.apache.commons.io.IOUtils;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  26. import org.sonar.runner.RunnerException;
  27. import org.sonar.test.TestUtils;
  28. import java.io.File;
  29. import java.io.FileInputStream;
  30. import java.io.FileNotFoundException;
  31. import java.io.IOException;
  32. import java.util.List;
  33. import java.util.Properties;
  34. import static org.fest.assertions.Assertions.assertThat;
  35. public class SonarProjectBuilderTest {
  36. @Rule
  37. public ExpectedException thrown = ExpectedException.none();
  38. @Test
  39. public void shouldDefineSimpleProject() throws IOException {
  40. ProjectDefinition projectDefinition = loadProjectDefinition("simple-project");
  41. assertThat(projectDefinition.getKey()).isEqualTo("com.foo.project");
  42. assertThat(projectDefinition.getName()).isEqualTo("Foo Project");
  43. assertThat(projectDefinition.getVersion()).isEqualTo("1.0-SNAPSHOT");
  44. assertThat(projectDefinition.getDescription()).isEqualTo("Description of Foo Project");
  45. assertThat(projectDefinition.getSourceDirs()).contains("sources");
  46. assertThat(projectDefinition.getTestDirs()).contains("tests");
  47. assertThat(projectDefinition.getBinaries()).contains("target/classes");
  48. assertThat(projectDefinition.getLibraries()).contains(TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath(),
  49. TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath());
  50. }
  51. @Test
  52. public void shouldDefineSimpleProjectWithDeprecatedProperties() throws IOException {
  53. ProjectDefinition projectDefinition = loadProjectDefinition("simple-project-with-deprecated-props");
  54. assertThat(projectDefinition.getSourceDirs()).contains("sources");
  55. assertThat(projectDefinition.getTestDirs()).contains("tests");
  56. assertThat(projectDefinition.getBinaries()).contains("target/classes");
  57. assertThat(projectDefinition.getLibraries()).contains(
  58. TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath(),
  59. TestUtils.getResource(this.getClass(), "simple-project-with-deprecated-props/libs/lib2.txt").getAbsolutePath());
  60. }
  61. @Test
  62. public void shouldFailIfUnexistingSourceDirectory() throws IOException {
  63. thrown.expect(RunnerException.class);
  64. thrown.expectMessage("The source folder 'unexisting-source-dir' does not exist for 'com.foo.project' project/module (base directory = "
  65. + TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-source-dir") + ")");
  66. loadProjectDefinition("simple-project-with-unexisting-source-dir");
  67. }
  68. @Test
  69. public void shouldDefineMultiModuleProject() throws IOException {
  70. ProjectDefinition rootProject = loadProjectDefinition("multi-module");
  71. // CHECK ROOT
  72. assertThat(rootProject.getKey()).isEqualTo("com.foo.project");
  73. assertThat(rootProject.getName()).isEqualTo("Foo Project");
  74. assertThat(rootProject.getVersion()).isEqualTo("1.0-SNAPSHOT");
  75. assertThat(rootProject.getDescription()).isEqualTo("Description of Foo Project");
  76. // root project must not contain some properties - even if they are defined in the root properties file
  77. assertThat(rootProject.getSourceDirs().contains("sources")).isFalse();
  78. assertThat(rootProject.getTestDirs().contains("tests")).isFalse();
  79. assertThat(rootProject.getBinaries().contains("target/classes")).isFalse();
  80. // and module properties must have been cleaned
  81. assertThat(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  82. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  83. // CHECK MODULES
  84. List<ProjectDefinition> modules = rootProject.getSubProjects();
  85. assertThat(modules.size()).isEqualTo(2);
  86. // Module 1
  87. ProjectDefinition module1 = modules.get(0);
  88. assertThat(module1.getKey()).isEqualTo("com.foo.project.module1");
  89. assertThat(module1.getName()).isEqualTo("Foo Module 1");
  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(rootProject.getProperties().getProperty("module1.sonar.projectKey")).isNull();
  98. assertThat(rootProject.getProperties().getProperty("module2.sonar.projectKey")).isNull();
  99. // Module 2
  100. ProjectDefinition module2 = modules.get(1);
  101. assertThat(module2.getKey()).isEqualTo("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 shouldDefineMultiModuleProjectWithBaseDir() throws IOException {
  114. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-basedir");
  115. List<ProjectDefinition> modules = rootProject.getSubProjects();
  116. assertThat(modules.size()).isEqualTo(1);
  117. assertThat(modules.get(0).getKey()).isEqualTo("com.foo.project.module1");
  118. }
  119. @Test
  120. public void shouldDefineMultiModuleProjectWithConfigFile() throws IOException {
  121. ProjectDefinition rootProject = loadProjectDefinition("multi-module-with-configfile");
  122. List<ProjectDefinition> modules = rootProject.getSubProjects();
  123. assertThat(modules.size()).isEqualTo(1);
  124. ProjectDefinition module = modules.get(0);
  125. assertThat(module.getKey()).isEqualTo("com.foo.project.module1");
  126. // verify the base directory that has been changed in this config file
  127. assertThat(module.getBaseDir().getCanonicalFile()).isEqualTo(TestUtils.getResource(this.getClass(), "multi-module-with-configfile/any-folder"));
  128. }
  129. @Test
  130. public void shouldFailIfUnexistingModuleBaseDir() throws IOException {
  131. thrown.expect(RunnerException.class);
  132. thrown.expectMessage("The base directory of the module 'module1' does not exist: "
  133. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-basedir").getAbsolutePath() + File.separator + "module1");
  134. loadProjectDefinition("multi-module-with-unexisting-basedir");
  135. }
  136. @Test
  137. public void shouldFailIfUnexistingModuleFile() throws IOException {
  138. thrown.expect(RunnerException.class);
  139. thrown.expectMessage("The properties file of the module 'module1' does not exist: "
  140. + TestUtils.getResource(this.getClass(), "multi-module-with-unexisting-file").getAbsolutePath() + File.separator + "any-folder"
  141. + File.separator + "any-file.properties");
  142. loadProjectDefinition("multi-module-with-unexisting-file");
  143. }
  144. @Test
  145. public void shouldExtractModuleProperties() {
  146. Properties props = new Properties();
  147. props.setProperty("sources", "src/main/java");
  148. props.setProperty("tests", "src/test/java");
  149. props.setProperty("foo.sources", "src/main/java");
  150. props.setProperty("foobar.tests", "src/test/java");
  151. props.setProperty("foobar.binaries", "target/classes");
  152. Properties moduleProps = SonarProjectBuilder.extractModuleProperties("bar", props);
  153. assertThat(moduleProps.size()).isEqualTo(0);
  154. moduleProps = SonarProjectBuilder.extractModuleProperties("foo", props);
  155. assertThat(moduleProps.size()).isEqualTo(1);
  156. assertThat(moduleProps.get("sources")).isEqualTo("src/main/java");
  157. moduleProps = SonarProjectBuilder.extractModuleProperties("foobar", props);
  158. assertThat(moduleProps.size()).isEqualTo(2);
  159. assertThat(moduleProps.get("tests")).isEqualTo("src/test/java");
  160. assertThat(moduleProps.get("binaries")).isEqualTo("target/classes");
  161. }
  162. @Test
  163. public void shouldFailIfMandatoryPropertiesAreNotPresent() {
  164. Properties props = new Properties();
  165. props.setProperty("foo1", "bla");
  166. props.setProperty("foo4", "bla");
  167. thrown.expect(RunnerException.class);
  168. thrown.expectMessage("You must define the following mandatory properties for 'foo': foo2, foo3");
  169. SonarProjectBuilder.checkMandatoryProperties("foo", props, new String[] {"foo1", "foo2", "foo3"});
  170. }
  171. @Test
  172. public void shouldNotFailIfMandatoryPropertiesArePresent() {
  173. Properties props = new Properties();
  174. props.setProperty("foo1", "bla");
  175. props.setProperty("foo4", "bla");
  176. SonarProjectBuilder.checkMandatoryProperties("foo", props, new String[] {"foo1"});
  177. // No exception should be thrown
  178. }
  179. @Test
  180. public void shouldFilterFiles() throws Exception {
  181. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  182. assertThat(SonarProjectBuilder.getLibraries(baseDir, "in*.txt").length).isEqualTo(1);
  183. assertThat(SonarProjectBuilder.getLibraries(baseDir, "*.txt").length).isEqualTo(2);
  184. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/in*.txt").length).isEqualTo(1);
  185. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), "shouldFilterFiles/*.txt").length).isEqualTo(2);
  186. }
  187. @Test
  188. public void shouldWorkWithAbsolutePath() throws Exception {
  189. File baseDir = new File("not-exists");
  190. String absolutePattern = TestUtils.getResource(this.getClass(), "shouldFilterFiles").getAbsolutePath() + "/in*.txt";
  191. assertThat(SonarProjectBuilder.getLibraries(baseDir.getParentFile(), absolutePattern).length).isEqualTo(1);
  192. }
  193. @Test
  194. public void shouldThrowExceptionWhenNoFilesMatchingPattern() throws Exception {
  195. File baseDir = TestUtils.getResource(this.getClass(), "shouldFilterFiles");
  196. thrown.expect(RunnerException.class);
  197. thrown.expectMessage("No files matching pattern \"*.jar\" in directory");
  198. SonarProjectBuilder.getLibraries(baseDir, "*.jar");
  199. }
  200. @Test
  201. public void shouldGetRelativeFile() {
  202. assertThat(SonarProjectBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/")))
  203. .isEqualTo(TestUtils.getResource("org/sonar/runner/model/SonarProjectBuilderTest/shouldGetFile/foo.properties"));
  204. }
  205. @Test
  206. public void shouldGetAbsoluteFile() {
  207. File file = TestUtils.getResource("org/sonar/runner/model/SonarProjectBuilderTest/shouldGetFile/foo.properties");
  208. assertThat(SonarProjectBuilder.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/")))
  209. .isEqualTo(file);
  210. }
  211. @Test
  212. public void shouldMergeParentProperties() {
  213. Properties parentProps = new Properties();
  214. parentProps.setProperty("toBeMergeProps", "fooParent");
  215. parentProps.setProperty("existingChildProp", "barParent");
  216. parentProps.setProperty("sonar.modules", "mod1,mod2");
  217. parentProps.setProperty("sonar.projectDescription", "Desc from Parent");
  218. parentProps.setProperty("mod1.sonar.projectDescription", "Desc for Mod1");
  219. parentProps.setProperty("mod2.sonar.projectkey", "Key for Mod2");
  220. Properties childProps = new Properties();
  221. childProps.setProperty("existingChildProp", "barChild");
  222. childProps.setProperty("otherProp", "tutuChild");
  223. SonarProjectBuilder.mergeParentProperties(childProps, parentProps);
  224. assertThat(childProps.size()).isEqualTo(3);
  225. assertThat(childProps.getProperty("toBeMergeProps")).isEqualTo("fooParent");
  226. assertThat(childProps.getProperty("existingChildProp")).isEqualTo("barChild");
  227. assertThat(childProps.getProperty("otherProp")).isEqualTo("tutuChild");
  228. assertThat(childProps.getProperty("sonar.modules")).isNull();
  229. assertThat(childProps.getProperty("sonar.projectDescription")).isNull();
  230. assertThat(childProps.getProperty("mod1.sonar.projectDescription")).isNull();
  231. assertThat(childProps.getProperty("mod2.sonar.projectkey")).isNull();
  232. }
  233. private ProjectDefinition loadProjectDefinition(String projectFolder) throws FileNotFoundException, IOException {
  234. Properties props = loadPropsFromFile(projectFolder + "/sonar-project.properties");
  235. props.put("sonar.projectBaseDir", TestUtils.getResource(this.getClass(), projectFolder).getAbsolutePath());
  236. ProjectDefinition projectDefinition = SonarProjectBuilder.create(props)
  237. .generateProjectDefinition();
  238. return projectDefinition;
  239. }
  240. private Properties loadPropsFromFile(String filePath) throws FileNotFoundException, IOException {
  241. Properties props = new Properties();
  242. FileInputStream fileInputStream = null;
  243. try {
  244. fileInputStream = new FileInputStream(TestUtils.getResource(this.getClass(), filePath));
  245. props.load(fileInputStream);
  246. } finally {
  247. IOUtils.closeQuietly(fileInputStream);
  248. }
  249. return props;
  250. }
  251. @Test
  252. public void shouldInitWorkDir() {
  253. SonarProjectBuilder builder = SonarProjectBuilder.create(new Properties());
  254. File baseDir = new File("target/tmp/baseDir");
  255. File workDir = builder.initWorkDir(baseDir);
  256. assertThat(workDir).isEqualTo(new File(baseDir, ".sonar"));
  257. }
  258. @Test
  259. public void shouldInitWorkDirWithCustomRelativeFolder() {
  260. Properties properties = new Properties();
  261. properties.put("sonar.working.directory", ".foo");
  262. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  263. File baseDir = new File("target/tmp/baseDir");
  264. File workDir = builder.initWorkDir(baseDir);
  265. assertThat(workDir).isEqualTo(new File(baseDir, ".foo"));
  266. }
  267. @Test
  268. public void shouldInitWorkDirWithCustomAbsoluteFolder() {
  269. Properties properties = new Properties();
  270. properties.put("sonar.working.directory", new File("src").getAbsolutePath());
  271. SonarProjectBuilder builder = SonarProjectBuilder.create(properties);
  272. File baseDir = new File("target/tmp/baseDir");
  273. File workDir = builder.initWorkDir(baseDir);
  274. assertThat(workDir).isEqualTo(new File("src").getAbsoluteFile());
  275. }
  276. }