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.

MainTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-2024 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.sonarsource.scanner.cli;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.mockito.ArgumentCaptor;
  26. import org.mockito.InOrder;
  27. import org.mockito.Mock;
  28. import org.mockito.Mockito;
  29. import org.mockito.MockitoAnnotations;
  30. import org.sonar.api.utils.MessageException;
  31. import org.sonarsource.scanner.lib.ScanProperties;
  32. import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
  33. import org.sonarsource.scanner.lib.ScannerEngineFacade;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.ArgumentMatchers.any;
  36. import static org.mockito.ArgumentMatchers.anyString;
  37. import static org.mockito.Mockito.doThrow;
  38. import static org.mockito.Mockito.never;
  39. import static org.mockito.Mockito.times;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.when;
  42. public class MainTest {
  43. @Mock
  44. private Exit exit;
  45. @Mock
  46. private Cli cli;
  47. @Mock
  48. private Conf conf;
  49. @Mock
  50. private Properties properties;
  51. @Mock
  52. private ScannerEngineBootstrapperFactory scannerEngineBootstrapperFactory;
  53. @Mock
  54. private ScannerEngineBootstrapper bootstrapper;
  55. @Mock
  56. private ScannerEngineFacade engine;
  57. @Mock
  58. private Logs logs;
  59. @Before
  60. public void setUp() {
  61. MockitoAnnotations.initMocks(this);
  62. when(scannerEngineBootstrapperFactory.create(any(Properties.class), any(String.class))).thenReturn(bootstrapper);
  63. when(bootstrapper.bootstrap()).thenReturn(engine);
  64. when(conf.properties()).thenReturn(properties);
  65. }
  66. @Test
  67. public void should_execute_scanner_engine() {
  68. when(cli.getInvokedFrom()).thenReturn("");
  69. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  70. main.analyze();
  71. verify(exit).exit(Exit.SUCCESS);
  72. verify(scannerEngineBootstrapperFactory).create(properties, "");
  73. verify(bootstrapper, times(1)).bootstrap();
  74. verify(engine, times(1)).analyze((Map) properties);
  75. }
  76. @Test
  77. public void should_exit_with_error_on_error_during_analysis() {
  78. Exception e = new NullPointerException("NPE");
  79. e = new IllegalStateException("Error", e);
  80. doThrow(e).when(engine).analyze(any());
  81. when(cli.getInvokedFrom()).thenReturn("");
  82. when(cli.isDebugEnabled()).thenReturn(true);
  83. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  84. main.analyze();
  85. verify(exit).exit(Exit.INTERNAL_ERROR);
  86. verify(logs).error("Error during SonarScanner execution", e);
  87. }
  88. @Test
  89. public void should_exit_with_error_on_error_during_bootstrap() {
  90. Exception e = new NullPointerException("NPE");
  91. e = new IllegalStateException("Error", e);
  92. doThrow(e).when(bootstrapper).bootstrap();
  93. when(cli.getInvokedFrom()).thenReturn("");
  94. when(cli.isDebugEnabled()).thenReturn(true);
  95. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  96. main.analyze();
  97. verify(bootstrapper).bootstrap();
  98. verify(engine, never()).analyze(any());
  99. verify(exit).exit(Exit.INTERNAL_ERROR);
  100. verify(logs).error("Error during SonarScanner execution", e);
  101. }
  102. @Test
  103. public void show_stacktrace() {
  104. Exception e = createException(false);
  105. testException(e, false, false, Exit.INTERNAL_ERROR);
  106. verify(logs).error("Error during SonarScanner execution", e);
  107. verify(logs).error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  108. }
  109. @Test
  110. public void dont_show_MessageException_stacktrace() {
  111. Exception e = createException(true);
  112. testException(e, false, false, Exit.USER_ERROR);
  113. verify(logs, times(5)).error(anyString());
  114. verify(logs).error("Error during SonarScanner execution");
  115. verify(logs).error("my message");
  116. verify(logs).error("Caused by: A functional cause");
  117. verify(logs).error("");
  118. verify(logs).error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  119. }
  120. @Test
  121. public void dont_show_MessageException_stacktrace_embedded() {
  122. Exception e = createException(true);
  123. testException(e, false, true, Exit.USER_ERROR);
  124. verify(logs, times(4)).error(anyString());
  125. verify(logs).error("Error during SonarScanner execution");
  126. verify(logs).error("my message");
  127. verify(logs).error("Caused by: A functional cause");
  128. verify(logs).error("");
  129. }
  130. @Test
  131. public void show_MessageException_stacktrace_in_debug() {
  132. Exception e = createException(true);
  133. testException(e, true, false, Exit.USER_ERROR);
  134. verify(logs, times(1)).error(anyString(), any(Throwable.class));
  135. verify(logs).error("Error during SonarScanner execution", e);
  136. }
  137. @Test
  138. public void show_MessageException_stacktrace_in_debug_embedded() {
  139. Exception e = createException(true);
  140. testException(e, true, true, Exit.USER_ERROR);
  141. verify(logs, times(1)).error(anyString(), any(Throwable.class));
  142. verify(logs).error("Error during SonarScanner execution", e);
  143. }
  144. @Test
  145. public void show_stacktrace_in_debug() {
  146. Exception e = createException(false);
  147. testException(e, true, false, Exit.INTERNAL_ERROR);
  148. verify(logs).error("Error during SonarScanner execution", e);
  149. verify(logs, never()).error("Re-run SonarScanner using the -X switch to enable full debug logging.");
  150. }
  151. private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) {
  152. when(cli.isDebugEnabled()).thenReturn(debugEnabled);
  153. when(cli.isEmbedded()).thenReturn(isEmbedded);
  154. when(cli.getInvokedFrom()).thenReturn("");
  155. doThrow(e).when(engine).analyze(any());
  156. when(scannerEngineBootstrapperFactory.create(any(Properties.class), any(String.class))).thenReturn(bootstrapper);
  157. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  158. main.analyze();
  159. verify(exit).exit(expectedExitCode);
  160. }
  161. private Exception createException(boolean messageException) {
  162. Exception e;
  163. if (messageException) {
  164. e = new MessageException("my message", new IllegalStateException("A functional cause"));
  165. } else {
  166. e = new IllegalStateException("Error", new NullPointerException("NPE"));
  167. }
  168. return e;
  169. }
  170. @Test
  171. public void should_only_display_version() {
  172. Properties p = new Properties();
  173. when(cli.isDisplayVersionOnly()).thenReturn(true);
  174. when(cli.getInvokedFrom()).thenReturn("");
  175. when(conf.properties()).thenReturn(p);
  176. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  177. main.analyze();
  178. InOrder inOrder = Mockito.inOrder(exit, scannerEngineBootstrapperFactory);
  179. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  180. inOrder.verify(scannerEngineBootstrapperFactory, times(1)).create(p, "");
  181. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  182. }
  183. @Test
  184. public void should_skip() {
  185. Properties p = new Properties();
  186. p.setProperty(ScanProperties.SKIP, "true");
  187. when(conf.properties()).thenReturn(p);
  188. when(cli.getInvokedFrom()).thenReturn("");
  189. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  190. main.analyze();
  191. verify(logs).info("SonarScanner analysis skipped");
  192. InOrder inOrder = Mockito.inOrder(exit, scannerEngineBootstrapperFactory);
  193. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  194. inOrder.verify(scannerEngineBootstrapperFactory, times(1)).create(p, "");
  195. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  196. }
  197. @Test
  198. public void shouldLogServerVersion() {
  199. when(engine.isSonarCloud()).thenReturn(false);
  200. when(engine.getServerVersion()).thenReturn("5.5");
  201. Properties p = new Properties();
  202. when(cli.isDisplayVersionOnly()).thenReturn(true);
  203. when(cli.getInvokedFrom()).thenReturn("");
  204. when(conf.properties()).thenReturn(p);
  205. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  206. main.analyze();
  207. verify(logs).info("Analyzing on SonarQube server 5.5");
  208. }
  209. @Test
  210. public void should_log_SonarCloud_server() {
  211. when(engine.isSonarCloud()).thenReturn(true);
  212. Properties p = new Properties();
  213. when(conf.properties()).thenReturn(p);
  214. when(cli.getInvokedFrom()).thenReturn("");
  215. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  216. main.analyze();
  217. verify(logs).info("Analyzing on SonarCloud");
  218. }
  219. @Test
  220. public void should_configure_logging() {
  221. Properties analysisProps = testLogging("sonar.verbose", "true");
  222. assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true");
  223. }
  224. @Test
  225. public void should_configure_logging_trace() {
  226. Properties analysisProps = testLogging("sonar.log.level", "TRACE");
  227. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
  228. }
  229. @Test
  230. public void should_set_bootstrap_start_time_in_millis() {
  231. Properties analysisProps = execute("sonar.scanner.bootstrapStartTime", "1714137496104");
  232. assertThat(analysisProps.getProperty("sonar.scanner.bootstrapStartTime")).isEqualTo("1714137496104");
  233. }
  234. @Test
  235. public void should_configure_logging_debug() {
  236. Properties analysisProps = testLogging("sonar.log.level", "DEBUG");
  237. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG");
  238. }
  239. private Properties testLogging(String propKey, String propValue) {
  240. Properties actualProps = execute(propKey, propValue);
  241. // Logger used for callback should have debug enabled
  242. verify(logs).setDebugEnabled(true);
  243. return actualProps;
  244. }
  245. private Properties execute(String propKey, String propValue) {
  246. Properties p = new Properties();
  247. p.put(propKey, propValue);
  248. when(conf.properties()).thenReturn(p);
  249. when(cli.getInvokedFrom()).thenReturn("");
  250. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory, logs);
  251. main.analyze();
  252. ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
  253. verify(engine).analyze((Map) propertiesCapture.capture());
  254. return propertiesCapture.getValue();
  255. }
  256. }