diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-09-15 16:24:36 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-09-16 13:36:49 +0200 |
commit | 1d6a79e5221587c98f451b233b99eeea7d66f4d7 (patch) | |
tree | 6b7c5260bd14d3bcce06cc7fdc44eedd18d9caf5 /src/test/java/org | |
parent | 12c373fad66da9ccb9cfb2ce7e3c5d2de4f60fe2 (diff) | |
download | sonar-scanner-cli-1d6a79e5221587c98f451b233b99eeea7d66f4d7.tar.gz sonar-scanner-cli-1d6a79e5221587c98f451b233b99eeea7d66f4d7.zip |
SQSCANNER-23 Support the new 'MessageException' unchecked exception and log by default the error stack trace only when a non-MessageException is thrown
Diffstat (limited to 'src/test/java/org')
3 files changed, 57 insertions, 17 deletions
diff --git a/src/test/java/org/sonar/api/utils/MessageException/MessageException.java b/src/test/java/org/sonar/api/utils/MessageException/MessageException.java new file mode 100644 index 0000000..163fc0f --- /dev/null +++ b/src/test/java/org/sonar/api/utils/MessageException/MessageException.java @@ -0,0 +1,26 @@ +/* + * SonarQube Scanner + * Copyright (C) 2011-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils.MessageException; + +public class MessageException extends RuntimeException { + public MessageException(String msg) { + super(msg); + } +} diff --git a/src/test/java/org/sonarsource/scanner/cli/CliTest.java b/src/test/java/org/sonarsource/scanner/cli/CliTest.java index 840f35b..640ca8d 100644 --- a/src/test/java/org/sonarsource/scanner/cli/CliTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/CliTest.java @@ -35,7 +35,6 @@ public class CliTest { cli.parse(new String[0]); assertThat(cli.properties()).isNotEmpty(); assertThat(cli.isDebugEnabled()).isFalse(); - assertThat(cli.isDisplayStackTrace()).isFalse(); assertThat(cli.isDisplayVersionOnly()).isFalse(); } @@ -48,6 +47,11 @@ public class CliTest { } @Test + public void should_not_fail_with_errors_option() { + cli.parse(new String[] {"-e"}); + } + + @Test public void should_parse_optional_task() { cli.parse(new String[] {"-D", "foo=bar"}); assertThat(cli.properties().get("sonar.task")).isNull(); @@ -67,7 +71,6 @@ public class CliTest { public void should_enable_stacktrace_log() { cli.parse(new String[] {"-e"}); assertThat(cli.isDebugEnabled()).isFalse(); - assertThat(cli.isDisplayStackTrace()).isTrue(); assertThat(cli.properties().get("sonar.verbose")).isNull(); } @@ -75,7 +78,6 @@ public class CliTest { public void should_disable_debug_mode_and_stacktrace_log_by_default() { cli.parse(new String[0]); assertThat(cli.isDebugEnabled()).isFalse(); - assertThat(cli.isDisplayStackTrace()).isFalse(); assertThat(cli.properties().get("sonar.verbose")).isNull(); } diff --git a/src/test/java/org/sonarsource/scanner/cli/MainTest.java b/src/test/java/org/sonarsource/scanner/cli/MainTest.java index 450d0a7..dc4b77d 100644 --- a/src/test/java/org/sonarsource/scanner/cli/MainTest.java +++ b/src/test/java/org/sonarsource/scanner/cli/MainTest.java @@ -28,6 +28,7 @@ import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.sonar.api.utils.MessageException.MessageException; import org.sonarsource.scanner.api.EmbeddedScanner; import org.sonarsource.scanner.api.ScanProperties; @@ -95,33 +96,34 @@ public class MainTest { } @Test - public void show_error_stacktrace() { - Exception e = show_error(false, true); - verify(logs).error("Error during SonarQube Scanner execution", e); + public void show_error() { + Exception e = createException(false, false); + testException(e, false); + + verify(logs).error("Error during SonarQube Scanner execution"); 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); + public void show_error_MessageException() { + Exception e = createException(false, true); + testException(e, 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."); + verify(logs).error("Error during SonarQube Scanner execution"); + verify(logs).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); + public void show_error_debug() { + Exception e = createException(true, false); + testException(e, 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); + private void testException(Exception e, boolean debugEnabled) { when(cli.isDebugEnabled()).thenReturn(debugEnabled); - when(cli.isDisplayStackTrace()).thenReturn(stackTraceEnabled); EmbeddedScanner runner = mock(EmbeddedScanner.class); doThrow(e).when(runner).runAnalysis(any(Properties.class)); @@ -132,6 +134,16 @@ public class MainTest { verify(runner).stop(); verify(exit).exit(Exit.ERROR); + } + + private Exception createException(boolean debugEnabled, boolean messageException) { + Exception e; + if (messageException) { + e = new MessageException("my message"); + } else { + e = new IllegalStateException("Error", new NullPointerException("NPE")); + } + return e; } @@ -151,7 +163,7 @@ public class MainTest { inOrder.verify(runnerFactory, times(1)).create(p); inOrder.verify(exit, times(1)).exit(Exit.SUCCESS); } - + @Test public void should_skip() throws IOException { Properties p = new Properties(); |