diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-01-16 00:09:04 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-01-16 00:27:08 +0100 |
commit | 640f9c74ea381aa45d5e92d00d79029471195992 (patch) | |
tree | cd95c13fb500a2cda5812ab5d8eed2f05c8050a2 | |
parent | 445ac3e5a16c86d8906e82567657fefdb30d50e1 (diff) | |
download | sonarqube-640f9c74ea381aa45d5e92d00d79029471195992.tar.gz sonarqube-640f9c74ea381aa45d5e92d00d79029471195992.zip |
SONAR-5077 Lines metric is computed by the core
6 files changed, 113 insertions, 9 deletions
diff --git a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/medium/CpdMediumTest.java b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/medium/CpdMediumTest.java index 44de4e1e576..e7de8cb8625 100644 --- a/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/medium/CpdMediumTest.java +++ b/plugins/sonar-cpd-plugin/src/test/java/org/sonar/plugins/cpd/medium/CpdMediumTest.java @@ -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 diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java index 8d0f2bfb00d..a531afc577a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java @@ -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 diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index 64886cc1529..eeb37de63ed 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -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 index 00000000000..3272825fd1f --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/LinesSensor.java @@ -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(); + } + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java index f11c843c541..6cd0ed34a0e 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java @@ -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)); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java index 1f1b35c080c..03e92541e61 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java @@ -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") |