Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RunnerTest.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Sonar Runner - API
  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;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import org.junit.rules.TemporaryFolder;
  25. import java.io.File;
  26. import java.nio.charset.Charset;
  27. import java.util.Properties;
  28. import static org.fest.assertions.Assertions.assertThat;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.when;
  31. public class RunnerTest {
  32. @Rule
  33. public TemporaryFolder tempFolder = new TemporaryFolder();
  34. @Rule
  35. public ExpectedException thrown = ExpectedException.none();
  36. @Test
  37. public void shouldHaveDefaultEncodingIfNotForce() {
  38. Runner runner = Runner.create(new Properties());
  39. assertThat(runner.getSourceCodeEncoding()).isEqualTo(Charset.defaultCharset().name());
  40. assertThat(runner.isEncodingPlatformDependant()).isTrue();
  41. }
  42. @Test
  43. public void shouldKeepEncodingIfSpecified() {
  44. Properties props = new Properties();
  45. // Yeah, windows charset!
  46. props.setProperty("sonar.sourceEncoding", "cp1252");
  47. Runner runner = Runner.create(props);
  48. assertThat(runner.getSourceCodeEncoding()).isEqualTo("cp1252");
  49. assertThat(runner.isEncodingPlatformDependant()).isFalse();
  50. }
  51. @Test
  52. public void shouldHaveDefaultEnvironmentInformationValues() {
  53. Runner runner = Runner.create(new Properties());
  54. assertThat(runner.getProperties().getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_KEY)).isEqualTo("Runner");
  55. assertThat(runner.getProperties().getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_VERSION)).contains(".");
  56. assertThat(runner.getProperties().getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_VERSION)).doesNotContain("$");
  57. }
  58. @Test
  59. public void shouldOverwriteDefaultEnvironmentInformationValues() {
  60. Runner runner = Runner.create(new Properties());
  61. runner.setEnvironmentInformation("Ant", "1.2.3");
  62. assertThat(runner.getProperties().getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_KEY)).isEqualTo("Ant");
  63. assertThat(runner.getProperties().getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_VERSION)).isEqualTo("1.2.3");
  64. }
  65. @Test
  66. public void shouldCheckVersion() {
  67. assertThat(Runner.isUnsupportedVersion("1.0")).isTrue();
  68. assertThat(Runner.isUnsupportedVersion("2.0")).isTrue();
  69. assertThat(Runner.isUnsupportedVersion("2.1")).isTrue();
  70. assertThat(Runner.isUnsupportedVersion("2.2")).isTrue();
  71. assertThat(Runner.isUnsupportedVersion("2.3")).isTrue();
  72. assertThat(Runner.isUnsupportedVersion("2.4")).isTrue();
  73. assertThat(Runner.isUnsupportedVersion("2.4.1")).isTrue();
  74. assertThat(Runner.isUnsupportedVersion("2.5")).isTrue();
  75. assertThat(Runner.isUnsupportedVersion("2.11")).isFalse();
  76. assertThat(Runner.isUnsupportedVersion("3.0")).isFalse();
  77. assertThat(Runner.isUnsupportedVersion("3.1")).isFalse();
  78. assertThat(Runner.isUnsupportedVersion("3.2")).isFalse();
  79. assertThat(Runner.isUnsupportedVersion("3.3")).isFalse();
  80. assertThat(Runner.isUnsupportedVersion("3.4")).isFalse();
  81. assertThat(Runner.isUnsupportedVersion("3.5")).isFalse();
  82. }
  83. @Test
  84. public void shouldCheckVersionForTasks() {
  85. assertThat(Runner.isUnsupportedVersionForTasks("1.0")).isTrue();
  86. assertThat(Runner.isUnsupportedVersionForTasks("2.0")).isTrue();
  87. assertThat(Runner.isUnsupportedVersionForTasks("2.1")).isTrue();
  88. assertThat(Runner.isUnsupportedVersionForTasks("2.2")).isTrue();
  89. assertThat(Runner.isUnsupportedVersionForTasks("2.3")).isTrue();
  90. assertThat(Runner.isUnsupportedVersionForTasks("2.4")).isTrue();
  91. assertThat(Runner.isUnsupportedVersionForTasks("2.4.1")).isTrue();
  92. assertThat(Runner.isUnsupportedVersionForTasks("2.5")).isTrue();
  93. assertThat(Runner.isUnsupportedVersionForTasks("2.11")).isTrue();
  94. assertThat(Runner.isUnsupportedVersionForTasks("3.0")).isTrue();
  95. assertThat(Runner.isUnsupportedVersionForTasks("3.1")).isTrue();
  96. assertThat(Runner.isUnsupportedVersionForTasks("3.2")).isTrue();
  97. assertThat(Runner.isUnsupportedVersionForTasks("3.3")).isTrue();
  98. assertThat(Runner.isUnsupportedVersionForTasks("3.4")).isTrue();
  99. assertThat(Runner.isUnsupportedVersionForTasks("3.5")).isFalse();
  100. }
  101. @Test
  102. public void shouldGetServerUrl() {
  103. Properties properties = new Properties();
  104. Runner runner = Runner.create(properties);
  105. assertThat(runner.getSonarServerURL()).isEqualTo("http://localhost:9000");
  106. properties.setProperty("sonar.host.url", "foo");
  107. assertThat(runner.getSonarServerURL()).isEqualTo("foo");
  108. }
  109. @Test
  110. public void shouldInitDirs() throws Exception {
  111. Properties props = new Properties();
  112. File home = tempFolder.newFolder("shouldInitDirs").getCanonicalFile();
  113. props.setProperty(Runner.PROPERTY_SONAR_PROJECT_BASEDIR, home.getCanonicalPath());
  114. Runner runner = Runner.create(props);
  115. assertThat(runner.getProperties().get(Runner.PROPERTY_SONAR_PROJECT_BASEDIR)).isEqualTo(home.getCanonicalPath());
  116. assertThat(runner.getProjectDir().getCanonicalFile()).isEqualTo(home);
  117. assertThat(runner.getWorkDir().getCanonicalFile()).isEqualTo(new File(home, ".sonar"));
  118. }
  119. @Test
  120. public void shouldInitProjectDirWithCurrentDir() throws Exception {
  121. Runner runner = Runner.create(new Properties());
  122. assertThat(runner.getProjectDir().isDirectory()).isTrue();
  123. assertThat(runner.getProjectDir().exists()).isTrue();
  124. }
  125. @Test
  126. public void shouldSetValidBaseDirOnConstructor() {
  127. File baseDir = tempFolder.newFolder("shouldInitDirs");
  128. Runner runner = Runner.create(new Properties(), baseDir);
  129. assertThat(runner.getProjectDir()).isEqualTo(baseDir);
  130. }
  131. @Test
  132. public void shouldFailIfBaseDirDoesNotExist() {
  133. File fakeBasedir = new File("fake");
  134. thrown.expect(RunnerException.class);
  135. thrown.expectMessage("Project home must be an existing directory: " + fakeBasedir.getAbsolutePath());
  136. Runner.create(new Properties(), fakeBasedir);
  137. }
  138. @Test
  139. public void shouldSpecifyWorkingDirectory() {
  140. Properties properties = new Properties();
  141. Runner runner = Runner.create(properties);
  142. assertThat(runner.getWorkDir()).isEqualTo(new File(".", ".sonar"));
  143. // empty string
  144. properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, " ");
  145. runner = Runner.create(properties);
  146. assertThat(runner.getWorkDir()).isEqualTo(new File(".", ".sonar").getAbsoluteFile());
  147. // real relative path
  148. properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, "temp-dir");
  149. runner = Runner.create(properties);
  150. assertThat(runner.getWorkDir()).isEqualTo(new File(".", "temp-dir").getAbsoluteFile());
  151. // real absolute path
  152. properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, new File("target", "temp-dir2").getAbsolutePath());
  153. runner = Runner.create(properties);
  154. assertThat(runner.getWorkDir()).isEqualTo(new File("target", "temp-dir2").getAbsoluteFile());
  155. }
  156. @Test
  157. public void shouldDeleteWorkingDirectory() {
  158. Properties properties = new Properties();
  159. File workDir = new File("target", "temp-dir-should-be-deleted");
  160. workDir.mkdirs();
  161. assertThat(workDir.exists()).isTrue();
  162. // real absolute path
  163. properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, workDir.getAbsolutePath());
  164. Runner.create(properties);
  165. assertThat(workDir.exists()).isFalse();
  166. }
  167. @Test
  168. public void shouldCheckSonarVersion() {
  169. Properties properties = new Properties();
  170. Runner runner = Runner.create(properties);
  171. Bootstrapper bootstrapper = mock(Bootstrapper.class);
  172. // nothing happens, OK
  173. when(bootstrapper.getServerVersion()).thenReturn("3.0");
  174. runner.checkSonarVersion(bootstrapper);
  175. // but fails with older versions
  176. when(bootstrapper.getServerVersion()).thenReturn("2.1");
  177. thrown.expect(RunnerException.class);
  178. thrown.expectMessage("Sonar 2.1 is not supported. Please upgrade Sonar to version 2.11 or more.");
  179. runner.checkSonarVersion(bootstrapper);
  180. }
  181. @Test
  182. public void shouldCheckSonarVersionForTasks() {
  183. Properties properties = new Properties();
  184. Runner runner = Runner.create("foo-cmd", properties, properties);
  185. Bootstrapper bootstrapper = mock(Bootstrapper.class);
  186. // nothing happens, OK
  187. when(bootstrapper.getServerVersion()).thenReturn("3.5");
  188. runner.checkSonarVersion(bootstrapper);
  189. // but fails with older versions
  190. when(bootstrapper.getServerVersion()).thenReturn("3.4");
  191. thrown.expect(RunnerException.class);
  192. thrown.expectMessage("Sonar 3.4 doesn't support tasks. Please upgrade Sonar to version 3.5 or more.");
  193. runner.checkSonarVersion(bootstrapper);
  194. }
  195. }