diff options
author | David Gageot <david@gageot.net> | 2012-07-13 14:24:01 +0200 |
---|---|---|
committer | David Gageot <david@gageot.net> | 2012-07-13 14:29:36 +0200 |
commit | a09a9af1ac83fc58d10bf61626b579911f86ff49 (patch) | |
tree | 26a4d05e545cedad8e64e47106457ddc812f50ab /plugins | |
parent | 9a9ea30f8be59e3289c9cebfc18cf40489a79adf (diff) | |
download | sonarqube-a09a9af1ac83fc58d10bf61626b579911f86ff49.tar.gz sonarqube-a09a9af1ac83fc58d10bf61626b579911f86ff49.zip |
SONAR-3539 With Jacoco & ant task, "lines_to_cover" is computed on unit tests
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), |