*/
package org.sonar.plugins.cobertura;
+import static org.hamcrest.CoreMatchers.is;
+import static org.mockito.Matchers.*;
+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.Before;
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 java.io.File;
import java.net.URISyntaxException;
-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.*;
-
public class CoberturaSensorTest {
+ private SensorContext context;
+ private CoberturaSensor sensor;
+
+ @Before
+ public void setUp() {
+ context = mock(SensorContext.class);
+ sensor = new CoberturaSensor();
+ }
+
+ @Test
+ public void shouldNotFailIfReportNotSpecifiedOrNotFound() throws URISyntaxException {
+ ProjectFileSystem pfs = mock(ProjectFileSystem.class);
+ when(pfs.resolvePath(anyString()))
+ .thenReturn(new File("notFound.xml"));
+
+ Project project = mock(Project.class);
+ when(project.getFileSystem()).thenReturn(pfs);
+ when(project.getProperty(eq(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)))
+ .thenReturn("notFound.xml")
+ .thenReturn(null);
+
+ sensor.analyse(project, context);
+ sensor.analyse(project, context);
+ }
+
@Test
public void doNotCollectProjectCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble());
}
@Test
public void doNotCollectProjectLineCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble());
verify(context, never()).saveMeasure(argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)));
@Test
public void doNotCollectProjectBranchCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
verify(context, never()).saveMeasure(eq(CoreMetrics.BRANCH_COVERAGE), anyDouble());
}
@Test
public void collectPackageLineCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.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());
@Test
public void collectPackageBranchCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.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());
@Test
public void packageCoverageIsCalculatedLaterByDecorator() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
verify(context, never()).saveMeasure((Resource) argThat(is(JavaPackage.class)), eq(CoreMetrics.COVERAGE), anyDouble());
}
@Test
public void collectFileLineCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
final JavaFile file = new JavaFile("org.apache.commons.chain.config.ConfigParser");
verify(context).saveMeasure(eq(file), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 30.0)));
@Test
public void collectFileBranchCoverage() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
final JavaFile file = new JavaFile("org.apache.commons.chain.config.ConfigParser");
verify(context).saveMeasure(eq(file), argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 6.0)));
@Test
public void testDoNotSaveMeasureOnResourceWhichDoesntExistInTheContext() throws URISyntaxException {
- SensorContext context = mock(SensorContext.class);
when(context.getResource(any(Resource.class))).thenReturn(null);
- new CoberturaSensor().parseReport(getCoverageReport(), context);
+ sensor.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().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
final JavaFile interfaze = new JavaFile("org.apache.commons.chain.Chain");
verify(context, never()).saveMeasure(eq(interfaze), argThat(new IsMeasure(CoreMetrics.COVERAGE)));
public void shouldInsertCoverageAtFileLevel() throws URISyntaxException {
File coverage = new File(getClass().getResource(
"/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().parseReport(coverage, context);
+ sensor.parseReport(coverage, context);
verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.InnerClass")),
argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 35.0)));
@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().parseReport(getCoverageReport(), context);
+ sensor.parseReport(getCoverageReport(), context);
verify(context).saveMeasure(
eq(new JavaFile("org.apache.commons.chain.impl.CatalogBase")),
argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA,
"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")));
}
-
@Test
public void shouldNotCountTwiceAnonymousClasses() throws URISyntaxException {
File coverage = new File(getClass().getResource("/org/sonar/plugins/cobertura/CoberturaSensorTest/shouldNotCountTwiceAnonymousClasses.xml").toURI());
- SensorContext context = mock(SensorContext.class);
when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.samples.MyClass"));
- new CoberturaSensor().parseReport(coverage, context);
+ sensor.parseReport(coverage, context);
verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.CLASS, "org.sonar.samples.MyFile")),
argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 5.0))); // do not count line 26 twice
}
-
private File getCoverageReport() throws URISyntaxException {
return new File(getClass().getResource("/org/sonar/plugins/cobertura/CoberturaSensorTest/commons-chain-coverage.xml").toURI());
}