diff options
Diffstat (limited to 'plugins/sonar-jacoco-plugin/src/test')
-rw-r--r-- | plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java | 96 | ||||
-rw-r--r-- | plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy | bin | 0 -> 666 bytes | |||
-rw-r--r-- | plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec | bin | 0 -> 5862 bytes | |||
-rw-r--r-- | plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec | bin | 0 -> 5850 bytes |
4 files changed, 96 insertions, 0 deletions
diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java new file mode 100644 index 00000000000..bc318e66b6f --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest.java @@ -0,0 +1,96 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 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 com.google.common.io.Files; +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.ProjectFileSystem; +import org.sonar.api.resources.Resource; +import org.sonar.test.TestUtils; + +import java.io.File; +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +public class JaCoCoAllTestsSensorTest { + private final JacocoConfiguration configuration= mock(JacocoConfiguration.class); + private final SensorContext context = mock(SensorContext.class); + private final ProjectFileSystem pfs = mock(ProjectFileSystem.class); + private final Project project = mock(Project.class); + private final JaCoCoAllTestsSensor sensor = new JaCoCoAllTestsSensor(configuration); + + @Test + public void should_execute_on_project() { + Project project = mock(Project.class); + when(configuration.getItReportPath()).thenReturn("target/it-jacoco.exec"); + when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC).thenReturn(Project.AnalysisType.REUSE_REPORTS); + + assertThat(sensor.shouldExecuteOnProject(project)).isTrue(); + } + + @Test + public void do_not_execute_when_report_path_not_specified() { + Project project = mock(Project.class); + when(configuration.getItReportPath()).thenReturn(""); + + assertThat(sensor.shouldExecuteOnProject(project)).isFalse(); + } + + @Test + public void should_save_measures() throws IOException { + File outputDir = TestUtils.getResource(JaCoCoAllTestsSensorTest.class, "."); + Files.copy(TestUtils.getResource("HelloWorld.class.toCopy"), new File(outputDir, "HelloWorld.class")); + + JavaFile resource = new JavaFile("com.sonar.coverages.HelloWorld"); + + when(project.getFileSystem()).thenReturn(pfs); + when(context.getResource(any(Resource.class))).thenReturn(resource); + when(configuration.getReportPath()).thenReturn("ut.exec"); + when(configuration.getItReportPath()).thenReturn("it.exec"); + when(pfs.getBuildOutputDir()).thenReturn(outputDir); + when(pfs.resolvePath("ut.exec")).thenReturn(new File(outputDir, "ut.exec")); + when(pfs.resolvePath("it.exec")).thenReturn(new File(outputDir, "it.exec")); + when(pfs.resolvePath("target/sonar/merged.exec")).thenReturn(new File("target/sonar/merged.exec")); + + sensor.analyse(project, context); + + verify(context).getResource(resource); + verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_LINES_TO_COVER, 5.0)); + verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, 0.0)); + verify(context).saveMeasure(resource, new Measure(CoreMetrics.MERGED_COVERAGE_LINE_HITS_DATA, "3=1;6=1;7=1;10=1;11=1")); + verifyNoMoreInteractions(context); + } + + @Test + public void testSensorDefinition() { + assertThat(sensor.toString()).isEqualTo("JaCoCoAllTestsSensor"); + } +} diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy b/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy Binary files differnew file mode 100644 index 00000000000..e9d34004571 --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/test/resources/HelloWorld.class.toCopy diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec Binary files differnew file mode 100644 index 00000000000..ce2421df376 --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/it.exec diff --git a/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec Binary files differnew file mode 100644 index 00000000000..09d84373641 --- /dev/null +++ b/plugins/sonar-jacoco-plugin/src/test/resources/org/sonar/plugins/jacoco/JaCoCoAllTestsSensorTest/ut.exec |