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 | |
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')
5 files changed, 189 insertions, 78 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(); } + } diff --git a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenInitializerTest.java b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenInitializerTest.java new file mode 100644 index 00000000000..0c252abdfef --- /dev/null +++ b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenInitializerTest.java @@ -0,0 +1,63 @@ +/* + * 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.junit.Before; +import org.junit.Test; +import org.sonar.api.resources.Project; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.IsNot.not; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class CoberturaMavenInitializerTest { + + private Project project; + private CoberturaMavenInitializer initializer; + + @Before + public void setUp() { + project = mock(Project.class); + initializer = new CoberturaMavenInitializer(new CoberturaMavenPluginHandler()); + } + + @Test + public void doNotExecuteMavenPluginIfReuseReports() { + when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS); + assertThat(initializer.getMavenPluginHandler(project), nullValue()); + } + + @Test + public void doNotExecuteMavenPluginIfStaticAnalysis() { + when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC); + assertThat(initializer.getMavenPluginHandler(project), nullValue()); + } + + @Test + public void executeMavenPluginIfDynamicAnalysis() { + when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC); + assertThat(initializer.getMavenPluginHandler(project), not(nullValue())); + assertThat(initializer.getMavenPluginHandler(project).getArtifactId(), is("cobertura-maven-plugin")); + } + +} diff --git a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java index 865aece0c53..0b57c57523c 100644 --- a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java +++ b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaSensorTest.java @@ -19,26 +19,12 @@ */ package org.sonar.plugins.cobertura; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyDouble; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import org.junit.Test; import org.sonar.api.batch.SensorContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Measure; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.JavaPackage; -import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.test.IsMeasure; import org.sonar.api.test.IsResource; @@ -46,35 +32,22 @@ import org.sonar.api.test.IsResource; import java.io.File; import java.net.URISyntaxException; -public class CoberturaSensorTest { - - @Test - public void doNotExecuteMavenPluginIfReuseReports() { - Project project = mock(Project.class); - when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS); - assertThat(new CoberturaSensor(new CoberturaMavenPluginHandler()).getMavenPluginHandler(project), nullValue()); - } - - @Test - public void doNotExecuteMavenPluginIfStaticAnalysis() { - Project project = mock(Project.class); - when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC); - assertThat(new CoberturaSensor(new CoberturaMavenPluginHandler()).getMavenPluginHandler(project), nullValue()); - } +import static org.hamcrest.CoreMatchers.is; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyDouble; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; - @Test - public void executeMavenPluginIfDynamicAnalysis() { - Project project = mock(Project.class); - when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC); - assertThat(new CoberturaSensor(new CoberturaMavenPluginHandler()).getMavenPluginHandler(project), not(nullValue())); - assertThat(new CoberturaSensor(new CoberturaMavenPluginHandler()).getMavenPluginHandler(project).getArtifactId(), - is("cobertura-maven-plugin")); - } +public class CoberturaSensorTest { @Test public void doNotCollectProjectCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble()); } @@ -82,7 +55,7 @@ public class CoberturaSensorTest { @Test public void doNotCollectProjectLineCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble()); verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA))); @@ -91,7 +64,7 @@ public class CoberturaSensorTest { @Test public void doNotCollectProjectBranchCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble()); verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE_HITS_DATA))); @@ -100,7 +73,7 @@ public class CoberturaSensorTest { @Test public void collectPackageLineCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.LINE_COVERAGE), anyDouble()); verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.UNCOVERED_LINES), anyDouble()); @@ -109,7 +82,7 @@ public class CoberturaSensorTest { @Test public void collectPackageBranchCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.BRANCH_COVERAGE), anyDouble()); verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.UNCOVERED_CONDITIONS), anyDouble()); @@ -118,7 +91,7 @@ public class CoberturaSensorTest { @Test public void packageCoverageIsCalculatedLaterByDecorator() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.COVERAGE), anyDouble()); } @@ -127,7 +100,7 @@ public class CoberturaSensorTest { public void collectFileLineCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); final JavaFile file = new JavaFile("org.apache.commons.chain.config.ConfigParser"); // verify(context).saveMeasure(eq(file), argThat(new IsMeasure(CoreMetrics.LINE_COVERAGE, 83.3))); @@ -139,7 +112,7 @@ public class CoberturaSensorTest { public void collectFileBranchCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); final JavaFile file = new JavaFile("org.apache.commons.chain.config.ConfigParser"); verify(context).saveMeasure(eq(file), argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE, 66.7))); @@ -151,14 +124,14 @@ public class CoberturaSensorTest { public void testDoNotSaveMeasureOnResourceWhichDoesntExistInTheContext() throws URISyntaxException { SensorContext context = mock(SensorContext.class); when(context.getResource(any(Resource.class))).thenReturn(null); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context, never()).saveMeasure(any(Resource.class), any(Measure.class)); } @Test public void javaInterfaceHasNoCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); final JavaFile interfaze = new JavaFile("org.apache.commons.chain.Chain"); verify(context, never()).saveMeasure(eq(interfaze), argThat(new IsMeasure(CoreMetrics.COVERAGE))); @@ -178,7 +151,7 @@ public class CoberturaSensorTest { "/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldInsertCoverageAtFileLevel/coverage.xml").toURI()); SensorContext context = mock(SensorContext.class); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - new CoberturaSensor(null).parseReport(coverage, context); + new CoberturaSensor().parseReport(coverage, context); verify(context).saveMeasure(argThat(new IsResource(Resource.SCOPE_ENTITY, Resource.QUALIFIER_CLASS, "org.sonar.samples.InnerClass")), argThat(new IsMeasure(CoreMetrics.LINE_COVERAGE, 37.1))); @@ -234,28 +207,28 @@ public class CoberturaSensorTest { verify(context) .saveMeasure( - eq(new JavaFile("org.sonar.samples.InnerClass")), - argThat(new IsMeasure( - CoreMetrics.COVERAGE_LINE_HITS_DATA, - "22=2;25=0;26=0;29=0;30=0;31=0;34=1;35=1;36=1;37=0;39=1;41=1;44=2;46=1;47=1;50=0;51=0;52=0;53=0;55=0;57=0;60=0;61=0;64=1;71=1;73=1;76=0;77=0;80=0;81=0;85=0;87=0;91=0;93=0;96=1"))); + eq(new JavaFile("org.sonar.samples.InnerClass")), + argThat(new IsMeasure( + CoreMetrics.COVERAGE_LINE_HITS_DATA, + "22=2;25=0;26=0;29=0;30=0;31=0;34=1;35=1;36=1;37=0;39=1;41=1;44=2;46=1;47=1;50=0;51=0;52=0;53=0;55=0;57=0;60=0;61=0;64=1;71=1;73=1;76=0;77=0;80=0;81=0;85=0;87=0;91=0;93=0;96=1"))); } @Test public void collectFileLineHitsData() throws URISyntaxException { SensorContext context = mock(SensorContext.class); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); verify(context).saveMeasure( eq(new JavaFile("org.apache.commons.chain.impl.CatalogBase")), argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA, - "111=18;121=0;122=0;125=0;126=0;127=0;128=0;131=0;133=0;48=117;56=234;66=0;67=0;68=0;84=999;86=999;98=318"))); + "111=18;121=0;122=0;125=0;126=0;127=0;128=0;131=0;133=0;48=117;56=234;66=0;67=0;68=0;84=999;86=999;98=318"))); } @Test public void collectFileBranchHitsData() throws URISyntaxException { SensorContext context = mock(SensorContext.class); when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - new CoberturaSensor(null).parseReport(getCoverageReport(), context); + new CoberturaSensor().parseReport(getCoverageReport(), context); // no conditions verify(context, never()).saveMeasure( |