Skip to content
Permalink
Browse files

Merge pull request #4 from madmuffin1/include-namespaced-classes

Correctly parse and display namespaced classes, fixes [JENKINS-26722]
  • Loading branch information...
ssogabe committed Nov 6, 2015
2 parents 085299e + 6e34990 commit 5269383e11ce36119b77180bf7d281b0578c68e0
@@ -6,3 +6,4 @@ target
*.ipr
*.iws
work
.idea/
@@ -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 {
@@ -69,21 +81,7 @@ public static ProjectCoverage parse(InputStream in) throws IOException {
if (in == null) {
throw new NullPointerException();
}
Digester digester = new Digester();
digester.setClassLoader(CloverCoverageParser.class.getClassLoader());
digester.addObjectCreate("coverage/project", ProjectCoverage.class);
digester.addSetProperties("coverage/project");
digester.addSetProperties("coverage/project/metrics");

digester.addObjectCreate("coverage/project/file", FileCoverage.class);
digester.addSetProperties("coverage/project/file");
digester.addSetProperties("coverage/project/file/metrics");
digester.addSetNext("coverage/project/file", "addFileCoverage", FileCoverage.class.getName());

digester.addObjectCreate("coverage/project/file/class", ClassCoverage.class);
digester.addSetProperties("coverage/project/file/class");
digester.addSetProperties("coverage/project/file/class/metrics");
digester.addSetNext("coverage/project/file/class", "addClassCoverage", ClassCoverage.class.getName());
Digester digester = buildDigester();

try {
ProjectCoverage coverage = (ProjectCoverage) digester.parse(in);
@@ -95,4 +93,32 @@ public static ProjectCoverage parse(InputStream in) throws IOException {
throw new IOException2("Cannot parse coverage results", e);
}
}

protected static Digester buildDigester() {
Digester digester = new Digester();
digester.setClassLoader(CloverCoverageParser.class.getClassLoader());

addDigester(digester, "coverage/project", ProjectCoverage.class);
addDigester(digester, "coverage/project/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;
}

private static void addDigester(Digester digester, String path, Class _class) {
addDigester(digester, path, _class, null);
}

private static void addDigester(Digester digester, String path, Class _class, String next) {
digester.addObjectCreate(path, _class);
digester.addSetProperties(path);
digester.addSetProperties(path + "/metrics");
if(next != null) {
digester.addSetNext(path, next, _class.getName());
}
}
}
@@ -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,132 @@
package org.jenkinsci.plugins.cloverphp.results;

import hudson.model.AbstractBuild;

/**
* 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);
}
}

@Override
public AbstractClassMetrics getPreviousResult() {

ProjectCoverage parentPreviousResult = (ProjectCoverage) getParent().getPreviousResult();
if (parentPreviousResult == null) {
return null;
}
return parentPreviousResult.findPackageCoverage(getURLSafeName());
}
}

0 comments on commit 5269383

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