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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact 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.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.fest.assertions.Assertions.assertThat;
  34. import static org.mockito.Matchers.any;
  35. import static org.mockito.Mockito.doThrow;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.never;
  38. import static org.mockito.Mockito.times;
  39. import static org.mockito.Mockito.verify;
  40. import static org.mockito.Mockito.when;
  41. public class MainTest {
  42. @Mock
  43. private Exit exit;
  44. @Mock
  45. private Cli cli;
  46. @Mock
  47. private Conf conf;
  48. @Mock
  49. private Properties properties;
  50. @Mock
  51. private ScannerFactory runnerFactory;
  52. @Mock
  53. private EmbeddedScanner runner;
  54. @Mock
  55. private Logs logs;
  56. @Before
  57. public void setUp() throws IOException {
  58. MockitoAnnotations.initMocks(this);
  59. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  60. when(conf.properties()).thenReturn(properties);
  61. }
  62. @Test
  63. public void should_execute_runner() {
  64. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  65. main.execute();
  66. verify(exit).exit(Exit.SUCCESS);
  67. verify(runnerFactory).create(properties);
  68. verify(runner, times(1)).start();
  69. verify(runner, times(1)).runAnalysis(properties);
  70. verify(runner, times(1)).stop();
  71. }
  72. @Test
  73. public void should_stop_on_error() {
  74. EmbeddedScanner runner = mock(EmbeddedScanner.class);
  75. Exception e = new NullPointerException("NPE");
  76. e = new IllegalStateException("Error", e);
  77. doThrow(e).when(runner).runAnalysis(any(Properties.class));
  78. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  79. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  80. main.execute();
  81. verify(runner).stop();
  82. verify(exit).exit(Exit.ERROR);
  83. verify(logs).error("Error during SonarQube Scanner execution", e);
  84. }
  85. @Test
  86. public void show_error() {
  87. Exception e = createException(false);
  88. testException(e, false);
  89. verify(logs).error("Error during SonarQube Scanner execution", e);
  90. verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  91. }
  92. @Test
  93. public void show_error_MessageException() {
  94. Exception e = createException(true);
  95. testException(e, false);
  96. verify(logs).error("Error during SonarQube Scanner execution");
  97. verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  98. }
  99. @Test
  100. public void show_error_debug() {
  101. Exception e = createException(false);
  102. testException(e, true);
  103. verify(logs).error("Error during SonarQube Scanner execution", e);
  104. verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging.");
  105. }
  106. private void testException(Exception e, boolean debugEnabled) {
  107. when(cli.isDebugEnabled()).thenReturn(debugEnabled);
  108. EmbeddedScanner runner = mock(EmbeddedScanner.class);
  109. doThrow(e).when(runner).runAnalysis(any(Properties.class));
  110. when(runnerFactory.create(any(Properties.class))).thenReturn(runner);
  111. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  112. main.execute();
  113. verify(runner).stop();
  114. verify(exit).exit(Exit.ERROR);
  115. }
  116. private Exception createException(boolean messageException) {
  117. Exception e;
  118. if (messageException) {
  119. e = new MessageException("my message");
  120. } else {
  121. e = new IllegalStateException("Error", new NullPointerException("NPE"));
  122. }
  123. return e;
  124. }
  125. @Test
  126. public void should_only_display_version() throws IOException {
  127. Properties p = new Properties();
  128. when(cli.isDisplayVersionOnly()).thenReturn(true);
  129. when(conf.properties()).thenReturn(p);
  130. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  131. main.execute();
  132. InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
  133. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  134. inOrder.verify(runnerFactory, times(1)).create(p);
  135. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  136. }
  137. @Test
  138. public void should_skip() throws IOException {
  139. Properties p = new Properties();
  140. p.setProperty(ScanProperties.SKIP, "true");
  141. when(conf.properties()).thenReturn(p);
  142. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  143. main.execute();
  144. verify(logs).info("SonarQube Scanner analysis skipped");
  145. InOrder inOrder = Mockito.inOrder(exit, runnerFactory);
  146. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  147. inOrder.verify(runnerFactory, times(1)).create(p);
  148. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  149. }
  150. @Test
  151. public void shouldLogServerVersion() throws IOException {
  152. when(runner.serverVersion()).thenReturn("5.5");
  153. Properties p = new Properties();
  154. when(cli.isDisplayVersionOnly()).thenReturn(true);
  155. when(conf.properties()).thenReturn(p);
  156. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  157. main.execute();
  158. verify(logs).info("SonarQube server 5.5");
  159. }
  160. @Test
  161. public void should_configure_logging() throws IOException {
  162. Properties analysisProps = testLogging("sonar.verbose", "true");
  163. assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true");
  164. }
  165. @Test
  166. public void should_configure_logging_trace() throws IOException {
  167. Properties analysisProps = testLogging("sonar.log.level", "TRACE");
  168. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
  169. }
  170. @Test
  171. public void should_configure_logging_debug() throws IOException {
  172. Properties analysisProps = testLogging("sonar.log.level", "DEBUG");
  173. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG");
  174. }
  175. private Properties testLogging(String propKey, String propValue) throws IOException {
  176. Properties p = new Properties();
  177. p.put(propKey, propValue);
  178. when(conf.properties()).thenReturn(p);
  179. Main main = new Main(exit, cli, conf, runnerFactory, logs);
  180. main.execute();
  181. // Logger used for callback should have debug enabled
  182. verify(logs).setDebugEnabled(true);
  183. ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
  184. verify(runner).runAnalysis(propertiesCapture.capture());
  185. return propertiesCapture.getValue();
  186. }
  187. }