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

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