diff options
Diffstat (limited to 'it/it-plugins/batch-plugin')
5 files changed, 118 insertions, 0 deletions
diff --git a/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java index 7c4c479c64b..f26975594f9 100644 --- a/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java +++ b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/BatchPlugin.java @@ -1,5 +1,9 @@ package com.sonarsource; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleMeasureComputer; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleMetric; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleProperty; +import com.sonarsource.decimal_scale_of_measures.DecimalScaleSensor; import java.util.Arrays; import java.util.List; import org.sonar.api.SonarPlugin; @@ -8,6 +12,12 @@ public class BatchPlugin extends SonarPlugin { public List getExtensions() { return Arrays.asList( + // SONAR-6939 decimal_scale_of_measures + DecimalScaleMeasureComputer.class, + DecimalScaleMetric.class, + DecimalScaleSensor.class, + DecimalScaleProperty.definition(), + DumpSettingsInitializer.class, RaiseMessageException.class, TempFolderExtension.class, diff --git a/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMeasureComputer.java b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMeasureComputer.java new file mode 100644 index 00000000000..f3dc985c7e8 --- /dev/null +++ b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMeasureComputer.java @@ -0,0 +1,34 @@ +package com.sonarsource.decimal_scale_of_measures; + +import org.sonar.api.ce.measure.Measure; +import org.sonar.api.ce.measure.MeasureComputer; + +public class DecimalScaleMeasureComputer implements MeasureComputer { + + @Override + public MeasureComputerDefinition define(MeasureComputerDefinitionContext defContext) { + return defContext.newDefinitionBuilder() + // Output metrics must contains at least one metric + .setOutputMetrics(DecimalScaleMetric.KEY) + + .build(); + } + + @Override + public void compute(MeasureComputerContext context) { + if (context.getMeasure(DecimalScaleMetric.KEY) == null) { + Iterable<Measure> childMeasures = context.getChildrenMeasures(DecimalScaleMetric.KEY); + int count = 0; + double total = 0.0; + for (Measure childMeasure : childMeasures) { + count++; + total += childMeasure.getDoubleValue(); + } + double value = 0.0; + if (count > 0) { + value = total / count; + } + context.addMeasure(DecimalScaleMetric.KEY, value); + } + } +} diff --git a/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMetric.java b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMetric.java new file mode 100644 index 00000000000..ef12e051fb1 --- /dev/null +++ b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleMetric.java @@ -0,0 +1,25 @@ +package com.sonarsource.decimal_scale_of_measures; + +import java.util.Collections; +import java.util.List; +import org.sonar.api.measures.Metric; +import org.sonar.api.measures.Metrics; + +public class DecimalScaleMetric implements Metrics { + + public static final String KEY = "decimal_scale"; + + private static final Metric METRIC = new Metric.Builder(KEY, "Decimal Scale", Metric.ValueType.FLOAT) + .setDescription("Numeric metric with overridden decimal scale") + .setDecimalScale(4) + .create(); + + @Override + public List getMetrics() { + return Collections.singletonList(definition()); + } + + public static Metric definition() { + return METRIC; + } +} diff --git a/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleProperty.java b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleProperty.java new file mode 100644 index 00000000000..5fa85e71e59 --- /dev/null +++ b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleProperty.java @@ -0,0 +1,13 @@ +package com.sonarsource.decimal_scale_of_measures; + +import org.sonar.api.PropertyType; +import org.sonar.api.config.PropertyDefinition; + +public class DecimalScaleProperty { + + public static final String KEY = "sonar.scanner.feedDecimalScaleMetric"; + + public static PropertyDefinition definition() { + return PropertyDefinition.builder(KEY).name("Enable test decimal_scale_of_measures").type(PropertyType.BOOLEAN).defaultValue(String.valueOf(false)).build(); + } +} diff --git a/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleSensor.java b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleSensor.java new file mode 100644 index 00000000000..c6fe8c6ea03 --- /dev/null +++ b/it/it-plugins/batch-plugin/src/main/java/com/sonarsource/decimal_scale_of_measures/DecimalScaleSensor.java @@ -0,0 +1,36 @@ +package com.sonarsource.decimal_scale_of_measures; + +import org.sonar.api.batch.Sensor; +import org.sonar.api.batch.SensorContext; +import org.sonar.api.batch.fs.FilePredicate; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.resources.Project; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; + +public class DecimalScaleSensor implements Sensor { + private static final Logger LOG = Loggers.get(DecimalScaleSensor.class); + + @Override + public boolean shouldExecuteOnProject(Project project) { + return true; + } + + @Override + public void analyse(Project module, SensorContext context) { + if (context.settings().getBoolean(DecimalScaleProperty.KEY)) { + FilePredicate all = context.fileSystem().predicates().all(); + Iterable<InputFile> files = context.fileSystem().inputFiles(all); + double value = 0.0001; + for (InputFile file : files) { + LOG.info("Value for {}: {}", file.relativePath(), value); + context.newMeasure() + .on(file) + .forMetric(DecimalScaleMetric.definition()) + .withValue(value) + .save(); + value += 0.0001; + } + } + } +} |