diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2016-03-24 15:53:36 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2016-03-30 11:42:16 +0200 |
commit | 10955ec396a053d596a40ac3fdf7ca73dd911327 (patch) | |
tree | c390cd08b8b44656206306c94213204baeec737c /plugins/sonar-xoo-plugin/src | |
parent | 946a448c08ccc10ad1c0af778ae5d9f179655ef8 (diff) | |
download | sonarqube-10955ec396a053d596a40ac3fdf7ca73dd911327.tar.gz sonarqube-10955ec396a053d596a40ac3fdf7ca73dd911327.zip |
SONAR-7458 Expose SQ Version in SensorContext
Diffstat (limited to 'plugins/sonar-xoo-plugin/src')
4 files changed, 86 insertions, 66 deletions
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index 679d3c10897..74699697e87 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -19,9 +19,7 @@ */ package org.sonar.xoo; -import java.util.Arrays; -import java.util.List; -import org.sonar.api.SonarPlugin; +import org.sonar.api.Plugin; import org.sonar.xoo.coverage.ItCoverageSensor; import org.sonar.xoo.coverage.OverallCoverageSensor; import org.sonar.xoo.coverage.UtCoverageSensor; @@ -60,17 +58,16 @@ import org.sonar.xoo.scm.XooScmProvider; import org.sonar.xoo.test.CoveragePerTestSensor; import org.sonar.xoo.test.TestExecutionSensor; +import static org.sonar.api.SonarQubeVersion.V5_5; + /** * Plugin entry-point, as declared in pom.xml. */ -public class XooPlugin extends SonarPlugin { +public class XooPlugin implements Plugin { - /** - * Declares all the extensions implemented in the plugin - */ @Override - public List getExtensions() { - return Arrays.asList( + public void define(Context context) { + context.addExtensions( Xoo.class, Xoo2.class, XooRulesDefinition.class, @@ -98,7 +95,6 @@ public class XooPlugin extends SonarPlugin { ChecksSensor.class, RandomAccessSensor.class, DeprecatedResourceApiSensor.class, - CpdTokenizerSensor.class, OneBlockerIssuePerFileSensor.class, OneIssuePerLineSensor.class, @@ -125,6 +121,10 @@ public class XooPlugin extends SonarPlugin { // Other XooProjectBuilder.class, XooPostJob.class); + + if (context.getSonarQubeVersion().isGreaterThanOrEqual(V5_5)) { + context.addExtension(CpdTokenizerSensor.class); + } } } 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 cb1e685803a..2b9474ec391 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,8 +19,7 @@ */ package org.sonar.xoo.rule; -import org.sonar.xoo.Xoo2; - +import org.sonar.api.SonarQubeVersion; import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; @@ -32,11 +31,12 @@ import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.batch.sensor.issue.NewIssue; import org.sonar.api.rule.RuleKey; import org.sonar.xoo.Xoo; +import org.sonar.xoo.Xoo2; public class OneIssuePerLineSensor implements Sensor { public static final String RULE_KEY = "OneIssuePerLine"; - private static final String EFFORT_TO_FIX_PROPERTY = "sonar.oneIssuePerLine.effortToFix"; + public static final String EFFORT_TO_FIX_PROPERTY = "sonar.oneIssuePerLine.effortToFix"; public static final String FORCE_SEVERITY_PROPERTY = "sonar.oneIssuePerLine.forceSeverity"; @Override @@ -72,9 +72,13 @@ public class OneIssuePerLineSensor implements Sensor { .on(file) .at(file.selectLine(line)) .message("This issue is generated on each line")) - .effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)) - .overrideSeverity(severity != null ? Severity.valueOf(severity) : null) - .save(); + .overrideSeverity(severity != null ? Severity.valueOf(severity) : null); + if (context.getSonarQubeVersion().isGreaterThanOrEqual(SonarQubeVersion.V5_5)) { + newIssue.gap(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); + } else { + newIssue.effortToFix(context.settings().getDouble(EFFORT_TO_FIX_PROPERTY)); + } + newIssue.save(); } } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java index f31e1b667d8..69cc0823895 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java @@ -20,13 +20,24 @@ package org.sonar.xoo; import org.junit.Test; +import org.sonar.api.Plugin; +import org.sonar.api.SonarQubeVersion; +import org.sonar.api.utils.Version; +import org.sonar.xoo.lang.CpdTokenizerSensor; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.SonarQubeVersion.V5_5; public class XooPluginTest { @Test - public void provide_extensions() { - assertThat(new XooPlugin().getExtensions().size()).isGreaterThan(0); + public void provide_extensions_for_5_5() { + Plugin.Context context = new Plugin.Context(new SonarQubeVersion(V5_5)); + new XooPlugin().define(context); + assertThat(context.getExtensions()).hasSize(39).contains(CpdTokenizerSensor.class); + + context = new Plugin.Context(new SonarQubeVersion(Version.parse("5.4"))); + new XooPlugin().define(context); + assertThat(context.getExtensions()).hasSize(38).doesNotContain(CpdTokenizerSensor.class); } } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java index e225fd5e71c..8993bac751e 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java @@ -24,26 +24,16 @@ import java.io.StringReader; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.mockito.ArgumentCaptor; -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.fs.internal.FileMetadata; import org.sonar.api.batch.rule.Severity; -import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; -import org.sonar.api.batch.sensor.internal.SensorStorage; +import org.sonar.api.batch.sensor.internal.SensorContextTester; import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; -import org.sonar.api.config.Settings; +import org.sonar.api.utils.Version; import org.sonar.xoo.Xoo; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; public class OneIssuePerLineSensorTest { @@ -61,54 +51,69 @@ public class OneIssuePerLineSensorTest { @Test public void testRule() throws IOException { - DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder().toPath()); DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY) .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n"))); - fs.add(inputFile); - - SensorContext context = mock(SensorContext.class); - final SensorStorage sensorStorage = mock(SensorStorage.class); - when(context.settings()).thenReturn(new Settings()); - when(context.fileSystem()).thenReturn(fs); - when(context.newIssue()).thenAnswer(new Answer<Issue>() { - @Override - public Issue answer(InvocationOnMock invocation) throws Throwable { - return new DefaultIssue(sensorStorage); - } - }); + + SensorContextTester context = SensorContextTester.create(temp.newFolder()); + context.fileSystem().add(inputFile); sensor.execute(context); - ArgumentCaptor<DefaultIssue> argCaptor = ArgumentCaptor.forClass(DefaultIssue.class); - verify(sensorStorage, times(10)).store(argCaptor.capture()); - assertThat(argCaptor.getAllValues()).hasSize(10); // One issue per line - assertThat(argCaptor.getValue().overriddenSeverity()).isNull(); + assertThat(context.allIssues()).hasSize(10); // One issue per line + for (Issue issue : context.allIssues()) { + assertThat(issue.gap()).isNull(); + } } @Test public void testForceSeverity() throws IOException { - DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder().toPath()); DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY) .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n"))); - fs.add(inputFile); - - SensorContext context = mock(SensorContext.class); - final SensorStorage sensorStorage = mock(SensorStorage.class); - Settings settings = new Settings(); - settings.setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR"); - when(context.settings()).thenReturn(settings); - when(context.fileSystem()).thenReturn(fs); - when(context.newIssue()).thenAnswer(new Answer<Issue>() { - @Override - public Issue answer(InvocationOnMock invocation) throws Throwable { - return new DefaultIssue(sensorStorage); - } - }); + + SensorContextTester context = SensorContextTester.create(temp.newFolder()); + context.fileSystem().add(inputFile); + context.settings().setProperty(OneIssuePerLineSensor.FORCE_SEVERITY_PROPERTY, "MINOR"); + + sensor.execute(context); + + assertThat(context.allIssues()).hasSize(10); // One issue per line + for (Issue issue : context.allIssues()) { + assertThat(issue.overriddenSeverity()).isEqualTo(Severity.MINOR); + } + } + + @Test + public void testProvideGap() throws IOException { + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY) + .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n"))); + + SensorContextTester context = SensorContextTester.create(temp.newFolder()); + context.fileSystem().add(inputFile); + context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); + + sensor.execute(context); + + assertThat(context.allIssues()).hasSize(10); // One issue per line + for (Issue issue : context.allIssues()) { + assertThat(issue.gap()).isEqualTo(1.2d); + } + } + + @Test + public void testProvideGap_before_5_5() throws IOException { + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY) + .initMetadata(new FileMetadata().readMetadata(new StringReader("a\nb\nc\nd\ne\nf\ng\nh\ni\n"))); + + SensorContextTester context = SensorContextTester.create(temp.newFolder()); + context.fileSystem().add(inputFile); + context.settings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"); + context.setSonarQubeVersion(Version.parse("5.4")); + sensor.execute(context); - ArgumentCaptor<DefaultIssue> argCaptor = ArgumentCaptor.forClass(DefaultIssue.class); - verify(sensorStorage, times(10)).store(argCaptor.capture()); - assertThat(argCaptor.getAllValues()).hasSize(10); // One issue per line - assertThat(argCaptor.getValue().overriddenSeverity()).isEqualTo(Severity.MINOR); + assertThat(context.allIssues()).hasSize(10); // One issue per line + for (Issue issue : context.allIssues()) { + assertThat(issue.gap()).isEqualTo(1.2d); + } } } |