aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-xoo-plugin/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-09-25 13:48:35 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-10-02 17:52:23 +0200
commitcb639f864815f8dfd7dbbd0f21fd4ad08b7db8cc (patch)
tree89798ee35e415d2a024e320e6369636654780520 /plugins/sonar-xoo-plugin/src
parenta1cb900c90178390776c987462a4f2501b1e4e2c (diff)
downloadsonarqube-cb639f864815f8dfd7dbbd0f21fd4ad08b7db8cc.tar.gz
sonarqube-cb639f864815f8dfd7dbbd0f21fd4ad08b7db8cc.zip
SONAR-5644 Rework SCM API
Diffstat (limited to 'plugins/sonar-xoo-plugin/src')
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java4
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/package-info.java23
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/OneIssuePerLineSensor.java2
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java)36
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java43
-rw-r--r--plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/package-info.java23
-rw-r--r--plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java2
-rw-r--r--plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java104
-rw-r--r--plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooBlameCommandTest.java75
9 files changed, 281 insertions, 31 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 e1988ac37ab..148a6df0b7c 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
@@ -25,13 +25,14 @@ import org.sonar.xoo.lang.MeasureSensor;
import org.sonar.xoo.lang.SymbolReferencesSensor;
import org.sonar.xoo.lang.SyntaxHighlightingSensor;
import org.sonar.xoo.lang.TestCaseSensor;
-import org.sonar.xoo.lang.XooScmProvider;
import org.sonar.xoo.lang.XooTokenizerSensor;
import org.sonar.xoo.rule.CreateIssueByInternalKeySensor;
import org.sonar.xoo.rule.OneIssueOnDirPerFileSensor;
import org.sonar.xoo.rule.OneIssuePerLineSensor;
import org.sonar.xoo.rule.XooQualityProfile;
import org.sonar.xoo.rule.XooRulesDefinition;
+import org.sonar.xoo.scm.XooBlameCommand;
+import org.sonar.xoo.scm.XooScmProvider;
import java.util.Arrays;
import java.util.List;
@@ -53,6 +54,7 @@ public class XooPlugin extends SonarPlugin {
// SCM
XooScmProvider.class,
+ XooBlameCommand.class,
// sensors
MeasureSensor.class,
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/package-info.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/package-info.java
new file mode 100644
index 00000000000..9963cc34bd9
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.xoo.lang;
+
+import javax.annotation.ParametersAreNonnullByDefault;
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 7a11c639e38..31db08c01f2 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
@@ -32,7 +32,7 @@ public class OneIssuePerLineSensor implements Sensor {
public static final String RULE_KEY = "OneIssuePerLine";
private static final String EFFORT_TO_FIX_PROPERTY = "sonar.oneIssuePerLine.effortToFix";
- private static final String FORCE_SEVERITY_PROPERTY = "sonar.oneIssuePerLine.forceSeverity";
+ public static final String FORCE_SEVERITY_PROPERTY = "sonar.oneIssuePerLine.forceSeverity";
@Override
public void describe(SensorDescriptor descriptor) {
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java
index 895d47dfbbe..7d492cc315b 100644
--- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/XooScmProvider.java
+++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooBlameCommand.java
@@ -17,18 +17,16 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-package org.sonar.xoo.lang;
+package org.sonar.xoo.scm;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.scm.BlameCommand;
import org.sonar.api.batch.scm.BlameLine;
-import org.sonar.api.batch.scm.ScmProvider;
-import org.sonar.api.config.Settings;
import org.sonar.api.utils.DateUtils;
import java.io.File;
@@ -37,37 +35,19 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
-public class XooScmProvider implements ScmProvider {
-
- private static final Logger LOG = LoggerFactory.getLogger(XooScmProvider.class);
+public class XooBlameCommand implements BlameCommand {
private static final String SCM_EXTENSION = ".scm";
- private final Settings settings;
-
- public XooScmProvider(Settings settings) {
- this.settings = settings;
- }
-
- @Override
- public String key() {
- return "xoo";
- }
-
- @Override
- public boolean supports(File baseDir) {
- return false;
- }
-
@Override
- public void blame(Iterable<InputFile> files, BlameResult handler) {
+ public void blame(FileSystem fs, Iterable<InputFile> files, BlameResult result) {
for (InputFile inputFile : files) {
- processFile(inputFile, handler);
+ processFile(inputFile, result);
}
}
@VisibleForTesting
- protected void processFile(InputFile inputFile, BlameResult handler) {
+ protected void processFile(InputFile inputFile, BlameResult result) {
File ioFile = inputFile.file();
File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION);
if (!scmDataFile.exists()) {
@@ -94,7 +74,7 @@ public class XooScmProvider implements ScmProvider {
blame.add(new BlameLine(date, revision, author));
}
}
- handler.add(inputFile, blame);
+ result.add(inputFile, blame);
} catch (IOException e) {
throw new IllegalStateException(e);
}
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java
new file mode 100644
index 00000000000..5807d820043
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/XooScmProvider.java
@@ -0,0 +1,43 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.xoo.scm;
+
+import org.sonar.api.batch.scm.BlameCommand;
+import org.sonar.api.batch.scm.ScmProvider;
+
+public class XooScmProvider extends ScmProvider {
+
+ private final XooBlameCommand blame;
+
+ public XooScmProvider(XooBlameCommand blame) {
+ this.blame = blame;
+ }
+
+ @Override
+ public String key() {
+ return "xoo";
+ }
+
+ @Override
+ public BlameCommand blameCommand() {
+ return blame;
+ }
+
+}
diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/package-info.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/package-info.java
new file mode 100644
index 00000000000..f5c68e3c563
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/scm/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.xoo.scm;
+
+import javax.annotation.ParametersAreNonnullByDefault;
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 96ea0ea923c..e8100740345 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
@@ -27,6 +27,6 @@ public class XooPluginTest {
@Test
public void provide_extensions() {
- assertThat(new XooPlugin().getExtensions()).hasSize(13);
+ assertThat(new XooPlugin().getExtensions()).hasSize(14);
}
}
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
new file mode 100644
index 00000000000..31d109e3653
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/OneIssuePerLineSensorTest.java
@@ -0,0 +1,104 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.xoo.rule;
+
+import org.junit.Test;
+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.sensor.SensorContext;
+import org.sonar.api.batch.sensor.SensorStorage;
+import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
+import org.sonar.api.batch.sensor.issue.Issue;
+import org.sonar.api.batch.sensor.issue.Issue.Severity;
+import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
+import org.sonar.api.config.Settings;
+import org.sonar.xoo.Xoo;
+
+import static org.fest.assertions.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 {
+
+ private OneIssuePerLineSensor sensor = new OneIssuePerLineSensor();
+
+ @Test
+ public void testDescriptor() {
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
+ sensor.describe(descriptor);
+ assertThat(descriptor.ruleRepositories()).containsOnly(XooRulesDefinition.XOO_REPOSITORY);
+ }
+
+ @Test
+ public void testRule() {
+ DefaultFileSystem fs = new DefaultFileSystem();
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY).setLines(10);
+ 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);
+ }
+ });
+ 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().overridenSeverity()).isNull();
+ }
+
+ @Test
+ public void testForceSeverity() {
+ DefaultFileSystem fs = new DefaultFileSystem();
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/Foo.xoo").setLanguage(Xoo.KEY).setLines(10);
+ 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);
+ }
+ });
+ 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().overridenSeverity()).isEqualTo(Severity.MINOR);
+ }
+
+}
diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooBlameCommandTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooBlameCommandTest.java
new file mode 100644
index 00000000000..bba495dd400
--- /dev/null
+++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/scm/XooBlameCommandTest.java
@@ -0,0 +1,75 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.xoo.scm;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultFileSystem;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.scm.BlameCommand.BlameResult;
+import org.sonar.api.batch.scm.BlameLine;
+import org.sonar.api.utils.DateUtils;
+import org.sonar.xoo.Xoo;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class XooBlameCommandTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private DefaultFileSystem fs;
+ private File baseDir;
+
+ @Before
+ public void prepare() throws IOException {
+ baseDir = temp.newFolder();
+ fs = new DefaultFileSystem();
+ }
+
+ @Test
+ public void testBlame() throws IOException {
+ File source = new File(baseDir, "src/foo.xoo");
+ FileUtils.write(source, "sample content");
+ File scm = new File(baseDir, "src/foo.xoo.scm");
+ FileUtils.write(scm, "123,julien,2014-12-12\n234,julien,2014-12-24");
+ DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage(Xoo.KEY);
+ fs.add(inputFile);
+
+ BlameResult result = mock(BlameResult.class);
+ new XooBlameCommand().blame(fs, Arrays.<InputFile>asList(inputFile), result);
+ verify(result).add(inputFile, Arrays.asList(new BlameLine(DateUtils.parseDate("2014-12-12"), "123", "julien"),
+ new BlameLine(DateUtils.parseDate("2014-12-24"), "234", "julien")));
+ }
+
+}