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");
.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
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;
ScmConfiguration.class,
ScmSensor.class,
+ LinesSensor.class,
+
// dbcleaner
DefaultPeriodCleaner.class,
DefaultPurgeTask.class
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,
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;
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)) {
--- /dev/null
+/*
+ * 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();
+ }
+ }
+
+}
.newScanTask(new File(projectDir, "sonar-project.properties"))
.start();
- assertThat(result.measures()).hasSize(14);
+ assertThat(result.measures()).hasSize(13);
}
@Test
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));
}
*/
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")