use of org.xmlunit.xpath.JAXPXPathEngine in project spring-framework by spring-projects.
the class XStreamMarshallerTests method assertXpathNotExists.
private static void assertXpathNotExists(String xPathExpression, String inXMLString) {
Source source = Input.fromString(inXMLString).build();
Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);
assertEquals("Should be zero matches for Xpath " + xPathExpression, 0, count(nodes));
}
use of org.xmlunit.xpath.JAXPXPathEngine in project tutorials by eugenp.
the class XMLUnitTest method givenXPath_whenAbleToRetrieveNodes_thenCorrect.
@Test
public void givenXPath_whenAbleToRetrieveNodes_thenCorrect() {
ClassLoader classLoader = getClass().getClassLoader();
Iterable<Node> i = new JAXPXPathEngine().selectNodes("//teacher", Input.fromFile(new File(classLoader.getResource("teachers.xml").getFile())).build());
assertNotNull(i);
int count = 0;
for (Iterator<Node> it = i.iterator(); it.hasNext(); ) {
count++;
Node node = it.next();
assertEquals("teacher", node.getNodeName());
NamedNodeMap map = node.getAttributes();
assertEquals("department", map.item(0).getNodeName());
assertEquals("id", map.item(1).getNodeName());
assertEquals("teacher", node.getNodeName());
}
assertEquals(2, count);
}
use of org.xmlunit.xpath.JAXPXPathEngine in project pom-manipulation-ext by release-engineering.
the class XMLIOTest method removePartFile.
@Test
public void removePartFile() throws ManipulationException, IOException, XPathExpressionException {
String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
Document doc = xmlIO.parseXML(xmlFile);
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate(tomcatPath, doc, XPathConstants.NODESET);
logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength());
assertTrue(nodeList.getLength() == 1);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
logger.debug("Found node {} with type {} and value {}", node.getNodeName(), node.getNodeType(), node.getTextContent());
node.getParentNode().removeChild(node);
}
File target = tf.newFile();
xmlIO.writeXML(target, doc);
Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
assertTrue(diff.toString(), diff.hasDifferences());
String xpathForHamcrest = "/*/*/*/*/*[starts-with(.,'org.apache.tomcat') and local-name() = 'include']";
Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build());
int count = 0;
for (Node ignored : i) {
count++;
}
assertEquals(0, count);
}
use of org.xmlunit.xpath.JAXPXPathEngine in project pom-manipulation-ext by release-engineering.
the class XMLIOTest method modifyMultiple.
@Test
public void modifyMultiple() throws ManipulationException, IOException, XPathExpressionException {
String updatePath = "/assembly/formats/format";
Document doc = xmlIO.parseXML(xmlFile);
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate(updatePath, doc, XPathConstants.NODESET);
logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
logger.debug("Found node {} with type {} and value {} ", node.getNodeName(), node.getNodeType(), node.getTextContent());
node.setTextContent("NEW-FORMAT-" + i);
}
File target = tf.newFile();
xmlIO.writeXML(target, doc);
Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.fromFile(target)).build();
assertTrue(diff.toString(), diff.hasDifferences());
String xpathForHamcrest = "/*/*/*[starts-with(.,'NEW-FORMAT') and local-name() = '" + updatePath.substring(updatePath.lastIndexOf('/') + 1) + "']";
Iterable<Node> i = new JAXPXPathEngine().selectNodes(xpathForHamcrest, Input.fromFile(target).build());
int count = 0;
for (Node anI : i) {
count++;
assertTrue(anI.getTextContent().startsWith("NEW-FORMAT"));
}
assertEquals(3, count);
}
use of org.xmlunit.xpath.JAXPXPathEngine in project pom-manipulation-ext by release-engineering.
the class XMLIOTest method modifyPartialFile.
@Test
public void modifyPartialFile() throws ManipulationException, IOException, XPathExpressionException {
String replacementGA = "com.rebuild:servlet-api";
String tomcatPath = "//include[starts-with(.,'org.apache.tomcat')]";
Document doc = xmlIO.parseXML(xmlFile);
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = (NodeList) xPath.evaluate(tomcatPath, doc, XPathConstants.NODESET);
logger.debug("Found node {} with size {} ", nodeList, nodeList.getLength());
assertTrue(nodeList.getLength() == 1);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
logger.debug("Found node {} with type {} and value {}", node.getNodeName(), node.getNodeType(), node.getTextContent());
node.setTextContent(replacementGA);
}
File target = tf.newFile();
xmlIO.writeXML(target, doc);
Diff diff = DiffBuilder.compare(fromFile(xmlFile)).withTest(Input.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, Input.fromFile(target).build());
int count = 0;
for (Node anI : i) {
count++;
assertTrue(anI.getTextContent().startsWith("com.rebuild:servlet-api"));
}
assertEquals(1, count);
}
Aggregations