diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-05 14:03:30 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-09-05 14:04:10 +0200 |
commit | e1c3a706319c397b436a28b7921b695baf712063 (patch) | |
tree | 342c942f72b71c6f2843e71a893252f383342772 /plugins/sonar-xoo-plugin/src | |
parent | a967da0f1192287036132a5876be693485e94ae6 (diff) | |
download | sonarqube-e1c3a706319c397b436a28b7921b695baf712063.tar.gz sonarqube-e1c3a706319c397b436a28b7921b695baf712063.zip |
SONAR-5389 New test API for batch 2.0
Diffstat (limited to 'plugins/sonar-xoo-plugin/src')
10 files changed, 232 insertions, 14 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 3477fab825c..c7e43d9e0ab 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 @@ -20,10 +20,12 @@ package org.sonar.xoo; import org.sonar.api.SonarPlugin; +import org.sonar.xoo.lang.CoveragePerTestSensor; import org.sonar.xoo.lang.MeasureSensor; import org.sonar.xoo.lang.ScmActivitySensor; import org.sonar.xoo.lang.SymbolReferencesSensor; import org.sonar.xoo.lang.SyntaxHighlightingSensor; +import org.sonar.xoo.lang.TestCaseSensor; import org.sonar.xoo.lang.XooTokenizerSensor; import org.sonar.xoo.rule.CreateIssueByInternalKeySensor; import org.sonar.xoo.rule.OneIssueOnDirPerFileSensor; @@ -55,6 +57,8 @@ public class XooPlugin extends SonarPlugin { SyntaxHighlightingSensor.class, SymbolReferencesSensor.class, XooTokenizerSensor.class, + TestCaseSensor.class, + CoveragePerTestSensor.class, OneIssuePerLineSensor.class, OneIssueOnDirPerFileSensor.class, diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java new file mode 100644 index 00000000000..aee8297811e --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CoveragePerTestSensor.java @@ -0,0 +1,110 @@ +/* + * 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.lang; + +import com.google.common.base.Splitter; +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.FilePredicates; +import org.sonar.api.batch.fs.FileSystem; +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.test.TestCase; +import org.sonar.xoo.Xoo; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +/** + * Parse files *.xoo.coveragePerTest + */ +public class CoveragePerTestSensor implements Sensor { + + private static final Logger LOG = LoggerFactory.getLogger(CoveragePerTestSensor.class); + + private static final String COVER_PER_TEST_EXTENSION = ".coveragePerTest"; + + private void processCoveragePerTest(InputFile inputFile, SensorContext context) { + File ioFile = inputFile.file(); + File testPlanFile = new File(ioFile.getParentFile(), ioFile.getName() + COVER_PER_TEST_EXTENSION); + if (testPlanFile.exists()) { + LOG.debug("Processing " + testPlanFile.getAbsolutePath()); + try { + List<String> lines = FileUtils.readLines(testPlanFile, context.fileSystem().encoding().name()); + int lineNumber = 0; + for (String line : lines) { + lineNumber++; + if (StringUtils.isBlank(line) || line.startsWith("#")) { + continue; + } + processLine(testPlanFile, lineNumber, context, line, inputFile); + } + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + } + + private void processLine(File testplanFile, int lineNumber, SensorContext context, String line, InputFile testFile) { + try { + Iterator<String> split = Splitter.on(":").split(line).iterator(); + String testCaseName = split.next(); + String mainFileRelativePath = split.next(); + FileSystem fs = context.fileSystem(); + InputFile mainFile = fs.inputFile(fs.predicates().hasRelativePath(mainFileRelativePath)); + List<Integer> coveredLines = new ArrayList<Integer>(); + Iterator<String> lines = Splitter.on(",").split(split.next()).iterator(); + while (lines.hasNext()) { + coveredLines.add(Integer.parseInt(lines.next())); + } + TestCase testCase = context.getTestCase(testFile, testCaseName); + if (testCase == null) { + throw new IllegalStateException("No test case with name " + testCaseName + " on file " + testFile); + } + context.saveCoveragePerTest(testCase, mainFile, coveredLines); + } catch (Exception e) { + throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testplanFile.getAbsolutePath(), e); + } + } + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name("Xoo Coverage Per Test Sensor") + .workOnLanguages(Xoo.KEY) + .workOnFileTypes(InputFile.Type.TEST); + } + + @Override + public void execute(SensorContext context) { + FileSystem fs = context.fileSystem(); + FilePredicates p = fs.predicates(); + for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.TEST)))) { + processCoveragePerTest(file, context); + } + } +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SymbolReferencesSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SymbolReferencesSensor.java index c8232484a51..61131e8b87a 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SymbolReferencesSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SymbolReferencesSensor.java @@ -30,7 +30,6 @@ import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.batch.sensor.symbol.Symbol; import org.sonar.api.batch.sensor.symbol.SymbolTableBuilder; -import org.sonar.api.measures.CoreMetrics; import org.sonar.xoo.Xoo; import java.io.File; @@ -88,7 +87,6 @@ public class SymbolReferencesSensor implements Sensor { public void describe(SensorDescriptor descriptor) { descriptor .name("Xoo Symbol Reference Sensor") - .provides(CoreMetrics.LINES) .workOnLanguages(Xoo.KEY) .workOnFileTypes(InputFile.Type.MAIN, InputFile.Type.TEST); } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SyntaxHighlightingSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SyntaxHighlightingSensor.java index c9c9b886406..a930c9c8f4c 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SyntaxHighlightingSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/SyntaxHighlightingSensor.java @@ -30,7 +30,6 @@ import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import org.sonar.api.batch.sensor.highlighting.TypeOfText; -import org.sonar.api.measures.CoreMetrics; import org.sonar.xoo.Xoo; import java.io.File; @@ -86,7 +85,6 @@ public class SyntaxHighlightingSensor implements Sensor { public void describe(SensorDescriptor descriptor) { descriptor .name("Xoo Highlighting Sensor") - .provides(CoreMetrics.LINES) .workOnLanguages(Xoo.KEY) .workOnFileTypes(InputFile.Type.MAIN, InputFile.Type.TEST); } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java new file mode 100644 index 00000000000..c4238b51e18 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/TestCaseSensor.java @@ -0,0 +1,108 @@ +/* + * 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.lang; + +import com.google.common.base.Splitter; +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.FilePredicates; +import org.sonar.api.batch.fs.FileSystem; +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.test.TestCase; +import org.sonar.xoo.Xoo; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +/** + * Parse files *.xoo.testplan + */ +public class TestCaseSensor implements Sensor { + + private static final Logger LOG = LoggerFactory.getLogger(TestCaseSensor.class); + + private static final String TESTPLAN_EXTENSION = ".testplan"; + + private void processFileTestPlan(InputFile inputFile, SensorContext context) { + File ioFile = inputFile.file(); + File testPlanFile = new File(ioFile.getParentFile(), ioFile.getName() + TESTPLAN_EXTENSION); + if (testPlanFile.exists()) { + LOG.debug("Processing " + testPlanFile.getAbsolutePath()); + try { + List<String> lines = FileUtils.readLines(testPlanFile, context.fileSystem().encoding().name()); + int lineNumber = 0; + for (String line : lines) { + lineNumber++; + if (StringUtils.isBlank(line) || line.startsWith("#")) { + continue; + } + processLine(testPlanFile, lineNumber, line, context, inputFile); + } + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + } + + private void processLine(File testplanFile, int lineNumber, String line, SensorContext context, InputFile testFile) { + try { + Iterator<String> split = Splitter.on(":").split(line).iterator(); + String name = split.next(); + String type = split.next(); + String status = split.next(); + String message = split.next(); + String stack = split.next(); + long duration = Long.parseLong(split.next()); + context.addTestCase(context.testCaseBuilder(testFile, name) + .type(TestCase.Type.valueOf(type)) + .status(TestCase.Status.valueOf(status)) + .message(message) + .stackTrace(stack) + .durationInMs(duration) + .build()); + } catch (Exception e) { + throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testplanFile.getAbsolutePath(), e); + } + } + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name("Xoo TestPlan Sensor") + .workOnLanguages(Xoo.KEY) + .workOnFileTypes(InputFile.Type.TEST); + } + + @Override + public void execute(SensorContext context) { + FileSystem fs = context.fileSystem(); + FilePredicates p = fs.predicates(); + for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.TEST)))) { + processFileTestPlan(file, context); + } + } +} 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 bfe1fd99afa..96ea0ea923c 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(11); + assertThat(new XooPlugin().getExtensions()).hasSize(13); } } 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 c06d4f307d3..f12bee28275 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 @@ -72,7 +72,7 @@ public class MeasureSensorTest { @Test public void testNoExecutionIfNoMeasureFile() { - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); sensor.execute(context); } @@ -81,7 +81,7 @@ public class MeasureSensorTest { public void testExecution() throws IOException { File measures = new File(baseDir, "src/foo.xoo.measures"); FileUtils.write(measures, "ncloc:12\nbranch_coverage:5.3\nsqale_index:300\nbool:true\ncomment_lines_data:1=1,2=1\n\n#comment"); - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); Metric<Boolean> booleanMetric = new Metric.Builder("bool", "Bool", Metric.ValueType.BOOL) @@ -108,7 +108,7 @@ public class MeasureSensorTest { public void failIfMetricNotFound() throws IOException { File measures = new File(baseDir, "src/foo.xoo.measures"); FileUtils.write(measures, "unknow:12\n\n#comment"); - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + 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()); diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SymbolReferencesSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SymbolReferencesSensorTest.java index 68fd40d0f80..d5f6a30723e 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SymbolReferencesSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SymbolReferencesSensorTest.java @@ -63,7 +63,7 @@ public class SymbolReferencesSensorTest { @Test public void testNoExecutionIfNoSymbolFile() { - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); sensor.execute(context); } @@ -72,7 +72,7 @@ public class SymbolReferencesSensorTest { public void testExecution() throws IOException { File symbol = new File(baseDir, "src/foo.xoo.symbol"); FileUtils.write(symbol, "1,4,7\n12,15,23\n\n#comment"); - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); SymbolTableBuilder symbolTableBuilder = mock(SymbolTableBuilder.class); when(context.symbolTableBuilder(inputFile)).thenReturn(symbolTableBuilder); diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java index 9918a3625ef..cad3b968113 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/SyntaxHighlightingSensorTest.java @@ -63,7 +63,7 @@ public class SyntaxHighlightingSensorTest { @Test public void testNoExecutionIfNoSyntaxFile() { - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); sensor.execute(context); } @@ -72,7 +72,7 @@ public class SyntaxHighlightingSensorTest { public void testExecution() throws IOException { File symbol = new File(baseDir, "src/foo.xoo.highlighting"); FileUtils.write(symbol, "1:4:k\n12:15:cppd\n\n#comment"); - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); HighlightingBuilder builder = mock(HighlightingBuilder.class); when(context.highlightingBuilder(inputFile)).thenReturn(builder); diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java index 941798f9436..448d45cfcc5 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/lang/XooTokenizerSensorTest.java @@ -70,7 +70,7 @@ public class XooTokenizerSensorTest { @Test public void testNoExecutionIfExclusion() { - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); settings.setProperty(CoreProperties.CPD_EXCLUSIONS, "**/foo.xoo"); sensor.execute(context); @@ -81,7 +81,7 @@ public class XooTokenizerSensorTest { public void testExecution() throws IOException { File source = new File(baseDir, "src/foo.xoo"); FileUtils.write(source, "token1 token2 token3\ntoken4"); - DefaultInputFile inputFile = new DefaultInputFile("src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setAbsolutePath(new File(baseDir, "src/foo.xoo").getAbsolutePath()).setLanguage("xoo"); fileSystem.add(inputFile); DuplicationTokenBuilder builder = mock(DuplicationTokenBuilder.class); when(context.duplicationTokenBuilder(inputFile)).thenReturn(builder); |