Search in sources :

Example 11 with JAXPXPathEngine

use of org.xmlunit.xpath.JAXPXPathEngine in project atlasmap by atlasmap.

the class AtlasTestUtil method validateXmlOrderElement.

public static void validateXmlOrderElement(Object orderObject, int expectedOrderId) {
    assertNotNull(orderObject);
    HashMap<String, String> ns = new HashMap<>();
    ns.put("ns", "http://atlasmap.io/xml/test/v2");
    assertThat(orderObject).withNamespaceContext(ns).valueByXPath("/ns:XmlOE/ns:orderId").isEqualTo(expectedOrderId);
    Source source = Input.from(orderObject).build();
    JAXPXPathEngine engine = new JAXPXPathEngine();
    engine.setNamespaceContext(ns);
    Iterable<Node> addresses = engine.selectNodes("/ns:XmlOE/ns:Address", source);
    validateXmlAddressElement(addresses.iterator().next());
    Iterable<Node> contacts = engine.selectNodes("/ns:XmlOE/ns:Contact", source);
    validateXmlContactElement(contacts.iterator().next());
}
Also used : HashMap(java.util.HashMap) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) Node(org.w3c.dom.Node) Source(javax.xml.transform.Source)

Example 12 with JAXPXPathEngine

use of org.xmlunit.xpath.JAXPXPathEngine in project spring-framework by spring-projects.

the class CastorMarshallerTests method assertXpathEvaluatesTo.

/**
	 * 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());
}
Also used : HashMap(java.util.HashMap) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) Node(org.w3c.dom.Node) Source(javax.xml.transform.Source)

Example 13 with JAXPXPathEngine

use of org.xmlunit.xpath.JAXPXPathEngine in project pom-manipulation-ext by release-engineering.

the class XMLManipulatorTest method alterFile.

@Test
public void alterFile() throws Exception {
    String replacementGA = "com.rebuild:servlet-api";
    String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
    File target = tf.newFile();
    FileUtils.copyFile(xmlFile, target);
    Project project = new Project(null, target, null);
    xmlManipulator.internalApplyChanges(project, new XMLState.XMLOperation(target.getName(), tomcatPath, replacementGA));
    Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(fromFile(target)).build();
    assertTrue(diff.toString(), diff.hasDifferences());
    String xpathForHamcrest = "/*/*/*/*/*[starts-with(.,'com.rebuild') and local-name() = 'include']";
    Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, fromFile(target).build());
    int count = 0;
    for (Node anI : i) {
        count++;
        assertTrue(anI.getTextContent().startsWith("com.rebuild:servlet-api"));
    }
    assertEquals(1, count);
}
Also used : Project(org.commonjava.maven.ext.common.model.Project) XMLState(org.commonjava.maven.ext.core.state.XMLState) Diff(org.xmlunit.diff.Diff) JAXPXPathEngine(org.xmlunit.xpath.JAXPXPathEngine) Node(org.w3c.dom.Node) Input.fromFile(org.xmlunit.builder.Input.fromFile) File(java.io.File) Test(org.junit.Test) XMLIOTest(org.commonjava.maven.ext.io.XMLIOTest)

Aggregations

JAXPXPathEngine (org.xmlunit.xpath.JAXPXPathEngine)13 Node (org.w3c.dom.Node)11 Source (javax.xml.transform.Source)8 File (java.io.File)7 Test (org.junit.Test)5 Input.fromFile (org.xmlunit.builder.Input.fromFile)4 Diff (org.xmlunit.diff.Diff)4 HashMap (java.util.HashMap)3 StreamSource (javax.xml.transform.stream.StreamSource)3 XPath (javax.xml.xpath.XPath)3 Document (org.w3c.dom.Document)3 NodeList (org.w3c.dom.NodeList)3 HasXPathMatcher.hasXPath (org.xmlunit.matchers.HasXPathMatcher.hasXPath)3 XPathEngine (org.xmlunit.xpath.XPathEngine)2 Project (org.commonjava.maven.ext.common.model.Project)1 XMLState (org.commonjava.maven.ext.core.state.XMLState)1 XMLIOTest (org.commonjava.maven.ext.io.XMLIOTest)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1