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/main/java/org/sonar/xoo | |
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/main/java/org/sonar/xoo')
5 files changed, 222 insertions, 4 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); + } + } +} |