xsl-list
[Top] [All Lists]

Re: [xsl] XSLT Unit testing

2011-08-18 04:35:46
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)));
    }
}


-- 
Andrew Welch
http://andrewjwelch.com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>