aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java4
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/MissingCoverageDecorator.java (renamed from plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/LinesToCoverDecorator.java)33
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/MissingCoverageDecoratorTest.java (renamed from plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/LinesToCoverDecoratorTest.java)21
3 files changed, 48 insertions, 10 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
index de837958f32..cc03c972881 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
@@ -47,7 +47,7 @@ import org.sonar.plugins.core.sensors.ItBranchCoverageDecorator;
import org.sonar.plugins.core.sensors.ItCoverageDecorator;
import org.sonar.plugins.core.sensors.ItLineCoverageDecorator;
import org.sonar.plugins.core.sensors.LineCoverageDecorator;
-import org.sonar.plugins.core.sensors.LinesToCoverDecorator;
+import org.sonar.plugins.core.sensors.MissingCoverageDecorator;
import org.sonar.plugins.core.sensors.ManualMeasureDecorator;
import org.sonar.plugins.core.sensors.OverallBranchCoverageDecorator;
import org.sonar.plugins.core.sensors.OverallCoverageDecorator;
@@ -336,7 +336,7 @@ public final class CorePlugin extends SonarPlugin {
DirectoriesDecorator.class,
FilesDecorator.class,
ManualMeasureDecorator.class,
- LinesToCoverDecorator.class,
+ MissingCoverageDecorator.class,
// time machine
TendencyDecorator.class,
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/LinesToCoverDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/MissingCoverageDecorator.java
index 65970a6582d..ef7ad46fd04 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/LinesToCoverDecorator.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/MissingCoverageDecorator.java
@@ -26,17 +26,25 @@ import org.sonar.api.batch.DependedUpon;
import org.sonar.api.batch.DependsUpon;
import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.CoverageMeasuresBuilder;
+import org.sonar.api.measures.Measure;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Resource;
+import org.sonar.api.utils.KeyValueFormat;
-public final class LinesToCoverDecorator implements Decorator {
+import java.util.Map;
+
+/**
+ * Compute coverage when it was not already saved by language plugin.
+ */
+public final class MissingCoverageDecorator implements Decorator {
private final Settings settings;
- public LinesToCoverDecorator(Settings settings) {
+ public MissingCoverageDecorator(Settings settings) {
this.settings = settings;
}
@@ -59,9 +67,24 @@ public final class LinesToCoverDecorator implements Decorator {
public void decorate(Resource resource, DecoratorContext context) {
if (Qualifiers.isFile(resource)) {
if (!MeasureUtils.hasValue(context.getMeasure(CoreMetrics.LINES_TO_COVER))) {
- double ncloc = MeasureUtils.getValue(context.getMeasure(CoreMetrics.NCLOC), 0.0);
- context.saveMeasure(CoreMetrics.LINES_TO_COVER, ncloc);
- context.saveMeasure(CoreMetrics.UNCOVERED_LINES, ncloc);
+ Measure nclocData = context.getMeasure(CoreMetrics.NCLOC_DATA);
+ if (MeasureUtils.hasData(nclocData)) {
+ CoverageMeasuresBuilder builder = CoverageMeasuresBuilder.create();
+ Map<Integer, Integer> nclocByLine = KeyValueFormat.parseIntInt(nclocData.getData());
+ for (Map.Entry<Integer, Integer> entry : nclocByLine.entrySet()) {
+ if (entry.getValue() == 1) {
+ builder.setHits(entry.getKey(), 0);
+ }
+ }
+ for (Measure m : builder.createMeasures()) {
+ context.saveMeasure(m);
+ }
+ } else {
+ // No details about ncloc so fallback on setting high level metrics
+ double ncloc = MeasureUtils.getValue(context.getMeasure(CoreMetrics.NCLOC), 0.0);
+ context.saveMeasure(CoreMetrics.LINES_TO_COVER, ncloc);
+ context.saveMeasure(CoreMetrics.UNCOVERED_LINES, ncloc);
+ }
}
}
}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/LinesToCoverDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/MissingCoverageDecoratorTest.java
index 1b9d9c25f6d..45cb50cfd07 100644
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/LinesToCoverDecoratorTest.java
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/MissingCoverageDecoratorTest.java
@@ -42,15 +42,15 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
-public class LinesToCoverDecoratorTest {
+public class MissingCoverageDecoratorTest {
private Settings settings;
- private LinesToCoverDecorator decorator;
+ private MissingCoverageDecorator decorator;
@Before
public void prepare() {
settings = new Settings(new PropertyDefinitions(CorePropertyDefinitions.all()));
- decorator = new LinesToCoverDecorator(settings);
+ decorator = new MissingCoverageDecorator(settings);
}
@Test
@@ -90,6 +90,21 @@ public class LinesToCoverDecoratorTest {
}
@Test
+ public void testUseNclocDataIfPossible() {
+ DecoratorContext context = mock(DecoratorContext.class);
+ File file = File.create("src/Foo.java");
+
+ when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(null);
+ when(context.getMeasure(CoreMetrics.NCLOC_DATA)).thenReturn(new Measure<>(CoreMetrics.NCLOC_DATA, "1=0;2=1;3=0;4=1"));
+
+ decorator.decorate(file, context);
+
+ verify(context).saveMeasure(new Measure(CoreMetrics.LINES_TO_COVER, 2.0));
+ verify(context).saveMeasure(new Measure(CoreMetrics.UNCOVERED_LINES, 2.0));
+ verify(context).saveMeasure(new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "2=0;4=0"));
+ }
+
+ @Test
public void testUseNclocAsLinesToCover() {
DecoratorContext context = mock(DecoratorContext.class);
File file = File.create("src/Foo.java");