Search in sources :

Example 21 with Element

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);
}
Also used : InputSource(org.xml.sax.InputSource) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) StringReader(java.io.StringReader) ResultType(com.facebook.buck.test.result.type.ResultType) Document(org.w3c.dom.Document)

Example 22 with Element

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();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 23 with Element

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 24 with Element

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);
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 25 with Element

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;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Aggregations

Element (org.w3c.dom.Element)9072 Document (org.w3c.dom.Document)2651 NodeList (org.w3c.dom.NodeList)2103 Node (org.w3c.dom.Node)1855 ArrayList (java.util.ArrayList)957 DocumentBuilder (javax.xml.parsers.DocumentBuilder)793 IOException (java.io.IOException)732 Test (org.junit.Test)693 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)591 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)489 HashMap (java.util.HashMap)434 SAXException (org.xml.sax.SAXException)406 File (java.io.File)358 Attr (org.w3c.dom.Attr)333 InputStream (java.io.InputStream)309 QName (javax.xml.namespace.QName)292 Map (java.util.Map)285 JAXBElement (javax.xml.bind.JAXBElement)285 NamedNodeMap (org.w3c.dom.NamedNodeMap)266 List (java.util.List)264