diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-08-16 17:46:06 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-08-16 17:46:06 +0200 |
commit | 3125bcc453e3973298b7cf417b9ea8a158ba3b8a (patch) | |
tree | b0a5838b76870f46469310aff5ab5138d174eb72 /sonar-maven-plugin | |
parent | 906c2b413615c37ead1371bb1422be4c733747fd (diff) | |
download | sonarqube-3125bcc453e3973298b7cf417b9ea8a158ba3b8a.tar.gz sonarqube-3125bcc453e3973298b7cf417b9ea8a158ba3b8a.zip |
SONAR-4547 improve support of MessageException
Diffstat (limited to 'sonar-maven-plugin')
4 files changed, 163 insertions, 19 deletions
diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/ExceptionHandling.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/ExceptionHandling.java new file mode 100644 index 00000000000..2fcd44e55e2 --- /dev/null +++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/ExceptionHandling.java @@ -0,0 +1,39 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.maven; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; + +class ExceptionHandling { + + static RuntimeException handle(Exception e, Log log) throws MojoExecutionException { + Throwable source = e; + if (e.getClass().getName().equals("org.sonar.runner.impl.RunnerException") && e.getCause() != null) { + source = e.getCause(); + } + log.error(source.getMessage()); + throw new MojoExecutionException(source.getMessage(), source); + } + + static RuntimeException handle(String message, Log log) throws MojoExecutionException { + return handle(new MojoExecutionException(message), log); + } +} diff --git a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java index 14213720c23..d62389c6970 100644 --- a/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java +++ b/sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java @@ -29,7 +29,6 @@ import org.apache.maven.execution.RuntimeInformation; import org.apache.maven.lifecycle.LifecycleExecutor; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectBuilder; import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder; @@ -127,23 +126,25 @@ public final class SonarMojo extends AbstractMojo { */ RuntimeInformation runtimeInformation; - public void execute() throws MojoExecutionException, MojoFailureException { + @Override + public void execute() throws MojoExecutionException { ArtifactVersion mavenVersion = getMavenVersion(); if (mavenVersion.getMajorVersion() == 2 && mavenVersion.getMinorVersion() < 2) { - throw new MojoExecutionException("Please use at least Maven 2.2.x to perform SonarQube analysis (current version is " + mavenVersion.toString() + ")"); + ExceptionHandling.handle("Please use at least Maven 2.2.x to perform SonarQube analysis (current version is " + mavenVersion.toString() + ")", getLog()); } - EmbeddedRunner runner = EmbeddedRunner.create() + try { + EmbeddedRunner runner = EmbeddedRunner.create() .setApp("Maven", mavenVersion.toString()) .addProperties(session.getExecutionProperties()) .addProperties(project.getModel().getProperties()) // Add user properties (ie command line arguments -Dsonar.xxx=yyyy) in last position to override all other .addProperties(session.getUserProperties()); - String encoding = getSourceEncoding(project); - if (encoding != null) { - runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, encoding); - } - runner + String encoding = getSourceEncoding(project); + if (encoding != null) { + runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, encoding); + } + runner .setProperty(ScanProperties.PROJECT_KEY, getSonarKey(project)) .setProperty(RunnerProperties.WORK_DIR, getSonarWorkDir(project).getAbsolutePath()) .setProperty(ScanProperties.PROJECT_BASEDIR, project.getBasedir().getAbsolutePath()) @@ -151,25 +152,28 @@ public final class SonarMojo extends AbstractMojo { .setProperty(ScanProperties.PROJECT_NAME, toString(project.getName())) .setProperty(ScanProperties.PROJECT_DESCRIPTION, toString(project.getDescription())) .setProperty(ScanProperties.PROJECT_SOURCE_DIRS, "."); - // Exclude log implementation to not conflict with Maven 3.1 logging impl - runner.mask("org.slf4j.LoggerFactory") + // Exclude log implementation to not conflict with Maven 3.1 logging impl + runner.mask("org.slf4j.LoggerFactory") // Include slf4j Logger that is exposed by some Sonar components .unmask("org.slf4j.Logger") .unmask("org.slf4j.ILoggerFactory") - // Exclude other slf4j classes - // .unmask("org.slf4j.impl.") + // Exclude other slf4j classes + // .unmask("org.slf4j.impl.") .mask("org.slf4j.") - // Exclude logback + // Exclude logback .mask("ch.qos.logback.") .mask("org.sonar.") - // Include everything else + // Include everything else .unmask(""); - runner.addExtensions(session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, + runner.addExtensions(session, getLog(), lifecycleExecutor, artifactFactory, localRepository, artifactMetadataSource, artifactCollector, dependencyTreeBuilder, projectBuilder); - if (getLog().isDebugEnabled()) { - runner.setProperty("sonar.verbose", "true"); + if (getLog().isDebugEnabled()) { + runner.setProperty("sonar.verbose", "true"); + } + runner.execute(); + } catch (Exception e) { + throw ExceptionHandling.handle(e, getLog()); } - runner.execute(); } private ArtifactVersion getMavenVersion() { diff --git a/sonar-maven-plugin/src/test/java/org/sonar/maven/ExceptionHandlingTest.java b/sonar-maven-plugin/src/test/java/org/sonar/maven/ExceptionHandlingTest.java new file mode 100644 index 00000000000..59d579f33d5 --- /dev/null +++ b/sonar-maven-plugin/src/test/java/org/sonar/maven/ExceptionHandlingTest.java @@ -0,0 +1,75 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.maven; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; +import org.junit.Test; +import org.sonar.runner.impl.RunnerException; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class ExceptionHandlingTest { + + private static final String MESSAGE = "the error message"; + + @Test + public void should_log_message_and_throw_exception() throws Exception { + Log log = mock(Log.class); + try { + ExceptionHandling.handle(MESSAGE, log); + fail(); + } catch (MojoExecutionException e) { + assertThat(e.getMessage()).isEqualTo(MESSAGE); + verify(log).error(MESSAGE); + } + } + + @Test + public void should_log_message_and_rethrow_exception() throws Exception { + Log log = mock(Log.class); + IllegalStateException cause = new IllegalStateException(MESSAGE); + try { + ExceptionHandling.handle(cause, log); + fail(); + } catch (MojoExecutionException e) { + assertThat(e.getMessage()).isEqualTo(MESSAGE); + assertThat(e.getCause()).isSameAs(cause); + verify(log).error(MESSAGE); + } + } + + @Test + public void should_hide_sonar_runner_stacktrace() throws Exception { + Log log = mock(Log.class); + IllegalStateException cause = new IllegalStateException(MESSAGE); + try { + ExceptionHandling.handle(new RunnerException(cause), log); + fail(); + } catch (MojoExecutionException e) { + assertThat(e.getMessage()).isEqualTo(MESSAGE); + assertThat(e.getCause()).isSameAs(cause); + verify(log).error(MESSAGE); + } + } +} diff --git a/sonar-maven-plugin/src/test/java/org/sonar/runner/impl/RunnerException.java b/sonar-maven-plugin/src/test/java/org/sonar/runner/impl/RunnerException.java new file mode 100644 index 00000000000..aff129c5355 --- /dev/null +++ b/sonar-maven-plugin/src/test/java/org/sonar/runner/impl/RunnerException.java @@ -0,0 +1,26 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.runner.impl; + +public class RunnerException extends RuntimeException { + public RunnerException(Throwable throwable) { + super(throwable); + } +} |