Instrumenting JSP files
Clover cannot instrument JSP files directly. However, as all JSP files are translated to Java and next compiled using standard javac compiler, it is possible to enhance generated Java sources with Clover instrumentation and compile them. Thanks to this it is possible to get code coverage and reports for them.
JSP on Tomcat
By default, Tomcat compiles JSP file on the first access. Generated Java source code as well as compiled classes are stored in <tomcat_home>/work/Catalina/localhost/<application_name>. Luckily, Tomcat allows to perform pre-compilation of JSP files - classes should be bundled into WAR and proper servlet definitions added to web.xml. More details can be found on http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html page.
Examples
Tomcat
1) Download sample application from http://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/
2) Take sample Ant build script from http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html
3) Add Clover-related tasks and properties, for instance:
<property name="clover.jar" location="${user.home}/clover.jar"/>
<property name="clover.db" location="${java.io.tmpdir}/clover/clover.db"/>
<property name="clover.license.path" location="${user.home}/clover.license"/>
<taskdef resource="cloverlib.xml" classpath="${clover.jar}"/>
<target name="init">
<clover-setup initstring="${clover.db}" flushpolicy="interval" flushinterval="500"/>
</target>
<target name="report">
<clover-html-report initstring="${clover.db}" outdir="report"/>
</target>
...
<target name="all" depends="init, jspc, compile"> <!-- add "init" -->
Comments:
- the "init" target has <clover-setup> task with flushpolicy="interval" set (so that coverage recording files will be written at specified time interval, instead of JVM shutdown - thanks to this it's required to shutdown Tomcat)
- the "init" target has "inistring" with an absolute path to database (in order to simplify deployment as there's no need to copy clover.db to Tomcat directory)
- the "all" target depends on "init, jspc, compile"
4) Build application using "ant" command:
- JSP files are precompiled into WEB-INF/classes/org/apache/jsp
- clover.db is created in ${java.io.tmpdir}/clover
- the WEB-INF\generated_web.xml is created
5) Copy definitions of servlets from WEB-INF/generated_web.xml into WEB-INF/web.xml. Otherwise Tomcat would compile JSP files (without Clover instrumentation).
6) Copy also clover.jar into <tomcat_home>/lib directory.
7) Package sample.war and deploy to Tomcat (copy to <tomcat_home>/webapps).
8) Open the http://localhost:8080/sample
page, click on the "To JSP page" link. Page should print "hello". Coverage files shall be generated in the location of clover.db database.
9) Run "ant report" in order to generate coverage report.