XSLT test case
public void test1()
{
DOMResult resultsink = new DOMResult();
Transformer tests = createTransformer(loadSource("xslt_unit_test.xsl"));
tests.transform(loadSource("test_data.xml"), resultsink);
Document resultDoc = (Document)resultsink.getNode();
NodeList results = resultDoc.getElementsByTagNameNS("http://xsltunit.org/0/", "assert");
if(results.getLength() == 0)
Assert.fail("No test results found");
StringBuffer sb = new StringBuffer();
for(int i = 0; i != results.getLength(); ++i)
{
Node result = results.item(i);
if(!result.getAttributes().getNamedItem("outcome").getNodeValue().equals("passed"))
sb.append(result.getParentNode().getAttributes().
getNamedItem("id").getNodeValue() + " failed!");
} // for ...
if(sb.length() != 0)
Assert.fail(sb.toString());
}
Should be reasonably clear, even if you're not entirely au fait with JAXP and DOM
load the stylesheet
twist it together with the test data
reach into the output and find the asserts
if the assert wasn't passed then grab the name of the test that failed
Right let's all go home ...
Problems?
Hard coded dependency on the stylesheet - no advantage over our Ant script original
Reporting isn't very granular - all the tests are run at once
Reporting isn't very helpful - a test failed, but not how
Let's have a go and fixing some of that. Let's take a look at what Ant tells us.