All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.bnd.make.coverage.CoverageResource Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.bnd.make.coverage;

import static aQute.bnd.make.coverage.Coverage.getCrossRef;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Clazz.MethodDef;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Descriptors;
import aQute.bnd.osgi.WriteResource;
import aQute.lib.tag.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	testsuite;
	Collection	service;

	public CoverageResource(Collection testsuite, Collection service) {
		this.testsuite = testsuite;
		this.service = service;
	}

	@Override
	public long lastModified() {
		return 0;
	}

	@Override
	public void write(OutputStream out) throws IOException {
		try {
			Map> 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> catalog) {
		Tag coverage = new Tag("coverage");
		String currentClass = null;
		Tag classTag = null;

		for (Map.Entry> 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());
			if (classTag != null)
				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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy