There’s myriad ways to validate XML these days; in fact, with Groovy, the mechanics of parsing XML with XMLSlurper couldn’t be easier! Nevertheless, from time to time, because it’s my bag, baby, I’ve found that I’ve needed an easy way to validate XML documents without having to actually parse them myself. Thus, XMLUnit has been a handy framework today as it has been for years.
While XMLUnit is a hip JUnit-style framework, you can certainly leverage it outside of JUnit. What’s more, via easyb’s plug-in framework, using XMLUnit just got a lot easier! For instance, leveraging XMLUnit without the new plug-in required a bit of manual manipulation of XMLUnit’s Diff class and then a boolean verification like so:
import org.custommonkey.xmlunit.XMLUnit
import org.custommonkey.xmlunit.Diff
XMLUnit.setIgnoreWhitespace(true)
scenario "the XMLRepresentationBuilder should build XML", {
given "a table name and collection of name value pairs", {
lst = [new ColumnNameValue("LN_ID", 10002130),
new ColumnNameValue("NIBA", "99.99"),
new ColumnNameValue("EFF_DATE", "2009-04-01")]
map = ["LN_ID":"id", "NIBBA":"nb","EFF_DATE":"effectiveDate"]
}
then "the XML produced should be valid", {
rep = new DatabaseTableResource()
rep.columnNameValues = lst
rep.columnNameMappings = map
rep.identifiers = ["LN_ID"]
out = XMLRepresentationBuilder.buildRepresentation(rep)
control = """<cmnres id='10002130'>
<nib>99.99</nib><effectiveDate>2009-04-01</effectiveDate>
</cmnres>"""
diff = new Diff(control, out)
diff.identical().shouldBe true
}
}
Note how in this code it’s necessary to:
- Ensure white space doesn’t cause comparison errors via the
XMLUnit.setIgnoreWhitespace(true)call - Use XMLUnit’s
Diffclass to compare two documents and then call theidentical(orsimilar) method to actually verify the documents
With easyb’s XMLUnit plug-in, things are a lot simpler! All you need to do is insert a using clause and ensure the plug-in is in your classpath. Watch:
using "xmlunit"
scenario "XML documents are compared easier via xml unit plug-in", {
given "some xml document", {
control = """<account><id>3A-00</id><name>acme</name></account>"""
}
then "the plugin should enable easy comparisons", {
testXML = """<account><id>3A-00</id><name>acme</name></account>"""
testXML.shouldBeIdenticalTo control
testXML.shouldBeIdenticalWith control //same behavior different call
testXML.shouldBeIdentical control //ditto
testXML.identical control //ditto
}
and "the instance of XMLUnit should be available for use", {
XMLUnit.version.shouldBe "1.3alpha"
XMLUnit.getIgnoreWhitespace().shouldBe true
}
}
Notice that there isn’t any need to alter the white space setting (it’s been done for you in the plug-in — yo can undo it easily too — see the and clause?), nor is it necessary to use XMLUnit’s Diff class — it’s all done under the covers. While this story uses the identical call, there is a corresponding similar call (and related pattern — that is, shouldBeSimilarTo and the like).
Also, the initialized instance of XMLUnit is available should you need further configurations, etc — the plug-in inserts the copacetic instance into the binding available to your behaviors as shown in the and phrase above.
As you can see, the XMLUnit plug-in for easyb makes workin with xml (that is, comparing documents) a lot easier. While there’s plenty of ways to skin a cat, so to speak, this is just one of them — I’ve certainly found it handy from time to time — I think you will too!
You can download the current version of the plug-in at easyb’s Google code site along with a few other hip gems like easyb’s DbUnit plug-in.
