From: Julien HENRY Date: Thu, 22 Oct 2015 08:57:55 +0000 (+0200) Subject: Add a medium test for float measures precision on batch side X-Git-Tag: 5.3-RC1~469 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0dc21da0c4d4f29e91637bc778b2cbf16b657d80;p=sonarqube.git Add a medium test for float measures precision on batch side --- diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/measures/ConstantFloatMeasureSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/measures/ConstantFloatMeasureSensor.java index 85e139f86e1..416efe2bc39 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/measures/ConstantFloatMeasureSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/measures/ConstantFloatMeasureSensor.java @@ -24,6 +24,7 @@ import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.config.Settings; import org.sonar.api.measures.Measure; import org.sonar.api.resources.Project; import org.sonar.xoo.Xoo; @@ -33,22 +34,30 @@ import org.sonar.xoo.Xoo; */ public class ConstantFloatMeasureSensor implements Sensor { - private static final double CONSTANT_VALUE = 1.2345678910111213d; + public static final String SONAR_XOO_ENABLE_FLOAT_SENSOR = "sonar.xoo.enableFloatSensor"; + public static final String SONAR_XOO_FLOAT_PRECISION = "sonar.xoo.floatPrecision"; + + public static final double CONSTANT_VALUE = 1.2345678910111213d; private final FileSystem fs; + private final Settings settings; - public ConstantFloatMeasureSensor(FileSystem fs) { + public ConstantFloatMeasureSensor(FileSystem fs, Settings settings) { this.fs = fs; + this.settings = settings; } @Override public boolean shouldExecuteOnProject(Project project) { - return fs.hasFiles(fs.predicates().hasLanguage(Xoo.KEY)); + return fs.hasFiles(fs.predicates().hasLanguage(Xoo.KEY)) && settings.getBoolean( + SONAR_XOO_ENABLE_FLOAT_SENSOR); } @Override public void analyse(Project project, SensorContext context) { - Measure floatMeasure = new Measure<>(XooMetrics.CONSTANT_FLOAT_MEASURE, CONSTANT_VALUE); + Measure floatMeasure = settings.hasKey(SONAR_XOO_FLOAT_PRECISION) + ? new Measure<>(XooMetrics.CONSTANT_FLOAT_MEASURE, CONSTANT_VALUE, settings.getInt(SONAR_XOO_FLOAT_PRECISION)) + : new Measure<>(XooMetrics.CONSTANT_FLOAT_MEASURE, CONSTANT_VALUE); for (InputFile inputFile : getSourceFiles()) { context.saveMeasure(inputFile, floatMeasure); } 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 9212cc46512..b78239bac82 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 @@ -35,6 +35,8 @@ import org.sonar.batch.mediumtest.BatchMediumTester; import org.sonar.batch.mediumtest.TaskResult; import org.sonar.batch.protocol.output.BatchReport.Measure; import org.sonar.xoo.XooPlugin; +import org.sonar.xoo.measures.ConstantFloatMeasureSensor; +import org.sonar.xoo.measures.XooMetrics; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; @@ -50,6 +52,7 @@ public class MeasuresMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") + .registerMetric(XooMetrics.CONSTANT_FLOAT_MEASURE) .build(); @Before @@ -99,6 +102,71 @@ public class MeasuresMediumTest { Tuple.tuple(CoreMetrics.LINES_KEY, 2)); } + @Test + public void floatMeasuresTruncatedTo1ByDefault() throws IOException { + File xooFile = new File(srcDir, "sample.xoo"); + File xooMeasureFile = new File(srcDir, "sample.xoo.measures"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + FileUtils.write(xooMeasureFile, "lines:20"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.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.cpd.xoo.skip", "true") + .put(ConstantFloatMeasureSensor.SONAR_XOO_ENABLE_FLOAT_SENSOR, "true") + .build()) + .start(); + + Map> allMeasures = result.allMeasures(); + + assertThat(allMeasures.get("com.foo.project")).extracting("metricKey", "intValue", "doubleValue", "stringValue").containsOnly( + Tuple.tuple(CoreMetrics.QUALITY_PROFILES_KEY, 0, 0.0, + "[{\"key\":\"Sonar Way\",\"language\":\"xoo\",\"name\":\"Sonar Way\",\"rulesUpdatedAt\":\"2009-02-13T23:31:31+0000\"}]")); + + assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue", "doubleValue").containsOnly( + Tuple.tuple(CoreMetrics.LINES_KEY, 2, 0.0), + Tuple.tuple(XooMetrics.CONSTANT_FLOAT_MEASURE_KEY, 0, 1.2)); + } + + @Test + public void floatMeasuresCustomPrecision() throws IOException { + File xooFile = new File(srcDir, "sample.xoo"); + File xooMeasureFile = new File(srcDir, "sample.xoo.measures"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + FileUtils.write(xooMeasureFile, "lines:20"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.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.cpd.xoo.skip", "true") + .put(ConstantFloatMeasureSensor.SONAR_XOO_ENABLE_FLOAT_SENSOR, "true") + .put(ConstantFloatMeasureSensor.SONAR_XOO_FLOAT_PRECISION, "5") + .build()) + .start(); + + Map> allMeasures = result.allMeasures(); + + assertThat(allMeasures.get("com.foo.project")).extracting("metricKey", "intValue", "doubleValue", "stringValue").containsOnly( + Tuple.tuple(CoreMetrics.QUALITY_PROFILES_KEY, 0, 0.0, + "[{\"key\":\"Sonar Way\",\"language\":\"xoo\",\"name\":\"Sonar Way\",\"rulesUpdatedAt\":\"2009-02-13T23:31:31+0000\"}]")); + + assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey", "intValue", "doubleValue").containsOnly( + Tuple.tuple(CoreMetrics.LINES_KEY, 2, 0.0), + Tuple.tuple(XooMetrics.CONSTANT_FLOAT_MEASURE_KEY, 0, 1.23457)); + } + @Test public void computeLinesOnAllFiles() throws IOException { File xooFile = new File(srcDir, "sample.xoo");