From: <bo...@us...> - 2014-12-31 11:25:55
|
Revision: 585 http://sourceforge.net/p/xmlunit/code/585 Author: bodewig Date: 2014-12-31 11:25:54 +0000 (Wed, 31 Dec 2014) Log Message: ----------- Add new overloads for XPath expressions that evaluate to QNames https://sourceforge.net/p/xmlunit/feature-requests/25/ Modified Paths: -------------- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java Modified: trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java 2014-12-30 19:02:43 UTC (rev 584) +++ trunk/src/java/org/custommonkey/xmlunit/QualifiedName.java 2014-12-31 11:25:54 UTC (rev 585) @@ -91,6 +91,16 @@ } /** + * Represents the QualifiedName as {NS-URI}LOCAL-NAME. + * + * <p>If the NS-URI is equal to NULL_NS_URI only the local name is returned.</p> + */ + public String toString() { + return XMLConstants.NULL_NS_URI.equals(namespaceUri) ? + localName : "{" + namespaceUri + "}" + localName; + } + + /** * Parses strings of the form "{NS-URI}LOCAL-NAME" or "prefix:localName" as QualifiedNames. * * <p>When using the prefix-version the prefix must be defined Modified: trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java =================================================================== --- trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java 2014-12-30 19:02:43 UTC (rev 584) +++ trunk/src/java/org/custommonkey/xmlunit/XMLAssert.java 2014-12-31 11:25:54 UTC (rev 585) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2007,2011 Jeff Martin, Tim Bacon +Copyright (c) 2001-2007,2011,2014 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -42,6 +42,8 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.util.Iterator; +import java.util.NoSuchElementException; import javax.xml.parsers.DocumentBuilder; @@ -862,6 +864,67 @@ } /** + * Assert the value of an Xpath expression in an XML document. + * @param expectedValue + * @param xpathExpression + * @param control + * @throws SAXException + * @throws IOException + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(QualifiedName expectedValue, + String xpathExpression, + InputSource control) + throws SAXException, IOException, + XpathException { + Document document = XMLUnit.buildControlDocument(control); + assertXpathEvaluatesTo(expectedValue, xpathExpression, document); + } + + /** + * Assert the value of an Xpath expression in an XML String + * @param expectedValue + * @param xpathExpression + * @param inXMLString + * @throws SAXException + * @throws IOException + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(QualifiedName expectedValue, + String xpathExpression, + String inXMLString) + throws SAXException, IOException, + XpathException { + Document document = XMLUnit.buildControlDocument(inXMLString); + assertXpathEvaluatesTo(expectedValue, xpathExpression, document); + } + + /** + * Assert the value of an Xpath expression in an DOM Document + * @param expectedValue + * @param xpathExpression + * @param inDocument + * @see XpathEngine which provides the underlying evaluation mechanism + */ + public static void assertXpathEvaluatesTo(QualifiedName expectedValue, + String xpathExpression, + Document inDocument) + throws XpathException { + XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine(); + NodeList nl = + simpleXpathEngine.getMatchingNodes(xpathExpression, inDocument); + NamespaceContext ctx = null; + if (nl.getLength() > 0) { + ctx = new NodeBasedNamespaceContext(nl.item(0)); + } + assertEquals(expectedValue, + QualifiedName + .valueOf(simpleXpathEngine.evaluate(xpathExpression, + inDocument), + ctx)); + } + + /** * Assert that a specific XPath exists in some given XML * @param inXpathExpression * @param control @@ -1125,4 +1188,29 @@ } return d; } + + private static class NodeBasedNamespaceContext implements NamespaceContext { + private final Node node; + NodeBasedNamespaceContext(Node n) { + node = n; + } + public String getNamespaceURI(String prefix) { + return node.lookupNamespaceURI(prefix); + } + + // not used in context of #assertXpathEvaluatesTo + public Iterator getPrefixes() { + return new Iterator() { + public boolean hasNext() { + return false; + } + public Object next() { + throw new NoSuchElementException(); + } + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + } } Modified: trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java =================================================================== --- trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java 2014-12-30 19:02:43 UTC (rev 584) +++ trunk/tests/java/org/custommonkey/xmlunit/test_XMLTestCase.java 2014-12-31 11:25:54 UTC (rev 585) @@ -1,6 +1,6 @@ /* ****************************************************************** -Copyright (c) 2001-2011, Jeff Martin, Tim Bacon +Copyright (c) 2001-2011,2014 Jeff Martin, Tim Bacon All rights reserved. Redistribution and use in source and binary forms, with or without @@ -40,6 +40,7 @@ import java.io.FileReader; import java.io.StringReader; import java.util.HashMap; +import java.util.Map; import junit.framework.AssertionFailedError; import junit.framework.TestSuite; @@ -582,6 +583,22 @@ "<foo><Bar a=\"1\" b=\"2\"/></foo>"); } + // https://sourceforge.net/p/xmlunit/feature-requests/25/ + public void testXpathEvaluatesToQualifiedName() throws Exception { + String faultDocument = "<env:Envelope " + + "xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" + + "<env:Body><env:Fault><faultcode>env:Server</faultcode>" + + "<faultstring>marche pas</faultstring><detail/></env:Fault>" + + "</env:Body></env:Envelope>"; + Map namespaces = new HashMap(); + namespaces.put("env11", "http://schemas.xmlsoap.org/soap/envelope/"); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(namespaces)); + XMLAssert.assertXpathEvaluatesTo(QualifiedName.valueOf("env11:Server"), + "//env11:Envelope/env11:Body/" + + "env11:Fault/faultcode", + faultDocument); + } + public test_XMLTestCase(String name) { super(name); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |