Skip to content

Commit

Permalink
add a cobertura coverage report parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nharrand committed Jul 2, 2018
1 parent b3e0bd0 commit f44b900
Show file tree
Hide file tree
Showing 6 changed files with 22,914 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void applyWithParent(String srcDir) throws Exception {


public void printJavaFile(String directory) throws IOException {
removeClass(directory + "/../target/classes");
CtType<?> type = getPosition().getCompilationUnit().getMainType();
Factory factory = type.getFactory();
Environment env = factory.getEnvironment();
Expand All @@ -70,6 +71,15 @@ public void printJavaFile(String directory) throws IOException {
Log.debug("write type {} in directory {}", type.getQualifiedName(), directory);
}

public void removeClass(String classDir) {
CtType<?> type = getPosition().getCompilationUnit().getMainType();
String qn = type.getQualifiedName();
qn.replace(".", "/");
File cl = new File(classDir + "/" + qn + ".class");
if(cl.exists()) cl.delete();

}

// /**
// * gets the parent method of an element
// * @param son
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class XMLCoverageReport implements ICoverageReport {

File reportFile;
Map<String, Map<String, Map<Integer, LineReport>>> report;
Map<String,Map<Integer, Double>> report;

public XMLCoverageReport(File reportFile) {
this.reportFile = reportFile;
Expand All @@ -56,9 +56,9 @@ public InputSource resolveEntity(String publicId, String systemId)
}
});*/
Document doc = dBuilder.parse(reportFile);
NodeList nList = doc.getElementsByTagName("report");
NodeList nList = doc.getElementsByTagName("packages");
if(nList.getLength() == 1) {
readReport(nList.item(0));
readPackages(nList.item(0));
System.out.println("OK");
} else {
System.err.println("Error in xml report coverage format");
Expand All @@ -71,49 +71,87 @@ public InputSource resolveEntity(String publicId, String systemId)
}
}

void readReport(Node r) {
void readPackages(Node r) {
NodeList reportChild = r.getChildNodes();
for(int i = 0; i < reportChild.getLength(); i++) {
if(reportChild.item(i).getNodeName().equals("package")) {
readPackage(reportChild.item(i));
} else if (reportChild.item(i).getNodeName().equals("class")) {
readClass(reportChild.item(i));
}
}
}

void readPackage(Node p) {
String packageName = p.getAttributes().getNamedItem("name").getNodeValue().replace('/','.');
report.put(packageName,new HashMap<>());
NodeList packageChild = p.getChildNodes();
for(int i = 0; i < packageChild.getLength(); i++) {
if(packageChild.item(i).getNodeName().equals("sourcefile")) {
readSourceFile(packageChild.item(i), packageName);
NodeList nList = p.getChildNodes();
for(int j = 0; j < nList.getLength(); j++) {
if (nList.item(j).getNodeName().equals("classes")) {
NodeList classes = nList.item(j).getChildNodes();
for (int i = 0; i < classes.getLength(); i++) {
if (classes.item(i).getNodeName().equals("class")) {
readClass(classes.item(i));
}
}
}
}
}

void readClass(Node c) {}

void readMethod() {}

void readSourceFile(Node s, String packageName) {
String fileName = s.getAttributes().getNamedItem("name").getNodeValue();
report.get(packageName).put(fileName, new HashMap<>());
NodeList sourceFileChild = s.getChildNodes();
for(int i = 0; i < sourceFileChild.getLength(); i++) {
if(sourceFileChild.item(i).getNodeName().equals("line")) {
Node l = sourceFileChild.item(i);
int nr = Integer.parseInt(l.getAttributes().getNamedItem("nr").getNodeValue());
int mi = Integer.parseInt(l.getAttributes().getNamedItem("mi").getNodeValue());
int ci = Integer.parseInt(l.getAttributes().getNamedItem("ci").getNodeValue());
int mb = Integer.parseInt(l.getAttributes().getNamedItem("mb").getNodeValue());
int cb = Integer.parseInt(l.getAttributes().getNamedItem("cb").getNodeValue());
report.get(packageName).get(fileName).put(nr, new LineReport(mi,ci,mb,cb));
void readClass(Node c) {
NodeList nList = c.getChildNodes();
String fileName = c.getAttributes().getNamedItem("filename").getNodeValue();
for(int j = 0; j < nList.getLength(); j++) {
if (nList.item(j).getNodeName().equals("methods")) {
NodeList methods = nList.item(j).getChildNodes();
for (int i = 0; i < methods.getLength(); i++) {
if (methods.item(i).getNodeName().equals("method")) {
readMethod(methods.item(i), fileName);
}
}
}
}
}

void readMethod(Node m, String fileName) {
NodeList nList = m.getChildNodes();
for(int j = 0; j < nList.getLength(); j++) {
if(nList.item(j).getNodeName().equals("lines")) {
NodeList lines = nList.item(j).getChildNodes();
for(int i = 0; i < lines.getLength(); i++) {
if(lines.item(i).getNodeName().equals("line")) {
readLine(lines.item(i), fileName);
}
}
}
}
}

void readLine(Node l, String fileName) {
double score;
int hits = Integer.parseInt(l.getAttributes().getNamedItem("hits").getNodeValue());
int number = Integer.parseInt(l.getAttributes().getNamedItem("number").getNodeValue());
if(hits != 0) {
Node conditionCov = l.getAttributes().getNamedItem("condition-coverage");
if(conditionCov != null) {
score = Integer.parseInt(conditionCov.getNodeValue().split("%")[0]) / 100.0;
} else {
score = 1;
}
} else {
score = 0;
}
if(!report.containsKey(fileName)) report.put(fileName,new HashMap<>());
Map<Integer,Double> lines = report.get(fileName);
lines.put(number,score);
/*NodeList nList = l.getChildNodes();
if(nList.getLength() == 1 && nList.item(0).getNodeName().equals("conditions")) {
NodeList lines = nList.item(0).getChildNodes();
for(int i = 0; i < lines.getLength(); i++) {
if(lines.item(i).getNodeName().equals("condition")) {
//readLine(lines.item(i));
}
}
}*/
}


@Override
public double codeFragmentCoverage(CodeFragment stmt) {
return 0;
Expand All @@ -138,38 +176,23 @@ public List<Integer> getCoverageDistribution(CodeFragment stmt) {
public double positionCoverage(SourcePosition position) {
CtType<?> cl = position.getCompilationUnit().getMainType();

String packageName = cl.getPackage().getQualifiedName();
String sourceFile = cl.getSimpleName() + ".java";
String packageName = cl.getPackage().getQualifiedName().replace(".","/");
String sourceFile = packageName + "/" + cl.getSimpleName() + ".java";

if(!report.containsKey(packageName)
|| !report.get(packageName).containsKey(sourceFile)) {
if(!report.containsKey(sourceFile)) {
return 0;
}

double ret = 0;
int start = position.getLine();
int end = position.getEndLine();
Map<Integer, LineReport> lineReports = report.get(packageName).get(sourceFile);
Map<Integer, Double> lineReports = report.get(sourceFile);
for (int i = start; i <= end; i++) {
if (lineReports.containsKey(i)) {
LineReport l = lineReports.get(i);
ret += ((double) l.ci) / (((double) l.mi) + ((double) l.ci));
Double l = lineReports.get(i);
ret += l;
}
}
return ret/(double)(end - start + 1);
}

public class LineReport {
public int mi;
public int ci;
public int mb;
public int cb;

public LineReport(int mi, int ci, int mb, int cb) {
this.mi = mi;
this.ci = ci;
this.mb = mb;
this.cb = cb;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import fr.inria.diversify.statistic.SinglePointSessionResults;
import fr.inria.diversify.transformation.AddMethodInvocation;
import fr.inria.diversify.transformation.SingleTransformation;
import fr.inria.diversify.transformation.Transformation;
import fr.inria.diversify.transformation.ast.ASTTransformation;
import fr.inria.diversify.util.Log;
Expand Down
81 changes: 44 additions & 37 deletions generator/src/main/java/fr/inria/diversify/runner/SmartRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,46 +63,53 @@ protected void run(Transformation trans) throws Exception {
Properties p = new Properties();
if(testImpact != null && method != null) {
String rawTests = getTests(method);
String tests[] = rawTests.split(",");
//p.setProperty("test", );
status = -2;
boolean divergent = false;
int i = 0;
for(; i < tests.length; i++) {
p.setProperty("test", tests[i]);
String argLine = buildAgentLine(tests[i]);
if(argLine != null) {
System.out.println("-DargLine=\"" + argLine + "\"");
p.setProperty("argLine", argLine);
} else {
System.out.println("No agent to run");
Log.debug("Run the following tests: <" + rawTests + ">");
if(rawTests.equals("")) {
status = -4;
Log.debug("No test covering method " + method);
} else {
String tests[] = rawTests.split(",");
//p.setProperty("test", );
status = -2;
boolean divergent = false;
int i = 0;
for (; i < tests.length; i++) {
p.setProperty("test", tests[i]);
String argLine = buildAgentLine(tests[i]);
if (argLine != null) {
System.out.println("-DargLine=\"" + argLine + "\"");
p.setProperty("argLine", argLine);
} else {
System.out.println("No agent to run");
}
p.setProperty("failIfNoTests", "false");
status = runTest(tmpDir, p);
System.out.println("[STATUS] -------> " + status);
if (status < 0) {
break;
} else if (status >= 1) {
divergent = true;
break;
}
}
p.setProperty("failIfNoTests","false");
status = runTest(tmpDir, p);
System.out.println("[STATUS] -------> " + status);
if(status < 0) {
break;
} else if (status >= 1) {
divergent = true;
break;
if (status >= 0) {
String remainingTests = "";
int first = i+1;//either all tests have been ran or previous loop has exited through break;
for (; i < tests.length; i++) {
if (i != first) remainingTests += "," + tests[i];
else remainingTests += tests[i];
}
Log.debug("Run the following remaining tests: <" + remainingTests + ">");
if (!remainingTests.equals("")) {

Properties p2 = new Properties();
p2.setProperty("test", remainingTests);
p2.setProperty("failIfNoTests", "false");
status = runTest(tmpDir, p2);
}
}
if (divergent && status >= 0) status = 1;
}
if(status >= 0) {
String remainingTests = "";
int first = i;
for(; i < tests.length; i++) {
if(i != first) remainingTests += "," + tests[i];
else remainingTests += tests[i];
}
if(!remainingTests.equals("")) {

Properties p2 = new Properties();
p2.setProperty("test", remainingTests);
p2.setProperty("failIfNoTests","false");
status = runTest(tmpDir, p2);
}
}
if(divergent && status >= 0) status = 1;
} else {
status = runTest(tmpDir, p);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package fr.inria.diversify.coverage;

import fr.inria.diversify.util.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.*;

public class XMLCoverageReportTest {

@Test
public void testCreation() throws IOException {
File coverageFile = new File(XMLCoverageReportTest.class.getClassLoader().getResource("coverage.xml").getFile());
assertTrue(coverageFile.exists() && coverageFile.isFile());

XMLCoverageReport report = new XMLCoverageReport(coverageFile);
report.create();
assertTrue(report.report.size() != 0);
}

}
Loading

0 comments on commit f44b900

Please sign in to comment.