Juxy

Juxy (http://juxy.tigris.org/), by Pavel Sher, is a library for unit testing XSLT stylesheets from Java. After a long time spent threatening to do so, I finally contributed an XML format for Juxy tests.

I’ve been riding my hobby horse for a while about the usefulness, nay necessity, of using more than XSLT when testing XSLT (and I’ll be back in that saddle again at XML Prague). Juxy fits that bill, since it’s in Java, but it always seems to me that writing tests in Java is more than many XSLT practitioners would want to do. So I wrote a stylesheet to generate Java from XML descriptions of the tests.

Here’s a Java test case from the Juxy project page:

public class SampleTestCase extends JuxyTestCase {
  public void testListTransformation() {
      newContext("stylesheet.xsl");
      context().setDocument("" +
          "<list>" +
          "	<item>item 1</item>" +
          "	<item>item 2</item>" +
          "	<item>item 3</item>" +
          "</list>");
      Node result = applyTemplates();
      xpathAssert("text()", "item 1, item 2, item 3").eval(result);
  }
}

and here’s a near-equivalent XML test case:

<test name="MoreThanOneElementInTheList_ApplyTemplates">
  <document select="/list"><list><item>first item</item><item>second item</item>
<item>third item</item></list></document>
  <apply-templates select="/list"/>
  <assert-equals>
    <expected>first item, second item, third item</expected>
  </assert-equals>
</test>

Testing for the correct result is the easy part. All the XSLT-only testing frameworks do the job when you’re just testing the happy scenario (my new phrase for the week, picked up from here). One reason for going outside XSLT is testing for <xsl:message terminate="yes">, which would make an XSLT-only test framework crash and burn along with the stylesheet under test.

The following Juxy test causes the stylesheet processing to terminate, but Juxy just records that the assert-error assertion was true, rather than Juxy terminating as well:

<test name="Stylesheet">
  <stylesheet href="xmlTest/x2j.xsl"/>
  <document><stylesheet href="href"><root/></stylesheet></document>
  <assert-error>
    <apply-templates/>
  </assert-error>
</test>