assertThat(log).contains("(analysis is platform dependent)"); | assertThat(log).contains("(analysis is platform dependent)"); | ||||
} | } | ||||
/** | |||||
* SONARUNNER-153 | |||||
*/ | |||||
@Test | |||||
public void should_enable_verbose() { | |||||
// this line should appear in all versions (LTS-DEV) in debug only | |||||
String expectedLog = "Available languages:"; | |||||
SonarRunner build = newScanner(new File("projects/java-sample")) | |||||
.setProperty("sonar.verbose", "true"); | |||||
String logs = orchestrator.executeBuild(build).getLogs(); | |||||
assertThat(logs).contains(expectedLog); | |||||
} | |||||
@Test | @Test | ||||
public void should_fail_if_unable_to_connect() { | public void should_fail_if_unable_to_connect() { | ||||
SonarRunner build = newScanner(new File("projects/java-sample")) | SonarRunner build = newScanner(new File("projects/java-sample")) |
class Cli { | class Cli { | ||||
private boolean debugMode = false; | |||||
private boolean debugEnabled = false; | |||||
private boolean displayVersionOnly = false; | private boolean displayVersionOnly = false; | ||||
private boolean displayStackTrace = false; | private boolean displayStackTrace = false; | ||||
private boolean interactive = false; | private boolean interactive = false; | ||||
this.logger = logger; | this.logger = logger; | ||||
} | } | ||||
boolean isDebugMode() { | |||||
return debugMode; | |||||
boolean isDebugEnabled() { | |||||
return debugEnabled; | |||||
} | } | ||||
boolean isDisplayVersionOnly() { | boolean isDisplayVersionOnly() { | ||||
} else if ("-X".equals(arg) || "--debug".equals(arg)) { | } else if ("-X".equals(arg) || "--debug".equals(arg)) { | ||||
props.setProperty("sonar.verbose", "true"); | props.setProperty("sonar.verbose", "true"); | ||||
displayStackTrace = true; | |||||
debugMode = true; | |||||
debugEnabled = true; | |||||
logger.setDebugEnabled(true); | logger.setDebugEnabled(true); | ||||
logger.setDisplayStackTrace(true); | logger.setDisplayStackTrace(true); | ||||
private void reset() { | private void reset() { | ||||
props.clear(); | props.clear(); | ||||
debugMode = false; | |||||
debugEnabled = false; | |||||
displayStackTrace = false; | displayStackTrace = false; | ||||
displayVersionOnly = false; | displayVersionOnly = false; | ||||
} | } |
public void setDisplayStackTrace(boolean displayStackTrace) { | public void setDisplayStackTrace(boolean displayStackTrace) { | ||||
this.displayStackTrace = displayStackTrace; | this.displayStackTrace = displayStackTrace; | ||||
} | } | ||||
public boolean isDebugEnabled() { | public boolean isDebugEnabled() { | ||||
return debugEnabled; | return debugEnabled; | ||||
} | } |
void execute() { | void execute() { | ||||
Stats stats = new Stats(logger).start(); | Stats stats = new Stats(logger).start(); | ||||
try { | try { | ||||
Properties p = conf.properties(); | Properties p = conf.properties(); | ||||
configureLogging(p); | |||||
init(p); | init(p); | ||||
runner.start(); | runner.start(); | ||||
} | } | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
displayExecutionResult(stats, "FAILURE"); | displayExecutionResult(stats, "FAILURE"); | ||||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace()); | |||||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled()); | |||||
shutdown.exit(Exit.ERROR); | shutdown.exit(Exit.ERROR); | ||||
} | } | ||||
runAnalysis(stats, p); | runAnalysis(stats, p); | ||||
} catch (Exception e) { | } catch (Exception e) { | ||||
displayExecutionResult(stats, "FAILURE"); | displayExecutionResult(stats, "FAILURE"); | ||||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace()); | |||||
showError("Error during SonarQube Scanner execution", e, cli.isDisplayStackTrace() || cli.isDebugEnabled()); | |||||
} | } | ||||
} while (waitForUser()); | } while (waitForUser()); | ||||
} | } | ||||
runner = runnerFactory.create(p); | runner = runnerFactory.create(p); | ||||
} | } | ||||
private void configureLogging(Properties props) throws IOException { | |||||
if("true".equals(props.getProperty("sonar.verbose")) | |||||
|| "DEBUG".equalsIgnoreCase(props.getProperty("sonar.log.level")) | |||||
|| "TRACE".equalsIgnoreCase(props.getProperty("sonar.log.level")) ) { | |||||
logger.setDebugEnabled(true); | |||||
logger.setDisplayStackTrace(true); | |||||
} | |||||
if(cli.isDisplayStackTrace()) { | |||||
logger.setDisplayStackTrace(true); | |||||
} | |||||
} | |||||
private void runAnalysis(Stats stats, Properties p) { | private void runAnalysis(Stats stats, Properties p) { | ||||
runner.runAnalysis(p); | runner.runAnalysis(p); | ||||
displayExecutionResult(stats, "SUCCESS"); | displayExecutionResult(stats, "SUCCESS"); | ||||
private void showError(String message, Throwable e, boolean showStackTrace) { | private void showError(String message, Throwable e, boolean showStackTrace) { | ||||
if (showStackTrace) { | if (showStackTrace) { | ||||
logger.error(message, e); | logger.error(message, e); | ||||
if (!cli.isDebugMode()) { | |||||
if (!cli.isDebugEnabled()) { | |||||
logger.error(""); | logger.error(""); | ||||
suggestDebugMode(); | suggestDebugMode(); | ||||
} | } | ||||
} | } | ||||
logger.error(""); | logger.error(""); | ||||
logger.error("To see the full stack trace of the errors, re-run SonarQube Scanner with the -e switch."); | logger.error("To see the full stack trace of the errors, re-run SonarQube Scanner with the -e switch."); | ||||
if (!cli.isDebugMode()) { | |||||
if (!cli.isDebugEnabled()) { | |||||
suggestDebugMode(); | suggestDebugMode(); | ||||
} | } | ||||
} | } |
public void should_parse_empty_arguments() { | public void should_parse_empty_arguments() { | ||||
cli.parse(new String[0]); | cli.parse(new String[0]); | ||||
assertThat(cli.properties()).isNotEmpty(); | assertThat(cli.properties()).isNotEmpty(); | ||||
assertThat(cli.isDebugMode()).isFalse(); | |||||
assertThat(cli.isDebugEnabled()).isFalse(); | |||||
assertThat(cli.isDisplayStackTrace()).isFalse(); | assertThat(cli.isDisplayStackTrace()).isFalse(); | ||||
assertThat(cli.isDisplayVersionOnly()).isFalse(); | assertThat(cli.isDisplayVersionOnly()).isFalse(); | ||||
} | } | ||||
@Test | @Test | ||||
public void should_enable_debug_mode() { | public void should_enable_debug_mode() { | ||||
cli.parse(new String[] {"-X"}); | cli.parse(new String[] {"-X"}); | ||||
assertThat(cli.isDebugMode()).isTrue(); | |||||
assertThat(cli.isDisplayStackTrace()).isTrue(); | |||||
assertThat(cli.isDebugEnabled()).isTrue(); | |||||
assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true"); | assertThat(cli.properties().get("sonar.verbose")).isEqualTo("true"); | ||||
} | } | ||||
@Test | @Test | ||||
public void should_enable_stacktrace_log() { | public void should_enable_stacktrace_log() { | ||||
cli.parse(new String[] {"-e"}); | cli.parse(new String[] {"-e"}); | ||||
assertThat(cli.isDebugMode()).isFalse(); | |||||
assertThat(cli.isDebugEnabled()).isFalse(); | |||||
assertThat(cli.isDisplayStackTrace()).isTrue(); | assertThat(cli.isDisplayStackTrace()).isTrue(); | ||||
assertThat(cli.properties().get("sonar.verbose")).isNull(); | assertThat(cli.properties().get("sonar.verbose")).isNull(); | ||||
} | } | ||||
@Test | @Test | ||||
public void should_disable_debug_mode_and_stacktrace_log_by_default() { | public void should_disable_debug_mode_and_stacktrace_log_by_default() { | ||||
cli.parse(new String[0]); | cli.parse(new String[0]); | ||||
assertThat(cli.isDebugMode()).isFalse(); | |||||
assertThat(cli.isDebugEnabled()).isFalse(); | |||||
assertThat(cli.isDisplayStackTrace()).isFalse(); | assertThat(cli.isDisplayStackTrace()).isFalse(); | ||||
assertThat(cli.properties().get("sonar.verbose")).isNull(); | assertThat(cli.properties().get("sonar.verbose")).isNull(); | ||||
} | } |
import java.util.Properties; | import java.util.Properties; | ||||
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.mockito.ArgumentCaptor; | |||||
import org.mockito.InOrder; | import org.mockito.InOrder; | ||||
import org.mockito.Mock; | import org.mockito.Mock; | ||||
import org.mockito.Mockito; | import org.mockito.Mockito; | ||||
import org.mockito.MockitoAnnotations; | import org.mockito.MockitoAnnotations; | ||||
import org.sonar.runner.api.EmbeddedRunner; | import org.sonar.runner.api.EmbeddedRunner; | ||||
import static org.fest.assertions.Assertions.assertThat; | |||||
import static org.mockito.Matchers.any; | import static org.mockito.Matchers.any; | ||||
import static org.mockito.Mockito.doThrow; | import static org.mockito.Mockito.doThrow; | ||||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | ||||
MockitoAnnotations.initMocks(this); | MockitoAnnotations.initMocks(this); | ||||
when(runnerFactory.create(any(Properties.class))).thenReturn(runner); | when(runnerFactory.create(any(Properties.class))).thenReturn(runner); | ||||
when(conf.properties()).thenReturn(properties); | when(conf.properties()).thenReturn(properties); | ||||
} | } | ||||
@Test | @Test | ||||
inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS); | inOrder.verify(shutdown, times(1)).exit(Exit.SUCCESS); | ||||
} | } | ||||
@Test | |||||
public void should_configure_logging() throws IOException { | |||||
Properties p = new Properties(); | |||||
p.put("sonar.verbose", "true"); | |||||
when(conf.properties()).thenReturn(p); | |||||
Main main = new Main(shutdown, cli, conf, runnerFactory, logs); | |||||
main.execute(); | |||||
// Logger used for callback should have debug enabled | |||||
verify(logs).setDebugEnabled(true); | |||||
verify(logs).setDisplayStackTrace(true); | |||||
ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class); | |||||
verify(runner).runAnalysis(propertiesCapture.capture()); | |||||
Properties analysisProps = propertiesCapture.getValue(); | |||||
assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true"); | |||||
} | |||||
@Test | |||||
public void should_configure_logging_trace() throws IOException { | |||||
Properties p = new Properties(); | |||||
p.put("sonar.log.level", "TRACE"); | |||||
when(conf.properties()).thenReturn(p); | |||||
Main main = new Main(shutdown, cli, conf, runnerFactory, logs); | |||||
main.execute(); | |||||
// Logger used for callback should have debug enabled | |||||
verify(logs).setDebugEnabled(true); | |||||
verify(logs).setDisplayStackTrace(true); | |||||
ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class); | |||||
verify(runner).runAnalysis(propertiesCapture.capture()); | |||||
Properties analysisProps = propertiesCapture.getValue(); | |||||
assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE"); | |||||
} | |||||
@Test(timeout = 30000) | @Test(timeout = 30000) | ||||
public void test_interactive_mode() throws IOException { | public void test_interactive_mode() throws IOException { | ||||
String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator(); | String inputStr = "qwe" + System.lineSeparator() + "qwe" + System.lineSeparator(); | ||||
input.close(); | input.close(); | ||||
when(cli.isInteractive()).thenReturn(true); | when(cli.isInteractive()).thenReturn(true); | ||||
when(cli.isDebugMode()).thenReturn(true); | |||||
when(cli.isDebugEnabled()).thenReturn(true); | |||||
when(cli.isDisplayStackTrace()).thenReturn(true); | when(cli.isDisplayStackTrace()).thenReturn(true); | ||||
Main main = new Main(shutdown, cli, conf, runnerFactory, logs); | Main main = new Main(shutdown, cli, conf, runnerFactory, logs); |