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

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