diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-02-04 02:46:42 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-02-05 02:58:16 +0300 |
commit | 3b51f04bd4d3b16957823f7e84a58375039c9540 (patch) | |
tree | 4fd443083d96c0f38a4711d95ed57087b52c0a1f /plugins/sonar-cobertura-plugin/src/main/java | |
parent | 04407c6b384ee7ec9ecdcc33485d7a6ef05c3026 (diff) | |
download | sonarqube-3b51f04bd4d3b16957823f7e84a58375039c9540.tar.gz sonarqube-3b51f04bd4d3b16957823f7e84a58375039c9540.zip |
SONAR-2172: New extension point - AbstractInitializer
* Mark interface DependsUponMavenPlugin by annotation
SupportedEnvironment with value "maven"
* Extract CoberturaMavenInitializer from CoberturaSensor
* Extract CloverMavenInitializer from CloverSensor
Diffstat (limited to 'plugins/sonar-cobertura-plugin/src/main/java')
3 files changed, 98 insertions, 23 deletions
diff --git a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaMavenInitializer.java b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaMavenInitializer.java new file mode 100644 index 00000000000..502cae448ad --- /dev/null +++ b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaMavenInitializer.java @@ -0,0 +1,90 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.cobertura; + +import org.apache.commons.configuration.Configuration; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.AbstractInitializer; +import org.sonar.api.batch.CoverageExtension; +import org.sonar.api.batch.Phase; +import org.sonar.api.batch.Phase.Name; +import org.sonar.api.batch.maven.DependsUponMavenPlugin; +import org.sonar.api.batch.maven.MavenPlugin; +import org.sonar.api.batch.maven.MavenPluginHandler; +import org.sonar.api.resources.Project; +import org.sonar.plugins.cobertura.api.CoberturaUtils; + +/** + * Provides {@link CoberturaMavenPluginHandler} and configures correct path to report. + * Enabled only in Maven environment. + */ +@Phase(name = Name.PRE) +public class CoberturaMavenInitializer extends AbstractInitializer implements CoverageExtension, DependsUponMavenPlugin { + + private CoberturaMavenPluginHandler handler; + + public CoberturaMavenInitializer(CoberturaMavenPluginHandler handler) { + this.handler = handler; + } + + public MavenPluginHandler getMavenPluginHandler(Project project) { + if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) { + return handler; + } + return null; + } + + @Override + public boolean shouldExecuteOnProject(Project project) { + return project.getAnalysisType().isDynamic(true) && + project.getFileSystem().hasJavaSourceFiles(); + } + + @Override + public void prepare(Project project) { + Configuration conf = project.getConfiguration(); + if (conf.containsKey(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)) { + String report = getReportPathFromPluginConfiguration(project); + if (report == null) { + report = getDefaultReportPath(project); + } + conf.setProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY, report); + } + } + + private static String getDefaultReportPath(Project project) { + return project.getFileSystem().getReportOutputDir() + "/cobertura/coverage.xml"; + } + + private static String getReportPathFromPluginConfiguration(Project project) { + MavenPlugin mavenPlugin = MavenPlugin.getPlugin( + project.getPom(), + CoberturaUtils.COBERTURA_GROUP_ID, + CoberturaUtils.COBERTURA_ARTIFACT_ID); + if (mavenPlugin != null) { + String path = mavenPlugin.getParameter("outputDirectory"); + if (path != null) { + return path + "/coverage.xml"; + } + } + return null; + } + +} diff --git a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaPlugin.java b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaPlugin.java index 11f3f1ab067..2ab62423cb1 100644 --- a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaPlugin.java +++ b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaPlugin.java @@ -37,8 +37,7 @@ import java.util.List; name = "Maxmem", description = "Maximum memory to pass to JVM of Cobertura processes", project = true, - global = true) -}) + global = true) }) public class CoberturaPlugin implements Plugin { public String getKey() { @@ -57,6 +56,7 @@ public class CoberturaPlugin implements Plugin { List<Class<? extends Extension>> list = new ArrayList<Class<? extends Extension>>(); list.add(CoberturaSensor.class); list.add(CoberturaMavenPluginHandler.class); + list.add(CoberturaMavenInitializer.class); return list; } diff --git a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java index f620b037d9d..3464d5e3a2c 100644 --- a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java +++ b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaSensor.java @@ -19,31 +19,22 @@ */ package org.sonar.plugins.cobertura; +import java.io.File; + import org.slf4j.LoggerFactory; -import org.sonar.api.batch.AbstractCoverageExtension; +import org.sonar.api.batch.CoverageExtension; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.maven.DependsUponMavenPlugin; -import org.sonar.api.batch.maven.MavenPluginHandler; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.plugins.cobertura.api.AbstractCoberturaParser; import org.sonar.plugins.cobertura.api.CoberturaUtils; -import java.io.File; - -public class CoberturaSensor extends AbstractCoverageExtension implements Sensor, DependsUponMavenPlugin { - - private CoberturaMavenPluginHandler handler; - - public CoberturaSensor(CoberturaMavenPluginHandler handler) { - this.handler = handler; - } +public class CoberturaSensor implements Sensor, CoverageExtension { - @Override public boolean shouldExecuteOnProject(Project project) { - return super.shouldExecuteOnProject(project) && project.getFileSystem().hasJavaSourceFiles(); + return project.getFileSystem().hasJavaSourceFiles(); } public void analyse(Project project, SensorContext context) { @@ -53,13 +44,6 @@ public class CoberturaSensor extends AbstractCoverageExtension implements Sensor } } - public MavenPluginHandler getMavenPluginHandler(Project project) { - if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) { - return handler; - } - return null; - } - protected void parseReport(File xmlFile, final SensorContext context) { LoggerFactory.getLogger(CoberturaSensor.class).info("parsing {}", xmlFile); new AbstractCoberturaParser() { @@ -74,4 +58,5 @@ public class CoberturaSensor extends AbstractCoverageExtension implements Sensor public String toString() { return getClass().getSimpleName(); } + } |