Java Code Examples for org.xmlunit.xpath.JAXPXPathEngine#selectNodes()

The following examples show how to use org.xmlunit.xpath.JAXPXPathEngine#selectNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: xmlunit   File: MultipleNodeAssert.java    License: Apache License 2.0 6 votes vote down vote up
static MultipleNodeAssert create(Object xmlSource, Map<String, String> prefix2Uri, DocumentBuilderFactory dbf,
                                 XPathFactory xpf, String xPath) {

    AssertionsAdapter.assertThat(xPath).isNotBlank();

    final JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(xmlSource).build();
    Node root = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    Iterable<Node> nodes = engine.selectNodes(xPath, root);

    return new MultipleNodeAssert(nodes, engine)
            .describedAs("XPath \"%s\" evaluated to node set", xPath);
}
 
Example 2
/**
 * Assert the values of xpath expression evaluation is exactly the same as expected value.
 * <p>The xpath may contain the xml namespace prefixes, since namespaces from flight example
 * are being registered.
 * @param msg the error message that will be used in case of test failure
 * @param expected the expected value
 * @param xpath the xpath to evaluate
 * @param xmlDoc the xml to use
 * @throws Exception if any error occurs during xpath evaluation
 */
private void assertXpathEvaluatesTo(String msg, String expected, String xpath, String xmlDoc) throws Exception {
	Map<String, String> namespaces = new HashMap<>();
	namespaces.put("tns", "http://samples.springframework.org/flight");
	namespaces.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");

	JAXPXPathEngine engine = new JAXPXPathEngine();
	engine.setNamespaceContext(namespaces);

	Source source = Input.fromString(xmlDoc).build();
	Iterable<Node> nodeList = engine.selectNodes(xpath, source);
	assertEquals(msg, expected, nodeList.iterator().next().getNodeValue());
}
 
Example 3
Source Project: xmlunit   File: HasXPathMatcher.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(Object object) {
    JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(object).build();
    Node n = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    Iterable<Node> nodes = engine.selectNodes(xPath, n);

    return nodes.iterator().hasNext();
}