aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-deprecated/src/main/java
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-07-05 11:32:50 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2013-07-05 11:49:48 +0200
commit55658bd42b6ac8567575bd98f17f143462820a00 (patch)
treebae831d3fa4ffa7304daa259918d9c7d5f89773b /sonar-deprecated/src/main/java
parent84851fffaa03911500dd2643970f4f7ff9ee4097 (diff)
downloadsonarqube-55658bd42b6ac8567575bd98f17f143462820a00.tar.gz
sonarqube-55658bd42b6ac8567575bd98f17f143462820a00.zip
SONAR-4358 Restore Cobertura API classes in sonar-deprecated
to not break plugins that rely on them (groovy, flex)
Diffstat (limited to 'sonar-deprecated/src/main/java')
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/AbstractCoberturaParser.java124
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java88
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/base/CoberturaConstants.java35
3 files changed, 247 insertions, 0 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
new file mode 100644
index 00000000000..a9394a29889
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/AbstractCoberturaParser.java
@@ -0,0 +1,124 @@
+/*
+ * 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.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 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
+ * @deprecated since 3.7 Used to keep backward compatibility since extraction
+ * of Cobertura plugin.
+ */
+@Deprecated
+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]));
+ }
+ }
+ }
+
+ 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
new file mode 100644
index 00000000000..3488d166ec0
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/api/CoberturaUtils.java
@@ -0,0 +1,88 @@
+/*
+ * 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.api;
+
+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.
+ */
+@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";
+
+ /**
+ * @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
+ */
+ @Deprecated
+ public static File getReport(Project project) {
+ File report = getReportFromProperty(project);
+ if (report == null) {
+ report = getReportFromPluginConfiguration(project);
+ }
+ if (report == null) {
+ report = getReportFromDefaultPath(project);
+ }
+
+ if (report == null || !report.exists() || !report.isFile()) {
+ Logs.INFO.warn("Cobertura report not found at {}", report);
+ report = null;
+ }
+ return report;
+ }
+
+ private static File getReportFromProperty(Project project) {
+ String path = (String) project.getProperty(CoberturaConstants.COBERTURA_REPORT_PATH_PROPERTY);
+ if (path != null) {
+ return project.getFileSystem().resolvePath(path);
+ }
+ return null;
+ }
+
+ private static File getReportFromPluginConfiguration(Project project) {
+ MavenPlugin mavenPlugin = MavenPlugin.getPlugin(project.getPom(), COBERTURA_GROUP_ID, COBERTURA_ARTIFACT_ID);
+ if (mavenPlugin != null) {
+ String path = mavenPlugin.getParameter("outputDirectory");
+ if (path != null) {
+ return new File(project.getFileSystem().resolvePath(path), "coverage.xml");
+ }
+ }
+ return null;
+ }
+
+ private static File getReportFromDefaultPath(Project project) {
+ return new File(project.getFileSystem().getReportOutputDir(), "cobertura/coverage.xml");
+ }
+
+ private CoberturaUtils() {
+ }
+
+}
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
new file mode 100644
index 00000000000..6cd73ac629d
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/plugins/cobertura/base/CoberturaConstants.java
@@ -0,0 +1,35 @@
+/*
+ * 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() {
+ }
+}