blob: d879e53d1f0aaa114e323911332a36159bb838a3 [file] [log] [blame]
package aQute.bnd.make.coverage;
import static aQute.bnd.make.coverage.Coverage.*;
import java.io.*;
import java.util.*;
import aQute.lib.osgi.*;
import aQute.lib.osgi.Clazz.MethodDef;
import aQute.lib.tag.*;
/**
* Creates an XML Coverage report. This class can be used as a resource so the
* report is created only when the JAR is written.
*
*/
public class CoverageResource extends WriteResource {
Collection<Clazz> testsuite;
Collection<Clazz> service;
public CoverageResource(Collection<Clazz> testsuite,
Collection<Clazz> service) {
this.testsuite = testsuite;
this.service = service;
}
@Override
public long lastModified() {
return 0;
}
@Override
public void write(OutputStream out) throws IOException {
try {
Map<MethodDef, List<MethodDef>> table = getCrossRef(testsuite,
service);
Tag coverage = toTag(table);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out,
Constants.DEFAULT_CHARSET));
try {
coverage.print(0, pw);
} finally {
pw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Tag toTag(Map<MethodDef, List<MethodDef>> catalog) {
Tag coverage = new Tag("coverage");
String currentClass = null;
Tag classTag = null;
for (Map.Entry<MethodDef, List<MethodDef>> m : catalog.entrySet()) {
String className = m.getKey().getContainingClass().getFQN();
if (!className.equals(currentClass)) {
classTag = new Tag("class");
classTag.addAttribute("name", className);
classTag.addAttribute("package", Descriptors.getPackage(className));
classTag.addAttribute("short", Descriptors.getShortName(className));
coverage.addContent(classTag);
currentClass = className;
}
Tag method = doMethod(new Tag("method"), m.getKey());
classTag.addContent(method);
for (MethodDef r : m.getValue()) {
Tag ref = doMethod(new Tag("ref"), r);
method.addContent(ref);
}
}
return coverage;
}
private static Tag doMethod(Tag tag, MethodDef method) {
tag.addAttribute("pretty", method.toString());
if (method.isPublic())
tag.addAttribute("public", true);
if (method.isStatic())
tag.addAttribute("static", true);
if (method.isProtected())
tag.addAttribute("protected", true);
if (method.isInterface())
tag.addAttribute("interface", true);
tag.addAttribute("constructor", method.isConstructor());
if (!method.isConstructor())
tag.addAttribute("name", method.getName());
tag.addAttribute("descriptor", method.getDescriptor());
return tag;
}
}