diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-19 15:55:47 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-22 10:49:34 +0200 |
commit | 78a1280ead8a756ca52dc13ce31b17e08ab2f703 (patch) | |
tree | ef3e7c152734db8508e4064d4085aa6a070525f9 /plugins/sonar-xoo-plugin/src | |
parent | c2959b87c6c1afe49acc15a372bc8530a6a6ffc6 (diff) | |
download | sonarqube-78a1280ead8a756ca52dc13ce31b17e08ab2f703.tar.gz sonarqube-78a1280ead8a756ca52dc13ce31b17e08ab2f703.zip |
SONAR-5389 Refactor new measure API
Diffstat (limited to 'plugins/sonar-xoo-plugin/src')
3 files changed, 36 insertions, 36 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java index 4ce85626d15..5ec2190ce2d 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java @@ -29,7 +29,6 @@ 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.Measure; -import org.sonar.api.batch.sensor.measure.MeasureBuilder; import org.sonar.api.measures.CoreMetrics; import org.sonar.xoo.Xoo; @@ -78,34 +77,34 @@ public class MeasureSensor implements Sensor { try { String metricKey = StringUtils.substringBefore(line, ":"); String value = line.substring(metricKey.length() + 1); - context.addMeasure(createMeasure(context, inputFile, metricKey, value)); + saveMeasure(context, inputFile, metricKey, value); } catch (Exception e) { throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e); } } - private Measure createMeasure(SensorContext context, InputFile xooFile, String metricKey, String value) { + private void saveMeasure(SensorContext context, InputFile xooFile, String metricKey, String value) { org.sonar.api.batch.measure.Metric<Serializable> metric = metricFinder.findByKey(metricKey); if (metric == null) { throw new IllegalStateException("Unknow metric with key: " + metricKey); } - MeasureBuilder<Serializable> builder = context.measureBuilder() + Measure<Serializable> newMeasure = context.newMeasure() .forMetric(metric) .onFile(xooFile); if (Boolean.class.equals(metric.valueType())) { - builder.withValue(Boolean.parseBoolean(value)); + newMeasure.withValue(Boolean.parseBoolean(value)); } else if (Integer.class.equals(metric.valueType())) { - builder.withValue(Integer.valueOf(value)); + newMeasure.withValue(Integer.valueOf(value)); } else if (Double.class.equals(metric.valueType())) { - builder.withValue(Double.valueOf(value)); + newMeasure.withValue(Double.valueOf(value)); } else if (String.class.equals(metric.valueType())) { - builder.withValue(value); + newMeasure.withValue(value); } else if (Long.class.equals(metric.valueType())) { - builder.withValue(Long.valueOf(value)); + newMeasure.withValue(Long.valueOf(value)); } else { throw new UnsupportedOperationException("Unsupported type :" + metric.valueType()); } - return builder.build(); + newMeasure.save(); } @Override diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java index e65efa43718..e9fab499c18 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java @@ -19,13 +19,11 @@ */ package org.sonar.xoo.rule; -import org.slf4j.LoggerFactory; import org.sonar.api.batch.fs.InputFile; 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.issue.IssueBuilder; -import org.sonar.api.batch.sensor.measure.Measure; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.rule.RuleKey; import org.sonar.xoo.Xoo; @@ -55,21 +53,16 @@ public class OneIssuePerLineSensor implements Sensor { private void createIssues(InputFile file, SensorContext context) { RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); - Measure<Integer> linesMeasure = context.getMeasure(file, CoreMetrics.LINES); - if (linesMeasure == null) { - LoggerFactory.getLogger(getClass()).warn("Missing measure " + CoreMetrics.LINES_KEY + " on " + file); - } else { - IssueBuilder issueBuilder = context.issueBuilder(); - for (int line = 1; line <= (Integer) linesMeasure.value(); line++) { - context.addIssue(issueBuilder - .ruleKey(ruleKey) - .onFile(file) - .atLine(line) - .effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)) - .severity(context.settings().getString(FORCE_SEVERITY_PROPERTY)) - .message("This issue is generated on each line") - .build()); - } + IssueBuilder issueBuilder = context.issueBuilder(); + for (int line = 1; line <= file.lines(); line++) { + context.addIssue(issueBuilder + .ruleKey(ruleKey) + .onFile(file) + .atLine(line) + .effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)) + .severity(context.settings().getString(FORCE_SEVERITY_PROPERTY)) + .message("This issue is generated on each line") + .build()); } } } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java index f12bee28275..11be6dfcccc 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/MeasureSensorTest.java @@ -25,12 +25,15 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.measure.MetricFinder; +import org.sonar.api.batch.sensor.SensorStorage; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasureBuilder; +import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; @@ -55,6 +58,7 @@ public class MeasureSensorTest { private File baseDir; private MetricFinder metricFinder; + private SensorStorage<DefaultMeasure> persister; @Before public void prepare() throws IOException { @@ -63,6 +67,13 @@ public class MeasureSensorTest { sensor = new MeasureSensor(metricFinder); fileSystem = new DefaultFileSystem(); when(context.fileSystem()).thenReturn(fileSystem); + persister = mock(SensorStorage.class); + when(context.newMeasure()).then(new Answer<DefaultMeasure>() { + @Override + public DefaultMeasure answer(InvocationOnMock invocation) throws Throwable { + return new DefaultMeasure(persister); + } + }); } @Test @@ -92,15 +103,14 @@ public class MeasureSensorTest { when(metricFinder.findByKey("sqale_index")).thenReturn(CoreMetrics.TECHNICAL_DEBT); when(metricFinder.findByKey("comment_lines_data")).thenReturn(CoreMetrics.COMMENT_LINES_DATA); when(metricFinder.findByKey("bool")).thenReturn(booleanMetric); - when(context.measureBuilder()).thenReturn(new DefaultMeasureBuilder()); sensor.execute(context); - verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.NCLOC).onFile(inputFile).withValue(12).build()); - verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.BRANCH_COVERAGE).onFile(inputFile).withValue(5.3).build()); - verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.TECHNICAL_DEBT).onFile(inputFile).withValue(300L).build()); - verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(booleanMetric).onFile(inputFile).withValue(true).build()); - verify(context).addMeasure(new DefaultMeasureBuilder().forMetric(CoreMetrics.COMMENT_LINES_DATA).onFile(inputFile).withValue("1=1,2=1").build()); + verify(persister).store(new DefaultMeasure().forMetric(CoreMetrics.NCLOC).onFile(inputFile).withValue(12)); + verify(persister).store(new DefaultMeasure().forMetric(CoreMetrics.BRANCH_COVERAGE).onFile(inputFile).withValue(5.3)); + verify(persister).store(new DefaultMeasure().forMetric(CoreMetrics.TECHNICAL_DEBT).onFile(inputFile).withValue(300L)); + verify(persister).store(new DefaultMeasure().forMetric(booleanMetric).onFile(inputFile).withValue(true)); + verify(persister).store(new DefaultMeasure().forMetric(CoreMetrics.COMMENT_LINES_DATA).onFile(inputFile).withValue("1=1,2=1")); } @@ -111,8 +121,6 @@ public class MeasureSensorTest { DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); - when(context.measureBuilder()).thenReturn(new DefaultMeasureBuilder()); - thrown.expect(IllegalStateException.class); sensor.execute(context); |