]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5077 Lines metric is computed by the core
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 15 Jan 2015 23:09:04 +0000 (00:09 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 15 Jan 2015 23:27:08 +0000 (00:27 +0100)
plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/medium/CpdMediumTest.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java

index 44de4e1e5761e1aa59b7af86e2a2b5a083f4dd17..e7de8cb8625429adb3b6865ae60f1f8521743338 100644 (file)
@@ -103,8 +103,8 @@ public class CpdMediumTest {
 
     assertThat(result.inputFiles()).hasSize(2);
 
-    // 4 measures per file + quality profile measure
-    assertThat(result.measures()).hasSize(9);
+    // 5 measures per file + quality profile measure
+    assertThat(result.measures()).hasSize(11);
 
     InputFile inputFile1 = result.inputFile("src/sample1.xoo");
     InputFile inputFile2 = result.inputFile("src/sample2.xoo");
@@ -151,8 +151,8 @@ public class CpdMediumTest {
         .build())
       .start();
 
-    // 4 measures per file + QP measure
-    assertThat(result.measures()).hasSize(5);
+    // 5 measures per file + QP measure
+    assertThat(result.measures()).hasSize(6);
 
     InputFile inputFile = result.inputFile("src/sample.xoo");
     // One clone group
index 8d0f2bfb00dd25488bae7a77b6183d2d8b143760..a531afc577a9a8eeac8212034d1e7e72964f3b94 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.batch.maven.MavenProjectBuilder;
 import org.sonar.batch.maven.MavenProjectConverter;
 import org.sonar.batch.scm.ScmConfiguration;
 import org.sonar.batch.scm.ScmSensor;
+import org.sonar.batch.source.LinesSensor;
 import org.sonar.core.computation.dbcleaner.DefaultPurgeTask;
 import org.sonar.core.computation.dbcleaner.period.DefaultPeriodCleaner;
 import org.sonar.core.config.CorePropertyDefinitions;
@@ -61,6 +62,8 @@ public class BatchComponents {
       ScmConfiguration.class,
       ScmSensor.class,
 
+      LinesSensor.class,
+
       // dbcleaner
       DefaultPeriodCleaner.class,
       DefaultPurgeTask.class
index 64886cc15298bcdb59e243b32d3be890ea347aea..eeb37de63ed228cddabcb7733fa4fa9b87bda8fc 100644 (file)
@@ -87,7 +87,7 @@ public class DefaultIndex extends SonarIndex {
     CoreMetrics.FILE_FEEDBACK_EDGES,
     CoreMetrics.FILE_TANGLE_INDEX,
     CoreMetrics.FILE_TANGLES,
-    // Computed by ScmActivitySensor
+    // Computed by ScmSensor
     CoreMetrics.SCM_AUTHORS_BY_LINE,
     CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE,
     CoreMetrics.SCM_REVISIONS_BY_LINE,
@@ -96,7 +96,9 @@ public class DefaultIndex extends SonarIndex {
     CoreMetrics.DUPLICATION_LINES_DATA,
     CoreMetrics.DUPLICATED_FILES,
     CoreMetrics.DUPLICATED_LINES,
-    CoreMetrics.DUPLICATED_BLOCKS
+    CoreMetrics.DUPLICATED_BLOCKS,
+    // Computed by LinesSensor
+    CoreMetrics.LINES
     );
 
   private final ResourceCache resourceCache;
@@ -256,7 +258,7 @@ public class DefaultIndex extends SonarIndex {
         throw new SonarException("Unknown metric: " + measure.getMetricKey());
       }
       if (!isTechnicalProjectCopy(resource) && !measure.isFromCore() && INTERNAL_METRICS.contains(metric)) {
-        LOG.debug("Metric " + metric.key() + " is an internal metric computed by SonarQube. Please update your plugin.");
+        LOG.debug("Metric " + metric.key() + " is an internal metric computed by SonarQube. Provided value is ignored.");
         return measure;
       }
       if (measureCache.contains(resource, measure)) {
diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java b/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java
new file mode 100644 (file)
index 0000000..3272825
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.batch.source;
+
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.InputFile.Type;
+import org.sonar.api.batch.sensor.Sensor;
+import org.sonar.api.batch.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorDescriptor;
+import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
+import org.sonar.api.measures.CoreMetrics;
+
+public final class LinesSensor implements Sensor {
+
+  private final FileSystem fs;
+
+  public LinesSensor(FileSystem fs) {
+    this.fs = fs;
+  }
+
+  @Override
+  public void describe(SensorDescriptor descriptor) {
+    descriptor.name("Lines Sensor");
+  }
+
+  @Override
+  public void execute(final SensorContext context) {
+    for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) {
+      ((DefaultMeasure<Integer>) context.<Integer>newMeasure()
+        .onFile(f)
+        .forMetric(CoreMetrics.LINES)
+        .withValue(f.lines()))
+        .setFromCore()
+        .save();
+    }
+  }
+
+}
index f11c843c541328c5f9f2be1b3a8d0b09556755b5..6cd0ed34a0e90150d291ff8e4f91d9590f9fe8c3 100644 (file)
@@ -66,7 +66,7 @@ public class MeasuresMediumTest {
       .newScanTask(new File(projectDir, "sonar-project.properties"))
       .start();
 
-    assertThat(result.measures()).hasSize(14);
+    assertThat(result.measures()).hasSize(13);
   }
 
   @Test
@@ -98,7 +98,47 @@ public class MeasuresMediumTest {
     assertThat(result.measures()).contains(new DefaultMeasure<Integer>()
       .forMetric(CoreMetrics.LINES)
       .onFile(new DefaultInputFile("com.foo.project", "src/sample.xoo"))
-      .withValue(20));
+      .withValue(2));
+
+  }
+
+  @Test
+  public void computeLinesOnAllFiles() throws IOException {
+
+    File baseDir = temp.newFolder();
+    File srcDir = new File(baseDir, "src");
+    srcDir.mkdir();
+
+    File xooFile = new File(srcDir, "sample.xoo");
+    FileUtils.write(xooFile, "Sample xoo\ncontent");
+
+    File otherFile = new File(srcDir, "sample.other");
+    FileUtils.write(otherFile, "Sample other\ncontent\n");
+
+    TaskResult result = tester.newTask()
+      .properties(ImmutableMap.<String, String>builder()
+        .put("sonar.task", "scan")
+        .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+        .put("sonar.projectKey", "com.foo.project")
+        .put("sonar.projectName", "Foo Project")
+        .put("sonar.projectVersion", "1.0-SNAPSHOT")
+        .put("sonar.projectDescription", "Description of Foo Project")
+        .put("sonar.sources", "src")
+        .put("sonar.index_all_files", "true")
+        .build())
+      .start();
+
+    // QP + 2 x lines
+    assertThat(result.measures()).hasSize(3);
+
+    assertThat(result.measures()).contains(new DefaultMeasure<Integer>()
+      .forMetric(CoreMetrics.LINES)
+      .onFile(new DefaultInputFile("com.foo.project", "src/sample.xoo"))
+      .withValue(2));
+    assertThat(result.measures()).contains(new DefaultMeasure<Integer>()
+      .forMetric(CoreMetrics.LINES)
+      .onFile(new DefaultInputFile("com.foo.project", "src/sample.other"))
+      .withValue(3));
 
   }
 
index 1f1b35c080c6ceb4e540ad91e0775b972077401f..03e92541e61fe8e145e99b23ffb7dbf38f9b08ce 100644 (file)
@@ -56,6 +56,9 @@ public final class CoreMetrics {
    */
   public static String DOMAIN_TECHNICAL_DEBT = "Technical Debt";
 
+  /**
+   * Computed by the platform since SQ 5.1
+   */
   public static final String LINES_KEY = "lines";
   public static final Metric<Integer> LINES = new Metric.Builder(LINES_KEY, "Lines", Metric.ValueType.INT)
     .setDescription("Lines")