diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-07-11 12:22:50 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-07-11 14:17:11 +0200 |
commit | 405494e6d7ed3a35d67b295fab031bb428cfab52 (patch) | |
tree | 19eea838620b2b6df4311cdd48603892db55ae02 /sonar-deprecated | |
parent | c245aaf4387a86a81fd3f97e745e7613e2fb989c (diff) | |
download | sonarqube-405494e6d7ed3a35d67b295fab031bb428cfab52.tar.gz sonarqube-405494e6d7ed3a35d67b295fab031bb428cfab52.zip |
SONAR-4481 Create a new Cobertura Report parser API
Diffstat (limited to 'sonar-deprecated')
4 files changed, 12 insertions, 124 deletions
diff --git a/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/AbstractCoberturaParser.java b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/AbstractCoberturaParser.java index a9394a29889..1f6ab554da2 100644 --- a/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/AbstractCoberturaParser.java +++ b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/AbstractCoberturaParser.java @@ -19,26 +19,12 @@ */ package org.sonar.plugins.cobertura.api; -import com.google.common.collect.Maps; -import org.apache.commons.io.FilenameUtils; -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.measures.CoverageMeasuresBuilder; -import org.sonar.api.measures.Measure; import org.sonar.api.resources.Resource; -import org.sonar.api.utils.StaxParser; -import org.sonar.api.utils.XmlParserException; - -import javax.xml.stream.XMLStreamException; +import org.sonar.api.utils.CoberturaReportParserUtils; +import org.sonar.api.utils.CoberturaReportParserUtils.FileResolver; import java.io.File; -import java.text.ParseException; -import java.util.Map; - -import static java.util.Locale.ENGLISH; -import static org.sonar.api.utils.ParsingUtils.parseNumber; /** * @since 2.4 @@ -49,75 +35,12 @@ import static org.sonar.api.utils.ParsingUtils.parseNumber; public abstract class AbstractCoberturaParser { public void parseReport(File xmlFile, final SensorContext context) { - try { - StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() { - - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - try { - rootCursor.advance(); - collectPackageMeasures(rootCursor.descendantElementCursor("package"), context); - } catch (ParseException e) { - throw new XMLStreamException(e); - } - } - }); - parser.parse(xmlFile); - } catch (XMLStreamException e) { - throw new XmlParserException(e); - } - } - - private void collectPackageMeasures(SMInputCursor pack, SensorContext context) throws ParseException, XMLStreamException { - while (pack.getNext() != null) { - Map<String, CoverageMeasuresBuilder> builderByFilename = Maps.newHashMap(); - collectFileMeasures(pack.descendantElementCursor("class"), builderByFilename); - for (Map.Entry<String, CoverageMeasuresBuilder> entry : builderByFilename.entrySet()) { - String filename = sanitizeFilename(entry.getKey()); - Resource file = getResource(filename); - if (fileExists(context, file)) { - for (Measure measure : entry.getValue().createMeasures()) { - context.saveMeasure(file, measure); - } - } - } - } - } - - private boolean fileExists(SensorContext context, Resource file) { - return context.getResource(file) != null; - } - - private void collectFileMeasures(SMInputCursor clazz, Map<String, CoverageMeasuresBuilder> builderByFilename) throws ParseException, 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 void collectFileData(SMInputCursor clazz, CoverageMeasuresBuilder builder) throws ParseException, XMLStreamException { - SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line"); - while (line.getNext() != null) { - int lineId = Integer.parseInt(line.getAttrValue("number")); - builder.setHits(lineId, (int) parseNumber(line.getAttrValue("hits"), ENGLISH)); - - 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])); + CoberturaReportParserUtils.parseReport(xmlFile, context, new FileResolver() { + @Override + public Resource<?> resolve(String filename) { + return getResource(filename); } - } - } - - private String sanitizeFilename(String s) { - String fileName = FilenameUtils.removeExtension(s); - fileName = fileName.replace('/', '.').replace('\\', '.'); - return fileName; + }); } protected abstract Resource getResource(String fileName); diff --git a/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java index 3488d166ec0..36b88a0672c 100644 --- a/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java +++ b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java @@ -23,24 +23,25 @@ import org.sonar.api.batch.maven.MavenPlugin; import org.sonar.api.batch.maven.MavenUtils; import org.sonar.api.resources.Project; import org.sonar.api.utils.Logs; -import org.sonar.plugins.cobertura.base.CoberturaConstants; import java.io.File; /** * @since 2.4 * @deprecated since 3.7 Used to keep backward compatibility since extraction - * of Cobertura plugin. + * of Cobertura plugin. Should be duplicated in each project. */ @Deprecated public final class CoberturaUtils { public static final String COBERTURA_GROUP_ID = MavenUtils.GROUP_ID_CODEHAUS_MOJO; public static final String COBERTURA_ARTIFACT_ID = "cobertura-maven-plugin"; + public static final String COBERTURA_REPORT_PATH_PROPERTY = "sonar.cobertura.reportPath"; /** * @deprecated in 2.8, because assumes that Sonar executed from Maven. Not used any more in sonar-cobertura-plugin. * See http://jira.codehaus.org/browse/SONAR-2321 + * Used in groovy and flex plugins. */ @Deprecated public static File getReport(Project project) { @@ -60,7 +61,7 @@ public final class CoberturaUtils { } private static File getReportFromProperty(Project project) { - String path = (String) project.getProperty(CoberturaConstants.COBERTURA_REPORT_PATH_PROPERTY); + String path = (String) project.getProperty(COBERTURA_REPORT_PATH_PROPERTY); if (path != null) { return project.getFileSystem().resolvePath(path); } diff --git a/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/base/CoberturaConstants.java b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/base/CoberturaConstants.java deleted file mode 100644 index 6cd73ac629d..00000000000 --- a/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/base/CoberturaConstants.java +++ /dev/null @@ -1,35 +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.plugins.cobertura.base; - -/** - * @deprecated since 3.7 Used to keep backward compatibility since extraction - * of Cobertura plugin. - */ -@Deprecated -public final class CoberturaConstants { - - public static final String COBERTURA_REPORT_PATH_PROPERTY = "sonar.cobertura.reportPath"; - public static final String COBERTURA_MAXMEM_PROPERTY = "sonar.cobertura.maxmem"; - public static final String COBERTURA_MAXMEM_DEFAULT_VALUE = "64m"; - - private CoberturaConstants() { - } -} diff --git a/sonar-deprecated/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java b/sonar-deprecated/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java index 4e17519812a..afb1ae13f4b 100644 --- a/sonar-deprecated/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java +++ b/sonar-deprecated/src/test/java/org/sonar/plugins/cobertura/api/CoberturaUtilsTest.java @@ -24,7 +24,6 @@ import org.junit.Test; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.test.MavenTestUtils; -import org.sonar.plugins.cobertura.base.CoberturaConstants; import java.io.File; import java.net.URISyntaxException; @@ -42,7 +41,7 @@ public class CoberturaUtilsTest { Project project = mock(Project.class); when(project.getFileSystem()).thenReturn(fileSystem); - when(project.getProperty(CoberturaConstants.COBERTURA_REPORT_PATH_PROPERTY)).thenReturn("foo"); + when(project.getProperty(CoberturaUtils.COBERTURA_REPORT_PATH_PROPERTY)).thenReturn("foo"); File report = CoberturaUtils.getReport(project); verify(fileSystem).resolvePath("foo"); |