001      <?xml version="1.0" encoding="ISO-8859-1"?>
 002      
 003      <!--
 004         - Copyright 2005-2006 Jens Voß.
 005         -
 006         - Licensed under the Apache License, Version 2.0 (the "License");
 007         - you may not use this file except in compliance with the License.
 008         - You may obtain a copy of the License at
 009         -
 010         -     http://www.apache.org/licenses/LICENSE-2.0
 011         -
 012         - Unless required by applicable law or agreed to in writing, software
 013         - distributed under the License is distributed on an "AS IS" BASIS,
 014         - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 015         - See the License for the specific language governing permissions and
 016         - limitations under the License.
 017         -
 018        -->
 019      
 020      <!---
 021          - The <code>alsterTestRunner.xsl</code> stylesheet is the "engine" of the
 022          - Alster XSLT unit testing framework.
 023         -->
 024      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 025                      version="1.0"
 026                      xmlns:xalan="http://xml.apache.org/xalan"
 027                      xmlns:java="http://xml.apache.org/xalan/java"
 028                      xmlns:exsl="http://exslt.org/common"
 029                      xmlns:alster="http://alster.sourceforge.net"
 030                      xmlns:reflect="http://nalax.sourceforge.net/reflect"
 031                      xmlns:io="http://nalax.sourceforge.net/io"
 032                      xmlns:log="http://nalax.sourceforge.net/log"
 033                      exclude-result-prefixes="alster"
 034                      extension-element-prefixes="java exsl reflect io log">
 035      
 036        <!---
 037            - The <code>testDir</code> stylesheet parameter specifies the path of the
 038            - Alster test stylesheet directory. It is passed to this stylesheet by the
 039            - <code>AlsterTestRunner</code> class.
 040           -->
 041        <xsl:param name="testDir"/>
 042        <!---
 043            - The <code>recurseTests</code> stylesheet parameter specifies whether of
 044            - not subdirectories of the directory specified by the <code>testDir</code>
 045            - parameter are to be included in the search for Alster test stylesheets.
 046           -->
 047        <xsl:param name="recurseTests"/>
 048      
 049        <xsl:include href="classpath:/net/sf/nalax/xsl/nalax.xsl"/>
 050      
 051      
 052        <!---
 053            - This template hooks into the root template of the test case
 054            - template. It either calls the
 055            - {@link #xsl:template~name~alster:runTestSuites template with
 056            - name="alster:runTestSuites"} (if and only if the <code>testDir</code>
 057            - parameter is set), or it runs the test cases contained in the
 058            - current enclosing stylesheet (if the XML file transformed is the
 059            - <code>alster.xml</code> file). In all other cases, the template
 060            - delegates to the imported root template. 
 061           -->
 062        <xsl:template match="/">
 063          <xsl:variable name="alster:runTests">
 064            <xsl:call-template name="alster:checkAlster"/>
 065          </xsl:variable>
 066          <xsl:choose>
 067            <xsl:when test="string-length($testDir)">
 068              <xsl:call-template name="alster:runTestSuites">
 069                <xsl:with-param name="dir" select="$testDir"/>
 070                <xsl:with-param name="recurse" select="$recurseTests"/>
 071              </xsl:call-template>
 072            </xsl:when>
 073            <xsl:when test="$alster:runTests = 1">
 074              <xsl:call-template name="alster:runTestCases"/>
 075            </xsl:when>
 076            <xsl:otherwise>
 077              <xsl:apply-imports/>
 078            </xsl:otherwise>
 079          </xsl:choose>
 080        </xsl:template>
 081      
 082        <!---
 083            - This template ensures tests whether the XML file the enclosing
 084            - stylesheet is applied to is identical to the file 'alster.xml'
 085            - (which merely consists of one zero-attribute top-level element
 086            - named "alster" without any descendants).<br/>
 087            - If this is the case, the template produces the string "1"; otherwise,
 088            - it generates an error message.
 089           -->
 090        <xsl:template name="alster:checkAlster">
 091          <xsl:choose>
 092            <xsl:when test="not (name(/*) = 'alster')">
 093              <xsl:text>name=</xsl:text>
 094              <xsl:value-of select="name(/*)"/>
 095            </xsl:when>
 096            <xsl:when test="count(/*/*) &gt; 0">
 097              <xsl:text>count=</xsl:text>
 098              <xsl:value-of select="count(/*/*)"/>
 099            </xsl:when>
 100            <xsl:otherwise>1</xsl:otherwise>
 101          </xsl:choose>
 102        </xsl:template>
 103      
 104      
 105        <!---
 106            - This template executes all test stylesheets in a given directory. It
 107            - iterates over the directory's files (and recurses into subdirectories
 108            - if so specified) and applies all stylesheets whose name ends with
 109            - "Test.xsl" to the XML root element.
 110            -
 111            - @param dir The path of the directory in which to execute the test
 112            -        stylesheets
 113            - @param recurse Specifies whether the execution should be performed
 114            -        recursively in all subdirectories of the specified directory
 115            -        (it does so if the parameter has value "1" and fails to do so
 116            -        otherwise)
 117            - @param package The name of the current package (ensures that the
 118            -        results can be identified with the correct source stylesheets)
 119           -->
 120        <xsl:template name="alster:runTestSuites">
 121          <xsl:param name="dir"/>
 122          <xsl:param name="recurse"/>
 123          <xsl:param name="package"/>
 124          <log:debug>
 125            <xsl:value-of select="$package"/>
 126          </log:debug>
 127          <testSuites>
 128            <xsl:if test="string-length($package)">
 129              <xsl:attribute name="package">
 130                <xsl:value-of select="$package"/>
 131              </xsl:attribute>
 132            </xsl:if>
 133            <io:files dir="$dir">
 134              <xsl:variable name="name" select="io:name()"/>
 135              <xsl:choose>
 136                <xsl:when test="io:isDirectory() and ($recurse = 1)">
 137                  <xsl:call-template name="alster:runTestSuites">
 138                    <xsl:with-param name="dir" select="io:path()"/>
 139                    <xsl:with-param name="recurse" select="$recurse"/>
 140                    <xsl:with-param name="package" select="concat($package, '/', $name)"/>
 141                  </xsl:call-template>
 142                </xsl:when>
 143                <xsl:when test="not (io:isDirectory())
 144                          and (string-length($name) &gt; 7)
 145                          and ($name = concat(substring($name, 1, string-length($name) - 8), 'Test.xsl'))">
 146                  <log:printOut>
 147                    <xsl:text>Running </xsl:text>
 148                    <xsl:value-of select="$name"/>
 149                  </log:printOut>
 150                  <!--<log:printOut>-->
 151                    <!--<xsl:value-of select="name(/*)"/>-->
 152                  <!--</log:printOut>-->
 153                  <xsl:variable name="result">
 154                    <reflect:transform select="." stylesheet="io:path()"/>
 155                  </xsl:variable>
 156                  <log:printOut>
 157                    <xsl:text>Tests run: </xsl:text>
 158                    <xsl:value-of select="count(exsl:node-set($result)//testCase)"/>
 159                    <xsl:text>, Failures: </xsl:text>
 160                    <xsl:value-of select="count(exsl:node-set($result)//testCase[string-length(.) &gt; 0])"/>
 161                  </log:printOut>
 162                  <testSuite>
 163                    <xsl:attribute name="name">
 164                      <xsl:value-of select="$name"/>
 165                    </xsl:attribute>
 166                    <xsl:copy-of select="exsl:node-set($result)//testCase"/>
 167                  </testSuite>
 168                </xsl:when>
 169              </xsl:choose>
 170            </io:files>
 171          </testSuites>
 172        </xsl:template>
 173      
 174      
 175        <!---
 176            - This template iterates over all named templates within the enclosing
 177            - Alster test stylesheet. If the name of one of these templates has
 178            - namespace "alster" and begins with "test", it is considered an Alster
 179            - test template and is then reflectively (via the Nalax-Reflection
 180            - extension) called. The output of the thus called templates is collected
 181            - into <code>&lt;testCase&gt;</code> subelements of one XML element named
 182            - "<code>testCases</code>".
 183           -->
 184        <xsl:template name="alster:runTestCases">
 185          <testCases>
 186            <reflect:namedTemplates>
 187              <xsl:variable name="tName">
 188                <reflect:templateName/>
 189              </xsl:variable>
 190              <xsl:if test="starts-with($tName, 'alster:test')">
 191                <testCase>
 192                  <xsl:attribute name="name">
 193                    <xsl:value-of select="$tName"/>
 194                  </xsl:attribute>
 195                  <xsl:variable name="startTime">
 196                    <xsl:value-of select="java:java.lang.System.currentTimeMillis()"/>
 197                  </xsl:variable>
 198                  <xsl:variable name="message">
 199                    <reflect:callTemplate name="$tName"/>
 200                  </xsl:variable>
 201                  <xsl:attribute name="millis">
 202                    <xsl:value-of select="java:java.lang.System.currentTimeMillis() - $startTime"/>
 203                  </xsl:attribute>
 204                  <xsl:if test="string-length(normalize-space($message))">
 205                    <xsl:value-of select="$message"/>
 206                  </xsl:if>
 207                </testCase>
 208              </xsl:if>
 209            </reflect:namedTemplates>
 210          </testCases>
 211        </xsl:template>
 212      
 213      </xsl:stylesheet>