diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-04-22 22:07:54 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-04-22 22:18:07 +0200 |
commit | c96993d22c7b1a41b4c0152985e8193496cdaaae (patch) | |
tree | 43a995216518361b8795b2fe8db358753dfd966c /plugins/sonar-xoo-plugin/src | |
parent | c8d6f37f0f6ff1bc7c6107350e4954bda086a14d (diff) | |
download | sonarqube-c96993d22c7b1a41b4c0152985e8193496cdaaae.tar.gz sonarqube-c96993d22c7b1a41b4c0152985e8193496cdaaae.zip |
SONAR-6200 New Sensor API to report UT/IT/Overall coverage
Diffstat (limited to 'plugins/sonar-xoo-plugin/src')
8 files changed, 533 insertions, 0 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 a94fe8c055c..48756de3679 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,6 +20,9 @@ package org.sonar.xoo; import org.sonar.api.SonarPlugin; +import org.sonar.xoo.coverage.ItCoverageSensor; +import org.sonar.xoo.coverage.OverallCoverageSensor; +import org.sonar.xoo.coverage.UtCoverageSensor; import org.sonar.xoo.extensions.XooPostJob; import org.sonar.xoo.extensions.XooProjectBuilder; import org.sonar.xoo.lang.*; @@ -70,6 +73,11 @@ public class XooPlugin extends SonarPlugin { OneIssueOnDirPerFileSensor.class, CreateIssueByInternalKeySensor.class, + // Coverage + UtCoverageSensor.class, + ItCoverageSensor.class, + OverallCoverageSensor.class, + // Other XooProjectBuilder.class, XooPostJob.class); diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/AbstractCoverageSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/AbstractCoverageSensor.java new file mode 100644 index 00000000000..2930df6c32a --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/AbstractCoverageSensor.java @@ -0,0 +1,102 @@ +/* + * 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.coverage; + +import com.google.common.base.Splitter; +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +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.coverage.CoverageType; +import org.sonar.api.batch.sensor.coverage.NewCoverage; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.xoo.Xoo; + +import java.io.File; +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +public abstract class AbstractCoverageSensor implements Sensor { + private static final Logger LOG = Loggers.get(AbstractCoverageSensor.class); + + private void processCoverage(InputFile inputFile, SensorContext context) { + File coverageFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + getCoverageExtension()); + if (coverageFile.exists()) { + LOG.debug("Processing " + coverageFile.getAbsolutePath()); + try { + List<String> lines = FileUtils.readLines(coverageFile, context.fileSystem().encoding().name()); + NewCoverage coverageBuilder = context.newCoverage() + .onFile(inputFile) + .ofType(getCoverageType()); + int lineNumber = 0; + for (String line : lines) { + lineNumber++; + if (StringUtils.isBlank(line)) { + continue; + } + if (line.startsWith("#")) { + continue; + } + try { + Iterator<String> split = Splitter.on(":").split(line).iterator(); + int lineId = Integer.parseInt(split.next()); + int lineHits = Integer.parseInt(split.next()); + coverageBuilder.lineHits(lineId, lineHits); + if (split.hasNext()) { + int conditions = Integer.parseInt(split.next()); + int coveredConditions = Integer.parseInt(split.next()); + coverageBuilder.conditions(lineId, conditions, coveredConditions); + } + } catch (Exception e) { + throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverageFile.getAbsolutePath(), e); + } + } + coverageBuilder.save(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + protected abstract String getCoverageExtension(); + + protected abstract CoverageType getCoverageType(); + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name(getSensorName()) + .onlyOnLanguages(Xoo.KEY); + } + + protected abstract String getSensorName(); + + @Override + public void execute(SensorContext context) { + for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { + processCoverage(file, context); + } + } + +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/ItCoverageSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/ItCoverageSensor.java new file mode 100644 index 00000000000..6a53ec410e9 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/ItCoverageSensor.java @@ -0,0 +1,44 @@ +/* + * 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.coverage; + +import org.sonar.api.batch.sensor.coverage.CoverageType; + +/** + * Parse files *.xoo.itcoverage + */ +public class ItCoverageSensor extends AbstractCoverageSensor { + + @Override + protected String getCoverageExtension() { + return ".itcoverage"; + } + + @Override + protected CoverageType getCoverageType() { + return CoverageType.IT; + } + + @Override + protected String getSensorName() { + return "Xoo IT Coverage Sensor"; + } + +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/OverallCoverageSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/OverallCoverageSensor.java new file mode 100644 index 00000000000..3b321ca8129 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/OverallCoverageSensor.java @@ -0,0 +1,44 @@ +/* + * 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.coverage; + +import org.sonar.api.batch.sensor.coverage.CoverageType; + +/** + * Parse files *.xoo.overallcoverage + */ +public class OverallCoverageSensor extends AbstractCoverageSensor { + + @Override + protected String getCoverageExtension() { + return ".overallcoverage"; + } + + @Override + protected CoverageType getCoverageType() { + return CoverageType.OVERALL; + } + + @Override + protected String getSensorName() { + return "Xoo Overall Coverage Sensor"; + } + +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/UtCoverageSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/UtCoverageSensor.java new file mode 100644 index 00000000000..224df0522db --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/coverage/UtCoverageSensor.java @@ -0,0 +1,44 @@ +/* + * 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.coverage; + +import org.sonar.api.batch.sensor.coverage.CoverageType; + +/** + * Parse files *.xoo.coverage + */ +public class UtCoverageSensor extends AbstractCoverageSensor { + + @Override + protected String getCoverageExtension() { + return ".coverage"; + } + + @Override + protected CoverageType getCoverageType() { + return CoverageType.UNIT; + } + + @Override + protected String getSensorName() { + return "Xoo UT Coverage Sensor"; + } + +} diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/ItCoverageSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/ItCoverageSensorTest.java new file mode 100644 index 00000000000..1fb232fa3bb --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/ItCoverageSensorTest.java @@ -0,0 +1,95 @@ +/* + * 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.coverage; + +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.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.coverage.CoverageType; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; + +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ItCoverageSensorTest { + + private ItCoverageSensor sensor; + private SensorContextTester context; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new ItCoverageSensor(); + context = SensorContextTester.create(baseDir); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testNoExecutionIfNoCoverageFile() { + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + sensor.execute(context); + } + + @Test + public void testLineHitNoConditions() throws IOException { + File coverage = new File(baseDir, "src/foo.xoo.itcoverage"); + FileUtils.write(coverage, "1:3\n\n#comment"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + + sensor.execute(context); + + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(3); + } + + @Test + public void testLineHitAndConditions() throws IOException { + File coverage = new File(baseDir, "src/foo.xoo.itcoverage"); + FileUtils.write(coverage, "1:3:4:2"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + + sensor.execute(context); + + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(3); + assertThat(context.conditions("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(4); + assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.IT, 1)).isEqualTo(2); + } +} diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/OverallCoverageSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/OverallCoverageSensorTest.java new file mode 100644 index 00000000000..3a626f91bed --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/OverallCoverageSensorTest.java @@ -0,0 +1,95 @@ +/* + * 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.coverage; + +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.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.coverage.CoverageType; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; + +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class OverallCoverageSensorTest { + + private OverallCoverageSensor sensor; + private SensorContextTester context; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new OverallCoverageSensor(); + context = SensorContextTester.create(baseDir); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testNoExecutionIfNoCoverageFile() { + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + sensor.execute(context); + } + + @Test + public void testLineHitNoConditions() throws IOException { + File coverage = new File(baseDir, "src/foo.xoo.overallcoverage"); + FileUtils.write(coverage, "1:3\n\n#comment"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + + sensor.execute(context); + + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(3); + } + + @Test + public void testLineHitAndConditions() throws IOException { + File coverage = new File(baseDir, "src/foo.xoo.overallcoverage"); + FileUtils.write(coverage, "1:3:4:2"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + + sensor.execute(context); + + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(3); + assertThat(context.conditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(4); + assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isEqualTo(2); + } +} diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/UtCoverageSensorTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/UtCoverageSensorTest.java new file mode 100644 index 00000000000..a8f14d7dfe2 --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/coverage/UtCoverageSensorTest.java @@ -0,0 +1,101 @@ +/* + * 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.coverage; + +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.internal.DefaultInputFile; +import org.sonar.api.batch.sensor.coverage.CoverageType; +import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor; +import org.sonar.api.batch.sensor.internal.SensorContextTester; + +import java.io.File; +import java.io.IOException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UtCoverageSensorTest { + + private UtCoverageSensor sensor; + private SensorContextTester context; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + private File baseDir; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder(); + sensor = new UtCoverageSensor(); + context = SensorContextTester.create(baseDir); + } + + @Test + public void testDescriptor() { + sensor.describe(new DefaultSensorDescriptor()); + } + + @Test + public void testNoExecutionIfNoCoverageFile() { + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + sensor.execute(context); + } + + @Test + public void testLineHitNoConditions() throws IOException { + File coverage = new File(baseDir, "src/foo.xoo.coverage"); + FileUtils.write(coverage, "1:3\n\n#comment"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + + sensor.execute(context); + + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(3); + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.IT, 1)).isNull(); + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isNull(); + } + + @Test + public void testLineHitAndConditions() throws IOException { + File coverage = new File(baseDir, "src/foo.xoo.coverage"); + FileUtils.write(coverage, "1:3:4:2"); + DefaultInputFile inputFile = new DefaultInputFile("foo", "src/foo.xoo").setLanguage("xoo"); + context.fileSystem().add(inputFile); + + sensor.execute(context); + + assertThat(context.lineHits("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(3); + assertThat(context.conditions("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(4); + assertThat(context.conditions("foo:src/foo.xoo", CoverageType.IT, 1)).isNull(); + assertThat(context.conditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isNull(); + assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.UNIT, 1)).isEqualTo(2); + assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.IT, 1)).isNull(); + assertThat(context.coveredConditions("foo:src/foo.xoo", CoverageType.OVERALL, 1)).isNull(); + } +} |