XmlFixture is the class that helps XML assertions in an easy way.
Download
http://sourceforge.net/project/showfiles.php?group_id=54719&package_id=156151
Creation
XmlFixture works using JDom, and it can be created from various type of sources using its static factory methods:
- String:
XmlFixture.fromText(String content) - Reader:
XmlFixture.fromReader(Reader reader) - w3c Document:
XmlFixture.fromW3cDocument(org.w3c.dom.Document document)
XPath Match
After a XmlFixture instance is created, it can be used for XPath matching by calling xpathMatch.
This methods will create a XpathMatch object that contains the XPath match result, with the following method:
node(): This will return the matched node. IMPORTANT: It will returnnullif there is no match found, and throwIllegalStateExceptionif more than one node matched.stringValue(): This is the same as node method except that it will return the string value of the matched node.list: Return the list of matched nodes.count: Convenience method that returns the number of the matched nodeshasMatch: Convenience method that returns true if there are matched nodes and false otherwise.
The following code block illustrates how a typical XML assertion looks like with XmlFixture:
XmlFixture xml = XmlFixture.fromText("<person><address type=\"test\">address value</address></person>");
assertEquals("address value", xml.xpath("/person/address[@type='test']"));
assertEquals(1, xml.xpathCount("/person/address"));
Exception Handling
XmlFixture wraps all the excepion into a RuntimeException so that you don't have to.
Dependency
The following are the jars that XmlFixture depends on:
- commons-lang-2.0.jar
- jaxen-1.0-dev.jar
- jdom-b9/jdom.jar
- saxpath-1.0-FCS.jar
- junit.jar: For backward compatibility. Although you should use JUnit in your project anyway.