From 3b51f04bd4d3b16957823f7e84a58375039c9540 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 4 Feb 2011 02:46:42 +0300 Subject: [PATCH] SONAR-2172: New extension point - AbstractInitializer * Mark interface DependsUponMavenPlugin by annotation SupportedEnvironment with value "maven" * Extract CoberturaMavenInitializer from CoberturaSensor * Extract CloverMavenInitializer from CloverSensor --- .../clover/CloverMavenInitializer.java | 84 +++++++++++++++++ .../sonar/plugins/clover/CloverPlugin.java | 5 +- .../sonar/plugins/clover/CloverSensor.java | 65 +++----------- ...t.java => CloverMavenInitializerTest.java} | 22 +++-- .../plugins/clover/CloverPluginTest.java | 8 +- .../cobertura/CoberturaMavenInitializer.java | 90 +++++++++++++++++++ .../plugins/cobertura/CoberturaPlugin.java | 4 +- .../plugins/cobertura/CoberturaSensor.java | 27 ++---- .../CoberturaMavenInitializerTest.java | 63 +++++++++++++ .../cobertura/CoberturaSensorTest.java | 83 ++++++----------- .../sonar/api/batch/AbstractInitializer.java | 46 ++++++++++ .../sonar/api/batch/SupportedEnvironment.java | 5 +- .../batch/maven/DependsUponMavenPlugin.java | 4 +- 13 files changed, 357 insertions(+), 149 deletions(-) create mode 100644 plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenInitializer.java rename plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/{CloverSensorTest.java => CloverMavenInitializerTest.java} (74%) create mode 100644 plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/CoberturaMavenInitializer.java create mode 100644 plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/CoberturaMavenInitializerTest.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java diff --git a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenInitializer.java b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenInitializer.java new file mode 100644 index 00000000000..90026cc17fd --- /dev/null +++ b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverMavenInitializer.java @@ -0,0 +1,84 @@ +/* + * 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.clover; + +import org.apache.commons.configuration.Configuration; +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; + +/** + * Provides {@link CloverMavenPluginHandler} and configures correct path to report. + * Enabled only in Maven environment. + */ +@Phase(name = Name.PRE) +public class CloverMavenInitializer extends AbstractInitializer implements CoverageExtension, DependsUponMavenPlugin { + + private CloverMavenPluginHandler handler; + + public CloverMavenInitializer(CloverMavenPluginHandler 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(CloverConstants.REPORT_PATH_PROPERTY)) { + String report = getReportPathFromPluginConfiguration(project); + if (report == null) { + report = getDefaultReportPath(project); + } + conf.setProperty(CloverConstants.REPORT_PATH_PROPERTY, report); + } + } + + private String getDefaultReportPath(Project project) { + return project.getFileSystem().getReportOutputDir() + "/clover/clover.xml"; + } + + private String getReportPathFromPluginConfiguration(Project project) { + MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), CloverConstants.MAVEN_GROUP_ID, CloverConstants.MAVEN_ARTIFACT_ID); + if (plugin != null) { + String path = plugin.getParameter("outputDirectory"); + if (path != null) { + return path + "/clover.xml"; + } + } + return null; + } +} diff --git a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverPlugin.java b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverPlugin.java index 9e173aaebe6..9cbd76de8aa 100644 --- a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverPlugin.java +++ b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverPlugin.java @@ -41,8 +41,7 @@ import java.util.List; key = CloverConstants.VERSION_PROPERTY, name = "Clover version", description = "Override the Clover version to use. Default value is read from pom, else " + CloverConstants.DEFAULT_VERSION, - project = true, global = true) -}) + project = true, global = true) }) public class CloverPlugin implements Plugin { public String getKey() { @@ -58,6 +57,6 @@ public class CloverPlugin implements Plugin { } public List getExtensions() { - return Arrays.asList(CloverMavenPluginHandler.class, CloverSensor.class); + return Arrays.asList(CloverMavenPluginHandler.class, CloverMavenInitializer.class, CloverSensor.class); } } diff --git a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverSensor.java b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverSensor.java index 8d131178445..f6b8a2d3923 100644 --- a/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverSensor.java +++ b/plugins/sonar-clover-plugin/src/main/java/org/sonar/plugins/clover/CloverSensor.java @@ -19,34 +19,22 @@ */ package org.sonar.plugins.clover; +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.MavenPlugin; -import org.sonar.api.batch.maven.MavenPluginHandler; import org.sonar.api.resources.Project; -import java.io.File; - -public class CloverSensor extends AbstractCoverageExtension implements Sensor, DependsUponMavenPlugin { - - private CloverMavenPluginHandler handler; - - public CloverSensor(CloverMavenPluginHandler handler) { - this.handler = handler; - } +public class CloverSensor implements Sensor, CoverageExtension { - public MavenPluginHandler getMavenPluginHandler(Project project) { - if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) { - return handler; - } - return null; + public boolean shouldExecuteOnProject(Project project) { + return project.getFileSystem().hasJavaSourceFiles(); } public void analyse(Project project, SensorContext context) { - File report = getReport(project); + File report = getReportFromProperty(project); if (reportExists(report)) { new XmlReportParser(context).collect(report); } else { @@ -54,47 +42,16 @@ public class CloverSensor extends AbstractCoverageExtension implements Sensor, D } } - @Override - public boolean shouldExecuteOnProject(Project project) { - return super.shouldExecuteOnProject(project) && - project.getFileSystem().hasJavaSourceFiles(); - } - - protected File getReport(Project pom) { - File report = getReportFromProperty(pom); - if (report == null) { - report = getReportFromPluginConfiguration(pom); - } - if (report == null) { - report = getReportFromDefaultPath(pom); - } - return report; - } - - private File getReportFromProperty(Project pom) { - String path = (String) pom.getProperty(CloverConstants.REPORT_PATH_PROPERTY); + private File getReportFromProperty(Project project) { + String path = (String) project.getProperty(CloverConstants.REPORT_PATH_PROPERTY); if (path != null) { - return pom.getFileSystem().resolvePath(path); - } - return null; - } - - private File getReportFromPluginConfiguration(Project pom) { - MavenPlugin plugin = MavenPlugin.getPlugin(pom.getPom(), CloverConstants.MAVEN_GROUP_ID, CloverConstants.MAVEN_ARTIFACT_ID); - if (plugin != null) { - String path = plugin.getParameter("outputDirectory"); - if (path != null) { - return new File(pom.getFileSystem().resolvePath(path), "clover.xml"); - } + return project.getFileSystem().resolvePath(path); } return null; } - private File getReportFromDefaultPath(Project pom) { - return new File(pom.getFileSystem().getReportOutputDir(), "clover/clover.xml"); - } - private boolean reportExists(File report) { return report != null && report.exists() && report.isFile(); } + } diff --git a/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverSensorTest.java b/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverMavenInitializerTest.java similarity index 74% rename from plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverSensorTest.java rename to plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverMavenInitializerTest.java index 06003ff2baf..f2ead9ddf84 100644 --- a/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverSensorTest.java +++ b/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverMavenInitializerTest.java @@ -20,6 +20,7 @@ package org.sonar.plugins.clover; import org.apache.commons.configuration.PropertiesConfiguration; +import org.junit.Before; import org.junit.Test; import org.sonar.api.resources.Project; @@ -29,26 +30,33 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class CloverSensorTest { +public class CloverMavenInitializerTest { + + private Project project; + private CloverMavenInitializer initializer; + + @Before + public void setUp() { + project = mock(Project.class); + initializer = new CloverMavenInitializer(new CloverMavenPluginHandler(new PropertiesConfiguration())); + } @Test public void doNotExecuteMavenPluginIfReuseReports() { - Project project = mock(Project.class); when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS); - assertThat(new CloverSensor(new CloverMavenPluginHandler(new PropertiesConfiguration())).getMavenPluginHandler(project), nullValue()); + assertThat(initializer.getMavenPluginHandler(project), nullValue()); } @Test public void doNotExecuteMavenPluginIfStaticAnalysis() { - Project project = mock(Project.class); when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC); - assertThat(new CloverSensor(new CloverMavenPluginHandler(new PropertiesConfiguration())).getMavenPluginHandler(project), nullValue()); + assertThat(initializer.getMavenPluginHandler(project), nullValue()); } @Test public void executeMavenPluginIfDynamicAnalysis() { - Project project = mock(Project.class); when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC); - assertThat(new CloverSensor(new CloverMavenPluginHandler(new PropertiesConfiguration())).getMavenPluginHandler(project), not(nullValue())); + assertThat(initializer.getMavenPluginHandler(project), not(nullValue())); } + } diff --git a/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverPluginTest.java b/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverPluginTest.java index 9efcb86c256..a759caee39c 100644 --- a/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverPluginTest.java +++ b/plugins/sonar-clover-plugin/src/test/java/org/sonar/plugins/clover/CloverPluginTest.java @@ -19,15 +19,15 @@ */ package org.sonar.plugins.clover; -import org.junit.Test; - -import static org.hamcrest.Matchers.is; +import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertThat; +import org.junit.Test; + public class CloverPluginTest { @Test public void someExtensions() { - assertThat(new CloverPlugin().getExtensions().size(), is(2)); + assertThat(new CloverPlugin().getExtensions().size(), greaterThan(1)); } } 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> list = new ArrayList>(); 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( diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java new file mode 100644 index 00000000000..9c34f2869c9 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/AbstractInitializer.java @@ -0,0 +1,46 @@ +/* + * 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.api.batch; + +import org.sonar.api.batch.Phase.Name; +import org.sonar.api.resources.Project; + +/** + * @since 2.6 + */ +@Phase(name = Name.PRE) +public abstract class AbstractInitializer implements Sensor { + + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + public void analyse(Project project, SensorContext context) { + prepare(project); + } + + public abstract void prepare(Project project); + + @Override + public String toString() { + return getClass().getSimpleName(); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java index 7862c224b29..5a120ebbbe3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SupportedEnvironment.java @@ -26,8 +26,9 @@ import java.lang.annotation.Target; /** * This annotation allows to specify in which environments {@link org.sonar.api.BatchExtension} would be active. - * Consult to {@link org.sonar.api.platform.Environment} to find possible values, for example - "maven2". - * We strictly recommend you to not overuse this annotation - most preferable is to design extensions to work in all environments. + * For example: "maven", "ant". + * Usage of this annotation is discouraged and we strictly recommend you to not overuse it. + * Most preferable is to design extensions to work in all environments. * * @since 2.6 */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java index 26e69a20f43..11ea8d00f75 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/maven/DependsUponMavenPlugin.java @@ -20,13 +20,15 @@ package org.sonar.api.batch.maven; import org.sonar.api.BatchExtension; +import org.sonar.api.batch.SupportedEnvironment; import org.sonar.api.resources.Project; /** * Used for Sensors and PostJobs only. - * + * * @since 1.10 */ +@SupportedEnvironment("maven") public interface DependsUponMavenPlugin extends BatchExtension { MavenPluginHandler getMavenPluginHandler(Project project); -- 2.39.5