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

The following examples show how to use org.xmlunit.xpath.JAXPXPathEngine#setNamespaceContext() . 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 File: MultipleNodeAssert.java    From xmlunit with 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
Source File: ValueAssert.java    From xmlunit with Apache License 2.0 6 votes vote down vote up
static ValueAssert 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);
    String value = engine.evaluate(xPath, root);

    return new ValueAssert(value)
            .describedAs("XPath \"%s\" evaluated to value", xPath);
}
 
Example 3
Source File: CastorMarshallerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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 4
Source File: EvaluateXPathMatcher.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluates the provided XML input to the configured <code>xPath</code> field XPath expression.
 * @param input an XML input
 * @return the result of the XPath evaluation
 */
private String xPathEvaluate(Object input) {
    JAXPXPathEngine engine = xpf == null ? new JAXPXPathEngine() : new JAXPXPathEngine(xpf);
    if (prefix2Uri != null) {
        engine.setNamespaceContext(prefix2Uri);
    }

    Source s = Input.from(input).build();
    Node n = dbf != null ? Convert.toNode(s, dbf) : Convert.toNode(s);
    return engine.evaluate(xPath, n);
}
 
Example 5
Source File: HasXPathMatcher.java    From xmlunit with 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();
}