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

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