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.

ProjectReactorValidatorTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Arrays;
  25. import java.util.Optional;
  26. import java.util.function.Consumer;
  27. import org.junit.Before;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.ExpectedException;
  31. import org.junit.runner.RunWith;
  32. import org.sonar.api.CoreProperties;
  33. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  34. import org.sonar.api.batch.bootstrap.ProjectReactor;
  35. import org.sonar.api.utils.MessageException;
  36. import org.sonar.core.config.ScannerProperties;
  37. import org.sonar.scanner.ProjectInfo;
  38. import org.sonar.scanner.bootstrap.GlobalConfiguration;
  39. import static org.apache.commons.lang.RandomStringUtils.randomAscii;
  40. import static org.mockito.ArgumentMatchers.anyString;
  41. import static org.mockito.ArgumentMatchers.eq;
  42. import static org.mockito.Mockito.mock;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(DataProviderRunner.class)
  45. public class ProjectReactorValidatorTest {
  46. @Rule
  47. public ExpectedException thrown = ExpectedException.none();
  48. private GlobalConfiguration settings = mock(GlobalConfiguration.class);
  49. private ProjectInfo projectInfo = mock(ProjectInfo.class);
  50. private ProjectReactorValidator underTest = new ProjectReactorValidator(settings);
  51. @Before
  52. public void prepare() {
  53. when(settings.get(anyString())).thenReturn(Optional.empty());
  54. }
  55. @Test
  56. @UseDataProvider("validKeys")
  57. public void not_fail_with_valid_key(String validKey) {
  58. underTest.validate(createProjectReactor(validKey));
  59. }
  60. @DataProvider
  61. public static Object[][] validKeys() {
  62. return new Object[][] {
  63. {"foo"},
  64. {"123foo"},
  65. {"foo123"},
  66. {"1Z3"},
  67. {"a123"},
  68. {"123a"},
  69. {"1:2"},
  70. {"3-3"},
  71. {"-:"},
  72. {"Foobar2"},
  73. {"foo.bar"},
  74. {"foo-bar"},
  75. {"foo:bar"},
  76. {"foo_bar"}
  77. };
  78. }
  79. @Test
  80. public void fail_with_invalid_key() {
  81. ProjectReactor reactor = createProjectReactor(" ");
  82. thrown.expect(MessageException.class);
  83. thrown.expectMessage("\" \" is not a valid project or module key");
  84. underTest.validate(reactor);
  85. }
  86. @Test
  87. public void fail_when_branch_name_is_specified_but_branch_plugin_not_present() {
  88. ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
  89. ProjectReactor reactor = new ProjectReactor(def);
  90. when(settings.get(eq(ScannerProperties.BRANCH_NAME))).thenReturn(Optional.of("feature1"));
  91. thrown.expect(MessageException.class);
  92. thrown.expectMessage("To use the property \"sonar.branch.name\" and analyze branches, Developer Edition or above is required");
  93. underTest.validate(reactor);
  94. }
  95. @Test
  96. public void fail_when_branch_target_is_specified_but_branch_plugin_not_present() {
  97. ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
  98. ProjectReactor reactor = new ProjectReactor(def);
  99. when(settings.get(eq(ScannerProperties.BRANCH_TARGET))).thenReturn(Optional.of("feature1"));
  100. thrown.expect(MessageException.class);
  101. thrown.expectMessage("To use the property \"sonar.branch.target\" and analyze branches, Developer Edition or above is required");
  102. underTest.validate(reactor);
  103. }
  104. @Test
  105. public void fail_when_pull_request_id_specified_but_branch_plugin_not_present() {
  106. ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
  107. ProjectReactor reactor = new ProjectReactor(def);
  108. when(settings.get(eq(ScannerProperties.PULL_REQUEST_KEY))).thenReturn(Optional.of("#1984"));
  109. thrown.expect(MessageException.class);
  110. thrown.expectMessage("To use the property \"sonar.pullrequest.key\" and analyze pull requests, Developer Edition or above is required");
  111. underTest.validate(reactor);
  112. }
  113. @Test
  114. public void fail_when_pull_request_branch_is_specified_but_branch_plugin_not_present() {
  115. ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
  116. ProjectReactor reactor = new ProjectReactor(def);
  117. when(settings.get(eq(ScannerProperties.PULL_REQUEST_BRANCH))).thenReturn(Optional.of("feature1"));
  118. thrown.expect(MessageException.class);
  119. thrown.expectMessage("To use the property \"sonar.pullrequest.branch\" and analyze pull requests, Developer Edition or above is required");
  120. underTest.validate(reactor);
  121. }
  122. @Test
  123. public void fail_when_pull_request_base_specified_but_branch_plugin_not_present() {
  124. ProjectDefinition def = ProjectDefinition.create().setProperty(CoreProperties.PROJECT_KEY_PROPERTY, "foo");
  125. ProjectReactor reactor = new ProjectReactor(def);
  126. when(settings.get(eq(ScannerProperties.PULL_REQUEST_BASE))).thenReturn(Optional.of("feature1"));
  127. thrown.expect(MessageException.class);
  128. thrown.expectMessage("To use the property \"sonar.pullrequest.base\" and analyze pull requests, Developer Edition or above is required");
  129. underTest.validate(reactor);
  130. }
  131. @Test
  132. @UseDataProvider("validVersions")
  133. public void not_fail_with_valid_version(String validVersion) {
  134. when(projectInfo.getProjectVersion()).thenReturn(Optional.ofNullable(validVersion));
  135. underTest.validate(createProjectReactor("foo"));
  136. }
  137. @DataProvider
  138. public static Object[][] validVersions() {
  139. return new Object[][] {
  140. {null},
  141. {"1.0"},
  142. {"2017-10-16"},
  143. {randomAscii(100)}
  144. };
  145. }
  146. @Test
  147. @UseDataProvider("validBuildStrings")
  148. public void not_fail_with_valid_buildString(String validBuildString) {
  149. when(projectInfo.getBuildString()).thenReturn(Optional.ofNullable(validBuildString));
  150. underTest.validate(createProjectReactor("foo"));
  151. }
  152. @DataProvider
  153. public static Object[][] validBuildStrings() {
  154. return new Object[][] {
  155. {null},
  156. {"1.0"},
  157. {"2017-10-16"},
  158. {randomAscii(100)}
  159. };
  160. }
  161. private ProjectReactor createProjectReactor(String projectKey, Consumer<ProjectDefinition>... consumers) {
  162. ProjectDefinition def = ProjectDefinition.create()
  163. .setProperty(CoreProperties.PROJECT_KEY_PROPERTY, projectKey);
  164. Arrays.stream(consumers).forEach(c -> c.accept(def));
  165. return new ProjectReactor(def);
  166. }
  167. }