From 70f100f4b21adae3ba49b8347b95c61ce21cfba1 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Wed, 29 Jan 2014 23:47:30 +0100 Subject: [PATCH] Remove CoberturaReportParser --- .../batch/coverage/CoberturaReportParser.java | 161 ----------------- .../api/batch/coverage/package-info.java | 23 --- .../api/utils/CoberturaReportParserUtils.java | 3 +- .../coverage/CoberturaReportParserTest.java | 170 ------------------ 4 files changed, 1 insertion(+), 356 deletions(-) delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/CoberturaReportParser.java delete mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/package-info.java delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/coverage/CoberturaReportParserTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/CoberturaReportParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/CoberturaReportParser.java deleted file mode 100644 index 1da16286b99..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/CoberturaReportParser.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.api.batch.coverage; - -import com.google.common.base.Joiner; -import com.google.common.collect.Maps; -import org.apache.commons.lang.StringUtils; -import org.codehaus.staxmate.in.SMHierarchicCursor; -import org.codehaus.staxmate.in.SMInputCursor; -import org.jfree.util.Log; -import org.sonar.api.BatchComponent; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.measures.CoverageMeasuresBuilder; -import org.sonar.api.measures.Measure; -import org.sonar.api.scan.filesystem.InputFile; -import org.sonar.api.scan.filesystem.ModuleFileSystem; -import org.sonar.api.utils.StaxParser; -import org.sonar.api.utils.XmlParserException; - -import javax.annotation.CheckForNull; -import javax.xml.stream.XMLStreamException; - -import java.io.File; -import java.text.ParseException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static java.util.Locale.ENGLISH; -import static org.sonar.api.utils.ParsingUtils.parseNumber; - -/** - * Parse a provided cobertura report and create appropriate measures. - * @since 4.2 - */ -public class CoberturaReportParser implements BatchComponent { - - private final ModuleFileSystem fs; - - public CoberturaReportParser(ModuleFileSystem fs) { - this.fs = fs; - } - - /** - * Parse a Cobertura xml report and create measures accordingly - */ - public void parseReport(File xmlFile, final SensorContext context) { - try { - StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() { - - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - rootCursor.advance(); - SMInputCursor rootChildCursor = rootCursor.childElementCursor(); - - List sourceDirs = new ArrayList(); - - while (rootChildCursor.getNext() != null) { - handleRootChildElement(sourceDirs, context, rootChildCursor); - } - } - - }); - parser.parse(xmlFile); - } catch (XMLStreamException e) { - throw new XmlParserException("Fail to parse " + xmlFile.getAbsolutePath(), e); - } - } - - private void handleRootChildElement(List sourceDirs, SensorContext context, SMInputCursor rootChildCursor) throws XMLStreamException { - if ("sources".equals(rootChildCursor.getLocalName())) { - collectSourceFolders(rootChildCursor.childElementCursor(), sourceDirs); - } else if ("packages".equals(rootChildCursor.getLocalName())) { - collectPackageMeasures(rootChildCursor.childElementCursor(), context, sourceDirs); - } - } - - private void collectSourceFolders(SMInputCursor source, List sourceDirs) throws XMLStreamException { - while (source.getNext() != null) { - sourceDirs.add(new File(source.collectDescendantText())); - } - } - - private void collectPackageMeasures(SMInputCursor pack, SensorContext context, List sourceDirs) throws XMLStreamException { - while (pack.getNext() != null) { - Map builderByFilename = Maps.newHashMap(); - collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename); - for (Map.Entry entry : builderByFilename.entrySet()) { - String filename = entry.getKey(); - - InputFile inputfile = findInputFile(filename, sourceDirs); - if (inputfile != null) { - for (Measure measure : entry.getValue().createMeasures()) { - context.saveMeasure(inputfile, measure); - } - } - } - } - } - - @CheckForNull - private InputFile findInputFile(String filename, List sourceDirs) { - for (File srcDir : sourceDirs) { - File possibleFile = new File(srcDir, filename); - InputFile inputFile = fs.inputFile(possibleFile); - if (inputFile != null) { - return inputFile; - } - } - Log.debug("Filename " + filename + " was not found as an InputFile is any of the source folders: " + Joiner.on(", ").join(sourceDirs)); - return null; - } - - private static void collectFileMeasures(SMInputCursor clazz, Map builderByFilename) throws XMLStreamException { - while (clazz.getNext() != null) { - String fileName = clazz.getAttrValue("filename"); - CoverageMeasuresBuilder builder = builderByFilename.get(fileName); - if (builder == null) { - builder = CoverageMeasuresBuilder.create(); - builderByFilename.put(fileName, builder); - } - collectFileData(clazz, builder); - } - } - - private static void collectFileData(SMInputCursor clazz, CoverageMeasuresBuilder builder) throws XMLStreamException { - SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line"); - while (line.getNext() != null) { - int lineId = Integer.parseInt(line.getAttrValue("number")); - try { - builder.setHits(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH)); - } catch (ParseException e) { - throw new XmlParserException(e); - } - - String isBranch = line.getAttrValue("branch"); - String text = line.getAttrValue("condition-coverage"); - if (StringUtils.equals(isBranch, "true") && StringUtils.isNotBlank(text)) { - String[] conditions = StringUtils.split(StringUtils.substringBetween(text, "(", ")"), "/"); - builder.setConditions(lineId, Integer.parseInt(conditions[1]), Integer.parseInt(conditions[0])); - } - } - } - -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/package-info.java deleted file mode 100644 index dd27e41d405..00000000000 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/coverage/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.api.batch.coverage; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/CoberturaReportParserUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/CoberturaReportParserUtils.java index f7e376f631d..1fc0bd38448 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/CoberturaReportParserUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/CoberturaReportParserUtils.java @@ -25,7 +25,6 @@ import org.apache.commons.lang.StringUtils; import org.codehaus.staxmate.in.SMHierarchicCursor; import org.codehaus.staxmate.in.SMInputCursor; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.coverage.CoberturaReportParser; import org.sonar.api.measures.CoverageMeasuresBuilder; import org.sonar.api.measures.Measure; import org.sonar.api.resources.Resource; @@ -41,7 +40,7 @@ import static org.sonar.api.utils.ParsingUtils.parseNumber; /** * @since 3.7 - * @deprecated since 4.2 use {@link CoberturaReportParser} + * @deprecated since 4.2 should be handled by language plugins */ @Deprecated public class CoberturaReportParserUtils { diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/coverage/CoberturaReportParserTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/coverage/CoberturaReportParserTest.java deleted file mode 100644 index a620ec8eb12..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/coverage/CoberturaReportParserTest.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.api.batch.coverage; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.resources.JavaFile; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.Scopes; -import org.sonar.api.scan.filesystem.InputFile; -import org.sonar.api.scan.filesystem.ModuleFileSystem; -import org.sonar.api.test.IsMeasure; -import org.sonar.api.test.IsResource; - -import java.io.File; -import java.net.URISyntaxException; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class CoberturaReportParserTest { - - private SensorContext context; - private ModuleFileSystem fs; - private CoberturaReportParser parser; - - @Before - public void setUp() { - context = mock(SensorContext.class); - fs = mock(ModuleFileSystem.class); - parser = new CoberturaReportParser(fs); - } - - @Test - public void collectFileLineCoverage() throws URISyntaxException { - InputFile inputFile = mock(InputFile.class); - when(fs.inputFile(eq(new File("/Users/simon/projects/commons-chain/src/java", "org/apache/commons/chain/config/ConfigParser.java")))).thenReturn(inputFile); - parser.parseReport(getCoverageReport(), context); - - verify(context).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 30.0))); - verify(context).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES, 5.0))); - } - - @Test - public void collectFileBranchCoverage() throws URISyntaxException { - InputFile inputFile = mock(InputFile.class); - when(fs.inputFile(eq(new File("/Users/simon/projects/commons-chain/src/java", "org/apache/commons/chain/config/ConfigParser.java")))).thenReturn(inputFile); - parser.parseReport(getCoverageReport(), context); - - verify(context).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 6.0))); - verify(context).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS, 2.0))); - } - - @Test - public void testDoNotSaveMeasureOnResourceWhichDoesntExistInTheFileSystem() throws URISyntaxException { - when(fs.inputFile(any(File.class))).thenReturn(null); - parser.parseReport(getCoverageReport(), context); - verify(context, never()).saveMeasure(any(InputFile.class), any(Measure.class)); - } - - @Test - public void javaInterfaceHasNoCoverage() throws URISyntaxException { - InputFile inputFile = mock(InputFile.class); - when(fs.inputFile(eq(new File("/Users/simon/projects/commons-chain/src/java", "org/apache/commons/chain/Chain.java")))).thenReturn(inputFile); - parser.parseReport(getCoverageReport(), context); - - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.COVERAGE))); - - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.LINE_COVERAGE))); - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER))); - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES))); - - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.BRANCH_COVERAGE))); - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER))); - verify(context, never()).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS))); - } - - @Test - public void shouldInsertCoverageAtFileLevel() throws URISyntaxException { - File coverage = new File(getClass().getResource( - "/org/sonar/api/utils/CoberturaReportParserUtilsTest/shouldInsertCoverageAtFileLevel/coverage.xml").toURI()); - - InputFile innerClass = mock(InputFile.class); - when(fs.inputFile(eq(new File("/Users/simon/projects/sonar/trunk/tests/integration/reference-projects/reference/src/main/java", - "org/sonar/samples/InnerClass.java")))).thenReturn(innerClass); - parser.parseReport(coverage, context); - - verify(context).saveMeasure(eq(innerClass), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 35.0))); - verify(context).saveMeasure(eq(innerClass), argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES, 22.0))); - - verify(context).saveMeasure(eq(innerClass), argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER, 4.0))); - verify(context).saveMeasure(eq(innerClass), argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS, 3.0))); - - verify(context, never()).saveMeasure( - argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.sonar.samples.InnerClass$InnerClassInside")), - argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER))); - verify(context, never()).saveMeasure( - argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.sonar.samples.InnerClass$InnerClassInside")), - argThat(new IsMeasure(CoreMetrics.CONDITIONS_TO_COVER))); - verify(context, never()).saveMeasure( - argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.sonar.samples.InnerClass$InnerClassInside")), - argThat(new IsMeasure(CoreMetrics.UNCOVERED_CONDITIONS))); - verify(context, never()).saveMeasure( - argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.sonar.samples.InnerClass$InnerClassInside")), - argThat(new IsMeasure(CoreMetrics.UNCOVERED_LINES))); - - verify(fs, never()).inputFile(eq(new File("/Users/simon/projects/sonar/trunk/tests/integration/reference-projects/reference/src/main/java", - "org/sonar/samples/PrivateClass.java"))); - - verify(context) - .saveMeasure( - eq(innerClass), - argThat(new IsMeasure( - CoreMetrics.COVERAGE_LINE_HITS_DATA, - "22=2;25=0;26=0;29=0;30=0;31=0;34=1;35=1;36=1;37=0;39=1;41=1;44=2;46=1;47=1;50=0;51=0;52=0;53=0;55=0;57=0;60=0;61=0;64=1;71=1;73=1;76=0;77=0;80=0;81=0;85=0;87=0;91=0;93=0;96=1"))); - } - - @Test - public void collectFileLineHitsData() throws URISyntaxException { - InputFile inputFile = mock(InputFile.class); - when(fs.inputFile(eq(new File("/Users/simon/projects/commons-chain/src/java", "org/apache/commons/chain/impl/CatalogBase.java")))).thenReturn(inputFile); - when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass")); - parser.parseReport(getCoverageReport(), context); - verify(context).saveMeasure( - eq(inputFile), - argThat(new IsMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA, - "48=117;56=234;66=0;67=0;68=0;84=999;86=999;98=318;111=18;121=0;122=0;125=0;126=0;127=0;128=0;131=0;133=0"))); - } - - @Test - public void shouldNotCountTwiceAnonymousClasses() throws URISyntaxException { - File coverage = new File(getClass().getResource("/org/sonar/api/utils/CoberturaReportParserUtilsTest/shouldNotCountTwiceAnonymousClasses.xml").toURI()); - InputFile inputFile = mock(InputFile.class); - when(fs.inputFile(eq(new File("/Users/simon/projects/sonar/trunk/tests/integration/reference-projects/reference/src/main/java", - "org/sonar/samples/MyFile.java")))).thenReturn(inputFile); - parser.parseReport(coverage, context); - - verify(context).saveMeasure(eq(inputFile), argThat(new IsMeasure(CoreMetrics.LINES_TO_COVER, 5.0))); // do not count line 26 twice - } - - private File getCoverageReport() throws URISyntaxException { - return new File(getClass().getResource("/org/sonar/api/utils/CoberturaReportParserUtilsTest/commons-chain-coverage.xml").toURI()); - } -} -- 2.39.5