Subject: Re: [xsl] XSLT Unit testing From: davep <davep@xxxxxxxxxxxxx> Date: Thu, 18 Aug 2011 14:37:03 +0100 |
My experience does suggest that it's not always going to be a worthwhile investment of effort, but that when it is worthwhile it can be very worthwhile.That kind of sums up how I feel about writing tests in general, most of the time its a chore but once they are in place you are really glad you did it :)
For what its worth, I've not used xslt specific test frameworks, instead using standard junt tests that run the transform and then use xpaths on the result (well, xquery).
Because its just a normal junit test, it slots straight in with existing build process. You can test individual templates and functions by using a custom xslt that imports the main transform and then just calls the function or applies templates to a variable. You can be as simple or a complex as you like - for example you could write a single transform that calls a function 10 different ways producing some output xml with the 10 results, then in the junit test pull out those values and compare them.
The basic code is below, most of it is boilerplate to run the transform (using Saxon) and make the actual test methods small. To use it, just copy and paste it, edit the XML and XSLT paths, and then start adding tests. The tests themselves can be as simple as:
@Test public void testTheTitle() throws Exception { assertEquals("Some value for the title", runXQuery("//title/string(.)")); }
Here's the code:
import static org.junit.Assert.*;
import java.util.logging.Level; import java.util.logging.Logger; import org.xml.sax.InputSource; import javax.xml.transform.sax.SAXSource; import net.sf.saxon.s9api.Processor; import net.sf.saxon.s9api.XQueryCompiler; import net.sf.saxon.s9api.XQueryEvaluator; import net.sf.saxon.s9api.XdmDestination; import net.sf.saxon.s9api.XdmNode; import net.sf.saxon.s9api.XsltTransformer; import org.junit.BeforeClass; import org.junit.Test;
public class TestingXSLTUsingXQuery {
private static final String XSLT = "file:///c:/path/to/test.xslt"; private static final String XML = "file:///c:/path/to/test.xml";
private static XQueryCompiler xqueryCompiler; private static XdmNode doc;
@BeforeClass public static void setUp() { try { Processor processor = new Processor(false); xqueryCompiler = processor.newXQueryCompiler();
XsltTransformer transformer = processor.newXsltCompiler().compile(new SAXSource(new InputSource(XSLT))).load();
XdmDestination destination = new XdmDestination(); transformer.setDestination(destination); transformer.setSource(new SAXSource(new InputSource(XML))); transformer.transform();
doc = destination.getXdmNode(); } catch (Exception ex) { Logger.getLogger(TestingXSLTUsingXQuery.class.getName()).log(Level.SEVERE, null, ex); } }
private String runXQuery(String xquery) throws Exception { XQueryEvaluator xqEval = xqueryCompiler.compile(xquery).load(); xqEval.setContextItem(doc);
return xqEval.evaluate().toString(); }
@Test public void testTheFirstFooStringValue() throws Exception {
String xquery = "//foo[1]/string(.)";
String expected = "123";
assertEquals(expected, runXQuery(xquery)); }
@Test public void testFooContent() throws Exception {
String xquery = "let $expected := <root><foo>123</foo><foo>456</foo></root> return deep-equal(/root, $expected)";
assertTrue(Boolean.valueOf(runXQuery(xquery))); } }
-- Dave Pawson XSLT XSL-FO FAQ. http://www.dpawson.co.uk
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XSLT Unit testing, Florent Georges | Thread | Re: [xsl] XSLT Unit testing, Florent Georges |
Re: [xsl] XSLT Unit testing, Florent Georges | Date | Re: [xsl] XSLT Unit testing, Florent Georges |
Month |