Skip to content
Permalink
Browse files

Also display of namespaced classes. fixes [JENKINS-26722]

  • Loading branch information...
madmuffin1 committed Aug 24, 2015
1 parent c05137b commit e74766b0be4371d569cc0984bd80d44da0a7436d
@@ -2,13 +2,16 @@

import org.jenkinsci.plugins.cloverphp.results.ClassCoverage;
import org.jenkinsci.plugins.cloverphp.results.FileCoverage;
import org.jenkinsci.plugins.cloverphp.results.PackageCoverage;
import org.jenkinsci.plugins.cloverphp.results.ProjectCoverage;
import hudson.util.IOException2;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

@@ -33,13 +36,22 @@ public static ProjectCoverage trimPaths(ProjectCoverage result, String pathPrefi
if (pathPrefix == null) {
return result;
}
for (FileCoverage f : result.getFileCoverages()) {
trimPathPrefix(pathPrefix, result.getFileCoverages());

for(PackageCoverage pc : result.getPackageCoverages()) {
trimPathPrefix(pathPrefix, pc.getFileCoverages());
}

return result;
}

protected static void trimPathPrefix(String pathPrefix, List<FileCoverage> fileCoverages) {
for (FileCoverage f : fileCoverages) {
if (f.getName().startsWith(pathPrefix)) {
f.setName(f.getName().substring(pathPrefix.length()));
}
f.setName(f.getName().replace('\\', '/'));
}
return result;
}

public static ProjectCoverage parse(File inFile, String pathPrefix) throws IOException {
@@ -87,10 +99,11 @@ protected static Digester buildDigester() {
digester.setClassLoader(CloverCoverageParser.class.getClassLoader());

addDigester(digester, "coverage/project", ProjectCoverage.class);
addDigester(digester, "coverage/project/package", ProjectCoverage.class);
addDigester(digester, "coverage/project/file", FileCoverage.class, "addFileCoverage");
addDigester(digester, "coverage/project/package/file", FileCoverage.class, "addFileCoverage");
addDigester(digester, "coverage/project/file/class", ClassCoverage.class, "addClassCoverage");

addDigester(digester, "coverage/project/package", PackageCoverage.class, "addPackageCoverage");
addDigester(digester, "coverage/project/package/file", FileCoverage.class, "addFileCoverage");
addDigester(digester, "coverage/project/package/file/class", ClassCoverage.class, "addClassCoverage");

return digester;
@@ -59,15 +59,15 @@
private AbstractBuild owner;

public Ratio getMethodCoverage() {
return Ratio.create(coveredmethods, methods);
return Ratio.create(getCoveredmethods(), getMethods());
}

public Ratio getStatementCoverage() {
return Ratio.create(coveredstatements, statements);
return Ratio.create(getCoveredstatements(), getStatements());
}

public Ratio getElementCoverage() {
return Ratio.create(coveredelements, elements);
return Ratio.create(getCoveredelements(), getElements());
}

/**
@@ -188,7 +188,11 @@ public String getName() {
}

public String getURLSafeName() {
return Util.rawEncode(name.replaceAll("[/+]", "_"));
return safelyEncodedName(this.name);
}

public static String safelyEncodedName(String name) {
return Util.rawEncode(name.replaceAll("[/\\\\+]", "_"));
}

/**
@@ -9,6 +9,16 @@
*/
public class ClassCoverage extends AbstractClassMetrics {

String namespace;

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

@Override
public AbstractClassMetrics getPreviousResult() {
CloverBuildAction action = getPreviousCloverBuildAction();
@@ -19,10 +29,22 @@ public AbstractClassMetrics getPreviousResult() {
if (pc == null) {
return null;
}
FileCoverage fc = pc.findFileCoverage(getParent().getURLSafeName());
FileCoverage fc = null;
if(getNamespace() != null) {
PackageCoverage packageCoverage = pc.findPackageCoverage(safelyEncodedName(getNamespace()));
if(packageCoverage != null) {
fc = packageCoverage.findFileCoverage(getParent().getURLSafeName());
}
}

if (fc == null) {
fc = pc.findFileCoverage(getParent().getURLSafeName());
}

if (fc == null) {
return null;
}

return fc.findClassCoverage(getURLSafeName());
}
}
@@ -55,7 +55,17 @@ public AbstractClassMetrics getPreviousResult() {
if (projectCoverage == null) {
return null;
}
return projectCoverage.findFileCoverage(getURLSafeName());
FileCoverage fileCoverage = null;
if(getParent() instanceof PackageCoverage) {
PackageCoverage packageCoverage = projectCoverage.findPackageCoverage(getParent().getURLSafeName());
if (packageCoverage != null) {
fileCoverage = packageCoverage.findFileCoverage(getURLSafeName());
}
}
if (fileCoverage == null) {
fileCoverage = projectCoverage.findFileCoverage(getURLSafeName());
}
return fileCoverage;
}

@Override
@@ -0,0 +1,124 @@
package org.jenkinsci.plugins.cloverphp.results;

import hudson.model.AbstractBuild;
import org.jenkinsci.plugins.cloverphp.CloverBuildAction;

/**
* Created by Patrick Brückner on 24.08.15.
*/
public class PackageCoverage extends ProjectCoverage {

@Override
public boolean addFileCoverage(FileCoverage result) {
return super.addFileCoverage(result);
}

/**
* for some reason, the clover results generated by recent PHPUnit do not contain metrics on package level.
* if we encounter this, we'll just quickly add up the file metrics in that package
* @return int
*/
@Override
public int getLoc() {
if(super.getLoc()==0) {
int loc = 0;
for(FileCoverage f : this.getFileCoverages()) {
loc += f.getLoc();
}
super.setLoc(loc);
}
return super.getLoc();
}

@Override
public int getNcloc() {
if(super.getNcloc()==0) {
int ncloc = 0;
for(FileCoverage f : this.getFileCoverages()) {
ncloc += f.getNcloc();
}
super.setNcloc(ncloc);
}
return super.getNcloc();
}

@Override
public int getMethods() {
if(super.getMethods()==0) {
int methods = 0;
for(FileCoverage f : this.getFileCoverages()) {
methods += f.getMethods();
}
super.setMethods(methods);
}
return super.getMethods();
}

@Override
public int getCoveredstatements() {
if(super.getCoveredstatements()==0) {
int coveredstatements = 0;
for(FileCoverage f : this.getFileCoverages()) {
coveredstatements += f.getCoveredstatements();
}
super.setCoveredstatements(coveredstatements);
}
return super.getCoveredstatements();
}

@Override
public int getCoveredmethods() {
if(super.getCoveredmethods()==0) {
int coveredmethods = 0;
for(FileCoverage f : this.getFileCoverages()) {
coveredmethods += f.getCoveredmethods();
}
super.setCoveredmethods(coveredmethods);
}
return super.getCoveredmethods();
}

@Override
public int getStatements() {
if(super.getStatements()==0) {
int statements = 0;
for(FileCoverage f : this.getFileCoverages()) {
statements += f.getStatements();
}
super.setStatements(statements);
}
return super.getStatements();
}

@Override
public int getCoveredelements() {
if(super.getCoveredelements()==0) {
int coveredelements = 0;
for(FileCoverage f : this.getFileCoverages()) {
coveredelements += f.getCoveredelements();
}
super.setCoveredelements(coveredelements);
}
return super.getCoveredelements();
}

@Override
public int getElements() {
if(super.getElements()==0) {
int elements = 0;
for(FileCoverage f : this.getFileCoverages()) {
elements += f.getElements();
}
super.setElements(elements);
}
return super.getElements();
}

public void setOwner(AbstractBuild owner) {
super.setOwner(owner);
for (FileCoverage p : this.getFileCoverages()) {
p.setOwner(owner);
}
}

}
@@ -19,16 +19,26 @@
public class ProjectCoverage extends AbstractProjectMetrics {

private List<FileCoverage> fileCoverages = new ArrayList<FileCoverage>();
private List<PackageCoverage> packageCoverages = new ArrayList<PackageCoverage>();

public List<FileCoverage> getChildren() {
return getFileCoverages();
}

public List<PackageCoverage> getPackageCoverages() {
return packageCoverages;
}

public boolean addFileCoverage(FileCoverage result) {
result.setParent(this);
return fileCoverages.add(result);
}

public boolean addPackageCoverage(PackageCoverage result) {
result.setParent(this);
return packageCoverages.add(result);
}

public List<FileCoverage> getFileCoverages() {
return fileCoverages;
}
@@ -41,9 +51,34 @@ public FileCoverage findFileCoverage(String token) {
}
return null;
}

public PackageCoverage findPackageCoverage(String token) {
for (PackageCoverage i : packageCoverages) {
if (token.equals(i.getURLSafeName())) {
return i;
}
}
return null;
}

/**
* Method to expose subpages to the Stapler HTTP server.
* It tries from the list of files first and then tries from the list of packages.
*
* @param token
* @param req
* @param rsp
* @return AbstractClassMetrics can be any of FileCoverage or PackageCoverage
* @throws IOException
*/
public AbstractClassMetrics getDynamic(String token, StaplerRequest req, StaplerResponse rsp) throws IOException {
AbstractClassMetrics candidate = findFileCoverage(token);
if(candidate != null) {
return candidate;
}

public FileCoverage getDynamic(String token, StaplerRequest req, StaplerResponse rsp) throws IOException {
return findFileCoverage(token);
candidate = findPackageCoverage(token);
return candidate;
}

@Override
@@ -61,5 +96,17 @@ public void setOwner(AbstractBuild owner) {
for (FileCoverage p : fileCoverages) {
p.setOwner(owner);
}
for (PackageCoverage p: packageCoverages) {
p.setOwner(owner);
}
}

@Override
public String getName() {
String name = super.getName();
if(name == null && getOwner() != null) {
name = getOwner().getDisplayName();
}
return name;
}
}
@@ -68,17 +68,11 @@ public boolean isEmpty() {
Map<CoverageMetric, Integer> result = new HashMap<CoverageMetric, Integer>();

j = calcRangeScore(methodCoverage, min.methodCoverage, coverage.getMethodCoverage().getPercentage());
if (j != null) {
result.put(CoverageMetric.METHOD, Integer.valueOf(j));
}
result.put(CoverageMetric.METHOD, j);
j = calcRangeScore(statementCoverage, min.statementCoverage, coverage.getStatementCoverage().getPercentage());
if (j != null) {
result.put(CoverageMetric.STATEMENT, Integer.valueOf(j));
}
result.put(CoverageMetric.STATEMENT, j);
j = calcRangeScore(elementCoverage, min.elementCoverage, coverage.getElementCoverage().getPercentage());
if (j != null) {
result.put(CoverageMetric.ELEMENT, Integer.valueOf(j));
}
result.put(CoverageMetric.ELEMENT, j);
return result;
}

@@ -0,0 +1,13 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<l:layout xmlns:clover="/org/jenkinsci/plugins/cloverphp/tags" css="/plugin/cloverphp/css/style.css" title="${%Clover PHP Coverage Report}">
<st:include it="${it.owner}" page="sidepanel.jelly" />
<l:main-panel>

<j:set var="scope" value="${%Package}"/>
<clover:main />

</l:main-panel>
</l:layout>
</j:jelly>

0 comments on commit e74766b

Please sign in to comment.
You can’t perform that action at this time.