]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5772 Force coverage measure computation by default
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 19 Feb 2015 15:53:02 +0000 (16:53 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 19 Feb 2015 20:57:29 +0000 (21:57 +0100)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/LinesToCoverDecorator.java [deleted file]
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/MissingCoverageDecorator.java [new file with mode: 0644]
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/LinesToCoverDecoratorTest.java [deleted file]
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/MissingCoverageDecoratorTest.java [new file with mode: 0644]

index de837958f322d090ef0bf8db144154751502d2b7..cc03c972881ccb2c2ebd66c24e6639fa88c8e206 100644 (file)
@@ -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/LinesToCoverDecorator.java
deleted file mode 100644 (file)
index 65970a6..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.plugins.core.sensors;
-
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.Decorator;
-import org.sonar.api.batch.DecoratorContext;
-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.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;
-
-public final class LinesToCoverDecorator implements Decorator {
-
-  private final Settings settings;
-
-  public LinesToCoverDecorator(Settings settings) {
-    this.settings = settings;
-  }
-
-  @DependsUpon
-  public Metric dependsUpon() {
-    return CoreMetrics.NCLOC;
-  }
-
-  @DependedUpon
-  public Metric provides() {
-    return CoreMetrics.LINES_TO_COVER;
-  }
-
-  @Override
-  public boolean shouldExecuteOnProject(Project project) {
-    return !settings.getBoolean(CoreProperties.COVERAGE_UNFORCED_KEY);
-  }
-
-  @Override
-  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);
-      }
-    }
-  }
-}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/MissingCoverageDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/sensors/MissingCoverageDecorator.java
new file mode 100644 (file)
index 0000000..ef7ad46
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.Decorator;
+import org.sonar.api.batch.DecoratorContext;
+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;
+
+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 MissingCoverageDecorator(Settings settings) {
+    this.settings = settings;
+  }
+
+  @DependsUpon
+  public Metric dependsUpon() {
+    return CoreMetrics.NCLOC;
+  }
+
+  @DependedUpon
+  public Metric provides() {
+    return CoreMetrics.LINES_TO_COVER;
+  }
+
+  @Override
+  public boolean shouldExecuteOnProject(Project project) {
+    return !settings.getBoolean(CoreProperties.COVERAGE_UNFORCED_KEY);
+  }
+
+  @Override
+  public void decorate(Resource resource, DecoratorContext context) {
+    if (Qualifiers.isFile(resource)) {
+      if (!MeasureUtils.hasValue(context.getMeasure(CoreMetrics.LINES_TO_COVER))) {
+        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/LinesToCoverDecoratorTest.java
deleted file mode 100644 (file)
index 1b9d9c2..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.plugins.core.sensors;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.batch.DecoratorContext;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.Settings;
-import org.sonar.api.measures.CoreMetrics;
-import org.sonar.api.measures.Measure;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.File;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.Project;
-import org.sonar.core.config.CorePropertyDefinitions;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.anyDouble;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
-import static org.mockito.Mockito.when;
-
-public class LinesToCoverDecoratorTest {
-
-  private Settings settings;
-  private LinesToCoverDecorator decorator;
-
-  @Before
-  public void prepare() {
-    settings = new Settings(new PropertyDefinitions(CorePropertyDefinitions.all()));
-    decorator = new LinesToCoverDecorator(settings);
-  }
-
-  @Test
-  public void increaseCoverage() {
-    assertThat(decorator.provides()).isEqualTo(CoreMetrics.LINES_TO_COVER);
-    assertThat(decorator.dependsUpon()).isEqualTo(CoreMetrics.NCLOC);
-  }
-
-  @Test
-  public void testShouldExecute() {
-    assertThat(decorator.shouldExecuteOnProject(new Project("foo"))).isTrue();
-    settings.setProperty(CoreProperties.COVERAGE_UNFORCED_KEY, "true");
-    assertThat(decorator.shouldExecuteOnProject(new Project("foo"))).isFalse();
-  }
-
-  @Test
-  public void testExecuteOnlyOnMainFile() {
-    DecoratorContext context = mock(DecoratorContext.class);
-    decorator.decorate(File.create("test/FooTest.java", Java.INSTANCE, true), context);
-    decorator.decorate(Directory.create("src"), context);
-    decorator.decorate(new Project("foo"), context);
-    verifyNoMoreInteractions(context);
-  }
-
-  @Test
-  public void dontDoAnythingIfLinesToCoverAlreadyDefined() {
-    DecoratorContext context = mock(DecoratorContext.class);
-    File file = File.create("src/Foo.java");
-
-    when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure<>(CoreMetrics.LINES_TO_COVER, 0.0));
-    when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure<>(CoreMetrics.NCLOC, 10.0));
-
-    decorator.decorate(file, context);
-
-    verify(context, never()).saveMeasure(eq(CoreMetrics.LINES_TO_COVER), anyDouble());
-    verify(context, never()).saveMeasure(eq(CoreMetrics.UNCOVERED_LINES), anyDouble());
-  }
-
-  @Test
-  public void testUseNclocAsLinesToCover() {
-    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)).thenReturn(new Measure<>(CoreMetrics.NCLOC, 10.0));
-
-    decorator.decorate(file, context);
-
-    verify(context).saveMeasure(CoreMetrics.LINES_TO_COVER, 10.0);
-    verify(context).saveMeasure(CoreMetrics.UNCOVERED_LINES, 10.0);
-  }
-}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/MissingCoverageDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/sensors/MissingCoverageDecoratorTest.java
new file mode 100644 (file)
index 0000000..45cb50c
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.plugins.core.sensors;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.api.config.Settings;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Directory;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Project;
+import org.sonar.core.config.CorePropertyDefinitions;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+public class MissingCoverageDecoratorTest {
+
+  private Settings settings;
+  private MissingCoverageDecorator decorator;
+
+  @Before
+  public void prepare() {
+    settings = new Settings(new PropertyDefinitions(CorePropertyDefinitions.all()));
+    decorator = new MissingCoverageDecorator(settings);
+  }
+
+  @Test
+  public void increaseCoverage() {
+    assertThat(decorator.provides()).isEqualTo(CoreMetrics.LINES_TO_COVER);
+    assertThat(decorator.dependsUpon()).isEqualTo(CoreMetrics.NCLOC);
+  }
+
+  @Test
+  public void testShouldExecute() {
+    assertThat(decorator.shouldExecuteOnProject(new Project("foo"))).isTrue();
+    settings.setProperty(CoreProperties.COVERAGE_UNFORCED_KEY, "true");
+    assertThat(decorator.shouldExecuteOnProject(new Project("foo"))).isFalse();
+  }
+
+  @Test
+  public void testExecuteOnlyOnMainFile() {
+    DecoratorContext context = mock(DecoratorContext.class);
+    decorator.decorate(File.create("test/FooTest.java", Java.INSTANCE, true), context);
+    decorator.decorate(Directory.create("src"), context);
+    decorator.decorate(new Project("foo"), context);
+    verifyNoMoreInteractions(context);
+  }
+
+  @Test
+  public void dontDoAnythingIfLinesToCoverAlreadyDefined() {
+    DecoratorContext context = mock(DecoratorContext.class);
+    File file = File.create("src/Foo.java");
+
+    when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure<>(CoreMetrics.LINES_TO_COVER, 0.0));
+    when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure<>(CoreMetrics.NCLOC, 10.0));
+
+    decorator.decorate(file, context);
+
+    verify(context, never()).saveMeasure(eq(CoreMetrics.LINES_TO_COVER), anyDouble());
+    verify(context, never()).saveMeasure(eq(CoreMetrics.UNCOVERED_LINES), anyDouble());
+  }
+
+  @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");
+
+    when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(null);
+    when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure<>(CoreMetrics.NCLOC, 10.0));
+
+    decorator.decorate(file, context);
+
+    verify(context).saveMeasure(CoreMetrics.LINES_TO_COVER, 10.0);
+    verify(context).saveMeasure(CoreMetrics.UNCOVERED_LINES, 10.0);
+  }
+}