From a7643bd79d1ccde8dba0c2ded5cbb005bab5bbbd Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Thu, 10 Mar 2016 17:22:51 +0100 Subject: [PATCH] improve quality --- .../java/org/sonarsource/scanner/cli/Cli.java | 5 +- .../org/sonarsource/scanner/cli/MainTest.java | 60 ++++++++++++------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/sonarsource/scanner/cli/Cli.java b/src/main/java/org/sonarsource/scanner/cli/Cli.java index b660a86..f8f1fb8 100644 --- a/src/main/java/org/sonarsource/scanner/cli/Cli.java +++ b/src/main/java/org/sonarsource/scanner/cli/Cli.java @@ -62,7 +62,7 @@ class Cli { props.putAll(System.getProperties()); for (int i = 0; i < args.length; i++) { String arg = args[i]; - if (i == 0 && !arg.startsWith("-")) { + if (i == 0 && arg.charAt(0) != '-') { props.setProperty(RunnerProperties.TASK, arg); } else if ("-h".equals(arg) || "--help".equals(arg)) { @@ -118,7 +118,8 @@ class Cli { } private static void appendPropertyTo(String arg, Properties props) { - final String key, value; + final String key; + final String value; int j = arg.indexOf('='); if (j == -1) { key = arg; diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 1936ca8..c0fbd65 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -38,6 +38,7 @@ import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -98,9 +99,32 @@ public class MainTest { @Test public void show_error_stacktrace() { + Exception e = show_error(false, true); + verify(logs).error("Error during SonarQube Scanner execution", e); + verify(logs).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); + } + + @Test + public void show_error_debug() { + Exception e = show_error(true, false); + + verify(logs).error("Error during SonarQube Scanner execution", e); + verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); + } + + @Test + public void show_error_debug_stack() { + Exception e = show_error(true, true); + + verify(logs).error("Error during SonarQube Scanner execution", e); + verify(logs, never()).error("Re-run SonarQube Scanner using the -X switch to enable full debug logging."); + } + + private Exception show_error(boolean debugEnabled, boolean stackTraceEnabled) { Exception e = new NullPointerException("NPE"); e = new IllegalStateException("Error", e); - when(cli.isDisplayStackTrace()).thenReturn(true); + when(cli.isDebugEnabled()).thenReturn(debugEnabled); + when(cli.isDisplayStackTrace()).thenReturn(stackTraceEnabled); EmbeddedRunner runner = mock(EmbeddedRunner.class); doThrow(e).when(runner).runAnalysis(any(Properties.class)); @@ -111,7 +135,7 @@ public class MainTest { verify(runner).stop(); verify(shutdown).exit(Exit.ERROR); - verify(logs).error("Error during SonarQube Scanner execution", e); + return e; } @Test @@ -164,28 +188,25 @@ public class MainTest { @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 propertiesCapture = ArgumentCaptor.forClass(Properties.class); - verify(runner).runAnalysis(propertiesCapture.capture()); - - Properties analysisProps = propertiesCapture.getValue(); + Properties analysisProps = testLogging("sonar.verbose", "true"); assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true"); } @Test public void should_configure_logging_trace() throws IOException { + Properties analysisProps = testLogging("sonar.log.level", "TRACE"); + assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE"); + } + + @Test + public void should_configure_logging_debug() throws IOException { + Properties analysisProps = testLogging("sonar.log.level", "DEBUG"); + assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG"); + } + + private Properties testLogging(String propKey, String propValue) throws IOException { Properties p = new Properties(); - p.put("sonar.log.level", "TRACE"); + p.put(propKey, propValue); when(conf.properties()).thenReturn(p); Main main = new Main(shutdown, cli, conf, runnerFactory, logs); @@ -198,8 +219,7 @@ public class MainTest { ArgumentCaptor propertiesCapture = ArgumentCaptor.forClass(Properties.class); verify(runner).runAnalysis(propertiesCapture.capture()); - Properties analysisProps = propertiesCapture.getValue(); - assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE"); + return propertiesCapture.getValue(); } @Test(timeout = 30000) -- 2.39.5