1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 23:05:47 +02:00

Fixed up test output so that it reports errors in an XML

friendly way.
This commit is contained in:
Doug Schaefer 2004-01-14 21:18:28 +00:00
parent 74f6ab27e8
commit 828dff9194

View file

@ -3,6 +3,12 @@ package org.eclipse.cdt.core.suite;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import junit.framework.AssertionFailedError; import junit.framework.AssertionFailedError;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestListener; import junit.framework.TestListener;
@ -16,6 +22,10 @@ import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.PlatformUI; import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.testing.ITestHarness; import org.eclipse.ui.testing.ITestHarness;
import org.eclipse.ui.testing.TestableObject; import org.eclipse.ui.testing.TestableObject;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ProcessingInstruction;
/** /**
* @see IPlatformRunnable * @see IPlatformRunnable
@ -26,6 +36,11 @@ public class RunTests implements IPlatformRunnable, ITestHarness, TestListener {
private PrintStream stream; private PrintStream stream;
private String testReport; private String testReport;
private String pluginName = "org.eclipse.cdt.core.tests"; private String pluginName = "org.eclipse.cdt.core.tests";
Document doc;
Element testRun;
Element testSuite;
Element testClass;
Element test;
/** /**
* *
@ -95,22 +110,29 @@ public class RunTests implements IPlatformRunnable, ITestHarness, TestListener {
TestResult results = new TestResult(); TestResult results = new TestResult();
results.addListener(RunTests.this); results.addListener(RunTests.this);
stream.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"); try {
stream.print("<?xml-stylesheet type=\"text/xsl\" href=\""); doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
stream.print(testReport);
stream.println("\"?>");
stream.print("<testRun name=\""); ProcessingInstruction pi = doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + testReport +"\"");
stream.print(pluginName); doc.appendChild(pi);
stream.println("\">");
testRun = doc.createElement("testRun");
doc.appendChild(testRun);
testRun.setAttribute("name", pluginName);
startSuite(AutomatedIntegrationSuite.class.getName()); startSuite(AutomatedIntegrationSuite.class.getName());
AutomatedIntegrationSuite.suite().run(results); AutomatedIntegrationSuite.suite().run(results);
endSuite(); currentTest = null;
stream.print("</testRun>");
results.removeListener(RunTests.this); results.removeListener(RunTests.this);
results.stop(); results.stop();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(stream));
} catch (Throwable t) {
System.out.println("runTests failed");
t.printStackTrace();
}
} }
}); });
testableObject.testingFinished(); testableObject.testingFinished();
@ -137,31 +159,25 @@ public class RunTests implements IPlatformRunnable, ITestHarness, TestListener {
/* (non-Javadoc) /* (non-Javadoc)
* @see junit.framework.TestListener#endTest(junit.framework.Test) * @see junit.framework.TestListener#endTest(junit.framework.Test)
*/ */
public void endTest(Test test) { public void endTest(Test t) {
double time = (System.currentTimeMillis() - startTime) / 1000.0; double time = (System.currentTimeMillis() - startTime) / 1000.0;
stream.print(" time=\""); test.setAttribute("time", String.valueOf(time));
stream.print(time);
stream.print("\" result=\"");
boolean printerror = false; String result;
if (failure == null) if (failure == null)
stream.print("pass"); result = "pass";
else if (failure instanceof AssertionFailedError)
stream.print("failed");
else { else {
stream.print("error"); CDATASection data = doc.createCDATASection(failure.toString());
printerror = true; test.appendChild(data);
if (failure instanceof AssertionFailedError)
result = "failed";
else
result = "error";
} }
stream.print("\""); test.setAttribute("result", result);
if (printerror) {
stream.println(">");
failure.printStackTrace(stream);
stream.println("\t\t\t</test>");
} else
stream.println("/>");
failure = null; failure = null;
} }
@ -173,21 +189,19 @@ public class RunTests implements IPlatformRunnable, ITestHarness, TestListener {
/* (non-Javadoc) /* (non-Javadoc)
* @see junit.framework.TestListener#startTest(junit.framework.Test) * @see junit.framework.TestListener#startTest(junit.framework.Test)
*/ */
public void startTest(Test test) { public void startTest(Test t) {
if (test.getClass() != currentTest) { if (t.getClass() != currentTest) {
if (currentTest != null) currentTest = t.getClass();
endClass(); testClass = doc.createElement("testClass");
stream.print("\t\t<testClass name=\""); testSuite.appendChild(testClass);
stream.print(test.getClass().getName()); testClass.setAttribute("name", currentTest.getName());
stream.println("\">");
currentTest = test.getClass();
} }
String name = test.toString(); test = doc.createElement("test");
testClass.appendChild(test);
String name = t.toString();
name = name.substring(0, name.indexOf('(')); name = name.substring(0, name.indexOf('('));
stream.print("\t\t\t<test name=\""); test.setAttribute("name", name);
stream.print(name);
stream.print("\"");
startTime = System.currentTimeMillis(); startTime = System.currentTimeMillis();
} }
@ -195,20 +209,9 @@ public class RunTests implements IPlatformRunnable, ITestHarness, TestListener {
// Report Generator // Report Generator
private void startSuite(String name) { private void startSuite(String name) {
stream.print("\t<testSuite name=\""); testSuite = doc.createElement("testSuite");
stream.print(name); testRun.appendChild(testSuite);
stream.println("\">"); testSuite.setAttribute("name", name);
} }
private void endSuite() {
if (currentTest != null) {
endClass();
currentTest = null;
}
stream.println("\t</testSuite>");
}
private void endClass() {
stream.println("\t\t</testClass>");
}
} }