diff options
Diffstat (limited to 'plugins')
4 files changed, 79 insertions, 17 deletions
diff --git a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java index fd487ac1c82..a2317fa1f54 100644 --- a/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java +++ b/plugins/sonar-jacoco-plugin/src/main/java/org/sonar/plugins/jacoco/AbstractAnalyzer.java @@ -19,6 +19,8 @@ */ package org.sonar.plugins.jacoco; +import org.sonar.api.resources.ResourceUtils; + import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import org.apache.commons.lang.StringUtils; @@ -81,9 +83,8 @@ public abstract class AbstractAnalyzer { int analyzedResources = 0; for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) { - JavaFile resource = getResource(coverage); - // Do not save measures on resource which doesn't exist in the context - if (context.getResource(resource) != null) { + JavaFile resource = getResource(coverage, context); + if (resource != null) { if (!isExcluded(coverage, excludes)) { analyzeFile(resource, coverage, context); } @@ -101,10 +102,21 @@ public abstract class AbstractAnalyzer { } @VisibleForTesting - static JavaFile getResource(ISourceFileCoverage coverage) { + static JavaFile getResource(ISourceFileCoverage coverage, SensorContext context) { String packageName = StringUtils.replaceChars(coverage.getPackageName(), '/', '.'); String fileName = StringUtils.substringBeforeLast(coverage.getName(), "."); - return new JavaFile(packageName, fileName); + + JavaFile resource = new JavaFile(packageName, fileName); + + JavaFile resourceInContext = context.getResource(resource); + if (null == resourceInContext) { + return null; // Do not save measures on resource which doesn't exist in the context + } + if (ResourceUtils.isUnitTestClass(resourceInContext)) { + return null; // Ignore unit tests + } + + return resourceInContext; } /** diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/AbstractAnalyzerTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/AbstractAnalyzerTest.java index a70db852388..2783b462dcc 100644 --- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/AbstractAnalyzerTest.java +++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/AbstractAnalyzerTest.java @@ -19,22 +19,72 @@ */ package org.sonar.plugins.jacoco; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import org.jacoco.core.analysis.ISourceFileCoverage; import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.JavaFile; +import org.sonar.api.resources.Resource; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class AbstractAnalyzerTest { + ISourceFileCoverage coverage = mock(ISourceFileCoverage.class); + SensorContext context = mock(SensorContext.class); + @Test - public void defaultPackage() { - ISourceFileCoverage coverage = mock(ISourceFileCoverage.class); - when(coverage.getPackageName()).thenReturn("").thenReturn("org/example"); + public void should_recognize_default_package() { + when(coverage.getPackageName()).thenReturn(""); when(coverage.getName()).thenReturn("Hello.java"); - assertThat(AbstractAnalyzer.getResource(coverage), is(new JavaFile("[default].Hello"))); - assertThat(AbstractAnalyzer.getResource(coverage), is(new JavaFile("org.example.Hello"))); + when(context.getResource(any(Resource.class))).thenAnswer(sameResource()); + + JavaFile resource = AbstractAnalyzer.getResource(coverage, context); + + assertThat(resource).isEqualTo(new JavaFile("[default].Hello")); + } + + @Test + public void should_recognize_non_default_package() { + when(coverage.getPackageName()).thenReturn("org/example"); + when(coverage.getName()).thenReturn("Hello.java"); + when(context.getResource(any(Resource.class))).thenAnswer(sameResource()); + + JavaFile resource = AbstractAnalyzer.getResource(coverage, context); + + assertThat(resource).isEqualTo(new JavaFile("org.example.Hello")); + } + + @Test + public void should_ignore_resource_not_found_in_context() { + when(coverage.getPackageName()).thenReturn("org/example"); + when(coverage.getName()).thenReturn("HelloTest.java"); + when(context.getResource(any(Resource.class))).thenReturn(null); + + JavaFile resource = AbstractAnalyzer.getResource(coverage, context); + + assertThat(resource).isNull(); + } + + @Test + public void should_ignore_unit_tests() { + when(coverage.getPackageName()).thenReturn("org/example"); + when(coverage.getName()).thenReturn("HelloTest.java"); + when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("HelloTest.java", true)); + + JavaFile resource = AbstractAnalyzer.getResource(coverage, context); + + assertThat(resource).isNull(); + } + + static Answer<Resource> sameResource() { + return new Answer<Resource>() { + public Resource answer(InvocationOnMock invocation) { + return (Resource) invocation.getArguments()[0]; + } + }; } } 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 index 950fdae7cfe..18d3a641f2b 100644 --- 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 @@ -106,7 +106,7 @@ public class JaCoCoItSensorTest { sensor.analyse(project, context); - verify(context).getResource(eq(resource)); + verify(context).getResource(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), diff --git a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoSensorTest.java b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoSensorTest.java index 7cb8879b232..87f3314e54c 100644 --- a/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoSensorTest.java +++ b/plugins/sonar-jacoco-plugin/src/test/java/org/sonar/plugins/jacoco/JaCoCoSensorTest.java @@ -108,7 +108,7 @@ public class JaCoCoSensorTest { sensor.analyse(project, context); - verify(context).getResource(eq(resource)); + verify(context).getResource(resource); verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 7.0))); verify(context).saveMeasure(eq(resource), argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES, 3.0))); verify(context).saveMeasure(eq(resource), |