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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2017 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.io.IOException;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.mockito.ArgumentCaptor;
  27. import org.mockito.InOrder;
  28. import org.mockito.Mock;
  29. import org.mockito.Mockito;
  30. import org.mockito.MockitoAnnotations;
  31. import org.sonar.api.utils.MessageException;
  32. import org.sonarsource.scanner.api.EmbeddedScanner;
  33. import org.sonarsource.scanner.api.ScanProperties;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. import static org.mockito.ArgumentMatchers.any;
  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() throws IOException {
  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(Map.class));
  78. when(scannerFactory.create(any(Properties.class))).thenReturn(runner);
  79. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  80. main.execute();
  81. verify(exit).exit(Exit.ERROR);
  82. verify(logs).error("Error during SonarQube Scanner execution", e);
  83. }
  84. @Test
  85. public void should_exit_with_error_on_error_during_start() {
  86. EmbeddedScanner runner = mock(EmbeddedScanner.class);
  87. Exception e = new NullPointerException("NPE");
  88. e = new IllegalStateException("Error", e);
  89. doThrow(e).when(runner).start();
  90. when(scannerFactory.create(any(Properties.class))).thenReturn(runner);
  91. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  92. main.execute();
  93. verify(runner).start();
  94. verify(runner, never()).execute(any(Map.class));
  95. verify(exit).exit(Exit.ERROR);
  96. verify(logs).error("Error during SonarQube Scanner execution", e);
  97. }
  98. @Test
  99. public void show_error_with_stacktrace() {
  100. Exception e = createException(false);
  101. testException(e, false);
  102. verify(logs).error("Error during SonarQube Scanner execution", e);
  103. verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  104. }
  105. @Test
  106. public void show_error_MessageException() {
  107. Exception e = createException(true);
  108. testException(e, false);
  109. verify(logs).error("Error during SonarQube Scanner execution");
  110. verify(logs).error("Caused by: NPE");
  111. verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  112. }
  113. @Test
  114. public void show_error_debug() {
  115. Exception e = createException(false);
  116. testException(e, true);
  117. verify(logs).error("Error during SonarQube Scanner execution", e);
  118. verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  119. }
  120. private void testException(Exception e, boolean debugEnabled) {
  121. when(cli.isDebugEnabled()).thenReturn(debugEnabled);
  122. EmbeddedScanner runner = mock(EmbeddedScanner.class);
  123. doThrow(e).when(runner).execute(any(Map.class));
  124. when(scannerFactory.create(any(Properties.class))).thenReturn(runner);
  125. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  126. main.execute();
  127. verify(exit).exit(Exit.ERROR);
  128. }
  129. private Exception createException(boolean messageException) {
  130. Exception e;
  131. if (messageException) {
  132. e = new MessageException("my message", new NullPointerException("NPE"));
  133. } else {
  134. e = new IllegalStateException("Error", new NullPointerException("NPE"));
  135. }
  136. return e;
  137. }
  138. @Test
  139. public void should_only_display_version() throws IOException {
  140. Properties p = new Properties();
  141. when(cli.isDisplayVersionOnly()).thenReturn(true);
  142. when(conf.properties()).thenReturn(p);
  143. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  144. main.execute();
  145. InOrder inOrder = Mockito.inOrder(exit, scannerFactory);
  146. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  147. inOrder.verify(scannerFactory, times(1)).create(p);
  148. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  149. }
  150. @Test
  151. public void should_skip() throws IOException {
  152. Properties p = new Properties();
  153. p.setProperty(ScanProperties.SKIP, "true");
  154. when(conf.properties()).thenReturn(p);
  155. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  156. main.execute();
  157. verify(logs).info("SonarQube Scanner analysis skipped");
  158. InOrder inOrder = Mockito.inOrder(exit, scannerFactory);
  159. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  160. inOrder.verify(scannerFactory, times(1)).create(p);
  161. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  162. }
  163. @Test
  164. public void shouldLogServerVersion() throws IOException {
  165. when(scanner.serverVersion()).thenReturn("5.5");
  166. Properties p = new Properties();
  167. when(cli.isDisplayVersionOnly()).thenReturn(true);
  168. when(conf.properties()).thenReturn(p);
  169. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  170. main.execute();
  171. verify(logs).info("SonarQube server 5.5");
  172. }
  173. @Test
  174. public void should_configure_logging() throws IOException {
  175. Properties analysisProps = testLogging("sonar.verbose", "true");
  176. assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true");
  177. }
  178. @Test
  179. public void should_configure_logging_trace() throws IOException {
  180. Properties analysisProps = testLogging("sonar.log.level", "TRACE");
  181. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
  182. }
  183. @Test
  184. public void should_configure_logging_debug() throws IOException {
  185. Properties analysisProps = testLogging("sonar.log.level", "DEBUG");
  186. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG");
  187. }
  188. private Properties testLogging(String propKey, String propValue) throws IOException {
  189. Properties p = new Properties();
  190. p.put(propKey, propValue);
  191. when(conf.properties()).thenReturn(p);
  192. Main main = new Main(exit, cli, conf, scannerFactory, logs);
  193. main.execute();
  194. // Logger used for callback should have debug enabled
  195. verify(logs).setDebugEnabled(true);
  196. ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
  197. verify(scanner).execute((Map) propertiesCapture.capture());
  198. return propertiesCapture.getValue();
  199. }
  200. }