diff options
author | Godin <mandrikov@gmail.com> | 2010-10-20 11:10:38 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-10-20 11:10:38 +0000 |
commit | 77c389b4620b3399fbb7946892c0cf1c420ec3e6 (patch) | |
tree | 0381865878f65a3bbffba9a88ff38d18d3f64936 /plugins/sonar-cobertura-plugin/src | |
parent | ab6d196d40e7879b96c3e944a379dc9b05fa934e (diff) | |
download | sonarqube-77c389b4620b3399fbb7946892c0cf1c420ec3e6.tar.gz sonarqube-77c389b4620b3399fbb7946892c0cf1c420ec3e6.zip |
SONAR-1808: Add CoberturaUtils
Diffstat (limited to 'plugins/sonar-cobertura-plugin/src')
4 files changed, 133 insertions, 92 deletions
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 3bcf2497ee7..1cb6061d8ab 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 @@ -20,17 +20,16 @@ package org.sonar.plugins.cobertura; import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; import org.sonar.api.batch.AbstractCoverageExtension; 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.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; @@ -48,7 +47,7 @@ public class CoberturaSensor extends AbstractCoverageExtension implements Sensor } public void analyse(Project project, SensorContext context) { - File report = getReport(project); + File report = CoberturaUtils.getReport(project); if (report != null) { parseReport(report, context); } @@ -61,46 +60,6 @@ public class CoberturaSensor extends AbstractCoverageExtension implements Sensor return null; } - protected File getReport(Project project) { - File report = getReportFromProperty(project); - if (report == null) { - report = getReportFromPluginConfiguration(project); - } - if (report == null) { - report = getReportFromDefaultPath(project); - } - - if (report == null || !report.exists() || !report.isFile()) { - LoggerFactory.getLogger(CoberturaSensor.class).warn("Cobertura report not found at {}", report); - report = null; - } - return report; - } - - private File getReportFromProperty(Project project) { - String path = (String) project.getProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY); - if (path != null) { - return project.getFileSystem().resolvePath(path); - } - return null; - } - - private File getReportFromPluginConfiguration(Project project) { - MavenPlugin mavenPlugin = MavenPlugin.getPlugin(project.getPom(), CoberturaMavenPluginHandler.GROUP_ID, - CoberturaMavenPluginHandler.ARTIFACT_ID); - if (mavenPlugin != null) { - String path = mavenPlugin.getParameter("outputDirectory"); - if (path != null) { - return new File(project.getFileSystem().resolvePath(path), "coverage.xml"); - } - } - return null; - } - - private File getReportFromDefaultPath(Project project) { - return new File(project.getFileSystem().getReportOutputDir(), "cobertura/coverage.xml"); - } - protected void parseReport(File xmlFile, final SensorContext context) { LoggerFactory.getLogger(CoberturaSensor.class).info("parsing {}", xmlFile); new AbstractCoberturaParser() { diff --git a/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java new file mode 100644 index 00000000000..e8d7cc1bcce --- /dev/null +++ b/plugins/sonar-cobertura-plugin/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java @@ -0,0 +1,58 @@ +package org.sonar.plugins.cobertura.api; + +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.maven.MavenPlugin; +import org.sonar.api.resources.Project; +import org.sonar.api.utils.Logs; +import org.sonar.plugins.cobertura.CoberturaMavenPluginHandler; + +import java.io.File; + +/** + * @since 2.4 + */ +public final class CoberturaUtils { + + public static File getReport(Project project) { + File report = getReportFromProperty(project); + if (report == null) { + report = getReportFromPluginConfiguration(project); + } + if (report == null) { + report = getReportFromDefaultPath(project); + } + + if (report == null || !report.exists() || !report.isFile()) { + Logs.INFO.warn("Cobertura report not found at {}", report); + report = null; + } + return report; + } + + private static File getReportFromProperty(Project project) { + String path = (String) project.getProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY); + if (path != null) { + return project.getFileSystem().resolvePath(path); + } + return null; + } + + private static File getReportFromPluginConfiguration(Project project) { + MavenPlugin mavenPlugin = MavenPlugin.getPlugin(project.getPom(), CoberturaMavenPluginHandler.GROUP_ID, CoberturaMavenPluginHandler.ARTIFACT_ID); + if (mavenPlugin != null) { + String path = mavenPlugin.getParameter("outputDirectory"); + if (path != null) { + return new File(project.getFileSystem().resolvePath(path), "coverage.xml"); + } + } + return null; + } + + private static File getReportFromDefaultPath(Project project) { + return new File(project.getFileSystem().getReportOutputDir(), "cobertura/coverage.xml"); + } + + private CoberturaUtils() { + } + +} 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 6935be5706d..4a3a5a21b7e 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,46 +19,34 @@ */ package org.sonar.plugins.cobertura; -import org.apache.maven.project.MavenProject; -import org.junit.Test; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.*; -import org.sonar.api.test.IsMeasure; -import org.sonar.api.test.IsResource; -import org.sonar.api.test.MavenTestUtils; - -import java.io.File; -import java.net.URISyntaxException; - import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.IsNot.not; -import static org.junit.Assert.assertNotNull; 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.*; - -public class CoberturaSensorTest { +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 shouldGetReportPathFromProperty() throws URISyntaxException { - DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class); - when(fileSystem.resolvePath("foo")).thenReturn(getCoverageReport()); +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; - Project project = mock(Project.class); - when(project.getFileSystem()).thenReturn(fileSystem); - when(project.getProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)).thenReturn("foo"); +import java.io.File; +import java.net.URISyntaxException; - File report = new CoberturaSensor(null).getReport(project); - verify(fileSystem).resolvePath("foo"); - assertNotNull(report); - } +public class CoberturaSensorTest { @Test public void doNotExecuteMavenPluginIfReuseReports() { @@ -84,21 +72,6 @@ public class CoberturaSensorTest { } @Test - public void shouldGetReportPathFromPom() { - MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldGetReportPathFromPom/pom.xml"); - - DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class); - - Project project = mock(Project.class); - when(project.getPom()).thenReturn(pom); - when(project.getFileSystem()).thenReturn(fileSystem); - - new CoberturaSensor(null).getReport(project); - - verify(fileSystem).resolvePath("overridden/dir"); - } - - @Test public void doNotCollectProjectCoverage() throws URISyntaxException { SensorContext context = mock(SensorContext.class); new CoberturaSensor(null).parseReport(getCoverageReport(), context); @@ -261,10 +234,10 @@ 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 @@ -275,7 +248,7 @@ public class CoberturaSensorTest { 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 diff --git a/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java new file mode 100644 index 00000000000..4588e4fd52f --- /dev/null +++ b/plugins/sonar-cobertura-plugin/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java @@ -0,0 +1,51 @@ +package org.sonar.plugins.cobertura.api; + +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.maven.project.MavenProject; +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.resources.DefaultProjectFileSystem; +import org.sonar.api.resources.Project; +import org.sonar.api.test.MavenTestUtils; + +import java.io.File; +import java.net.URISyntaxException; + +public class CoberturaUtilsTest { + @Test + public void shouldGetReportPathFromProperty() throws URISyntaxException { + DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class); + when(fileSystem.resolvePath("foo")).thenReturn(getCoverageReport()); + + Project project = mock(Project.class); + when(project.getFileSystem()).thenReturn(fileSystem); + when(project.getProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)).thenReturn("foo"); + + File report = CoberturaUtils.getReport(project); + verify(fileSystem).resolvePath("foo"); + assertNotNull(report); + } + + @Test + public void shouldGetReportPathFromPom() { + MavenProject pom = MavenTestUtils.loadPom("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldGetReportPathFromPom/pom.xml"); + + DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class); + + Project project = mock(Project.class); + when(project.getPom()).thenReturn(pom); + when(project.getFileSystem()).thenReturn(fileSystem); + + CoberturaUtils.getReport(project); + + verify(fileSystem).resolvePath("overridden/dir"); + } + + private File getCoverageReport() throws URISyntaxException { + return new File(getClass().getResource("/org/sonar/plugins/cobertura/CoberturaSensorTest/commons-chain-coverage.xml").toURI()); + } +} |