wiki:TestingXSLT/Juxy

Juxy

Juxy (  http://juxy.tigris.org/), by Pavel Sher, describes itself as “a library for unit testing XSLT stylesheets from Java”. It states that it is best suited for the projects where both Java and XSLT are used simultaneously.

In contrast to XSLTunit and its descendants, Juxy tests are written in Java. Tests can be written to run standalone, for use with JUnit, or for use with any other (probably Java-based) testing framework. Tests for use with JUnit are the most likely use for Juxy, both because the tests are less verbose and so are easier to both read and write and because many people are already using JUnit for testing Java code.

The input being tested can be from a file, a Document object or a String, as shown in the following JUnit-specific example excerpted from the Juxy website:

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);
  }
}

XML Format

At the time of this writing, Juxy CVS contains a stylesheet, by Tony Graham, for converting tests in XML format into Java, which is then compiled in the same manner as conventional Juxy tests. The following example has the same effect as the previous Java example:

<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>

Note that the requirement to translate tests into Java methods for use with JUnit currently limits test names to strings that (when prepended with 'text') make valid method names for use with JUnit.

Figure 1 shows the JUnit report from running the test cases generated from the complete XML file for the above example.

Juxy results. Figure 1 – Juxy test report

The advantage of generating tests as Java and running the stylesheet under test from a non-XSLT framework is that the framework can catch error conditions that would blow away an all-XSLT framework. For example, the following test asserts that x2j.xsl will terminate when the source document is the content of the <document> element:

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

It is intended to add the ability to make assertions about the content of xsl:message output and the names of files that are read and written.