Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
aQute.junit.JunitXmlReport Maven / Gradle / Ivy
Go to download
A bnd tester. If this bundle is used as the tester (previously aQute.junit) then it will add itself to the -runbundles at the end. At startup, this bundle will then run the tests. This bundle does NOT contain JUnit itself. It will import JUnit just like any other bundle.
package aQute.junit;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.runner.Describable;
import org.junit.runner.Description;
import org.osgi.framework.Bundle;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
public class JunitXmlReport implements TestReporter {
Tag testsuite = new Tag("testsuite");
Tag testcase;
static String hostname;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
long startTime;
long testStartTime;
int tests = 0;
int failures = 0;
int errors = 0;
int skipped = 0;
PrintWriter out;
boolean finished;
boolean progress;
Bundle bundle;
BasicTestReport basic;
public JunitXmlReport(Writer report, Bundle bundle, BasicTestReport basic) throws Exception {
if (hostname == null)
hostname = InetAddress.getLocalHost().getHostName();
out = new PrintWriter(report);
this.bundle = bundle;
this.basic = basic;
}
public void setProgress(boolean progress) {
this.progress = progress;
}
public void setup(Bundle fw, Bundle targetBundle) {
startTime = System.currentTimeMillis();
testsuite.addAttribute("hostname", hostname);
if (targetBundle != null) {
testsuite.addAttribute("name", "test." + targetBundle.getSymbolicName());
testsuite.addAttribute("target", targetBundle.getLocation());
} else {
testsuite.addAttribute("name", "test.run");
}
testsuite.addAttribute("timestamp", df.format(new Date()));
testsuite.addAttribute("framework", fw);
testsuite.addAttribute("framework-version", fw.getVersion());
Tag properties = new Tag("properties");
testsuite.addContent(properties);
for (Map.Entry entry : System.getProperties().entrySet()) {
Tag property = new Tag(properties, "property");
property.addAttribute("name", entry.getKey());
property.addAttribute("value", entry.getValue());
}
if (targetBundle != null) {
String header = targetBundle.getHeaders().get(aQute.bnd.osgi.Constants.BND_ADDXMLTOTEST);
if (header != null) {
StringTokenizer st = new StringTokenizer(header, " ,");
while (st.hasMoreTokens()) {
String resource = st.nextToken();
URL url = targetBundle.getEntry(resource);
if (url != null) {
String name = url.getFile();
int n = name.lastIndexOf('/');
if (n < 0)
n = 0;
else
n = n + 1;
if (name.endsWith(".xml"))
name = name.substring(n, name.length() - 4);
else
name = name.substring(n, name.length()).replace('.', '_');
testsuite.addContent(url);
} else {
Tag addxml = new Tag(testsuite, "error");
addxml.addAttribute("reason", "no such resource: " + resource);
}
}
}
}
}
public void begin(List classNames, int realcount) {}
public void end() {
if (!finished) {
finished = true;
testsuite.addAttribute("tests", tests);
testsuite.addAttribute("failures", failures);
testsuite.addAttribute("errors", errors);
testsuite.addAttribute("skipped", skipped);
testsuite.addAttribute("time", getFraction(System.currentTimeMillis() - startTime, 1000));
testsuite.addAttribute("timestamp", df.format(new Date()));
testsuite.print(0, out);
out.close();
}
}
private String getFraction(long l, @SuppressWarnings("unused") int i) {
return (l / 1000) + "." + (l % 1000);
}
//
static Pattern NAMEANDCLASS = Pattern.compile("(.*)\\((.*)\\)");
public void startTest(Test test) {
String nameAndClass = test.toString();
String name = nameAndClass;
String clazz = test.getClass().getName();
if (test instanceof Describable) {
Description description = ((Describable) test).getDescription();
clazz = description.getClassName();
if (description.getMethodName() != null)
name = description.getMethodName();
} else {
Matcher m = NAMEANDCLASS.matcher(nameAndClass);
if (m.matches()) {
name = m.group(1);
clazz = m.group(2);
}
}
testcase = new Tag("testcase");
testsuite.addContent(testcase);
testcase.addAttribute("classname", clazz);
int n = nameAndClass.indexOf('(');
if (n > 0 && nameAndClass.endsWith(")")) {
name = nameAndClass.substring(0, n);
}
testcase.addAttribute("name", name);
testStartTime = System.currentTimeMillis();
progress(name);
}
public void setTests(@SuppressWarnings("unused") List flattened) {}
//
// java.lang.Exception:
// at test.AnalyzerTest.testMultilevelInheritance(AnalyzerTest.java:47)
//
//
public void addError(Test test, Throwable t) {
Tag error = new Tag("error");
error.setCDATA();
error.addAttribute("type", t.getClass().getName());
error.addContent(getTrace(t));
if (testcase == null)
testsuite.addContent(error);
else
testcase.addContent(error);
progress(" e");
errors++;
}
private void progress(@SuppressWarnings("unused") String s) {}
private String getTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
return sw.toString().replace('\t', ' ');
}
//
// junit.framework.AssertionFailedError
// at test.AnalyzerTest.testFindClass(AnalyzerTest.java:25)
//
//
//
public void addFailure(Test test, AssertionFailedError t) {
Tag failure = new Tag("failure");
failure.setCDATA();
failure.addAttribute("type", t.getClass().getName());
failure.addContent(getTrace(t));
testcase.addContent(failure);
progress(" f");
failures++;
}
public void endTest(Test test) {
String[] outs = basic.getCaptured();
if (outs[0] != null) {
Tag sysout = new Tag(testcase, "system-out");
sysout.addContent(outs[0]);
}
if (outs[1] != null) {
Tag sysout = new Tag(testcase, "system-err");
sysout.addContent(outs[1]);
}
testcase.addAttribute("time", getFraction(System.currentTimeMillis() - testStartTime, 1000));
tests++;
tests++;
}
public void close() {
end();
}
public void aborted() {
testsuite.addAttribute("aborted", "true");
close();
}
public void addTag(Tag tag) {
testsuite.addContent(tag);
}
}