ch.hortis.sonar.mvn.mc.CoberturaCollector Maven / Gradle / Ivy
/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.mvn.mc;
import ch.hortis.sonar.model.Metrics;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collection;
/**
* Cobertura metric collector
*/
public class CoberturaCollector extends BaseMetricCollector implements MetricsCollector {
private Document document;
private XPath xpath;
public void enableLogging(Log log) {
}
public boolean initialize(MavenProject project) throws MojoExecutionException {
File reportFile = findReportFile(project);
boolean ok = false;
if (reportFile != null) {
ok = true;
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setValidating(false);
// make sure that the external DTD is not loaded
builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder builder = builderFactory.newDocumentBuilder();
document = builder.parse(reportFile);
xpath = XPathFactory.newInstance().newXPath();
} catch (Exception ex) {
throw new MojoExecutionException("Error during Cobertura reports parsing", ex);
}
}
return ok;
}
public Collection collectMeasures() throws MojoExecutionException {
Collection metrics = new ArrayList();
try {
double lineRate = parseNumber( xpath.evaluate("/coverage/@line-rate", document ) );
metrics.add(new MetricData( Metrics.COBERTURA_LINE_COVERAGE, convertPercentage( lineRate ) ) );
double branchRate = parseNumber( xpath.evaluate("/coverage/@branch-rate", document ) );
metrics.add(new MetricData( Metrics.COBERTURA_BRANCH_COVERAGE, convertPercentage( branchRate ) ) );
} catch (Exception ex) {
throw new MojoExecutionException("Error during Cobertura reports parsing", ex);
}
return metrics;
}
protected double convertPercentage(Number percentage) {
return scaleValue( percentage.doubleValue() * 100.0 );
}
private File findReportFile(final MavenProject project) throws MojoExecutionException {
String filename = project.getBuild().getDirectory() + "/site/cobertura/coverage.xml";
File result = new File(filename);
if (!result.exists()) {
result = null;
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy