]> source.dussan.org Git - sonarqube.git/commitdiff
Add a medium test for float measures precision on batch side
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 22 Oct 2015 08:57:55 +0000 (10:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 22 Oct 2015 11:08:06 +0000 (13:08 +0200)
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/measures/ConstantFloatMeasureSensor.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/measures/MeasuresMediumTest.java

index 85e139f86e1acefe949ac71946a3932582e7b4ac..416efe2bc39c066cf388b6252e5c74433df25b1a 100644 (file)
@@ -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);
     }
index 9212cc46512deb7db568295b2573b85f2af287b6..b78239bac82f5c18a670079a567015ec1fed2934 100644 (file)
@@ -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.<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.cpd.xoo.skip", "true")
+        .put(ConstantFloatMeasureSensor.SONAR_XOO_ENABLE_FLOAT_SENSOR, "true")
+        .build())
+      .start();
+
+    Map<String, List<Measure>> 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.<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.cpd.xoo.skip", "true")
+        .put(ConstantFloatMeasureSensor.SONAR_XOO_ENABLE_FLOAT_SENSOR, "true")
+        .put(ConstantFloatMeasureSensor.SONAR_XOO_FLOAT_PRECISION, "5")
+        .build())
+      .start();
+
+    Map<String, List<Measure>> 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");