Generating JUnit source code
<xsl:template match="xc:tests" mode="java">
<xsl:param name="xc:classname"/>
<xsl:param name="xc:xslt-filename"/>
import junit.framework.*;
public class <xsl:value-of select="$xc:classname"/> extends TestCase
{
public <xsl:value-of select="$xc:classname"/>(String name) {
super(name); }
public static Test suite() { return new TestSuite(<xsl:value-of select="$xc:classname"/>.class); }
<xsl:apply-templates select="xc:test" mode="java">
<xsl:with-param name="xc:xslt-filename" select="$xc:xslt-filename"/>
</xsl:apply-templates>
} // class <xsl:value-of select="$xc:classname"/>
</xsl:template>
<xsl:template match="xc:test" mode="java">
<xsl:param name="xc:xslt-filename"/>
public void <xsl:value-of select="concat('test_', @id)"/>()
{
Helper.runTest("<xsl:value-of select="@id"/>",
"<xsl:value-of select="$xc:xslt-filename"/>",
"<xsl:value-of select="preceding-sibling::xc:xml/@href"/>");
}
</xsl:template>
Here's a stylesheet fragment that generates most of the Java.
XSLTTestHelper - moved all the stuff that actually does stuff into a helper class.
You can see we're grabbing the test names out of the stylesheet ('test-name' is not a valid Java name, so have to alter definition).Prefix with test_ so JUnit can find them.
We're also grabbing the xml source name from the definition.
Where have xc:classname and xc:xslt-filename come from?
Well, xc:classname is based on the test suite name at the top of our definition.
Note the mode. The full generator stylesheet does two things - creates an XSLTunit and this JUnit source. They both need to go into different files. We use the xsl:document extension (supported by Saxon and Xalan, and now part of XSLT 2.0). Derive XSLTUnit and Java filename based on test definition name. Since Java filenames must match the class name, work that out at the same time.