use of org.w3c.dom.Element in project buck by facebook.
the class XmlTestResultParser method doParse.
private static TestCaseSummary doParse(String xml) throws IOException, SAXException {
Document doc = XmlDomParser.parse(new InputSource(new StringReader(xml)), /* namespaceAware */
true);
Element root = doc.getDocumentElement();
Preconditions.checkState("testcase".equals(root.getTagName()));
String testCaseName = root.getAttribute("name");
NodeList testElements = doc.getElementsByTagName("test");
List<TestResultSummary> testResults = Lists.newArrayListWithCapacity(testElements.getLength());
for (int i = 0; i < testElements.getLength(); i++) {
Element node = (Element) testElements.item(i);
String testName = node.getAttribute("name");
long time = Long.parseLong(node.getAttribute("time"));
String typeString = node.getAttribute("type");
ResultType type = ResultType.valueOf(typeString);
String message;
String stacktrace;
if (type == ResultType.SUCCESS) {
message = null;
stacktrace = null;
} else {
message = node.getAttribute("message");
stacktrace = node.getAttribute("stacktrace");
}
NodeList stdoutElements = node.getElementsByTagName("stdout");
String stdOut;
if (stdoutElements.getLength() == 1) {
stdOut = stdoutElements.item(0).getTextContent();
} else {
stdOut = null;
}
NodeList stderrElements = node.getElementsByTagName("stderr");
String stdErr;
if (stderrElements.getLength() == 1) {
stdErr = stderrElements.item(0).getTextContent();
} else {
stdErr = null;
}
TestResultSummary testResult = new TestResultSummary(testCaseName, testName, type, time, message, stacktrace, stdOut, stdErr);
testResults.add(testResult);
}
return new TestCaseSummary(testCaseName, testResults);
}
use of org.w3c.dom.Element in project buck by facebook.
the class BaseRunner method writeResult.
/**
* The test result file is written as XML to avoid introducing a dependency on JSON (see class
* overview).
*/
protected void writeResult(String testClassName, List<TestResult> results) throws IOException, ParserConfigurationException, TransformerException {
// XML writer logic taken from:
// http://www.genedavis.com/library/xml/java_dom_xml_creation.jsp
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.setXmlVersion("1.1");
Element root = doc.createElement("testcase");
root.setAttribute("name", testClassName);
root.setAttribute("runner_capabilities", getRunnerCapabilities());
doc.appendChild(root);
for (TestResult result : results) {
Element test = doc.createElement("test");
// suite attribute
test.setAttribute("suite", result.testClassName);
// name attribute
test.setAttribute("name", result.testMethodName);
// success attribute
boolean isSuccess = result.isSuccess();
test.setAttribute("success", Boolean.toString(isSuccess));
// type attribute
test.setAttribute("type", result.type.toString());
// time attribute
long runTime = result.runTime;
test.setAttribute("time", String.valueOf(runTime));
// Include failure details, if appropriate.
Throwable failure = result.failure;
if (failure != null) {
String message = failure.getMessage();
test.setAttribute("message", message);
String stacktrace = stackTraceToString(failure);
test.setAttribute("stacktrace", stacktrace);
}
// stdout, if non-empty.
if (result.stdOut != null) {
Element stdOutEl = doc.createElement("stdout");
stdOutEl.appendChild(doc.createTextNode(result.stdOut));
test.appendChild(stdOutEl);
}
// stderr, if non-empty.
if (result.stdErr != null) {
Element stdErrEl = doc.createElement("stderr");
stdErrEl.appendChild(doc.createTextNode(result.stdErr));
test.appendChild(stdErrEl);
}
root.appendChild(test);
}
// Create an XML transformer that pretty-prints with a 2-space indent.
// The transformer factory uses a system property to find the class to use. We need to default
// to the system default since we have the user's classpath and they may not have everything set
// up for the XSLT transform to work.
String vendor = System.getProperty("java.vm.vendor");
String factoryClass;
if ("IBM Corporation".equals(vendor)) {
// Used in the IBM JDK --- from
// https://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.aix.80.doc/user/xml/using_xml.html
factoryClass = "com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl";
} else {
// Used in the OpenJDK and the Oracle JDK.
factoryClass = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
}
// When we get this far, we're exiting, so no need to reset the property.
System.setProperty("javax.xml.transform.TransformerFactory", factoryClass);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer trans = transformerFactory.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// Write the result to a file.
String testSelectorSuffix = "";
if (!testSelectorList.isEmpty()) {
testSelectorSuffix += ".test_selectors";
}
if (isDryRun) {
testSelectorSuffix += ".dry_run";
}
OutputStream output;
if (outputDirectory != null) {
File outputFile = new File(outputDirectory, testClassName + testSelectorSuffix + ".xml");
output = new BufferedOutputStream(new FileOutputStream(outputFile));
} else {
output = System.out;
}
StreamResult streamResult = new StreamResult(output);
DOMSource source = new DOMSource(doc);
trans.transform(source, streamResult);
if (outputDirectory != null) {
output.close();
}
}
use of org.w3c.dom.Element in project buck by facebook.
the class BuckXmlTestRunListener method addMainTestResult.
/**
* Adds one more XML element to the test_result.xml tracking the
* result of the whole process.
*/
private void addMainTestResult() {
try {
File resultFile = getResultFile(mReportDir);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(resultFile);
Node testsuite = doc.getElementsByTagName("testsuite").item(0);
if (mRunFailureMessage != null) {
Element failureNode = doc.createElement("failure");
failureNode.setTextContent(mRunFailureMessage);
testsuite.appendChild(failureNode);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(resultFile);
transformer.transform(source, result);
}
} catch (IOException | ParserConfigurationException | SAXException | TransformerException e) {
throw new RuntimeException(e);
}
}
use of org.w3c.dom.Element in project head by mifos.
the class TableTagParser method setFragmentDetails.
protected void setFragmentDetails(DisplayName displayName, Node displayNode) throws TableTagParseException {
NodeList fragmentNameNodeList = ((Element) displayNode).getElementsByTagName(TableTagConstants.FRAGMENTNAME);
Fragment[] fragment = new Fragment[fragmentNameNodeList.getLength()];
for (int i = 0; i < fragmentNameNodeList.getLength(); i++) {
fragment[i] = new Fragment();
setFragmentAttributes(fragment[i], fragmentNameNodeList.item(i).getAttributes());
}
displayName.setFragment(fragment);
}
use of org.w3c.dom.Element in project head by mifos.
the class TableTagParser method createParameters.
protected Parameters createParameters(Node column) throws TableTagParseException {
NodeList displayNodeList = ((Element) column).getElementsByTagName(TableTagConstants.PARAMETERS);
Parameters parameters = new Parameters();
if (displayNodeList.getLength() > 0) {
setParameterDetails(parameters, displayNodeList.item(0));
}
return parameters;
}
Aggregations