From 13ff8a717d1837a2a430aec0a2e1cb115da0afeb Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 2 Nov 2011 11:28:12 +0100 Subject: SONAR-2963 Add the widget "Integration Test Coverage" --- .../org/sonar/plugins/jacoco/JaCoCoItSensor.java | 101 +++++++++++++++++ .../org/sonar/plugins/jacoco/JaCoCoPlugin.java | 5 +- .../jacoco/itcoverage/ItCoverageWidget.java | 44 -------- .../plugins/jacoco/itcoverage/JaCoCoItSensor.java | 103 ----------------- .../plugins/jacoco/itcoverage/widget.html.erb | 32 ------ .../sonar/plugins/jacoco/JaCoCoItSensorTest.java | 122 ++++++++++++++++++++ .../jacoco/itcoverage/ItCoverageWidgetTest.java | 38 ------- .../jacoco/itcoverage/JaCoCoItSensorTest.java | 123 --------------------- 8 files changed, 224 insertions(+), 344 deletions(-) create mode 100644 plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java delete mode 100644 plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidget.java delete mode 100644 plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensor.java delete mode 100644 plugins/sonar-jacoco-plugin/src/main/resources/org/sonar/plugins/jacoco/itcoverage/widget.html.erb create mode 100644 plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoItSensorTest.java delete mode 100644 plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidgetTest.java delete mode 100644 plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensorTest.java (limited to 'plugins/sonar-jacoco-plugin/src') diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java new file mode 100644 index 00000000000..89a1c83ddf0 --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoItSensor.java @@ -0,0 +1,101 @@ +/* + * 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.jacoco; + +import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.Sensor; +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.Project; + +import java.util.Collection; + +/** + * Note that this class can't extend {@link org.sonar.api.batch.AbstractCoverageExtension}, because in this case this extension will be + * disabled under Sonar 2.3, if JaCoCo is not defined as the default code coverage plugin. + * + * @author Evgeny Mandrikov + */ +public class JaCoCoItSensor implements Sensor { + private JacocoConfiguration configuration; + + public JaCoCoItSensor(JacocoConfiguration configuration) { + this.configuration = configuration; + } + + public boolean shouldExecuteOnProject(Project project) { + return StringUtils.isNotBlank(configuration.getItReportPath()) + && project.getAnalysisType().isDynamic(true); + } + + public void analyse(Project project, SensorContext context) { + new ITAnalyzer().analyse(project, context); + } + + class ITAnalyzer extends AbstractAnalyzer { + @Override + protected String getReportPath(Project project) { + return configuration.getItReportPath(); + } + + @Override + protected void saveMeasures(SensorContext context, JavaFile resource, Collection measures) { + for (Measure measure : measures) { + Measure itMeasure = convertForIT(measure); + if (itMeasure != null) { + context.saveMeasure(resource, itMeasure); + } + } + } + + private Measure convertForIT(Measure measure) { + Measure itMeasure = null; + if (CoreMetrics.LINES_TO_COVER.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_LINES_TO_COVER, measure.getValue()); + + } else if (CoreMetrics.UNCOVERED_LINES.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_LINES, measure.getValue()); + + } else if (CoreMetrics.COVERAGE_LINE_HITS_DATA.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, measure.getData()); + + } else if (CoreMetrics.CONDITIONS_TO_COVER.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_TO_COVER, measure.getValue()); + + } else if (CoreMetrics.UNCOVERED_CONDITIONS.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, measure.getValue()); + + } else if (CoreMetrics.COVERED_CONDITIONS_BY_LINE.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE, measure.getData()); + + } else if (CoreMetrics.CONDITIONS_BY_LINE.equals(measure.getMetric())) { + itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_BY_LINE, measure.getData()); + } + return itMeasure; + } + } + + @Override + public String toString() { + return getClass().getSimpleName(); + } +} diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoPlugin.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoPlugin.java index aee588e9d47..447f746a826 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoPlugin.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/JaCoCoPlugin.java @@ -22,7 +22,6 @@ package org.sonar.plugins.jacoco; import org.sonar.api.Properties; import org.sonar.api.Property; import org.sonar.api.SonarPlugin; -import org.sonar.plugins.jacoco.itcoverage.*; import java.util.Arrays; import java.util.List; @@ -99,9 +98,7 @@ public class JaCoCoPlugin extends SonarPlugin { JaCoCoMavenPluginHandler.class, // Unit tests JaCoCoSensor.class, - // Integration tests - JaCoCoItSensor.class, - ItCoverageWidget.class); + JaCoCoItSensor.class); } } diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidget.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidget.java deleted file mode 100644 index 65ef6ef25bb..00000000000 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidget.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.jacoco.itcoverage; - -import org.sonar.api.web.AbstractRubyTemplate; -import org.sonar.api.web.NavigationSection; -import org.sonar.api.web.RubyRailsWidget; -import org.sonar.api.web.UserRole; - -@NavigationSection(NavigationSection.RESOURCE) -@UserRole(UserRole.USER) -public class ItCoverageWidget extends AbstractRubyTemplate implements RubyRailsWidget { - - @Override - protected String getTemplatePath() { - return "/org/sonar/plugins/jacoco/itcoverage/widget.html.erb"; - } - - public String getId() { - return "it-coverage"; - } - - public String getTitle() { - return "IT Coverage widget"; - } - -} diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensor.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensor.java deleted file mode 100644 index 8d1d54604bd..00000000000 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensor.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.jacoco.itcoverage; - -import org.apache.commons.lang.StringUtils; -import org.sonar.api.batch.Sensor; -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.Project; -import org.sonar.plugins.jacoco.AbstractAnalyzer; -import org.sonar.plugins.jacoco.JacocoConfiguration; - -import java.util.Collection; - -/** - * Note that this class can't extend {@link org.sonar.api.batch.AbstractCoverageExtension}, because in this case this extension will be - * disabled under Sonar 2.3, if JaCoCo is not defined as the default code coverage plugin. - * - * @author Evgeny Mandrikov - */ -public class JaCoCoItSensor implements Sensor { - private JacocoConfiguration configuration; - - public JaCoCoItSensor(JacocoConfiguration configuration) { - this.configuration = configuration; - } - - public boolean shouldExecuteOnProject(Project project) { - return StringUtils.isNotBlank(configuration.getItReportPath()) - && project.getAnalysisType().isDynamic(true); - } - - public void analyse(Project project, SensorContext context) { - new ITAnalyzer().analyse(project, context); - } - - class ITAnalyzer extends AbstractAnalyzer { - @Override - protected String getReportPath(Project project) { - return configuration.getItReportPath(); - } - - @Override - protected void saveMeasures(SensorContext context, JavaFile resource, Collection measures) { - for (Measure measure : measures) { - Measure itMeasure = convertForIT(measure); - if (itMeasure != null) { - context.saveMeasure(resource, itMeasure); - } - } - } - - private Measure convertForIT(Measure measure) { - Measure itMeasure = null; - if (CoreMetrics.LINES_TO_COVER.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_LINES_TO_COVER, measure.getValue()); - - } else if (CoreMetrics.UNCOVERED_LINES.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_LINES, measure.getValue()); - - } else if (CoreMetrics.COVERAGE_LINE_HITS_DATA.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, measure.getData()); - - } else if (CoreMetrics.CONDITIONS_TO_COVER.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_TO_COVER, measure.getValue()); - - } else if (CoreMetrics.UNCOVERED_CONDITIONS.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, measure.getValue()); - - } else if (CoreMetrics.COVERED_CONDITIONS_BY_LINE.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE, measure.getData()); - - } else if (CoreMetrics.CONDITIONS_BY_LINE.equals(measure.getMetric())) { - itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_BY_LINE, measure.getData()); - } - return itMeasure; - } - } - - @Override - public String toString() { - return getClass().getSimpleName(); - } -} diff --git a/plugins/sonar-jacoco-plugin/src/main/resources/org/sonar/plugins/jacoco/itcoverage/widget.html.erb b/plugins/sonar-jacoco-plugin/src/main/resources/org/sonar/plugins/jacoco/itcoverage/widget.html.erb deleted file mode 100644 index 00f91c0bb2b..00000000000 --- a/plugins/sonar-jacoco-plugin/src/main/resources/org/sonar/plugins/jacoco/itcoverage/widget.html.erb +++ /dev/null @@ -1,32 +0,0 @@ -<% - it_coverage_measure=measure('it_coverage') - if it_coverage_measure %> - - - - -
-
-

IT Code coverage

- -

- <%= format_measure(it_coverage_measure, :suffix => '', :url => url_for_drilldown('it_coverage'), :default => '-') %> - <%= dashboard_configuration.selected_period? ? format_variation(it_coverage_measure) : trend_icon(it_coverage_measure) -%> -

- <% it_line_coverage=measure('it_line_coverage') - if it_line_coverage %> -

- <%= format_measure(it_line_coverage, :suffix => ' IT line coverage', :url => url_for_drilldown('it_uncovered_lines', :highlight => 'it_line_coverage')) %> - <%= dashboard_configuration.selected_period? ? format_variation(it_line_coverage) : trend_icon(it_line_coverage) -%> -

- <% end %> - <% it_branch_coverage=measure('it_branch_coverage') - if it_branch_coverage %> -

- <%= format_measure(it_branch_coverage, :suffix => ' IT branch coverage', :url => url_for_drilldown('it_uncovered_conditions', :highlight => 'it_branch_coverage')) %> - <%= dashboard_configuration.selected_period? ? format_variation(it_branch_coverage) : trend_icon(it_branch_coverage) -%> -

- <% end %> -
-
-<% end %> diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoItSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoItSensorTest.java new file mode 100644 index 00000000000..2e2da4db9a6 --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoItSensorTest.java @@ -0,0 +1,122 @@ +/* + * 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.jacoco; + +import org.junit.Before; +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.Project; +import org.sonar.api.resources.Project.AnalysisType; +import org.sonar.api.resources.ProjectFileSystem; +import org.sonar.api.resources.Resource; +import org.sonar.api.test.IsMeasure; + +import java.io.File; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; + +public class JaCoCoItSensorTest { + private JacocoConfiguration configuration; + private JaCoCoItSensor sensor; + + @Before + public void setUp() { + configuration = mock(JacocoConfiguration.class); + sensor = new JaCoCoItSensor(configuration); + } + + @Test + public void testSensorDefinition() { + assertThat(sensor.toString(), is("JaCoCoItSensor")); + } + + @Test + public void doNotExecuteWhenReportPathNotSpecified() { + when(configuration.getItReportPath()).thenReturn(""); + Project project = mock(Project.class); + assertThat(sensor.shouldExecuteOnProject(project), is(false)); + } + + @Test + public void shouldExecuteOnProject() { + when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec"); + Project project = mock(Project.class); + when(project.getAnalysisType()).thenReturn(AnalysisType.DYNAMIC).thenReturn(AnalysisType.REUSE_REPORTS); + assertThat(sensor.shouldExecuteOnProject(project), is(true)); + assertThat(sensor.shouldExecuteOnProject(project), is(true)); + } + + @Test + public void testReadExecutionData() throws Exception { + File jacocoExecutionData = new File(getClass().getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest/jacoco.exec").getFile()); + File buildOutputDir = jacocoExecutionData.getParentFile(); + SensorContext context = mock(SensorContext.class); + + final JavaFile resource = new JavaFile("org.sonar.plugins.jacoco.tests.Hello"); + when(context.getResource(any(Resource.class))).thenReturn(resource); + + ProjectFileSystem pfs = mock(ProjectFileSystem.class); + when(pfs.getBuildOutputDir()).thenReturn(buildOutputDir); + when(pfs.resolvePath(anyString())).thenReturn(jacocoExecutionData); + + Project project = mock(Project.class); + when(project.getFileSystem()).thenReturn(pfs); + + sensor.analyse(project, context); + + verify(context).getResource(eq(resource)); + verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_LINES_TO_COVER, 7.0))); + verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_UNCOVERED_LINES, 3.0))); + verify(context).saveMeasure(eq(resource), + argThat(new IsMeasure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, "6=1;7=1;8=1;11=1;15=0;16=0;18=0"))); + verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER, 2.0))); + verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS, 2.0))); + verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_CONDITIONS_BY_LINE, "15=2"))); + verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE, "15=0"))); + verifyNoMoreInteractions(context); + } + + @Test + public void doNotSaveMeasureOnResourceWhichDoesntExistInTheContext() throws Exception { + File jacocoExecutionData = new File(getClass().getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest/jacoco.exec").getFile()); + File buildOutputDir = jacocoExecutionData.getParentFile(); + SensorContext context = mock(SensorContext.class); + when(context.getResource(any(Resource.class))).thenReturn(null); + + ProjectFileSystem pfs = mock(ProjectFileSystem.class); + when(pfs.getBuildOutputDir()).thenReturn(buildOutputDir); + + Project project = mock(Project.class); + when(project.getFileSystem()).thenReturn(pfs); + + sensor.analyse(project, context); + + verify(context, never()).saveMeasure(any(Resource.class), any(Measure.class)); + } +} diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidgetTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidgetTest.java deleted file mode 100644 index bb5870e7733..00000000000 --- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/ItCoverageWidgetTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.jacoco.itcoverage; - -import org.junit.Test; - -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; - -/** - * @author Evgeny Mandrikov - */ -public class ItCoverageWidgetTest { - - @Test - public void testGetTemplatePath() { - String path = new ItCoverageWidget().getTemplatePath(); - assertThat(getClass().getResource(path), notNullValue()); - } - -} diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensorTest.java deleted file mode 100644 index 524e9070908..00000000000 --- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/itcoverage/JaCoCoItSensorTest.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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.jacoco.itcoverage; - -import org.junit.Before; -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.Project; -import org.sonar.api.resources.Project.AnalysisType; -import org.sonar.api.resources.ProjectFileSystem; -import org.sonar.api.resources.Resource; -import org.sonar.api.test.IsMeasure; -import org.sonar.plugins.jacoco.JacocoConfiguration; - -import java.io.File; - -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; - -public class JaCoCoItSensorTest { - private JacocoConfiguration configuration; - private JaCoCoItSensor sensor; - - @Before - public void setUp() { - configuration = mock(JacocoConfiguration.class); - sensor = new JaCoCoItSensor(configuration); - } - - @Test - public void testSensorDefinition() { - assertThat(sensor.toString(), is("JaCoCoItSensor")); - } - - @Test - public void doNotExecuteWhenReportPathNotSpecified() { - when(configuration.getItReportPath()).thenReturn(""); - Project project = mock(Project.class); - assertThat(sensor.shouldExecuteOnProject(project), is(false)); - } - - @Test - public void shouldExecuteOnProject() { - when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec"); - Project project = mock(Project.class); - when(project.getAnalysisType()).thenReturn(AnalysisType.DYNAMIC).thenReturn(AnalysisType.REUSE_REPORTS); - assertThat(sensor.shouldExecuteOnProject(project), is(true)); - assertThat(sensor.shouldExecuteOnProject(project), is(true)); - } - - @Test - public void testReadExecutionData() throws Exception { - File jacocoExecutionData = new File(getClass().getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest/jacoco.exec").getFile()); - File buildOutputDir = jacocoExecutionData.getParentFile(); - SensorContext context = mock(SensorContext.class); - - final JavaFile resource = new JavaFile("org.sonar.plugins.jacoco.tests.Hello"); - when(context.getResource(any(Resource.class))).thenReturn(resource); - - ProjectFileSystem pfs = mock(ProjectFileSystem.class); - when(pfs.getBuildOutputDir()).thenReturn(buildOutputDir); - when(pfs.resolvePath(anyString())).thenReturn(jacocoExecutionData); - - Project project = mock(Project.class); - when(project.getFileSystem()).thenReturn(pfs); - - sensor.analyse(project, context); - - verify(context).getResource(eq(resource)); - verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_LINES_TO_COVER, 7.0))); - verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_UNCOVERED_LINES, 3.0))); - verify(context).saveMeasure(eq(resource), - argThat(new IsMeasure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, "6=1;7=1;8=1;11=1;15=0;16=0;18=0"))); - verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER, 2.0))); - verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS, 2.0))); - verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_CONDITIONS_BY_LINE, "15=2"))); - verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE, "15=0"))); - verifyNoMoreInteractions(context); - } - - @Test - public void doNotSaveMeasureOnResourceWhichDoesntExistInTheContext() throws Exception { - File jacocoExecutionData = new File(getClass().getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest/jacoco.exec").getFile()); - File buildOutputDir = jacocoExecutionData.getParentFile(); - SensorContext context = mock(SensorContext.class); - when(context.getResource(any(Resource.class))).thenReturn(null); - - ProjectFileSystem pfs = mock(ProjectFileSystem.class); - when(pfs.getBuildOutputDir()).thenReturn(buildOutputDir); - - Project project = mock(Project.class); - when(project.getFileSystem()).thenReturn(pfs); - - sensor.analyse(project, context); - - verify(context, never()).saveMeasure(any(Resource.class), any(Measure.class)); - } -} -- cgit v1.2.3